Skip to main content

KdTree

Struct KdTree 

Source
pub struct KdTree { /* private fields */ }
Expand description

Efficient KD-Tree implementation for nearest neighbor search.

Nodes live in a single contiguous Vec (nodes); children are referenced by index. root is the index of the tree root (always 0 when non-empty).

Implementations§

Source§

impl KdTree

Source

pub const NO_CHILD: u32 = NIL

Child value in FlatKdNode meaning “no child”.

Source

pub fn flat_nodes(&self) -> impl ExactSizeIterator<Item = FlatKdNode> + '_

The tree’s nodes in their stored order. The root is the first node (when the tree is not empty), and children are referenced by position in this sequence. A query walks it like any binary kd-tree: go to the child on the query’s side of the split, and visit the other child only if the split plane is closer than the current best match.

Source

pub fn new(points: &[Point3f]) -> Result<Self>

Create a new KD-tree from a slice of points.

Points with a NaN or infinite coordinate are left out of the tree and are never returned as neighbors (indices of the remaining points still refer to points). Organized clouds store invalid returns as NaN; a NaN split value would send every query down one side and make the finite points on the other side unreachable.

Source

pub fn from_points(points: Vec<Point3f>) -> Result<Self>

Like KdTree::new, but takes ownership of the points instead of copying them (useful when the caller already built a converted copy).

Source

pub fn find_k_nearest_into( &self, query: &Point3f, k: usize, out: &mut Vec<(usize, f32)>, )

Find the k nearest neighbors of query, writing them into out as (original_index, squared_distance), closest first.

out is cleared first and its allocation is reused, so calling this in a loop (one buffer per thread) does no allocation per query. This is the hot path of normal estimation. k is expected to be small (tens): neighbors are kept in a sorted array rather than a heap.

Source

pub fn find_nearest_bounded( &self, query: &Point3f, bound_sq: f32, seed: Option<(usize, f32)>, ) -> Option<(usize, f32)>

Find the single nearest neighbor of query, allocation-free.

bound_sq is a squared-distance upper bound: only points strictly closer than it are considered, and the far side of a split is skipped as soon as it cannot beat the best so far. Pass f32::INFINITY for an unbounded search. When the caller already knows a candidate (e.g. last ICP iteration’s match), pass its index and squared distance as seed so the search starts pruned; seed is returned if nothing closer is found.

Returns (original_index, squared_distance), or None if no point lies within the bound and no seed was given. This is the hot path for ICP correspondence search, so — unlike find_k_nearest — it uses a fixed-size stack and no heap, result Vec, or sqrt.

Trait Implementations§

Source§

impl NearestNeighborSearch for KdTree

Source§

fn find_k_nearest(&self, query: &Point3f, k: usize) -> Vec<(usize, f32)>

Find the k nearest neighbors, closest first, as (index, distance).

For many queries in a loop, KdTree::find_k_nearest_into avoids the allocation per call.

Source§

fn find_radius_neighbors( &self, query: &Point3f, radius: f32, ) -> Vec<(usize, f32)>

Find all neighbors within radius using an iterative stack-based traversal.

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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, 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.
Source§

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

Source§

fn vzip(self) -> V