pub struct KdTree<const N: usize> { /* private fields */ }Expand description
A k-d tree over [f64; N] states for the Euclidean metric.
Nodes are inserted one at a time without rebalancing. The split axis cycles with the
depth. Planners insert random samples, which keeps the tree balanced in expectation; a
sorted insertion order degrades it to a list until KdTree::rebalance is called.
Removal (RemovableNeighbors) finds the node through an id
index, marks it, and rebuilds the tree balanced once removed nodes outnumber live ones.
Queries return exactly what LinearScan returns, including its tie
breaking toward the earliest inserted state, because both compare the same
StateSpace::distance values. Expected cost per query is O(log n) in low dimensions;
beyond roughly ten dimensions the pruning stops paying off and a linear scan is as fast.
§Examples
use sampling_planning::{Euclidean, KdTree, NearestNeighbors};
let mut tree = KdTree::<2>::default();
tree.insert(7, [0.0, 0.0]);
tree.insert(8, [3.0, 4.0]);
assert_eq!(tree.nearest(&Euclidean::<2>, &[2.5, 3.0]), Some(8));Implementations§
Source§impl<const N: usize> KdTree<N>
impl<const N: usize> KdTree<N>
Sourcepub fn rebalance(&mut self)
pub fn rebalance(&mut self)
Rebuilds the tree as a balanced k-d tree over the live nodes, dropping removed ones.
Each subtree splits at the median of its states along the axis of its depth, so the
depth is O(log n) whatever the insertion order was. Insertion ranks keep their relative
order, so query results do not change. Costs O(n log^2 n).
Removal calls this automatically once removed nodes outnumber live ones. Call it by hand after inserting many states in sorted order, which degrades the incremental tree to a list.
§Examples
use sampling_planning::{Euclidean, KdTree, NearestNeighbors};
let mut tree = KdTree::<1>::default();
for id in 0..100 {
tree.insert(id, [f64::from(u32::try_from(id).expect("small"))]);
}
tree.rebalance();
assert_eq!(tree.nearest(&Euclidean::<1>, &[41.2]), Some(41));Trait Implementations§
Source§impl<const N: usize> NearestNeighbors<Euclidean<N>> for KdTree<N>
impl<const N: usize> NearestNeighbors<Euclidean<N>> for KdTree<N>
Source§fn nearest(&self, space: &Euclidean<N>, query: &[f64; N]) -> Option<usize>
fn nearest(&self, space: &Euclidean<N>, query: &[f64; N]) -> Option<usize>
query, or None if the structure is empty.Source§fn within_radius(
&self,
space: &Euclidean<N>,
query: &[f64; N],
radius: f64,
out: &mut Vec<usize>,
)
fn within_radius( &self, space: &Euclidean<N>, query: &[f64; N], radius: f64, out: &mut Vec<usize>, )
out with the ids of all nodes within radius of query,
in insertion order.Source§impl<const N: usize> RemovableNeighbors<Euclidean<N>> for KdTree<N>
impl<const N: usize> RemovableNeighbors<Euclidean<N>> for KdTree<N>
Source§fn remove(&mut self, id: usize) -> bool
fn remove(&mut self, id: usize) -> bool
Removes the node with id in O(log n) amortized.
The node is first marked as removed and keeps splitting space. Once removed nodes
outnumber live ones, the tree is rebuilt balanced over the live nodes
(KdTree::rebalance), so memory and query cost track the live node count.