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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
//! [Dynamization](https://en.wikipedia.org/wiki/Dynamization) for nearest neighbor search.
use acap::distance::Proximity;
use acap::kd::FlatKdTree;
use acap::knn::{NearestNeighbors, Neighborhood};
use acap::vp::FlatVpTree;
use std::iter;
/// The number of bits dedicated to the flat buffer.
const BUFFER_BITS: usize = 6;
/// The maximum size of the buffer.
const BUFFER_SIZE: usize = 1 << BUFFER_BITS;
/// A dynamic wrapper for a static nearest neighbor search data structure.
///
/// This type applies [dynamization](https://en.wikipedia.org/wiki/Dynamization) to an arbitrary
/// nearest neighbor search structure `T`, allowing new items to be added dynamically.
#[derive(Debug)]
pub struct Forest<T: IntoIterator> {
/// A flat buffer used for the first few items, to avoid repeatedly rebuilding small trees.
buffer: Vec<T::Item>,
/// The trees of the forest, with sizes in geometric progression.
trees: Vec<Option<T>>,
}
impl<T, U> Forest<U>
where
U: FromIterator<T> + IntoIterator<Item = T>,
{
/// Create a new empty forest.
pub fn new() -> Self {
Self {
buffer: Vec::new(),
trees: Vec::new(),
}
}
/// Add a new item to the forest.
pub fn push(&mut self, item: T) {
self.extend(iter::once(item));
}
/// Get the number of items in the forest.
pub fn len(&self) -> usize {
let mut len = self.buffer.len();
for (i, slot) in self.trees.iter().enumerate() {
if slot.is_some() {
len += 1 << (i + BUFFER_BITS);
}
}
len
}
/// Check if this forest is empty.
pub fn is_empty(&self) -> bool {
if !self.buffer.is_empty() {
return false;
}
self.trees.iter().flatten().next().is_none()
}
}
impl<T, U> Default for Forest<U>
where
U: FromIterator<T> + IntoIterator<Item = T>,
{
fn default() -> Self {
Self::new()
}
}
impl<T, U> Extend<T> for Forest<U>
where
U: FromIterator<T> + IntoIterator<Item = T>,
{
fn extend<I: IntoIterator<Item = T>>(&mut self, items: I) {
self.buffer.extend(items);
if self.buffer.len() < BUFFER_SIZE {
return;
}
let len = self.len();
for i in 0.. {
let bit = 1 << (i + BUFFER_BITS);
if bit > len {
break;
}
if i >= self.trees.len() {
self.trees.push(None);
}
if len & bit == 0 {
if let Some(tree) = self.trees[i].take() {
self.buffer.extend(tree);
}
} else if self.trees[i].is_none() {
let offset = self.buffer.len() - bit;
self.trees[i] = Some(self.buffer.drain(offset..).collect());
}
}
debug_assert!(self.buffer.len() < BUFFER_SIZE);
debug_assert!(self.len() == len);
}
}
impl<T, U> FromIterator<T> for Forest<U>
where
U: FromIterator<T> + IntoIterator<Item = T>,
{
fn from_iter<I: IntoIterator<Item = T>>(items: I) -> Self {
let mut forest = Self::new();
forest.extend(items);
forest
}
}
impl<T: IntoIterator> IntoIterator for Forest<T> {
type Item = T::Item;
type IntoIter = std::vec::IntoIter<T::Item>;
fn into_iter(mut self) -> Self::IntoIter {
self.buffer.extend(self.trees.into_iter().flatten().flatten());
self.buffer.into_iter()
}
}
impl<K, V, T> NearestNeighbors<K, V> for Forest<T>
where
K: Proximity<V>,
T: NearestNeighbors<K, V>,
T: IntoIterator<Item = V>,
{
fn search<'k, 'v, N>(&'v self, mut neighborhood: N) -> N
where
K: 'k,
V: 'v,
N: Neighborhood<&'k K, &'v V>
{
for item in &self.buffer {
neighborhood.consider(item);
}
self.trees
.iter()
.flatten()
.fold(neighborhood, |n, t| t.search(n))
}
}
/// A forest of k-d trees.
pub type KdForest<T> = Forest<FlatKdTree<T>>;
/// A forest of vantage-point trees.
pub type VpForest<T> = Forest<FlatVpTree<T>>;
#[cfg(test)]
mod tests {
use super::*;
use acap::euclid::Euclidean;
use acap::exhaustive::ExhaustiveSearch;
use acap::knn::{NearestNeighbors, Neighbor};
use rand::prelude::*;
type Point = Euclidean<[f32; 3]>;
fn test_empty<T, F>(from_iter: &F)
where
T: NearestNeighbors<Point>,
F: Fn(Vec<Point>) -> T,
{
let points = Vec::new();
let index = from_iter(points);
let target = Euclidean([0.0, 0.0, 0.0]);
assert_eq!(index.nearest(&target), None);
assert_eq!(index.nearest_within(&target, 1.0), None);
assert!(index.k_nearest(&target, 0).is_empty());
assert!(index.k_nearest(&target, 3).is_empty());
assert!(index.k_nearest_within(&target, 0, 1.0).is_empty());
assert!(index.k_nearest_within(&target, 3, 1.0).is_empty());
}
fn test_pythagorean<T, F>(from_iter: &F)
where
T: NearestNeighbors<Point>,
F: Fn(Vec<Point>) -> T,
{
let points = vec![
Euclidean([3.0, 4.0, 0.0]),
Euclidean([5.0, 0.0, 12.0]),
Euclidean([0.0, 8.0, 15.0]),
Euclidean([1.0, 2.0, 2.0]),
Euclidean([2.0, 3.0, 6.0]),
Euclidean([4.0, 4.0, 7.0]),
];
let index = from_iter(points);
let target = Euclidean([0.0, 0.0, 0.0]);
assert_eq!(
index.nearest(&target).expect("No nearest neighbor found"),
Neighbor::new(&Euclidean([1.0, 2.0, 2.0]), 3.0)
);
assert_eq!(index.nearest_within(&target, 2.0), None);
assert_eq!(
index.nearest_within(&target, 4.0).expect("No nearest neighbor found within 4.0"),
Neighbor::new(&Euclidean([1.0, 2.0, 2.0]), 3.0)
);
assert!(index.k_nearest(&target, 0).is_empty());
assert_eq!(
index.k_nearest(&target, 3),
vec![
Neighbor::new(&Euclidean([1.0, 2.0, 2.0]), 3.0),
Neighbor::new(&Euclidean([3.0, 4.0, 0.0]), 5.0),
Neighbor::new(&Euclidean([2.0, 3.0, 6.0]), 7.0),
]
);
assert!(index.k_nearest(&target, 0).is_empty());
assert_eq!(
index.k_nearest_within(&target, 3, 6.0),
vec![
Neighbor::new(&Euclidean([1.0, 2.0, 2.0]), 3.0),
Neighbor::new(&Euclidean([3.0, 4.0, 0.0]), 5.0),
]
);
assert_eq!(
index.k_nearest_within(&target, 3, 8.0),
vec![
Neighbor::new(&Euclidean([1.0, 2.0, 2.0]), 3.0),
Neighbor::new(&Euclidean([3.0, 4.0, 0.0]), 5.0),
Neighbor::new(&Euclidean([2.0, 3.0, 6.0]), 7.0),
]
);
}
fn test_random_points<T, F>(from_iter: &F)
where
T: NearestNeighbors<Point>,
F: Fn(Vec<Point>) -> T,
{
let mut points = Vec::new();
for _ in 0..255 {
points.push(Euclidean([random(), random(), random()]));
}
let target = Euclidean([random(), random(), random()]);
let eindex = ExhaustiveSearch::from_iter(points.clone());
let index = from_iter(points);
assert_eq!(index.k_nearest(&target, 3), eindex.k_nearest(&target, 3));
}
/// Test a [NearestNeighbors] impl.
fn test_nearest_neighbors<T, F>(from_iter: F)
where
T: NearestNeighbors<Point>,
F: Fn(Vec<Point>) -> T,
{
test_empty(&from_iter);
test_pythagorean(&from_iter);
test_random_points(&from_iter);
}
#[test]
fn test_exhaustive_forest() {
test_nearest_neighbors(Forest::<ExhaustiveSearch<_>>::from_iter);
}
#[test]
fn test_forest_forest() {
test_nearest_neighbors(Forest::<Forest<ExhaustiveSearch<_>>>::from_iter);
}
#[test]
fn test_kd_forest() {
test_nearest_neighbors(KdForest::from_iter);
}
#[test]
fn test_vp_forest() {
test_nearest_neighbors(VpForest::from_iter);
}
}
|