spart 0.6.1

A collection of space partitioning tree data structures for Rust
Documentation
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! One harness, every tree.
//!
//! These tests are written against [`SpatialIndex`] rather than against a concrete tree, so each
//! assertion runs five times over the quadtree, octree, Kd-tree, R-tree, and R*-tree. Before the
//! trait existed the same checks had to be copied per tree, which is how the trees drifted into five
//! different contracts for the same operations in the first place.

use spart::errors::SpartError;
use spart::geometry::{Cube, EuclideanDistance, Point2D, Point3D, Rectangle};
use spart::index::SpatialIndex;
use spart::kdtree::KdTree;
use spart::octree::Octree;
use spart::quadtree::Quadtree;
use spart::rstar_tree::RStarTree;
use spart::rtree::RTree;

const BOUNDARY_2D: Rectangle = Rectangle {
    x: 0.0,
    y: 0.0,
    width: 100.0,
    height: 100.0,
};

const BOUNDARY_3D: Cube = Cube {
    x: 0.0,
    y: 0.0,
    z: 0.0,
    width: 100.0,
    height: 100.0,
    depth: 100.0,
};

fn points_2d(n: u32) -> Vec<Point2D<u32>> {
    (0..n)
        .map(|i| Point2D::new(f64::from(i * 7 % 100), f64::from(i * 13 % 100), Some(i)))
        .collect()
}

fn points_3d(n: u32) -> Vec<Point3D<u32>> {
    (0..n)
        .map(|i| {
            Point3D::new(
                f64::from(i * 7 % 100),
                f64::from(i * 13 % 100),
                f64::from(i * 11 % 100),
                Some(i),
            )
        })
        .collect()
}

/// Exercises the contract every index must satisfy.
///
/// `everything` has to be a volume covering every item in `items`.
fn check_contract<I>(mut index: I, items: Vec<I::Item>, everything: &I::Volume, name: &str)
where
    I: SpatialIndex,
    I::Item: Clone + PartialEq + std::fmt::Debug,
    EuclideanDistance: spart::geometry::DistanceMetric<I::Item>,
{
    assert!(index.is_empty(), "{name}: a fresh index should be empty");
    assert_eq!(index.len(), 0, "{name}: a fresh index should have length 0");
    assert!(
        index.range_search_bbox(everything).is_empty(),
        "{name}: a fresh index should answer no results"
    );
    assert!(
        index
            .knn_search::<EuclideanDistance>(&items[0], 3)
            .is_empty(),
        "{name}: a fresh index should have no neighbors"
    );

    // Insertion reports acceptance and moves the count.
    for (i, item) in items.iter().enumerate() {
        assert_eq!(
            SpatialIndex::insert(&mut index, item.clone()),
            Ok(true),
            "{name}: item {i} should have been accepted"
        );
        assert_eq!(index.len(), i + 1, "{name}: length after {} inserts", i + 1);
    }
    assert!(!index.is_empty(), "{name}: a populated index is not empty");

    // Everything inserted is present and findable.
    for (i, item) in items.iter().enumerate() {
        assert!(index.contains(item), "{name}: item {i} should be present");
    }
    assert_eq!(
        index.range_search_bbox(everything).len(),
        items.len(),
        "{name}: a covering box query must return every item"
    );

    // A nearest-neighbor search finds the item itself first, and respects k.
    for (i, item) in items.iter().enumerate() {
        let nearest = index.knn_search::<EuclideanDistance>(item, 1);
        assert_eq!(nearest.len(), 1, "{name}: item {i} nearest count");
        assert_eq!(nearest[0], item, "{name}: item {i} is its own nearest");
    }
    for k in [0usize, 1, 3, items.len(), items.len() + 5] {
        let found = index.knn_search::<EuclideanDistance>(&items[0], k);
        assert_eq!(
            found.len(),
            k.min(items.len()),
            "{name}: knn with k={k} should return min(k, len) items"
        );
    }

    // A radius of zero finds the item, and a negative radius finds nothing.
    assert!(
        index
            .range_search::<EuclideanDistance>(&items[0], 0.0)
            .iter()
            .any(|found| *found == &items[0]),
        "{name}: a zero radius should still match the query point"
    );
    assert!(
        index
            .range_search::<EuclideanDistance>(&items[0], -1.0)
            .is_empty(),
        "{name}: a negative radius should match nothing"
    );

    // Deletion removes exactly one item and is reported honestly.
    let mut remaining = items.len();
    for (i, item) in items.iter().enumerate() {
        assert!(
            index.delete(item),
            "{name}: delete of item {i} should succeed"
        );
        remaining -= 1;
        assert_eq!(index.len(), remaining, "{name}: length after deleting {i}");
        assert!(
            !index.delete(item),
            "{name}: item {i} should not be deletable twice"
        );
    }
    assert!(index.is_empty(), "{name}: emptied index should be empty");
    assert!(index.range_search_bbox(everything).is_empty());

    // The index is reusable after being emptied, and `clear` empties it again.
    for item in &items {
        assert_eq!(SpatialIndex::insert(&mut index, item.clone()), Ok(true));
    }
    assert_eq!(index.len(), items.len(), "{name}: refilled length");
    index.clear();
    assert!(index.is_empty(), "{name}: clear should empty the index");
    assert_eq!(index.len(), 0);
    assert!(index.range_search_bbox(everything).is_empty());

    // Bulk insertion reports its count and agrees with the individual path.
    assert_eq!(
        SpatialIndex::insert_bulk(&mut index, items.clone()),
        Ok(items.len()),
        "{name}: insert_bulk should report every stored item"
    );
    assert_eq!(index.len(), items.len(), "{name}: length after insert_bulk");
    assert_eq!(index.range_search_bbox(everything).len(), items.len());
    for item in &items {
        assert!(
            index.contains(item),
            "{name}: bulk-inserted item should be present"
        );
    }
}

