sdd/kiddo

Within radius incoherent results

Closed this issue · 5 comments

STPR commented

Hello,
I would like to use it but it seems that some points aren't found while they obviously shall be.
I've made a simple example to explain my problem and I've the same result with the v0.2.5 and v2.0.0-beta.8.
This is the v2.0.0-beta.8 code:

use kiddo::{KdTree, distance::squared_euclidean};

fn main() -> Result<(), std::io::Error> {

let mut tree: KdTree<f64, 2> = KdTree::new();

tree.add(&[ 1.0,  2.0], 1);
tree.add(&[ 2.0,  3.0], 2);
tree.add(&[ 0.0, -1.0], 3);
tree.add(&[-2.0,  0.0], 4);
tree.add(&[ 2.0, -3.0], 5);
tree.add(&[-4.0,  1.0], 6);

println!("{:?}", tree);

let within = tree.within_unsorted(&[0.0, 0.0], 2.5f64, &squared_euclidean);
println!("{:?}", within);

    Ok(())
}

The ouput is:

KdTree { leaves: [LeafNode { content_points: [[1.0, 2.0], [2.0, 3.0], [0.0, -1.0], [-2.0, 0.0], [2.0, -3.0], [-4.0, 1.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], content_items: [1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], size: 6 }], stems: [], root_index: 2147483647, size: 6 }
[(1.0, 3)]

Which means it only found the point (0, -1) but 2 others points are within radius 2.5 at (0, 0), the (1, 2) and (-2, 0)

STPR commented

Every distances are squared 😅 so it works correctly only if I use the squared radius but it's not written in the documentation:

let within = tree.within_unsorted(&[0.0, 0.0], 6.25f64, &squared_euclidean);
sdd commented

Hi! Thanks for reporting - this was a regression that was introduced in 2.0.0.beta.7, I also spotted this today. I should have this fixed over the next day or two, but in the meantime you should be able to revert to 2.0.0.beta.6 to avoid the problem.

Thanks for reporting!

sdd commented

Actually sorry, I misread your report. Yes, for now the results are coming out squared, and that will be the same even in 2.0.0.beta.6. I'll update the documentation.

STPR commented

That the returned distances are squared must be in the documentation.
But I think the command must square the radius itself.
I've had to use 6.25 to search the points within radius 2.5.

sdd commented

I've updated the documentation to make the behaviour clearer, as well as renaming the parameter from radius to dist.

You'll need a slight refactor also as the results are now returned as a Neighbour struct rather than a tuple.

Released as https://crates.io/crates/kiddo/2.0.0-beta.9