Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
r_tree_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
38#include <cassert>
39#include <iostream>
40#include <string>
41
42#include <tpl_r_tree.H>
43#include <tpl_r_star_tree.H>
44
45using namespace Aleph;
46using namespace std;
47
48static Rectangle box(const int x1, const int y1, const int x2, const int y2)
49{
51 Geom_Number(x2), Geom_Number(y2));
52}
53
54int main()
55{
56 cout << "Aleph-w RTree Example" << endl;
57 cout << "=====================" << endl;
58
59 // A dynamic spatial index mapping bounding boxes to named regions.
60 RTree<string> map;
61
62 map.insert(box(0, 0, 10, 10), "downtown");
63 map.insert(box(8, 8, 20, 20), "midtown");
64 map.insert(box(30, 30, 40, 40), "airport");
65 map.insert(box(5, 5, 6, 6), "central-park");
66
67 cout << "Indexed " << map.size() << " regions, tree height "
68 << map.height() << "." << endl;
69
70 // Which regions overlap the query window (7,7)-(9,9)?
71 cout << "Regions intersecting (7,7)-(9,9):";
72 map.for_each_intersecting(box(7, 7, 9, 9),
73 [](const Rectangle &, const string &name) { cout << ' ' << name; });
74 cout << endl;
75
76 // Which regions contain the point (35,35)?
78 cout << "Region(s) containing point (35,35):";
79 for (size_t i = 0; i < here.size(); ++i)
80 cout << ' ' << here(i);
81 cout << endl;
82 assert(here.size() == 1);
83
84 // A far-away query returns nothing.
85 assert(map.search_intersects(box(100, 100, 110, 110)).is_empty());
86
87 // Remove a region; the index stays consistent and queryable.
88 const bool removed = map.erase(box(30, 30, 40, 40), "airport");
90 assert(map.size() == 3);
91 assert(map.search_contains(Point(Geom_Number(35), Geom_Number(35))).is_empty());
92 assert(map.verify());
93
94 cout << "After removing 'airport': " << map.size()
95 << " regions, all invariants valid." << endl;
96
97 // RStarTree has the exact same API but uses the R*-tree heuristics
98 // (overlap-minimizing subtree choice, margin/overlap split and forced
99 // reinsertion), which typically yield less node overlap and faster queries.
101 rstar.insert(box(0, 0, 10, 10), "a");
102 rstar.insert(box(8, 8, 20, 20), "b");
103 rstar.insert(box(30, 30, 40, 40), "c");
104 assert(rstar.search_intersects(box(7, 7, 9, 9)).size() == 2);
105 assert(rstar.verify());
106 cout << "RStarTree indexed " << rstar.size()
107 << " regions with the same API." << endl;
108
109 return 0;
110}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:221
Dynamic R-tree indexing axis-aligned rectangles by payload.
Definition tpl_r_tree.H:118
void insert(const Rectangle &bbox, const Payload &value)
Insert a (bbox, value) entry, copying value.
Definition tpl_r_tree.H:969
bool erase(const Rectangle &bbox, const Payload &value)
Remove one entry equal to (bbox, value).
size_t height() const noexcept
Return the number of node levels (0 when empty, 1 for a lone leaf).
Definition tpl_r_tree.H:946
Array< Payload > search_intersects(const Rectangle &rect) const
Return the payloads of every entry whose bbox intersects rect.
Array< Payload > search_contains(const Point &p) const
Return the payloads of every entry whose bbox contains p.
size_t size() const noexcept
Return the number of stored entries.
Definition tpl_r_tree.H:937
void for_each_intersecting(const Rectangle &rect, F &&f) const
Invoke f for every entry whose bbox intersects rect.
bool verify() const
Verify the R-tree structural invariants.
An axis-aligned rectangle.
Definition point.H:1789
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_y1_function > > y1(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4114
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
size_t size(Node *root) noexcept
mpq_class Geom_Number
Numeric type used by the geometry module.
Definition point.H:113
STL namespace.
static Rectangle box(const int x1, const int y1, const int x2, const int y2)
int main()
R*-tree: an R-tree tuned with the Beckmann-Kriegel-Schneider-Seeger heuristics.
Dynamic R-tree spatial index over axis-aligned rectangles.