forked from libigl/libigl
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiglhelpers.py
More file actions
55 lines (47 loc) · 1.81 KB
/
Copy pathiglhelpers.py
File metadata and controls
55 lines (47 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
import scipy.sparse as sparse
import pyigl as igl
def p2e(m):
if isinstance(m, np.ndarray):
if m.dtype.type == np.int32:
return igl.eigen.MatrixXi(m)
elif m.dtype.type == np.float64:
return igl.eigen.MatrixXd(m)
elif m.dtype.type == np.bool:
return igl.eigen.MatrixXb(m)
raise TypeError("p2e only support dtype float64, int32 and bool")
if sparse.issparse(m):
# convert in a dense matrix with triples
coo = m.tocoo()
triplets = np.vstack((coo.row, coo.col, coo.data)).T
triples_eigen_wrapper = igl.eigen.MatrixXd(triplets)
if m.dtype.type == np.int32:
t = igl.eigen.SparseMatrixi()
t.fromcoo(triples_eigen_wrapper)
return t
elif m.dtype.type == np.float64:
t = igl.eigen.SparseMatrixd()
t.fromCOO(triples_eigen_wrapper)
return t
raise TypeError("p2e only support numpy.array or scipy.sparse")
def e2p(m):
if isinstance(m, igl.eigen.MatrixXd):
return np.array(m, dtype='float64')
elif isinstance(m, igl.eigen.MatrixXi):
return np.array(m, dtype='int32')
elif isinstance(m, igl.eigen.MatrixXb):
return np.array(m, dtype='bool')
elif isinstance(m, igl.eigen.SparseMatrixd):
coo = np.array(m.toCOO())
I = coo[:, 0]
J = coo[:, 1]
V = coo[:, 2]
return sparse.coo_matrix((V,(I,J)), shape=(m.rows(),m.cols()), dtype='float64')
elif isinstance(m, igl.eigen.SparseMatrixi):
coo = np.array(m.toCOO())
I = coo[:, 0]
J = coo[:, 1]
V = coo[:, 2]
return sparse.coo_matrix((V,(I,J)), shape=(m.rows(),m.cols()), dtype='int32')
def printMatrixSizes(x,xn):
print(xn + " (" + str(x.rows()) + "," + str(x.cols()) + ")")