Skip to main content

thunderdome/
thunderdome.rs

1// SPDX-FileCopyrightText: 2026 rstared contributors
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use geo_types::{Point, Polygon, line_string};
6use rstared::AABB;
7use rstared::RTreed;
8use thunderdome::Arena;
9
10fn main() {
11    // A generational arena of 2D polygons from `geo` will be the underlying
12    // collection.
13    let polygon_arena: Arena<Polygon<f64>> = Arena::new();
14
15    // Wrap `RTreed` around the arena.
16    let mut rtreed = RTreed::new(polygon_arena);
17
18    // Insert two polygons (two non-regular pentagons), recording them in the
19    // R-tree.
20    let first = rtreed.push(Polygon::new(
21        line_string![
22            (x: 0.0, y: 0.0),
23            (x: 2.0, y: 0.5),
24            (x: 2.5, y: 2.0),
25            (x: 1.0, y: 2.5),
26            (x: 0.0, y: 1.0),
27            (x: 0.0, y: 0.0),
28        ],
29        vec![],
30    ));
31    rtreed.push(Polygon::new(
32        line_string![
33            (x: 2.0, y: 2.0),
34            (x: 5.0, y: 2.5),
35            (x: 4.5, y: 4.0),
36            (x: 3.0, y: 5.0),
37            (x: 2.0, y: 2.0),
38        ],
39        vec![],
40    ));
41
42    // Locate the two polygons in the R-tree.
43    assert_eq!(
44        rtreed
45            .rtree()
46            .locate_in_envelope(AABB::from_corners(
47                Point::new(0.0, 0.0),
48                Point::new(5.0, 5.0),
49            ))
50            .count(),
51        2
52    );
53
54    // Now remove one of the polygons, recording this in the R-tree.
55    rtreed.remove(&first);
56
57    // Make the same query to the R-tree as before.
58    // Only one polygon is now present.
59    assert_eq!(
60        rtreed
61            .rtree()
62            .locate_in_envelope(AABB::from_corners(
63                Point::new(0.0, 0.0),
64                Point::new(5.0, 5.0),
65            ))
66            .count(),
67        1
68    );
69}
70
71#[test]
72fn test() {
73    main();
74}