Skip to content

Commit

Permalink
Update orientation.py
Browse files Browse the repository at this point in the history
  • Loading branch information
romanarust committed Dec 2, 2024
1 parent e33f06a commit c13863b
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/compas/topology/orientation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from compas.topology import breadth_first_traverse


def _closest_faces(vertices, faces, nmax=10, max_distance=10.0):
def _closest_faces(vertices, faces, nmax, max_distance):
points = [centroid_points([vertices[index] for index in face]) for face in faces]

k = min(len(faces), nmax)
k = len(faces) if nmax is None else min(len(faces), nmax)

# determine the k closest faces for each face
# each item in "closest" is
Expand All @@ -24,20 +24,23 @@ def _closest_faces(vertices, faces, nmax=10, max_distance=10.0):
import numpy as np
from scipy.spatial import cKDTree

tree = cKDTree(points)
distances, closest = tree.query(points, k=k, workers=-1)
if max_distance is None:
return closest

Check warning on line 30 in src/compas/topology/orientation.py

View check run for this annotation

Codecov / codecov/patch

src/compas/topology/orientation.py#L30

Added line #L30 was not covered by tests

closest_within_distance = []
for i, closest_row in enumerate(closest):
idx = np.where(distances[i] < max_distance)[0]
closest_within_distance.append(closest_row[idx].tolist())
return closest_within_distance

except Exception:
try:
from Rhino.Geometry import Point3d # type: ignore
from Rhino.Geometry import RTree # type: ignore
from Rhino.Geometry import Sphere # type: ignore

except Exception:
from compas.geometry import KDTree

tree = KDTree(points)
closest = [tree.nearest_neighbors(point, k) for point in points]
closest = [[index for xyz, index, d in nnbrs if d < max_distance] for nnbrs in closest]

else:
tree = RTree()

for i, point in enumerate(points):
Expand All @@ -53,22 +56,21 @@ def callback(sender, e):
data = []
tree.Search(sphere, callback, data)
closest.append(data)
return closest

Check warning on line 59 in src/compas/topology/orientation.py

View check run for this annotation

Codecov / codecov/patch

src/compas/topology/orientation.py#L59

Added line #L59 was not covered by tests

else:
tree = cKDTree(points)
distances, closest = tree.query(points, k=k, workers=-1)
closest_within_distance = []
for i, closest_row in enumerate(closest):
idx = np.where(distances[i] < max_distance)[0]
closest_within_distance.append(closest_row[idx].tolist())
closest = closest_within_distance
except Exception:
from compas.geometry import KDTree

Check warning on line 62 in src/compas/topology/orientation.py

View check run for this annotation

Codecov / codecov/patch

src/compas/topology/orientation.py#L61-L62

Added lines #L61 - L62 were not covered by tests

return closest
tree = KDTree(points)
closest = [tree.nearest_neighbors(point, k) for point in points]
if max_distance is None:
return closest
return [[index for xyz, index, d in nnbrs if d < max_distance] for nnbrs in closest]

Check warning on line 68 in src/compas/topology/orientation.py

View check run for this annotation

Codecov / codecov/patch

src/compas/topology/orientation.py#L64-L68

Added lines #L64 - L68 were not covered by tests


def _face_adjacency(vertices, faces, nmax=None, max_distance=None):
nmax = nmax or 10
max_distance = max_distance or 10.0
if nmax is None and max_distance is None:
raise ValueError("Either nmax or max_distance should be specified.")

Check warning on line 73 in src/compas/topology/orientation.py

View check run for this annotation

Codecov / codecov/patch

src/compas/topology/orientation.py#L73

Added line #L73 was not covered by tests
closest = _closest_faces(vertices, faces, nmax=nmax, max_distance=max_distance)

adjacency = {}
Expand Down

0 comments on commit c13863b

Please sign in to comment.