#[test]
fn quadtree_honors_the_contract() {
    let tree: Quadtree<u32> = Quadtree::new(&BOUNDARY_2D, 4).unwrap();
    check_contract(tree, points_2d(40), &BOUNDARY_2D, "Quadtree");
}

#[test]
fn octree_honors_the_contract() {
    let tree: Octree<u32> = Octree::new(&BOUNDARY_3D, 4).unwrap();
    check_contract(tree, points_3d(40), &BOUNDARY_3D, "Octree");
}

#[test]
fn kdtree_2d_honors_the_contract() {
    let tree: KdTree<Point2D<u32>> = KdTree::new();
    check_contract(tree, points_2d(40), &BOUNDARY_2D, "KdTree2D");
}

#[test]
fn kdtree_3d_honors_the_contract() {
    let tree: KdTree<Point3D<u32>> = KdTree::new();
    check_contract(tree, points_3d(40), &BOUNDARY_3D, "KdTree3D");
}

#[test]
fn rtree_2d_honors_the_contract() {
    let tree: RTree<Point2D<u32>> = RTree::new(4).unwrap();
    check_contract(tree, points_2d(40), &BOUNDARY_2D, "RTree2D");
}

#[test]
fn rtree_3d_honors_the_contract() {
    let tree: RTree<Point3D<u32>> = RTree::new(4).unwrap();
    check_contract(tree, points_3d(40), &BOUNDARY_3D, "RTree3D");
}

#[test]
fn rstar_tree_2d_honors_the_contract() {
    let tree: RStarTree<Point2D<u32>> = RStarTree::new(4).unwrap();
    check_contract(tree, points_2d(40), &BOUNDARY_2D, "RStarTree2D");
}

#[test]
fn rstar_tree_3d_honors_the_contract() {
    let tree: RStarTree<Point3D<u32>> = RStarTree::new(4).unwrap();
    check_contract(tree, points_3d(40), &BOUNDARY_3D, "RStarTree3D");
}

/// The bounded trees decline a point outside their boundary rather than erroring or panicking.
#[test]
fn bounded_trees_decline_out_of_bounds_points() {
    let mut quadtree: Quadtree<u32> = Quadtree::new(&BOUNDARY_2D, 4).unwrap();
    assert_eq!(
        SpatialIndex::insert(&mut quadtree, Point2D::new(1000.0, 1000.0, Some(1))),
        Ok(false)
    );
    assert!(quadtree.is_empty());
    assert_eq!(
        SpatialIndex::insert_bulk(
            &mut quadtree,
            vec![
                Point2D::new(10.0, 10.0, Some(1)),
                Point2D::new(1000.0, 1000.0, Some(2)),
            ]
        ),
        Ok(1),
        "insert_bulk counts only the points it stored"
    );
    assert_eq!(quadtree.len(), 1);

    let mut octree: Octree<u32> = Octree::new(&BOUNDARY_3D, 4).unwrap();
    assert_eq!(
        SpatialIndex::insert(&mut octree, Point3D::new(1000.0, 1000.0, 1000.0, Some(1))),
        Ok(false)
    );
    assert!(octree.is_empty());
}

