pub struct KdTree<F>{ /* private fields */ }
Expand description
KD-Tree for efficient nearest neighbor searches
The KD-tree partitions space recursively, making nearest neighbor searches much more efficient than brute force methods.
§Examples
use scirs2_core::ndarray::Array2;
use scirs2_interpolate::spatial::kdtree::KdTree;
// Create sample 2D points
let points = Array2::from_shape_vec((5, 2), vec![
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 1.0,
0.5, 0.5,
]).unwrap();
// Build KD-tree
let kdtree = KdTree::new(points).unwrap();
// Find the nearest neighbor to point (0.6, 0.6)
let query = vec![0.6, 0.6];
let (idx, distance) = kdtree.nearest_neighbor(&query).unwrap();
// idx should be 4 (the point at (0.5, 0.5))
assert_eq!(idx, 4);
Implementations§
Source§impl<F> KdTree<F>
impl<F> KdTree<F>
Sourcepub fn with_leaf_size<S>(
_points: ArrayBase<S, Ix2>,
leaf_size: usize,
) -> InterpolateResult<Self>where
S: Data<Elem = F>,
pub fn with_leaf_size<S>(
_points: ArrayBase<S, Ix2>,
leaf_size: usize,
) -> InterpolateResult<Self>where
S: Data<Elem = F>,
Sourcepub fn nearest_neighbor(&self, query: &[F]) -> InterpolateResult<(usize, F)>
pub fn nearest_neighbor(&self, query: &[F]) -> InterpolateResult<(usize, F)>
Sourcepub fn k_nearest_neighbors(
&self,
query: &[F],
k: usize,
) -> InterpolateResult<Vec<(usize, F)>>
pub fn k_nearest_neighbors( &self, query: &[F], k: usize, ) -> InterpolateResult<Vec<(usize, F)>>
Sourcepub fn points_within_radius(
&self,
query: &[F],
radius: F,
) -> InterpolateResult<Vec<(usize, F)>>
pub fn points_within_radius( &self, query: &[F], radius: F, ) -> InterpolateResult<Vec<(usize, F)>>
Sourcepub fn radius_neighbors(
&self,
query: &[F],
radius: F,
) -> InterpolateResult<Vec<(usize, F)>>
pub fn radius_neighbors( &self, query: &[F], radius: F, ) -> InterpolateResult<Vec<(usize, F)>>
Sourcepub fn radius_neighbors_view(
&self,
query: &ArrayView1<'_, F>,
radius: F,
) -> InterpolateResult<Vec<(usize, F)>>
pub fn radius_neighbors_view( &self, query: &ArrayView1<'_, F>, radius: F, ) -> InterpolateResult<Vec<(usize, F)>>
Sourcepub fn k_nearest_neighbors_optimized(
&self,
query: &[F],
k: usize,
max_distance: Option<F>,
) -> InterpolateResult<Vec<(usize, F)>>
pub fn k_nearest_neighbors_optimized( &self, query: &[F], k: usize, max_distance: Option<F>, ) -> InterpolateResult<Vec<(usize, F)>>
Enhanced k-nearest neighbor search with early termination optimization
This method provides improved performance for k-NN queries by using adaptive search strategies and early termination when possible.
§Arguments
query
- Query point coordinatesk
- Number of nearest neighbors to findmax_distance
- Optional maximum search distance for early termination
§Returns
Vector of (point_index, distance) tuples, sorted by distance
Sourcepub fn query_nearest(
&self,
query: &ArrayView1<'_, F>,
k: usize,
) -> InterpolateResult<Array1<usize>>
pub fn query_nearest( &self, query: &ArrayView1<'_, F>, k: usize, ) -> InterpolateResult<Array1<usize>>
Trait Implementations§
Source§impl<F> OptimizedSpatialSearch<F> for KdTree<F>
Default implementation of OptimizedSpatialSearch for KdTree
impl<F> OptimizedSpatialSearch<F> for KdTree<F>
Default implementation of OptimizedSpatialSearch for KdTree
Source§fn batch_k_nearest_neighbors(
&self,
queries: &ArrayView2<'_, F>,
k: usize,
) -> InterpolateResult<Vec<Vec<(usize, F)>>>
fn batch_k_nearest_neighbors( &self, queries: &ArrayView2<'_, F>, k: usize, ) -> InterpolateResult<Vec<Vec<(usize, F)>>>
Perform batch k-nearest neighbor search for multiple queries
Source§fn parallel_k_nearest_neighbors(
&self,
queries: &ArrayView2<'_, F>,
k: usize,
workers: Option<usize>,
) -> InterpolateResult<Vec<Vec<(usize, F)>>>
fn parallel_k_nearest_neighbors( &self, queries: &ArrayView2<'_, F>, k: usize, workers: Option<usize>, ) -> InterpolateResult<Vec<Vec<(usize, F)>>>
Perform parallel k-nearest neighbor search
Source§fn adaptive_k_nearest_neighbors(
&self,
query: &[F],
k: usize,
) -> InterpolateResult<Vec<(usize, F)>>
fn adaptive_k_nearest_neighbors( &self, query: &[F], k: usize, ) -> InterpolateResult<Vec<(usize, F)>>
Adaptive k-nearest neighbor search that adjusts strategy based on query characteristics
Source§fn multi_radius_search(
&self,
query: &[F],
radii: &[F],
) -> InterpolateResult<Vec<Vec<(usize, F)>>>
fn multi_radius_search( &self, query: &[F], radii: &[F], ) -> InterpolateResult<Vec<Vec<(usize, F)>>>
Range search with multiple radii for the same query point
Auto Trait Implementations§
impl<F> Freeze for KdTree<F>
impl<F> RefUnwindSafe for KdTree<F>where
F: RefUnwindSafe,
impl<F> Send for KdTree<F>where
F: Send,
impl<F> Sync for KdTree<F>where
F: Sync,
impl<F> Unpin for KdTree<F>where
F: Unpin,
impl<F> UnwindSafe for KdTree<F>where
F: UnwindSafe + RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.