diff --git a/src/kdtree/mod.rs b/src/kdtree/mod.rs index aa16876..b126037 100644 --- a/src/kdtree/mod.rs +++ b/src/kdtree/mod.rs @@ -165,7 +165,8 @@ impl Kdtree { } fn build_tree(&mut self, nodes: &mut [KdtreePoint], bounds: &Bounds, depth : usize) -> usize { - let (splitting_index, pivot_value) = partition::partition_sliding_midpoint(nodes, bounds.get_midvalue_of_widest_dim(), bounds.get_widest_dim()); + let splitting_index = partition::partition_sliding_midpoint(nodes, bounds.get_midvalue_of_widest_dim(), bounds.get_widest_dim()); + let pivot_value = nodes[splitting_index].dims()[bounds.get_widest_dim()]; let node_id = self.add_node(nodes[splitting_index], bounds.get_widest_dim(), pivot_value); let nodes_len = nodes.len(); diff --git a/src/kdtree/partition.rs b/src/kdtree/partition.rs index 2d6a08c..2791b1a 100644 --- a/src/kdtree/partition.rs +++ b/src/kdtree/partition.rs @@ -8,7 +8,6 @@ enum PointsWereOnSide { struct PartitionPointHelper { points_were_on_side: PointsWereOnSide, - midpoint_split_value: f64, index_of_splitter: usize, } @@ -40,23 +39,21 @@ fn partition_sliding_midpoint_helper(vec: &mut [T], midpoin if has_points_on_sides != HAS_POINTS_ON_BOTH_SIDES { return PartitionPointHelper { index_of_splitter: closest_index, - midpoint_split_value: vec.get(closest_index).unwrap().dims()[partition_on_dimension], points_were_on_side: if has_points_on_sides == HAS_POINTS_ON_LEFT_SIDE { PointsWereOnSide::Left } else { PointsWereOnSide::Right } } } return PartitionPointHelper { index_of_splitter: closest_index, - midpoint_split_value: midpoint_value, points_were_on_side: PointsWereOnSide::Both } } -pub fn partition_sliding_midpoint(vec: &mut [T], midpoint_value: f64, partition_on_dimension: usize) -> (usize, f64) { +pub fn partition_sliding_midpoint(vec: &mut [T], midpoint_value: f64, partition_on_dimension: usize) -> usize { let vec_len = vec.len(); debug_assert!(vec[0].dims().len() > partition_on_dimension); if vec.len() == 1 { - return (0, vec[0].dims()[partition_on_dimension]); + return 0; } let partition_point_data = partition_sliding_midpoint_helper(vec, midpoint_value, partition_on_dimension); @@ -64,15 +61,15 @@ pub fn partition_sliding_midpoint(vec: &mut [T], midpoint_v match partition_point_data.points_were_on_side { PointsWereOnSide::Left => { vec.swap(partition_point_data.index_of_splitter, vec_len - 1); - (vec_len - 1, partition_point_data.midpoint_split_value) + vec_len - 1 }, PointsWereOnSide::Right => { vec.swap(partition_point_data.index_of_splitter, 0); - (0, partition_point_data.midpoint_split_value) + 0 } PointsWereOnSide::Both => { let index_of_splitting_point = partition_kdtree(vec, partition_point_data.index_of_splitter, partition_on_dimension); - (index_of_splitting_point, partition_point_data.midpoint_split_value) + index_of_splitting_point } } } @@ -191,8 +188,8 @@ mod tests { let p2 = Point2WithId::new(1, 4., 6.); let mut vec = vec![p1, p2]; - assert_eq! ((0, 3.), partition_sliding_midpoint(&mut vec, 3., 0)); - assert_eq! ((0, 5.), partition_sliding_midpoint(&mut vec, 5., 1)); + assert_eq! (0, partition_sliding_midpoint(&mut vec, 3., 0)); + assert_eq! (0, partition_sliding_midpoint(&mut vec, 5., 1)); } #[test] @@ -203,32 +200,32 @@ mod tests { let p4 = Point2WithId::new(4, 0., 8.); let mut vec = vec![p1, p2, p3]; - assert_eq! ((0, 2.), partition_sliding_midpoint(&mut vec, 1.9, 0)); + assert_eq! (0, partition_sliding_midpoint(&mut vec, 1.9, 0)); assert_eq! (1, vec[0].id); assert_eq! (2, vec[1].id); assert_eq! (3, vec[2].id); let mut vec = vec![p1, p2, p3]; - assert_eq! ((0, 2.), partition_sliding_midpoint(&mut vec, -5000., 0)); + assert_eq! (0, partition_sliding_midpoint(&mut vec, -5000., 0)); assert_eq! (1, vec[0].id); assert_eq! (2, vec[1].id); assert_eq! (3, vec[2].id); let mut vec = vec![p1, p2, p3]; - assert_eq! ((2, 4.), partition_sliding_midpoint(&mut vec, 10., 0)); + assert_eq! (2, partition_sliding_midpoint(&mut vec, 10., 0)); assert_eq! (1, vec[0].id); assert_eq! (3, vec[1].id); assert_eq! (2, vec[2].id); let mut vec = vec![p1, p2, p3]; - assert_eq! ((2, 7.), partition_sliding_midpoint(&mut vec, 10., 1)); + assert_eq! (2, partition_sliding_midpoint(&mut vec, 10., 1)); assert_eq! (1, vec[0].id); assert_eq! (2, vec[1].id); assert_eq! (3, vec[2].id); let mut vec = vec![p1, p2, p3, p4]; - assert_eq! ((0, 0.), partition_sliding_midpoint(&mut vec, -5000., 0)); + assert_eq! (0, partition_sliding_midpoint(&mut vec, -5000., 0)); assert_eq! (4, vec[0].id); assert_eq! (2, vec[1].id); assert_eq! (3, vec[2].id);