pub struct KdTree<const D: usize> { /* private fields */ }Expand description
A static, balanced k-d tree over points in D dimensions.
Built once in O(n log n) by recursive median splits (cycling through the axes), then queried
in roughly O(log n) for nearest neighbours. Queries return indices into the point slice the
tree was built from. Points with non-finite coordinates are not indexed.
Implementations§
Source§impl<const D: usize> KdTree<D>
impl<const D: usize> KdTree<D>
pub fn new(points: &[[f64; D]]) -> Self
pub fn is_empty(&self) -> bool
Sourcepub fn nearest(&self, query: [f64; D]) -> Option<(usize, f64)>
pub fn nearest(&self, query: [f64; D]) -> Option<(usize, f64)>
The closest point to query as (index, distance); ties go to the lower index.
Sourcepub fn k_nearest(&self, query: [f64; D], k: usize) -> Vec<(usize, f64)>
pub fn k_nearest(&self, query: [f64; D], k: usize) -> Vec<(usize, f64)>
The k closest points, nearest first, as (index, distance).
Sourcepub fn within_radius(&self, query: [f64; D], radius: f64) -> Vec<usize>
pub fn within_radius(&self, query: [f64; D], radius: f64) -> Vec<usize>
Indices of all points within radius of query (inclusive), in unspecified order.
Sourcepub fn query_pairs(&self, r: f64) -> Vec<(usize, usize)>
pub fn query_pairs(&self, r: f64) -> Vec<(usize, usize)>
All pairs (i, j), i < j, of indexed points at distance at most r (SciPy’s
query_pairs), sorted.
Sourcepub fn count_neighbors(&self, other: &KdTree<D>, r: f64) -> usize
pub fn count_neighbors(&self, other: &KdTree<D>, r: f64) -> usize
The number of pairs (i, j) with i from this tree and j from other at distance at
most r (SciPy’s count_neighbors with a scalar radius).