mirror of
https://github.com/andreytkachenko/kdtree-rust.git
synced 2024-11-22 01:16:25 +04:00
removed returning of midvalue, as it was invalid
This commit is contained in:
parent
27be03f264
commit
a9f8dd8f14
@ -165,7 +165,8 @@ impl<KdtreePoint: KdtreePointTrait> Kdtree<KdtreePoint> {
|
||||
}
|
||||
|
||||
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();
|
||||
|
@ -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<T: KdtreePointTrait>(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<T: KdtreePointTrait>(vec: &mut [T], midpoint_value: f64, partition_on_dimension: usize) -> (usize, f64) {
|
||||
pub fn partition_sliding_midpoint<T: KdtreePointTrait>(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<T: KdtreePointTrait>(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);
|
||||
|
Loading…
Reference in New Issue
Block a user