Skip to main content

KdTree

Struct KdTree 

Source
pub struct KdTree<T: Float> { /* private fields */ }
Expand description

A KD-tree for efficient spatial queries.

Points are stored as a flat Vec<T> with known dimensionality. The tree structure is a recursive enum of internal split nodes and leaf nodes.

§Examples

use scivex_core::spatial::KdTree;

let points: Vec<Vec<f64>> = vec![
    vec![0.0, 0.0],
    vec![1.0, 0.0],
    vec![0.0, 1.0],
    vec![1.0, 1.0],
];
let refs: Vec<&[f64]> = points.iter().map(|p| p.as_slice()).collect();
let tree = KdTree::build(&refs).unwrap();

let (indices, dists) = tree.query(&[0.1, 0.1], 1).unwrap();
assert_eq!(indices[0], 0);

Implementations§

Source§

impl<T: Float> KdTree<T>

Source

pub fn build(points: &[&[T]]) -> Result<Self>

Build a KD-tree from a set of points.

points is a slice of point slices, each of length dim.

§Errors

Returns CoreError::InvalidArgument if points is empty or if the point slices have inconsistent lengths.

Source

pub fn from_tensor(tensor: &Tensor<T>) -> Result<Self>

Build from a 2D tensor where each row is a point.

§Errors

Returns an error if the tensor is not 2-dimensional or is empty.

Source

pub fn query(&self, query: &[T], k: usize) -> Result<(Vec<usize>, Vec<T>)>

Find the k nearest neighbors to query.

Returns (indices, distances) sorted by distance (ascending).

§Errors

Returns an error if k == 0 or the query dimension does not match.

Source

pub fn query_radius( &self, query: &[T], radius: T, ) -> Result<(Vec<usize>, Vec<T>)>

Find all points within distance radius of query.

Returns (indices, distances).

§Errors

Returns an error if the query dimension does not match.

Source

pub fn query_pairs(&self, r: T) -> Vec<(usize, usize)>

Find all pairs of points within distance r of each other.

Returns pairs as (i, j) with i < j.

Source

pub fn len(&self) -> usize

Return the number of points.

Source

pub fn is_empty(&self) -> bool

Whether the tree contains no points.

Source

pub fn dim(&self) -> usize

Return the dimensionality.

Trait Implementations§

Source§

impl<T: Clone + Float> Clone for KdTree<T>

Source§

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

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> Debug for KdTree<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for KdTree<T>

§

impl<T> RefUnwindSafe for KdTree<T>
where T: RefUnwindSafe,

§

impl<T> Send for KdTree<T>

§

impl<T> Sync for KdTree<T>

§

impl<T> Unpin for KdTree<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for KdTree<T>

§

impl<T> UnwindSafe for KdTree<T>
where T: UnwindSafe,

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