99from compas .topology import breadth_first_traverse
1010
1111
12- def _closest_faces (vertices , faces , nmax = 10 , radius = 10.0 ):
12+ def _closest_faces (vertices , faces , nmax = 10 , max_distance = 10.0 ):
1313 points = [centroid_points ([vertices [index ] for index in face ]) for face in faces ]
1414
1515 k = min (len (faces ), nmax )
@@ -21,6 +21,7 @@ def _closest_faces(vertices, faces, nmax=10, radius=10.0):
2121 # [2] the distance between the test point and the face centroid
2222
2323 try :
24+ import numpy as np
2425 from scipy .spatial import cKDTree
2526
2627 except Exception :
@@ -48,22 +49,27 @@ def callback(sender, e):
4849
4950 closest = []
5051 for i , point in enumerate (points ):
51- sphere = Sphere (Point3d (* point ), radius )
52+ sphere = Sphere (Point3d (* point ), max_distance )
5253 data = []
5354 tree .Search (sphere , callback , data )
5455 closest .append (data )
5556
5657 else :
5758 tree = cKDTree (points )
58- _ , closest = tree .query (points , k = k , workers = - 1 )
59+ distances , closest = tree .query (points , k = k , workers = - 1 )
60+ closest_within_distance = []
61+ for i , closest_row in enumerate (closest ):
62+ idx = np .where (distances [i ] < max_distance )[0 ]
63+ closest_within_distance .append (closest_row [idx ].tolist ())
64+ closest = closest_within_distance
5965
6066 return closest
6167
6268
63- def _face_adjacency (vertices , faces , nmax = None , radius = None ):
69+ def _face_adjacency (vertices , faces , nmax = None , max_distance = None ):
6470 nmax = nmax or 10
65- radius = radius or 10.0
66- closest = _closest_faces (vertices , faces , nmax = nmax , radius = radius )
71+ max_distance = max_distance or 10.0
72+ closest = _closest_faces (vertices , faces , nmax = nmax , max_distance = max_distance )
6773
6874 adjacency = {}
6975
@@ -96,7 +102,7 @@ def _face_adjacency(vertices, faces, nmax=None, radius=None):
96102 return adjacency
97103
98104
99- def face_adjacency (points , faces , nmax = None , radius = None ):
105+ def face_adjacency (points , faces , nmax = None , max_distance = None ):
100106 """Build a face adjacency dict.
101107
102108 Parameters
@@ -106,9 +112,9 @@ def face_adjacency(points, faces, nmax=None, radius=None):
106112 faces : list[list[int]]
107113 The faces defined as list of indices in the points list.
108114 nmax : int, optional
109- The maximum number of neighboring faces to consider. If neither nmax nor radius is specified, all faces will be considered.
110- radius : float, optional
111- The radius of the search sphere for neighboring faces. If neither nmax nor radius is specified, all faces will be considered.
115+ The maximum number of neighboring faces to consider. If neither nmax nor max_distance is specified, all faces will be considered.
116+ max_distance : float, optional
117+ The max_distance of the search sphere for neighboring faces. If neither nmax nor max_distance is specified, all faces will be considered.
112118
113119 Returns
114120 -------
@@ -123,8 +129,8 @@ def face_adjacency(points, faces, nmax=None, radius=None):
123129 purely geometrical, but uses a spatial indexing tree to speed up the search.
124130
125131 """
126- if nmax or radius :
127- return _face_adjacency (points , faces , nmax = nmax , radius = radius )
132+ if nmax or max_distance :
133+ return _face_adjacency (points , faces , nmax = nmax , max_distance = max_distance )
128134
129135 adjacency = {}
130136
@@ -156,7 +162,7 @@ def face_adjacency(points, faces, nmax=None, radius=None):
156162 return adjacency
157163
158164
159- def unify_cycles (vertices , faces , root = None , nmax = None , radius = None ):
165+ def unify_cycles (vertices , faces , root = None , nmax = None , max_distance = None ):
160166 """Unify the cycle directions of all faces.
161167
162168 Unified cycle directions is a necessary condition for the data structure to
@@ -171,9 +177,9 @@ def unify_cycles(vertices, faces, root=None, nmax=None, radius=None):
171177 root : int, optional
172178 The key of the root face.
173179 nmax : int, optional
174- The maximum number of neighboring faces to consider. If neither nmax nor radius is specified, all faces will be considered.
175- radius : float, optional
176- The radius of the search sphere for neighboring faces. If neither nmax nor radius is specified, all faces will be considered.
180+ The maximum number of neighboring faces to consider. If neither nmax nor max_distance is specified, all faces will be considered.
181+ max_distance : float, optional
182+ The max_distance of the search sphere for neighboring faces. If neither nmax nor max_distance is specified, all faces will be considered.
177183
178184 Returns
179185 -------
@@ -204,7 +210,7 @@ def unify(node, nbr):
204210 if root is None :
205211 root = random .choice (list (range (len (faces ))))
206212
207- adj = face_adjacency (vertices , faces , nmax = nmax , radius = radius ) # this is the only place where the vertex coordinates are used
213+ adj = face_adjacency (vertices , faces , nmax = nmax , max_distance = max_distance ) # this is the only place where the vertex coordinates are used
208214
209215 visited = breadth_first_traverse (adj , root , unify )
210216
0 commit comments