pub struct KDTree<T: Float + Send + Sync + 'static, D: Distance<T> + 'static> { /* private fields */ }Expand description
A KD-Tree for efficient nearest neighbor searches
§Type Parameters
T- The floating point type for coordinatesD- The distance metric type
Implementations§
Source§impl<T: Float + Send + Sync + 'static> KDTree<T, EuclideanDistance<T>>
impl<T: Float + Send + Sync + 'static> KDTree<T, EuclideanDistance<T>>
Sourcepub fn new(points: &Array2<T>) -> SpatialResult<Self>
pub fn new(points: &Array2<T>) -> SpatialResult<Self>
Sourcepub fn with_leaf_size(
points: &Array2<T>,
leafsize: usize,
) -> SpatialResult<Self>
pub fn with_leaf_size( points: &Array2<T>, leafsize: usize, ) -> SpatialResult<Self>
Source§impl<T: Float + Send + Sync + 'static, D: Distance<T> + 'static> KDTree<T, D>
impl<T: Float + Send + Sync + 'static, D: Distance<T> + 'static> KDTree<T, D>
Sourcepub fn with_metric(points: &Array2<T>, metric: D) -> SpatialResult<Self>
pub fn with_metric(points: &Array2<T>, metric: D) -> SpatialResult<Self>
Sourcepub fn with_options(
points: &Array2<T>,
metric: D,
leafsize: usize,
) -> SpatialResult<Self>
pub fn with_options( points: &Array2<T>, metric: D, leafsize: usize, ) -> SpatialResult<Self>
Sourcepub fn query(
&self,
point: &[T],
k: usize,
) -> SpatialResult<(Vec<usize>, Vec<T>)>
pub fn query( &self, point: &[T], k: usize, ) -> SpatialResult<(Vec<usize>, Vec<T>)>
Find the k nearest neighbors to a query point
§Arguments
point- Query pointk- Number of nearest neighbors to find
§Returns
- (indices, distances) of the k nearest neighbors
§Examples
use scirs2_spatial::KDTree;
use scirs2_core::ndarray::array;
// Create points for the KDTree - use the exact same points from test_kdtree_with_custom_leaf_size
let points = array![[2.0, 3.0], [5.0, 4.0], [9.0, 6.0], [4.0, 7.0], [8.0, 1.0], [7.0, 2.0]];
let kdtree = KDTree::new(&points).unwrap();
// Find the 2 nearest neighbors to [0.5, 0.5]
let (indices, distances) = kdtree.query(&[0.5, 0.5], 2).unwrap();
assert_eq!(indices.len(), 2);
assert_eq!(distances.len(), 2);Sourcepub fn query_radius(
&self,
point: &[T],
radius: T,
) -> SpatialResult<(Vec<usize>, Vec<T>)>
pub fn query_radius( &self, point: &[T], radius: T, ) -> SpatialResult<(Vec<usize>, Vec<T>)>
Find all points within a radius of a query point
§Arguments
point- Query pointradius- Search radius
§Returns
- (indices, distances) of points within the radius
§Examples
use scirs2_spatial::KDTree;
use scirs2_core::ndarray::array;
let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]];
let kdtree = KDTree::new(&points)?;
// Find all points within radius 0.7 of [0.5, 0.5]
let (indices, distances) = kdtree.query_radius(&[0.5, 0.5], 0.7)?;
assert_eq!(indices.len(), 4); // All points are within 0.7 units of [0.5, 0.5]Sourcepub fn count_neighbors(&self, point: &[T], radius: T) -> SpatialResult<usize>
pub fn count_neighbors(&self, point: &[T], radius: T) -> SpatialResult<usize>
Count the number of points within a radius of a query point
This method is more efficient than query_radius when only the count is needed.
§Arguments
point- Query pointradius- Search radius
§Returns
- Number of points within the radius
§Examples
use scirs2_spatial::KDTree;
use scirs2_core::ndarray::array;
let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]];
let kdtree = KDTree::new(&points)?;
// Count points within radius 0.7 of [0.5, 0.5]
let count = kdtree.count_neighbors(&[0.5, 0.5], 0.7)?;
assert_eq!(count, 4); // All points are within 0.7 units of [0.5, 0.5]Trait Implementations§
Source§impl<T: Clone + Float + Send + Sync + 'static, D: Clone + Distance<T> + 'static> Clone for KDTree<T, D>
impl<T: Clone + Float + Send + Sync + 'static, D: Clone + Distance<T> + 'static> Clone for KDTree<T, D>
Source§impl<T: Debug + Float + Send + Sync + 'static, D: Debug + Distance<T> + 'static> Debug for KDTree<T, D>
impl<T: Debug + Float + Send + Sync + 'static, D: Debug + Distance<T> + 'static> Debug for KDTree<T, D>
Source§impl<T: Float + Send + Sync + 'static, D: Distance<T> + 'static> KDTreeOptimized<T, D> for KDTree<T, D>
impl<T: Float + Send + Sync + 'static, D: Distance<T> + 'static> KDTreeOptimized<T, D> for KDTree<T, D>
Source§fn directed_hausdorff_distance(
&self,
points: &ArrayView2<'_, T>,
_seed: Option<u64>,
) -> SpatialResult<(T, usize, usize)>
fn directed_hausdorff_distance( &self, points: &ArrayView2<'_, T>, _seed: Option<u64>, ) -> SpatialResult<(T, usize, usize)>
Compute the directed Hausdorff distance from one point set to another using KD-tree acceleration Read more
Source§fn hausdorff_distance(
&self,
points: &ArrayView2<'_, T>,
seed: Option<u64>,
) -> SpatialResult<T>
fn hausdorff_distance( &self, points: &ArrayView2<'_, T>, seed: Option<u64>, ) -> SpatialResult<T>
Compute the Hausdorff distance between two point sets using KD-tree acceleration Read more
Source§fn batch_nearest_neighbor(
&self,
points: &ArrayView2<'_, T>,
) -> SpatialResult<(Array1<usize>, Array1<T>)>
fn batch_nearest_neighbor( &self, points: &ArrayView2<'_, T>, ) -> SpatialResult<(Array1<usize>, Array1<T>)>
Compute the approximate nearest neighbor for each point in a set Read more
Auto Trait Implementations§
impl<T, D> Freeze for KDTree<T, D>where
D: Freeze,
impl<T, D> RefUnwindSafe for KDTree<T, D>where
D: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, D> Send for KDTree<T, D>
impl<T, D> Sync for KDTree<T, D>
impl<T, D> Unpin for KDTree<T, D>
impl<T, D> UnwindSafe for KDTree<T, D>
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.