-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrtree.rs
More file actions
93 lines (76 loc) · 3.39 KB
/
Copy pathrtree.rs
File metadata and controls
93 lines (76 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use spart::geometry::{DistanceMetric, EuclideanDistance, Point2D, Point3D};
use spart::r_tree::RTree;
// Define a custom distance metric (Manhattan distance)
struct ManhattanDistance;
impl<T> DistanceMetric<Point2D<T>> for ManhattanDistance {
fn distance_sq(p1: &Point2D<T>, p2: &Point2D<T>) -> f64 {
((p1.x - p2.x).abs() + (p1.y - p2.y).abs()).powi(2)
}
}
impl<T> DistanceMetric<Point3D<T>> for ManhattanDistance {
fn distance_sq(p1: &Point3D<T>, p2: &Point3D<T>) -> f64 {
((p1.x - p2.x).abs() + (p1.y - p2.y).abs() + (p1.z - p2.z).abs()).powi(2)
}
}
fn main() {
// --- 2D RTree Example ---
println!("--- 2D RTree Example ---");
let mut tree2d: RTree<Point2D<u32>> = RTree::new(4).unwrap();
// Insert some points
tree2d.insert(Point2D::new(10.0, 20.0, Some(1)));
tree2d.insert(Point2D::new(80.0, 30.0, Some(2)));
tree2d.insert(Point2D::new(45.0, 70.0, Some(3)));
// Query the tree for the 2 nearest neighbors to a point
let query_point_2d = Point2D::new(12.0, 22.0, None);
let results_2d_euclidean = tree2d.knn_search::<EuclideanDistance>(&query_point_2d, 2);
// Print the results
println!(
"2 nearest neighbors to {:?} (Euclidean): {:?}",
query_point_2d, results_2d_euclidean
);
// Query the tree for the 2 nearest neighbors to a point using Manhattan distance
let results_2d_manhattan = tree2d.knn_search::<ManhattanDistance>(&query_point_2d, 2);
// Print the results
println!(
"2 nearest neighbors to {:?} (Manhattan): {:?}",
query_point_2d, results_2d_manhattan
);
// --- 3D RTree Example ---
println!("\n--- 3D RTree Example ---");
let mut tree3d: RTree<Point3D<u32>> = RTree::new(4).unwrap();
// Insert some points
tree3d.insert(Point3D::new(10.0, 20.0, 30.0, Some(1)));
tree3d.insert(Point3D::new(80.0, 30.0, 40.0, Some(2)));
tree3d.insert(Point3D::new(45.0, 70.0, 50.0, Some(3)));
// Query the tree for the 2 nearest neighbors to a point
let query_point_3d = Point3D::new(12.0, 22.0, 32.0, None);
let results_3d_euclidean = tree3d.knn_search::<EuclideanDistance>(&query_point_3d, 2);
// Print the results
println!(
"2 nearest neighbors to {:?} (Euclidean): {:?}",
query_point_3d, results_3d_euclidean
);
// Query the tree for the 2 nearest neighbors to a point using Manhattan distance
let results_3d_manhattan = tree3d.knn_search::<ManhattanDistance>(&query_point_3d, 2);
// Print the results
println!(
"2 nearest neighbors to {:?} (Manhattan): {:?}",
query_point_3d, results_3d_manhattan
);
// Serialize the 2D tree
let encoded2d: Vec<u8> = bincode::serialize(&tree2d).unwrap();
let decoded2d: RTree<Point2D<u32>> = bincode::deserialize(&encoded2d[..]).unwrap();
let results_2d_decoded = decoded2d.knn_search::<EuclideanDistance>(&query_point_2d, 2);
println!(
"2 nearest neighbors to {:?} (Euclidean, decoded): {:?}",
query_point_2d, results_2d_decoded
);
// Serialize the 3D tree
let encoded3d: Vec<u8> = bincode::serialize(&tree3d).unwrap();
let decoded3d: RTree<Point3D<u32>> = bincode::deserialize(&encoded3d[..]).unwrap();
let results_3d_decoded = decoded3d.knn_search::<EuclideanDistance>(&query_point_3d, 2);
println!(
"2 nearest neighbors to {:?} (Euclidean, decoded): {:?}",
query_point_3d, results_3d_decoded
);
}