/// The unbounded trees accept anything a bounded tree would refuse.
#[test]
fn unbounded_trees_accept_any_coordinates() {
    let far = Point2D::new(-1.0e9, 1.0e9, Some(1));

    let mut kdtree: KdTree<Point2D<u32>> = KdTree::new();
    assert_eq!(SpatialIndex::insert(&mut kdtree, far.clone()), Ok(true));
    assert!(kdtree.contains(&far));

    let mut rtree: RTree<Point2D<u32>> = RTree::new(4).unwrap();
    assert_eq!(SpatialIndex::insert(&mut rtree, far.clone()), Ok(true));
    assert!(rtree.contains(&far));

    let mut rstar: RStarTree<Point2D<u32>> = RStarTree::new(4).unwrap();
    assert_eq!(SpatialIndex::insert(&mut rstar, far.clone()), Ok(true));
    assert!(rstar.contains(&far));
}

/// A Kd-tree is the only index that can reject an item outright, and it must not be left changed.
#[test]
fn kdtree_rejects_a_mismatched_dimension_without_changing() {
    let mut tree: KdTree<Point2D<u32>> = KdTree::with_dimension(3);
    let point = Point2D::new(1.0, 2.0, Some(1));

    let outcome = SpatialIndex::insert(&mut tree, point.clone());
    assert!(
        matches!(
            outcome,
            Err(SpartError::DimensionMismatch {
                expected: 3,
                actual: 2
            })
        ),
        "expected a dimension mismatch, got {outcome:?}"
    );
    assert!(
        tree.is_empty(),
        "a rejected insert must leave the tree empty"
    );

    let outcome = SpatialIndex::insert_bulk(&mut tree, vec![point]);
    assert!(matches!(outcome, Err(SpartError::DimensionMismatch { .. })));
    assert!(
        tree.is_empty(),
        "a rejected batch must leave the tree untouched"
    );
}

/// Duplicates are stored independently, and one delete removes one copy.
#[test]
fn every_index_keeps_duplicates_separate() {
    fn check<I>(mut index: I, item: I::Item, everything: &I::Volume, name: &str)
    where
        I: SpatialIndex,
        I::Item: Clone + PartialEq + std::fmt::Debug,
    {
        for _ in 0..3 {
            assert_eq!(SpatialIndex::insert(&mut index, item.clone()), Ok(true));
        }
        assert_eq!(index.len(), 3, "{name}: three duplicates stored");
        assert_eq!(index.range_search_bbox(everything).len(), 3);

        assert!(index.delete(&item));
        assert_eq!(index.len(), 2, "{name}: one delete removes one copy");
        assert_eq!(index.range_search_bbox(everything).len(), 2);
        assert!(index.contains(&item), "{name}: copies remain");
    }

    let point = Point2D::new(10.0, 10.0, Some(1u32));
    check(
        Quadtree::<u32>::new(&BOUNDARY_2D, 4).unwrap(),
        point.clone(),
        &BOUNDARY_2D,
        "Quadtree",
    );
    check(
        KdTree::<Point2D<u32>>::new(),
        point.clone(),
        &BOUNDARY_2D,
        "KdTree2D",
    );
    check(
        RTree::<Point2D<u32>>::new(4).unwrap(),
        point.clone(),
        &BOUNDARY_2D,
        "RTree2D",
    );
    check(
        RStarTree::<Point2D<u32>>::new(4).unwrap(),
        point,
        &BOUNDARY_2D,
        "RStarTree2D",
    );

    // The 3D trees too: coincident points are what drive the octree into its depth-capped bucket
    // branch, which is the path that touches the subtree counts.
    let point = Point3D::new(10.0, 10.0, 10.0, Some(1u32));
    check(
        Octree::<u32>::new(&BOUNDARY_3D, 4).unwrap(),
        point.clone(),
        &BOUNDARY_3D,
        "Octree",
    );
    check(
        KdTree::<Point3D<u32>>::new(),
        point.clone(),
        &BOUNDARY_3D,
        "KdTree3D",
    );
    check(
        RTree::<Point3D<u32>>::new(4).unwrap(),
        point.clone(),
        &BOUNDARY_3D,
        "RTree3D",
    );
    check(
        RStarTree::<Point3D<u32>>::new(4).unwrap(),
        point,
        &BOUNDARY_3D,
        "RStarTree3D",
    );
}

/// Enough coincident points to push the bounded trees past their depth cap, where a node stops
/// subdividing and keeps points directly. The subtree counts have to stay exact through that.
#[test]
fn bounded_trees_count_correctly_past_the_depth_cap() {
    let mut quadtree: Quadtree<u32> = Quadtree::new(&BOUNDARY_2D, 1).unwrap();
    let points: Vec<_> = (0..30).map(|i| Point2D::new(10.0, 10.0, Some(i))).collect();
    for (i, point) in points.iter().enumerate() {
        assert_eq!(SpatialIndex::insert(&mut quadtree, point.clone()), Ok(true));
        assert_eq!(
            quadtree.len(),
            i + 1,
            "quadtree len after {} inserts",
            i + 1
        );
    }
    for (i, point) in points.iter().enumerate() {
        assert!(quadtree.delete(point));
        assert_eq!(quadtree.len(), points.len() - i - 1);
    }
    assert!(quadtree.is_empty());

    let mut octree: Octree<u32> = Octree::new(&BOUNDARY_3D, 1).unwrap();
    let points: Vec<_> = (0..30)
        .map(|i| Point3D::new(10.0, 10.0, 10.0, Some(i)))
        .collect();
    for (i, point) in points.iter().enumerate() {
        assert_eq!(SpatialIndex::insert(&mut octree, point.clone()), Ok(true));
        assert_eq!(octree.len(), i + 1, "octree len after {} inserts", i + 1);
    }
    for (i, point) in points.iter().enumerate() {
        assert!(octree.delete(point));
        assert_eq!(octree.len(), points.len() - i - 1);
    }
    assert!(octree.is_empty());
}

