Skip to content

Commit

Permalink
Bug: distance_to_space on rong thing. Massive performance wins.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Jul 12, 2016
1 parent 074ee2c commit b910284
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/kdtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ impl<T, U: AsRef<[f64]>> KdTree<T, U> {
candidate = curr.left.as_ref().unwrap();
curr = curr.right.as_ref().unwrap();
}
let candidate_to_space =
util::distance_to_space(point, &*curr.min_bounds, &*curr.max_bounds, distance);
let candidate_to_space = util::distance_to_space(point,
&*candidate.min_bounds,
&*candidate.max_bounds,
distance);
if candidate_to_space <= evaluated_dist {
pending.push(HeapElement {
distance: candidate_to_space * -1f64,
Expand All @@ -158,7 +160,7 @@ impl<T, U: AsRef<[f64]>> KdTree<T, U> {
let bucket = curr.bucket.as_ref().unwrap().iter();
let iter = points.zip(bucket).map(|(p, d)| {
HeapElement {
distance: distance(p.as_ref(), point),
distance: distance(point, p.as_ref()),
element: d,
}
});
Expand Down Expand Up @@ -339,8 +341,8 @@ impl<'a, 'b, T: 'b, U: 'b + AsRef<[f64]>, F: 'a> Iterator for NearestIter<'a, 'b
}
self.pending.push(HeapElement {
distance: -distance_to_space(point,
&*curr.min_bounds,
&*curr.max_bounds,
&*candidate.min_bounds,
&*candidate.max_bounds,
distance),
element: &**candidate,
});
Expand All @@ -349,7 +351,7 @@ impl<'a, 'b, T: 'b, U: 'b + AsRef<[f64]>, F: 'a> Iterator for NearestIter<'a, 'b
let bucket = curr.bucket.as_ref().unwrap().iter();
self.evaluated.extend(points.zip(bucket).map(|(p, d)| {
HeapElement {
distance: -distance(p.as_ref(), point),
distance: -distance(point, p.as_ref()),
element: d,
}
}));
Expand Down
12 changes: 6 additions & 6 deletions tests/count_dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ fn it_works() {
assert_eq!(0, count.swap(0, Ordering::SeqCst));

kdtree.nearest(&POINT_A.0, 1, &new_dist).unwrap();
assert_eq!(4, count.swap(0, Ordering::SeqCst));
assert_eq!(2, count.swap(0, Ordering::SeqCst));

kdtree.nearest(&POINT_A.0, 2, &new_dist).unwrap();
assert_eq!(6, count.swap(0, Ordering::SeqCst));
assert_eq!(4, count.swap(0, Ordering::SeqCst));

kdtree.nearest(&POINT_A.0, 3, &new_dist).unwrap();
assert_eq!(6, count.swap(0, Ordering::SeqCst));
Expand All @@ -50,10 +50,10 @@ fn it_works() {


kdtree.within(&POINT_A.0, 0.0, &new_dist).unwrap();
assert_eq!(4, count.swap(0, Ordering::SeqCst));
assert_eq!(2, count.swap(0, Ordering::SeqCst));

kdtree.within(&POINT_B.0, 1.0, &new_dist).unwrap();
assert_eq!(6, count.swap(0, Ordering::SeqCst));
assert_eq!(3, count.swap(0, Ordering::SeqCst));

kdtree.within(&POINT_B.0, 2.0, &new_dist).unwrap();
assert_eq!(6, count.swap(0, Ordering::SeqCst));
Expand All @@ -62,13 +62,13 @@ fn it_works() {
assert_eq!(0, count.swap(0, Ordering::SeqCst));

iter.next().unwrap();
assert_eq!(4, count.swap(0, Ordering::SeqCst));
assert_eq!(2, count.swap(0, Ordering::SeqCst));

iter.next().unwrap();
assert_eq!(2, count.swap(0, Ordering::SeqCst));

iter.next().unwrap();
assert_eq!(0, count.swap(0, Ordering::SeqCst));
assert_eq!(2, count.swap(0, Ordering::SeqCst));

iter.next().unwrap();
assert_eq!(0, count.swap(0, Ordering::SeqCst));
Expand Down

0 comments on commit b910284

Please sign in to comment.