99from compas .topology import breadth_first_traverse
1010
1111
12- def _closest_faces (vertices , faces , nmax = 10 , max_distance = 10.0 ):
12+ def _closest_faces (vertices , faces , nmax , max_distance ):
1313 points = [centroid_points ([vertices [index ] for index in face ]) for face in faces ]
1414
15- k = min (len (faces ), nmax )
15+ k = len ( faces ) if nmax is None else min (len (faces ), nmax )
1616
1717 # determine the k closest faces for each face
1818 # each item in "closest" is
@@ -24,20 +24,23 @@ def _closest_faces(vertices, faces, nmax=10, max_distance=10.0):
2424 import numpy as np
2525 from scipy .spatial import cKDTree
2626
27+ tree = cKDTree (points )
28+ distances , closest = tree .query (points , k = k , workers = - 1 )
29+ if max_distance is None :
30+ return closest
31+
32+ closest_within_distance = []
33+ for i , closest_row in enumerate (closest ):
34+ idx = np .where (distances [i ] < max_distance )[0 ]
35+ closest_within_distance .append (closest_row [idx ].tolist ())
36+ return closest_within_distance
37+
2738 except Exception :
2839 try :
2940 from Rhino .Geometry import Point3d # type: ignore
3041 from Rhino .Geometry import RTree # type: ignore
3142 from Rhino .Geometry import Sphere # type: ignore
3243
33- except Exception :
34- from compas .geometry import KDTree
35-
36- tree = KDTree (points )
37- closest = [tree .nearest_neighbors (point , k ) for point in points ]
38- closest = [[index for xyz , index , d in nnbrs if d < max_distance ] for nnbrs in closest ]
39-
40- else :
4144 tree = RTree ()
4245
4346 for i , point in enumerate (points ):
@@ -53,22 +56,21 @@ def callback(sender, e):
5356 data = []
5457 tree .Search (sphere , callback , data )
5558 closest .append (data )
59+ return closest
5660
57- else :
58- tree = cKDTree (points )
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
61+ except Exception :
62+ from compas .geometry import KDTree
6563
66- return closest
64+ tree = KDTree (points )
65+ closest = [tree .nearest_neighbors (point , k ) for point in points ]
66+ if max_distance is None :
67+ return closest
68+ return [[index for xyz , index , d in nnbrs if d < max_distance ] for nnbrs in closest ]
6769
6870
6971def _face_adjacency (vertices , faces , nmax = None , max_distance = None ):
70- nmax = nmax or 10
71- max_distance = max_distance or 10.0
72+ if nmax is None and max_distance is None :
73+ raise ValueError ( "Either nmax or max_distance should be specified." )
7274 closest = _closest_faces (vertices , faces , nmax = nmax , max_distance = max_distance )
7375
7476 adjacency = {}
0 commit comments