make search point sent by value

This commit is contained in:
Andrey Tkachenko 2018-12-12 17:05:43 +04:00
parent ed6a9b6a8e
commit 2027591d36
2 changed files with 7 additions and 7 deletions

View File

@ -22,20 +22,20 @@ pub trait KdTreePoint: Copy + PartialEq {
} }
} }
pub struct NearestNeighboursIter<'a, 'b, T> { pub struct NearestNeighboursIter<'a, T> {
range: f64, range: f64,
kdtree: &'a KdTree<T>, kdtree: &'a KdTree<T>,
ref_node: &'b T, ref_node: T,
node_stack: Vec<usize>, node_stack: Vec<usize>,
} }
impl<'a, 'b, T> Iterator for NearestNeighboursIter<'a, 'b, T> impl<'a, T> Iterator for NearestNeighboursIter<'a, T>
where T: KdTreePoint where T: KdTreePoint
{ {
type Item = (f64, &'a T); type Item = (f64, &'a T);
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let p = self.ref_node; let p = &self.ref_node;
loop { loop {
let node_idx = self.node_stack.pop()?; let node_idx = self.node_stack.pop()?;
@ -130,13 +130,13 @@ impl<KP: KdTreePoint> KdTree<KP> {
(best_distance, &self.nodes[nearest_neighbor].point) (best_distance, &self.nodes[nearest_neighbor].point)
} }
pub fn nearest_search_dist<'a, 'b>(&'a self, node: &'b KP, dist: f64) -> NearestNeighboursIter<'a, 'b, KP> { pub fn nearest_search_dist(&self, node: KP, dist: f64) -> NearestNeighboursIter<'_, KP> {
let mut node_stack = Vec::with_capacity(16); let mut node_stack = Vec::with_capacity(16);
node_stack.push(0); node_stack.push(0);
NearestNeighboursIter { NearestNeighboursIter {
range: dist, range: dist,
kdtree: &self, kdtree: self,
ref_node: node, ref_node: node,
node_stack, node_stack,
} }

View File

@ -120,7 +120,7 @@ fn test_neighbour_search_with_distance() {
let p = Point3WithId::new(0i32, gen_random(), gen_random(), gen_random()); let p = Point3WithId::new(0i32, gen_random(), gen_random(), gen_random());
let mut found_by_linear_search = find_neigbours_with_linear_search(&points, p, dist * dist); let mut found_by_linear_search = find_neigbours_with_linear_search(&points, p, dist * dist);
let mut point_found_by_kdtree: Vec<_> = tree.nearest_search_dist(&p, dist * dist).collect(); let mut point_found_by_kdtree: Vec<_> = tree.nearest_search_dist(p, dist * dist).collect();
assert_eq!(found_by_linear_search.len(), point_found_by_kdtree.len()); assert_eq!(found_by_linear_search.len(), point_found_by_kdtree.len());