/// A query for zero neighbors must not walk the tree.
///
/// `KnnHeap::worst` reports the distance to beat, and when no neighbors are wanted that has to be
/// negative infinity so every prune test fails closed. Reporting infinity instead made each tree
/// traverse itself in full to return an empty vector.
#[test]
fn a_zero_neighbor_query_prunes_immediately() {
    use std::sync::atomic::{AtomicUsize, Ordering};

    static CALLS: AtomicUsize = AtomicUsize::new(0);

    struct Counting;
    impl<T> spart::geometry::DistanceMetric<Point2D<T>> for Counting {
        fn distance_sq(a: &Point2D<T>, b: &Point2D<T>) -> f64 {
            CALLS.fetch_add(1, Ordering::Relaxed);
            <EuclideanDistance as spart::geometry::DistanceMetric<Point2D<T>>>::distance_sq(a, b)
        }
    }

    fn check<I>(mut index: I, points: Vec<Point2D<u32>>, name: &str)
    where
        I: SpatialIndex<Item = Point2D<u32>>,
    {
        for point in points {
            let _ = SpatialIndex::insert(&mut index, point);
        }
        CALLS.store(0, Ordering::Relaxed);
        let found = index.knn_search::<Counting>(&Point2D::new(1.0, 1.0, None), 0);
        let calls = CALLS.load(Ordering::Relaxed);
        assert!(found.is_empty(), "{name}: k=0 must return nothing");
        assert_eq!(
            calls, 0,
            "{name}: k=0 computed {calls} distances, so it walked the tree"
        );
    }

    let points = points_2d(500);
    check(
        Quadtree::<u32>::new(&BOUNDARY_2D, 4).unwrap(),
        points.clone(),
        "Quadtree",
    );
    check(KdTree::<Point2D<u32>>::new(), points.clone(), "KdTree");
    check(
        RTree::<Point2D<u32>>::new(4).unwrap(),
        points.clone(),
        "RTree",
    );
    check(
        RStarTree::<Point2D<u32>>::new(4).unwrap(),
        points,
        "RStarTree",
    );
}

/// `range_search_bbox` must mean containment on every tree.
///
/// The R-tree family prunes on bounding-volume intersection, so an inflated volume for a single
/// point made it report points just outside the query while the point-exact trees did not.
#[test]
fn box_queries_are_exact_on_every_tree() {
    // Just outside the query's low edge, by less than the old point extent of 1e-10.
    let outside = Point2D::new(10.0 - 5e-11, 5.0, Some(1u32));
    let inside = Point2D::new(10.0, 5.0, Some(2u32));
    let query = Rectangle {
        x: 10.0,
        y: 0.0,
        width: 10.0,
        height: 10.0,
    };
    assert!(!query.contains(&outside), "the probe point must be outside");
    assert!(query.contains(&inside), "the control point must be inside");

    fn check<I>(mut index: I, items: &[Point2D<u32>], query: &I::Volume, name: &str)
    where
        I: SpatialIndex<Item = Point2D<u32>>,
    {
        for item in items {
            let _ = SpatialIndex::insert(&mut index, item.clone());
        }
        let found = index.range_search_bbox(query);
        assert_eq!(
            found.len(),
            1,
            "{name}: a box query returned {} items, expected only the contained one",
            found.len()
        );
        assert_eq!(found[0].data, Some(2), "{name}: wrong item returned");
    }

    let items = [outside, inside];
    check(
        Quadtree::<u32>::new(&BOUNDARY_2D, 4).unwrap(),
        &items,
        &query,
        "Quadtree",
    );
    check(KdTree::<Point2D<u32>>::new(), &items, &query, "KdTree");
    check(
        RTree::<Point2D<u32>>::new(4).unwrap(),
        &items,
        &query,
        "RTree",
    );
    check(
        RStarTree::<Point2D<u32>>::new(4).unwrap(),
        &items,
        &query,
        "RStarTree",
    );
}