Skip to main content

KdTree

Struct KdTree 

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

Source

pub fn new(points: &[[f64; D]]) -> Self

Source

pub fn len(&self) -> usize

Number of indexed points.

Source

pub fn is_empty(&self) -> bool

Source

pub fn nearest(&self, query: [f64; D]) -> Option<(usize, f64)>

The closest point to query as (index, distance); ties go to the lower index.

Source

pub fn k_nearest(&self, query: [f64; D], k: usize) -> Vec<(usize, f64)>

The k closest points, nearest first, as (index, distance).

Source

pub fn within_radius(&self, query: [f64; D], radius: f64) -> Vec<usize>

Indices of all points within radius of query (inclusive), in unspecified order.

Source

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.

Source

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

Source

pub fn range_query(&self, min: [f64; D], max: [f64; D]) -> Vec<usize>

Indices of all points inside the axis-aligned box [min, max] (inclusive).

Trait Implementations§

Source§

impl<const D: usize> Clone for KdTree<D>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<const D: usize> Debug for KdTree<D>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const D: usize> Freeze for KdTree<D>

§

impl<const D: usize> RefUnwindSafe for KdTree<D>

§

impl<const D: usize> Send for KdTree<D>

§

impl<const D: usize> Sync for KdTree<D>

§

impl<const D: usize> Unpin for KdTree<D>

§

impl<const D: usize> UnsafeUnpin for KdTree<D>

§

impl<const D: usize> UnwindSafe for KdTree<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> MaybeSerialize for T
where T: ?Sized,

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

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

fn try_from(value: U) -> Result<T, !>

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.