From b236cd2595d5c5802ae1caccb6175ca6c4a2a936 Mon Sep 17 00:00:00 2001 From: Olek Date: Fri, 30 Dec 2016 14:25:44 +0100 Subject: [PATCH] Working on add tree, fixed an issue on build tree where it did not build using sliding midpoint value --- src/kdtree/mod.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/kdtree/mod.rs b/src/kdtree/mod.rs index 93444d4..050eba8 100644 --- a/src/kdtree/mod.rs +++ b/src/kdtree/mod.rs @@ -49,6 +49,20 @@ impl Kdtree { squared_euclidean(&self.nearest_search(node).dims(), node.dims()) <= squared_range } + pub fn insert_node(&mut self, node_to_add : &KdtreePoint) { + + let current_index = 0; + loop { + let current_node = &self.nodes[current_index]; + if node_to_add.dims()[current_node.dimension] <= current_node.split_on { + } else { + + } + + break; + } + } + fn nearest_search_impl(&self, p: &KdtreePoint, searched_index: usize, best_distance_squared: &mut f64, best_leaf_found: &mut usize) { let node = &self.nodes[searched_index]; @@ -95,7 +109,7 @@ impl Kdtree { fn build_tree(&mut self, nodes: &mut [KdtreePoint], bounds: &Bounds) -> usize { let (splitting_index, pivot_value) = partition::partition_sliding_midpoint(nodes, bounds.get_midvalue_of_widest_dim(), bounds.get_widest_dim()); - let node_id = self.add_node(nodes[splitting_index], bounds.get_widest_dim(), bounds.get_midvalue_of_widest_dim()); + let node_id = self.add_node(nodes[splitting_index], bounds.get_widest_dim(), pivot_value); let nodes_len = nodes.len(); if splitting_index > 0 { @@ -214,6 +228,18 @@ mod tests { assert_eq!(true,tree.has_neighbor_in_range(&Point2WithId::new(0,0.,0.), 300.)); } + #[test] + fn incremental_add_adds_as_expected() { + let mut vec = vec![Point2WithId::new(0,0.,0.)]; + + let mut tree = Kdtree::new(&mut vec); + + tree.insert_node(&Point2WithId::new(0,1.,0.)); + tree.insert_node(&Point2WithId::new(0,-1.,0.)); + + assert_eq!(tree.nodes.len(), 3); + } + fn qc_value_vec_to_2d_points_vec(xs: &Vec) -> Vec { let mut vec: Vec = vec![]; for i in 0..xs.len() {