mirror of
https://github.com/andreytkachenko/kdtree-rust.git
synced 2024-11-22 09:26:25 +04:00
120 lines
3.3 KiB
Rust
120 lines
3.3 KiB
Rust
extern crate kdtree;
|
|
extern crate rand;
|
|
|
|
use rand::Rng;
|
|
|
|
use kdtree::kdtree::*;
|
|
use kdtree::kdtree::test_common::tests_utils::*;
|
|
|
|
//these could be taken from test_common, but I dont fully understand the module thingy yet.
|
|
#[derive(Copy, Clone, PartialEq)]
|
|
pub struct Point3WithId {
|
|
dims: [f64; 3],
|
|
pub id: i32,
|
|
}
|
|
|
|
impl Point3WithId {
|
|
pub fn new(id: i32, x: f64, y: f64, z: f64) -> Point3WithId {
|
|
Point3WithId {
|
|
dims: [x, y, z],
|
|
id: id,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl KdtreePointTrait for Point3WithId {
|
|
fn dims(&self) -> &[f64] {
|
|
return &self.dims;
|
|
}
|
|
}
|
|
|
|
fn gen_random() -> f64 {
|
|
rand::thread_rng().gen_range(0., 10000.)
|
|
}
|
|
|
|
fn gen_random_usize( max_value : usize) -> usize {
|
|
rand::thread_rng().gen_range(0usize, max_value)
|
|
}
|
|
|
|
fn find_nn_with_linear_search<'a>(points : &'a Vec<Point3WithId>, find_for : Point3WithId) -> &Point3WithId {
|
|
let distance_fun = kdtree::kdtree::distance::squared_euclidean;
|
|
|
|
let mut best_found_distance = distance_fun(find_for.dims(), points[0].dims());
|
|
let mut closed_found_point = &points[0];
|
|
|
|
for p in points {
|
|
let dist = distance_fun(find_for.dims(), p.dims());
|
|
|
|
if dist < best_found_distance {
|
|
best_found_distance = dist;
|
|
closed_found_point = &p;
|
|
}
|
|
}
|
|
|
|
closed_found_point
|
|
}
|
|
|
|
fn generate_points(point_count : usize) -> Vec<Point3WithId> {
|
|
let mut points : Vec<Point3WithId> = vec![];
|
|
|
|
for i in 0 .. point_count {
|
|
points.push(Point3WithId::new(i as i32, gen_random(),gen_random(),gen_random()));
|
|
}
|
|
|
|
points
|
|
}
|
|
|
|
#[test]
|
|
fn test_against_1000_random_points() {
|
|
let point_count = 1000usize;
|
|
let points = generate_points(point_count);
|
|
|
|
let tree = kdtree::kdtree::Kdtree::new(&mut points.clone());
|
|
|
|
//test points pushed into the tree, id should be equal.
|
|
for i in 0 .. point_count {
|
|
let p = &points[i];
|
|
|
|
assert_eq!(p.id, tree.nearest_search(p).id );
|
|
}
|
|
|
|
//test randomly generated points within the cube. and do the linear search. should match
|
|
for _ in 0 .. 500 {
|
|
let p = Point3WithId::new(0i32, gen_random(), gen_random(), gen_random());
|
|
|
|
let found_by_linear_search = find_nn_with_linear_search(&points, p);
|
|
let point_found_by_kdtree = tree.nearest_search(&p);
|
|
|
|
assert_eq!(point_found_by_kdtree.id, found_by_linear_search.id);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_incrementally_build_tree_against_built_at_once() {
|
|
let point_count = 2000usize;
|
|
let mut points = generate_points(point_count);
|
|
|
|
let tree_built_at_once = kdtree::kdtree::Kdtree::new(&mut points.clone());
|
|
let mut tree_built_incrementally = kdtree::kdtree::Kdtree::new(&mut points[0..1]);
|
|
|
|
for i in 1 .. point_count {
|
|
let p = &points[i];
|
|
|
|
tree_built_incrementally.insert_node(p.clone());
|
|
}
|
|
|
|
|
|
//test points pushed into the tree, id should be equal.
|
|
for i in 0 .. point_count {
|
|
let p = &points[i];
|
|
|
|
assert_eq!(tree_built_at_once.nearest_search(p).id, tree_built_incrementally.nearest_search(p).id);
|
|
}
|
|
|
|
|
|
//test randomly generated points within the cube. and do the linear search. should match
|
|
for _ in 0 .. 5000 {
|
|
let p = Point3WithId::new(0i32, gen_random(), gen_random(), gen_random());
|
|
assert_eq!(tree_built_at_once.nearest_search(&p).id, tree_built_incrementally.nearest_search(&p).id);
|
|
}
|
|
} |