KDTree

Struct KDTree 

Source
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 coordinates
  • D - The distance metric type

Implementations§

Source§

impl<T: Float + Send + Sync + 'static> KDTree<T, EuclideanDistance<T>>

Source

pub fn new(points: &Array2<T>) -> SpatialResult<Self>

Create a new KD-Tree with default Euclidean distance metric

§Arguments
  • points - Array of points, each row is a point
§Returns
  • A new KD-Tree
Source

pub fn with_leaf_size( points: &Array2<T>, leafsize: usize, ) -> SpatialResult<Self>

Create a new KD-Tree with custom leaf size (using Euclidean distance)

§Arguments
  • points - Array of points, each row is a point
  • leafsize - The maximum number of points in a leaf node
§Returns
  • A new KD-Tree
Source§

impl<T: Float + Send + Sync + 'static, D: Distance<T> + 'static> KDTree<T, D>

Source

pub fn with_metric(points: &Array2<T>, metric: D) -> SpatialResult<Self>

Create a new KD-Tree with custom distance metric

§Arguments
  • points - Array of points, each row is a point
  • metric - The distance metric to use
§Returns
  • A new KD-Tree
Source

pub fn with_options( points: &Array2<T>, metric: D, leafsize: usize, ) -> SpatialResult<Self>

Create a new KD-Tree with custom distance metric and leaf size

§Arguments
  • points - Array of points, each row is a point
  • metric - The distance metric to use
  • leafsize - The maximum number of points in a leaf node
§Returns
  • A new KD-Tree
Source

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 point
  • k - 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);
Source

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 point
  • radius - 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]
Source

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 point
  • radius - 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]
Source

pub fn shape(&self) -> (usize, usize)

Get the shape of the KD-Tree’s point set

§Returns
  • A tuple of (n_points, n_dimensions)
Source

pub fn npoints(&self) -> usize

Get the number of points in the KD-Tree

§Returns
  • The number of points
Source

pub fn ndim(&self) -> usize

Get the dimensionality of the points in the KD-Tree

§Returns
  • The dimensionality of the points
Source

pub fn leafsize(&self) -> usize

Get the leaf size of the KD-Tree

§Returns
  • The leaf size
Source

pub fn bounds(&self) -> &Rectangle<T>

Get the bounds of the KD-Tree

§Returns
  • The bounding rectangle of the entire dataset

Trait Implementations§

Source§

impl<T: Clone + Float + Send + Sync + 'static, D: Clone + Distance<T> + 'static> Clone for KDTree<T, D>

Source§

fn clone(&self) -> KDTree<T, D>

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<T: Debug + Float + Send + Sync + 'static, D: Debug + Distance<T> + 'static> Debug for KDTree<T, D>

Source§

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

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

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

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>

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

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>

§

impl<T, D> Send for KDTree<T, D>

§

impl<T, D> Sync for KDTree<T, D>

§

impl<T, D> Unpin for KDTree<T, D>
where D: Unpin, T: Unpin,

§

impl<T, D> UnwindSafe for KDTree<T, D>

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