1010This software is provided "as is", without warranty of any kind.
1111"""
1212
13- from typing import List
13+ from typing import List , Tuple
1414from numpy .typing import NDArray
1515import numpy as np
1616from scipy .spatial .transform import Rotation
17- from scipy .spatial import cKDTree # type: ignore
17+ from scipy .spatial import cKDTree # type: ignore
1818
1919
2020def faces_to_edges (faces : NDArray ) -> NDArray :
@@ -42,8 +42,39 @@ def faces_to_edges(faces: NDArray) -> NDArray:
4242def query_nearest (
4343 references_xyz : NDArray ,
4444 nodes_xyz : NDArray ,
45- radius : float = - 1.0 ,
46- n_neighbors : int = - 1 ,
45+ n_neighbors : int ,
46+ ) -> Tuple [NDArray , NDArray ]:
47+ """
48+ Find the nearest neighbors for a set of nodes based on given reference
49+ points.
50+
51+ This function returns the indices of the nearest reference neighbors
52+ for each point in `nodes_xyz`. The neighbors can be determined either by
53+ a specified number of neighbors (`n_neighbors`) or by a specified
54+ radius (`radius`). If both parameters are set, the function will
55+ prioritize the radius.
56+
57+ Args:
58+ references_xyz: reference points, shape (N, 3)
59+ nodes_xyz: nodes for which to find nearest neighbors, shape (M, 3)
60+ radius: The radius to consider for finding the neighbors.
61+ n_neighbors: The number of nearest neighbors to return for each node.
62+
63+ Returns:
64+ An array of arrays of indices of the nearest neighbors,
65+ of shape (M, n_neighbors) when n_neighbors is set
66+ """
67+
68+ distances , indices = cKDTree (references_xyz ).query (
69+ x = nodes_xyz , k = n_neighbors , workers = - 1
70+ )
71+ return distances , indices
72+
73+
74+ def query_radius (
75+ references_xyz : NDArray ,
76+ nodes_xyz : NDArray ,
77+ radius : float ,
4778) -> NDArray :
4879 """
4980 Find the nearest neighbors for a set of nodes based on given reference
@@ -65,22 +96,10 @@ def query_nearest(
6596 An array of arrays of indices of the nearest neighbors,
6697 of shape (M, n_neighbors) when n_neighbors is set
6798 """
68- # either n_neighbors or radius should be used but not both
69- # assert n_neighbors * radius < 0
70- if radius > 0 :
71- indices = cKDTree (references_xyz ).query_ball_point (
72- x = nodes_xyz , r = radius , workers = - 1
73- )
74- else :
75- if n_neighbors < 0 :
76- raise ValueError (
77- "Either radius or n_neighbors should be provided,"
78- f"(n_neighbors, radius)=({ n_neighbors } , { radius } ) "
79- )
80-
81- _ , indices = cKDTree (references_xyz ).query (
82- x = nodes_xyz , k = n_neighbors , workers = - 1
83- )
99+
100+ indices = cKDTree (references_xyz ).query_ball_point (
101+ x = nodes_xyz , r = radius , workers = - 1
102+ )
84103 return indices
85104
86105
0 commit comments