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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::envelope::Envelope;
use crate::object::RTreeObject;
use crate::params::{InsertionStrategy, RTreeParams};
use crate::point::Point;
use crate::rtree::{root_mut, RTree};
use crate::structures::node::{envelope_for_children, ParentNodeData, RTreeNode};
use num_traits::{Bounded, Zero};
pub enum RStarInsertionStrategy {}
enum InsertionResult<T>
where
T: RTreeObject,
{
Split(RTreeNode<T>),
Complete,
}
impl InsertionStrategy for RStarInsertionStrategy {
fn insert<T, Params>(tree: &mut RTree<T, Params>, t: T)
where
Params: RTreeParams,
T: RTreeObject,
{
let mut insertion_stack = vec![RTreeNode::Leaf(t)];
while let Some(next) = insertion_stack.pop() {
match recursive_insert::<_, Params>(root_mut(tree), next) {
InsertionResult::Split(node) => {
let old_root =
::std::mem::replace(root_mut(tree), ParentNodeData::new_root::<Params>());
let new_envelope = old_root.envelope.merged(&node.envelope());
let root = root_mut(tree);
root.envelope = new_envelope;
root.children.push(RTreeNode::Parent(old_root));
root.children.push(node);
}
InsertionResult::Complete => (),
}
}
}
}
fn recursive_insert<T, Params>(node: &mut ParentNodeData<T>, t: RTreeNode<T>) -> InsertionResult<T>
where
T: RTreeObject,
Params: RTreeParams,
{
node.envelope.merge(&t.envelope());
let expand_index = choose_subtree(node, &t);
if node.children.len() < expand_index {
node.children.push(t);
return resolve_overflow::<_, Params>(node);
}
let expand = if let RTreeNode::Parent(ref mut follow) = node.children[expand_index] {
recursive_insert::<_, Params>(follow, t)
} else {
panic!("This is a bug in rstar.")
};
match expand {
InsertionResult::Split(child) => {
node.envelope.merge(&child.envelope());
node.children.push(child);
resolve_overflow::<_, Params>(node)
}
InsertionResult::Complete => InsertionResult::Complete,
}
}
fn choose_subtree<'b, T>(node: &mut ParentNodeData<T>, to_insert: &'b RTreeNode<T>) -> usize
where
T: RTreeObject,
{
let all_leaves = match node.children.first() {
Some(RTreeNode::Leaf(_)) => return usize::max_value(),
Some(RTreeNode::Parent(ref data)) => {
data.children.first().map(|n| n.is_leaf()).unwrap_or(true)
}
_ => return usize::max_value(),
};
let zero: <<T::Envelope as Envelope>::Point as Point>::Scalar = Zero::zero();
let insertion_envelope = to_insert.envelope();
let mut inclusion_count = 0;
let mut min_area = <<T::Envelope as Envelope>::Point as Point>::Scalar::max_value();
let mut min_index = 0;
for (index, child) in node.children.iter().enumerate() {
let envelope = child.envelope();
if envelope.contains_envelope(&insertion_envelope) {
inclusion_count += 1;
let area = envelope.area();
if area < min_area {
min_area = area;
min_index = index;
}
}
}
if inclusion_count == 0 {
let mut min = (zero, zero, zero);
for (index, child1) in node.children.iter().enumerate() {
let envelope = child1.envelope();
let mut new_envelope = envelope;
new_envelope.merge(&insertion_envelope);
let overlap_increase = if all_leaves {
let mut overlap = zero;
let mut new_overlap = zero;
for child2 in &node.children {
if child1 as *const _ != child2 as *const _ {
let child_envelope = child2.envelope();
let temp1 = envelope.intersection_area(&child_envelope);
overlap = overlap + temp1;
let temp2 = new_envelope.intersection_area(&child_envelope);
new_overlap = new_overlap + temp2;
}
}
new_overlap - overlap
} else {
zero
};
let area = new_envelope.area();
let area_increase = area - envelope.area();
let new_min = (overlap_increase, area_increase, area);
if new_min < min || index == 0 {
min = new_min;
min_index = index;
}
}
}
min_index
}
fn resolve_overflow<T, Params>(node: &mut ParentNodeData<T>) -> InsertionResult<T>
where
T: RTreeObject,
Params: RTreeParams,
{
if node.children.len() > Params::MAX_SIZE {
let off_split = split::<_, Params>(node);
InsertionResult::Split(off_split)
} else {
InsertionResult::Complete
}
}
fn split<T, Params>(node: &mut ParentNodeData<T>) -> RTreeNode<T>
where
T: RTreeObject,
Params: RTreeParams,
{
let axis = get_split_axis::<_, Params>(node);
let zero = <<T::Envelope as Envelope>::Point as Point>::Scalar::zero();
debug_assert!(node.children.len() >= 2);
T::Envelope::sort_envelopes(axis, &mut node.children);
let mut best = (zero, zero);
let min_size = Params::MIN_SIZE;
let mut best_index = min_size;
for k in min_size..=node.children.len() - min_size {
let mut first_envelope = node.children[k - 1].envelope();
let mut second_envelope = node.children[k].envelope();
let (l, r) = node.children.split_at(k);
for child in l {
first_envelope.merge(&child.envelope());
}
for child in r {
second_envelope.merge(&child.envelope());
}
let overlap_value = first_envelope.intersection_area(&second_envelope);
let area_value = first_envelope.area() + second_envelope.area();
let new_best = (overlap_value, area_value);
if new_best < best || k == min_size {
best = new_best;
best_index = k;
}
}
let off_split = node.children.split_off(best_index);
node.envelope = envelope_for_children(&node.children);
RTreeNode::Parent(ParentNodeData::new_parent(off_split))
}
fn get_split_axis<T, Params>(node: &mut ParentNodeData<T>) -> usize
where
T: RTreeObject,
Params: RTreeParams,
{
let mut best_goodness = <<T::Envelope as Envelope>::Point as Point>::Scalar::max_value();
let mut best_axis = 0;
let min_size = Params::MIN_SIZE;
let until = node.children.len() - min_size + 1;
for axis in 0..<T::Envelope as Envelope>::Point::DIMENSIONS {
T::Envelope::sort_envelopes(axis, &mut node.children);
let mut first_envelope = T::Envelope::new_empty();
let mut second_envelope = T::Envelope::new_empty();
for child in &node.children[..min_size] {
first_envelope.merge(&child.envelope());
}
for child in &node.children[until..] {
second_envelope.merge(&child.envelope());
}
for k in min_size..until {
let mut first_modified = first_envelope;
let mut second_modified = second_envelope;
let (l, r) = node.children.split_at(k);
for child in l {
first_modified.merge(&child.envelope());
}
for child in r {
second_modified.merge(&child.envelope());
}
let margin_value = first_modified.margin_value() + second_modified.margin_value();
if best_goodness > margin_value {
best_axis = axis;
best_goodness = margin_value;
}
}
}
best_axis
}