iglhelpers.py 1.8 KB

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