-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpatialIndexing.rs
97 lines (79 loc) · 2.24 KB
/
SpatialIndexing.rs
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
94
95
96
97
/********************************** Data *******************************/
#[derive(Debug, Clone)]
struct Cell {
id: i64,
x: i32,
y: i32,
}
#[derive(Debug)]
struct CellIndirector {
idx: i64,
cells: Vec<Cell>,
children: Vec<CellIndirector>,
}
/********************************** Impl *******************************/
impl From<(i64, i32, i32)> for Cell {
fn from((id, x, y): (i64, i32, i32)) -> Self {
Self { id, x, y }
}
}
impl CellIndirector {
fn get_position(&self, id: i64) -> Option<&Cell> {
// Leaf node (data is only contained in leaf nodes)
if self.children.is_empty() {
self.cells.iter().find(|c| c.id == id)
} else {
// Visits all children in a linear fashion
self.children.iter().find_map(|n| n.get_position(id))
}
}
fn partition_until_bucket_size(&mut self, size: usize) {
if self.cells.len() <= size {
return;
}
let cells = &mut self.cells;
cells.sort_by(|u, v| u.x.cmp(&v.x).then_with(|| u.y.cmp(&v.y)));
let (left, right) = cells.split_at(size);
let left_node = CellIndirector {
idx: self.idx + 1,
cells: left.to_vec(),
children: vec![],
};
let mut right_node = CellIndirector {
idx: self.idx + 2,
cells: right.to_vec(),
children: vec![],
};
right_node.partition_until_bucket_size(size);
if !left.is_empty() {
self.children.push(left_node);
}
if !right.is_empty() {
self.children.push(right_node);
}
self.cells.clear();
}
fn add_point(&mut self, point: (i64, i32, i32)) {
self.cells.push(point.into());
}
}
/********************************** Run *******************************/
fn main() {
let mut root = CellIndirector {
idx: 0,
cells: vec![],
children: vec![],
};
// Add data
let mut idx: i64 = 0;
for y in 0..3 {
for x in 0..3 {
root.add_point((idx, x, y));
idx += 1;
}
}
// Query
println!("{:?}", root.get_position(6));
root.partition_until_bucket_size(1);
println!("{:?}", root);
}