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
impl KdTree
Sourcepub const NO_CHILD: u32 = NIL
pub const NO_CHILD: u32 = NIL
Child value in FlatKdNode meaning “no child”.
Sourcepub fn flat_nodes(&self) -> impl ExactSizeIterator<Item = FlatKdNode> + '_
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.
Sourcepub fn new(points: &[Point3f]) -> Result<Self>
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.
Sourcepub fn from_points(points: Vec<Point3f>) -> Result<Self>
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).
Sourcepub fn find_k_nearest_into(
&self,
query: &Point3f,
k: usize,
out: &mut Vec<(usize, f32)>,
)
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.
Sourcepub fn find_nearest_bounded(
&self,
query: &Point3f,
bound_sq: f32,
seed: Option<(usize, f32)>,
) -> Option<(usize, f32)>
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
impl NearestNeighborSearch for KdTree
Source§fn find_k_nearest(&self, query: &Point3f, k: usize) -> Vec<(usize, f32)>
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.
Auto Trait Implementations§
impl Freeze for KdTree
impl RefUnwindSafe for KdTree
impl Send for KdTree
impl Sync for KdTree
impl Unpin for KdTree
impl UnsafeUnpin for KdTree
impl UnwindSafe for KdTree
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.