KdTree

Struct KdTree 

Source
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>

Source

pub fn new<S>(points: ArrayBase<S, Ix2>) -> InterpolateResult<Self>
where S: Data<Elem = F>,

Create a new KD-tree from points

§Arguments
  • points - Point coordinates with shape (n_points, n_dims)
§Returns

A new KD-tree for efficient nearest neighbor searches

Source

pub fn with_leaf_size<S>( _points: ArrayBase<S, Ix2>, leaf_size: usize, ) -> InterpolateResult<Self>
where S: Data<Elem = F>,

Create a new KD-tree with a specified leaf size

§Arguments
  • points - Point coordinates with shape (n_points, n_dims)
  • leaf_size - Maximum number of points in a leaf node
§Returns

A new KD-tree for efficient nearest neighbor searches

Source

pub fn nearest_neighbor(&self, query: &[F]) -> InterpolateResult<(usize, F)>

Find the nearest neighbor to a query point

§Arguments
  • query - Query point coordinates
§Returns

Tuple containing (point_index, distance) of the nearest neighbor

Source

pub fn k_nearest_neighbors( &self, query: &[F], k: usize, ) -> InterpolateResult<Vec<(usize, F)>>

Find k nearest neighbors to a query point

§Arguments
  • query - Query point coordinates
  • k - Number of nearest neighbors to find
§Returns

Vector of (point_index, distance) tuples, sorted by distance

Source

pub fn points_within_radius( &self, query: &[F], radius: F, ) -> InterpolateResult<Vec<(usize, F)>>

Find all points within a specified radius of a query point

§Arguments
  • query - Query point coordinates
  • radius - Search radius
§Returns

Vector of (point_index, distance) tuples for all points within radius

Source

pub fn len(&self) -> usize

Get the number of points in the KD-tree

Source

pub fn is_empty(&self) -> bool

Check if the KD-tree is empty

Source

pub fn dim(&self) -> usize

Get the dimension of points in the KD-tree

Source

pub fn points(&self) -> &Array2<F>

Get a reference to the points in the KD-tree

Source

pub fn radius_neighbors( &self, query: &[F], radius: F, ) -> InterpolateResult<Vec<(usize, F)>>

Find all points within a specified radius (alias for points_within_radius)

§Arguments
  • query - Query point coordinates
  • radius - Search radius
§Returns

Vector of (point_index, distance) tuples for all points within radius

Source

pub fn radius_neighbors_view( &self, query: &ArrayView1<'_, F>, radius: F, ) -> InterpolateResult<Vec<(usize, F)>>

Find all points within a specified radius using an array view

§Arguments
  • query - Query point coordinates as an array view
  • radius - Search radius
§Returns

Vector of (point_index, distance) tuples for all points within radius

Source

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 coordinates
  • k - Number of nearest neighbors to find
  • max_distance - Optional maximum search distance for early termination
§Returns

Vector of (point_index, distance) tuples, sorted by distance

Source

pub fn query_nearest( &self, query: &ArrayView1<'_, F>, k: usize, ) -> InterpolateResult<Array1<usize>>

Find the nearest neighbors to a query point and return their indices

§Arguments
  • query - Coordinates of the query point as an array view
  • k - Number of nearest neighbors to find
§Returns

An array of indices of the k nearest neighbors

Trait Implementations§

Source§

impl<F> Clone for KdTree<F>

Source§

fn clone(&self) -> KdTree<F>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F> Debug for KdTree<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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)>>>

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)>>>

Perform parallel k-nearest neighbor search
Source§

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
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>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V