iglhelpers.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import numpy as np
  2. import scipy.sparse as sparse
  3. import igl
  4. def p2e(m):
  5. if isinstance(m, np.ndarray):
  6. if m.dtype.type == np.int32:
  7. return igl.eigen.MatrixXi(m)
  8. elif m.dtype.type == np.float64:
  9. return igl.eigen.MatrixXd(m)
  10. raise TypeError("p2e only support dtype float64 or int32")
  11. if sparse.issparse(m):
  12. # convert in a dense matrix with triples
  13. coo = m.tocoo()
  14. triplets = np.vstack((coo.row, coo.col, coo.data)).T
  15. triples_eigen_wrapper = igl.eigen.MatrixXd(triplets)
  16. if m.dtype.type == np.int32:
  17. t = igl.eigen.SparseMatrixi()
  18. t.fromcoo(triples_eigen_wrapper)
  19. return t
  20. elif m.dtype.type == np.float64:
  21. t = igl.eigen.SparseMatrixd()
  22. t.fromCOO(triples_eigen_wrapper)
  23. return t
  24. raise TypeError("p2e only support numpy.array or scipy.sparse")
  25. def e2p(m):
  26. if isinstance(m, igl.eigen.MatrixXd):
  27. return np.array(m, dtype='float64')
  28. elif isinstance(m, igl.eigen.MatrixXi):
  29. return np.array(m, dtype='int32')
  30. elif isinstance(m, igl.eigen.SparseMatrixd):
  31. coo = np.array(m.toCOO())
  32. I = coo[:, 0]
  33. J = coo[:, 1]
  34. V = coo[:, 2]
  35. return sparse.coo_matrix((V,(I,J)), shape=(m.rows(),m.cols()), dtype='float64')
  36. elif isinstance(m, igl.eigen.SparseMatrixi):
  37. coo = np.array(m.toCOO())
  38. I = coo[:, 0]
  39. J = coo[:, 1]
  40. V = coo[:, 2]
  41. return sparse.coo_matrix((V,(I,J)), shape=(m.rows(),m.cols()), dtype='int32')
  42. def printMatrixSizes(x,xn):
  43. print(xn + " (" + str(x.rows()) + "," + str(x.cols()) + ")")