@@ -61,61 +61,19 @@ from libcpp.vector cimport vector
6161# ###############################################################################
6262# ########### Defininitions to access the C++ geodesic distance library #########
6363# ###############################################################################
64- cdef extern from " geodesic_mesh_elements.h" namespace " geodesic" :
65- cdef cppclass Vertex:
66- Vertex()
67-
68- cdef extern from " geodesic_mesh_elements.h" namespace " geodesic" :
69- cdef cppclass SurfacePoint:
70- SurfacePoint()
71- SurfacePoint(Vertex* )
72- double & x()
73- double & y()
74- double & z()
75-
76- cdef extern from " geodesic_mesh.h" namespace " geodesic" :
77- cdef cppclass Mesh:
78- Mesh()
79- void initialize_mesh_data(vector[double ]& , vector[unsigned ]& , bool )
80- vector[Vertex]& vertices()
81-
8264cdef extern from " geodesic_utils.h" :
65+ cdef cppclass SparseMatrix:
66+ vector[unsigned ] rows
67+ vector[unsigned ] columns
68+ vector[double ] data
8369 vector[double ] compute_gdist_impl(vector[double ], vector[unsigned ], vector[unsigned ], vector[unsigned ], double , bool , bool )
84-
85- cdef extern from " geodesic_algorithm_exact.h" namespace " geodesic" :
86- cdef cppclass GeodesicAlgorithmExact:
87- GeodesicAlgorithmExact(Mesh* )
88- void propagate(vector[SurfacePoint]& , double , vector[SurfacePoint]* )
89- unsigned best_source(SurfacePoint& , double & )
70+ SparseMatrix local_gdist_matrix_impl(vector[double ], vector[unsigned ], double , bool )
9071
9172cdef extern from " geodesic_constants_and_simple_functions.h" namespace " geodesic" :
9273 double GEODESIC_INF
9374# ###############################################################################
9475
9576
96- cdef get_mesh(
97- numpy.ndarray[numpy.float64_t, ndim= 2 ] vertices,
98- numpy.ndarray[numpy.int32_t, ndim= 2 ] triangles,
99- Mesh & amesh,
100- bool is_one_indexed
101- ):
102- # Define C++ vectors to contain the mesh surface components.
103- cdef vector[double ] points
104- cdef vector[unsigned ] faces
105-
106- # Map numpy array of mesh "vertices" to C++ vector of mesh "points"
107- cdef numpy.float64_t coord
108- for coord in vertices.flatten():
109- points.push_back(coord)
110-
111- # Map numpy array of mesh "triangles" to C++ vector of mesh "faces"
112- cdef numpy.int32_t indx
113- for indx in triangles.flatten():
114- faces.push_back(indx)
115-
116- amesh.initialize_mesh_data(points, faces, is_one_indexed)
117-
118-
11977def compute_gdist (numpy.ndarray[numpy.float64_t , ndim = 2 ] vertices,
12078 numpy.ndarray[numpy.int32_t , ndim = 2 ] triangles,
12179 numpy.ndarray[numpy.int32_t , ndim = 1 ] source_indices= None ,
@@ -245,43 +203,26 @@ def local_gdist_matrix(numpy.ndarray[numpy.float64_t, ndim=2] vertices,
245203 from the propgate step...
246204 """
247205
248- cdef Mesh amesh
249- get_mesh(vertices, triangles, amesh, is_one_indexed)
206+ cdef Py_ssize_t N = vertices.shape[0 ]
250207
251- # Define and create object for exact algorithm on that mesh:
252- cdef GeodesicAlgorithmExact * algorithm = new GeodesicAlgorithmExact( & amesh)
208+ cdef vector[ double ] points
209+ cdef vector[ unsigned ] faces
253210
254- cdef vector[SurfacePoint] source, targets
255- cdef Py_ssize_t N = vertices.shape[0 ]
256- cdef Py_ssize_t k
257- cdef Py_ssize_t kk
258- cdef numpy.float64_t distance = 0
259-
260- # Add all vertices as targets
261- for k in range (N):
262- targets.push_back(SurfacePoint(& amesh.vertices()[k]))
263-
264- rows = []
265- columns = []
266- data = []
267- for k in range (N):
268- source.push_back(SurfacePoint(& amesh.vertices()[k]))
269- algorithm.propagate(source, max_distance, NULL )
270- source.pop_back()
271-
272- for kk in range (N): # TODO: Reduce to vertices reached during propagate.
273- algorithm.best_source(targets[kk], distance)
274-
275- if (
276- distance is not GEODESIC_INF
277- and distance is not 0
278- and distance <= max_distance
279- ):
280- rows.append(k)
281- columns.append(kk)
282- data.append(distance)
283-
284- return scipy.sparse.csc_matrix((data, (rows, columns)), shape = (N, N))
211+ for k in vertices.flatten():
212+ points.push_back(k)
213+ for k in triangles.flatten():
214+ faces.push_back(k)
215+
216+ cdef SparseMatrix distances = local_gdist_matrix_impl(
217+ points,
218+ faces,
219+ max_distance,
220+ is_one_indexed,
221+ )
222+
223+ return scipy.sparse.csc_matrix(
224+ (distances.data, (distances.rows, distances.columns)), shape = (N, N)
225+ )
285226
286227
287228def distance_matrix_of_selected_points (
0 commit comments