41volatile size_t sink = 0;
43Rectangle rect(
const int x1,
const int y1,
const int x2,
const int y2)
50double best_ms(Fn fn,
const int repeats = 3)
53 for (
int r = 0;
r < repeats; ++
r)
55 const auto t0 = std::chrono::steady_clock::now();
57 const auto t1 = std::chrono::steady_clock::now();
58 const double ms = std::chrono::duration<double, std::milli>(t1 - t0).count();
59 best = std::min(best, ms);
64void row(
const char *index,
const char *op,
const double ms)
66 std::printf(
" %-12s %-28s %10.2f ms\n", index, op, ms);
69std::vector<Rectangle> make_rectangles(
const size_t n,
const unsigned seed)
72 std::uniform_int_distribution<int> coord(0, 20000);
73 std::uniform_int_distribution<int> extent(1, 80);
75 std::vector<Rectangle>
out;
77 for (
size_t i = 0; i < n; ++i)
79 const int x = coord(
rng);
80 const int y = coord(
rng);
81 out.push_back(rect(x,
y, x + extent(
rng),
y + extent(
rng)));
89 for (
size_t i = 0; i < rects.size(); ++i)
97 for (
size_t i = 0; i < rects.size(); ++i)
102AABBTree build_aabb(
const std::vector<Rectangle> &rects)
106 for (
size_t i = 0; i < rects.size(); ++i)
118bool parse_size(
const char *text,
size_t &
out)
122 const std::string_view sv{text};
123 if (sv.empty() or sv.front() ==
'+' or sv.front() ==
'-')
125 const auto [ptr, ec] = std::from_chars(sv.data(), sv.data() + sv.size(),
out, 10);
126 return ec == std::errc{}
and ptr == sv.data() + sv.size();
129size_t brute_query_count(
const std::vector<Rectangle> &rects,
130 const std::vector<Rectangle> &queries)
135 total += b.intersects(q);
140size_t rtree_query_count(
const Tree &tree,
const std::vector<Rectangle> &queries)
144 tree.for_each_intersecting(q,
145 [&total](const
Rectangle &, const size_t &) { ++total; });
149size_t aabb_query_count(
const AABBTree &tree,
const std::vector<Rectangle> &queries)
153 total += tree.query(q).
size();
157void validate_counts(
const std::vector<Rectangle> &rects,
158 const std::vector<Rectangle> &queries,
163 const size_t brute = brute_query_count(rects, queries);
164 const size_t rt = rtree_query_count(rtree, queries);
165 const size_t rs = rtree_query_count(rstar, queries);
166 const size_t ab = aabb_query_count(aabb, queries);
167 if (brute != rt or brute != rs or brute != ab)
170 "validation failed: brute=%zu rtree=%zu rstar=%zu aabb=%zu\n",
174 sink += brute + rt + rs + ab;
179int main(
int argc,
char *argv[])
181 size_t entry_count = 10000;
182 size_t query_count = 2000;
184 if (argc > 1
and not parse_size(argv[1], entry_count))
186 std::fprintf(stderr,
"invalid entry_count: '%s'\n", argv[1]);
189 if (argc > 2
and not parse_size(argv[2], query_count))
191 std::fprintf(stderr,
"invalid query_count: '%s'\n", argv[2]);
195 const std::vector<Rectangle> rects = make_rectangles(entry_count, 0xA1E9u);
196 const std::vector<Rectangle> queries = make_rectangles(query_count, 0xBEEFu);
200 const AABBTree aabb = build_aabb(rects);
201 validate_counts(rects, queries, rtree, rstar, aabb);
203 std::printf(
"Spatial index benchmark (best of 3 runs, %zu entries, %zu queries)\n",
204 entry_count, query_count);
205 row(
"RTree",
"incremental insert",
207 row(
"RStarTree",
"incremental insert",
209 row(
"AABBTree",
"static build",
210 best_ms([&] {
const AABBTree t = build_aabb(rects); sink += t.
size(); }));
212 row(
"brute",
"intersect queries",
213 best_ms([&] { sink += brute_query_count(rects, queries); }));
214 row(
"RTree",
"intersect queries",
215 best_ms([&] { sink += rtree_query_count(rtree, queries); }));
216 row(
"RStarTree",
"intersect queries",
217 best_ms([&] { sink += rtree_query_count(rstar, queries); }));
218 row(
"AABBTree",
"intersect queries",
219 best_ms([&] { sink += aabb_query_count(aabb, queries); }));
221 std::printf(
"\n(sink=%zu)\n",
static_cast<size_t>(sink));
size_t size_t int32_t * out
Axis-aligned bounding box tree for spatial queries.
size_t build(Array< size_t > &idx, const size_t lo, const size_t hi)
size_t size() const
Number of entries.
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Dynamic R-tree indexing axis-aligned rectangles by payload.
void insert(const Rectangle &bbox, const Payload &value)
Insert a (bbox, value) entry, copying value.
size_t size() const noexcept
Return the number of stored entries.
An axis-aligned rectangle.
QuadTree - Hierarchical spatial index for 2D points.
Computational geometry algorithms.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_y1_function > > y1(const __gmp_expr< T, U > &expr)
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
and
Check uniqueness with explicit hash + equality functors.
mpq_class Geom_Number
Numeric type used by the geometry module.
An entry in the tree, consisting of a bounding box and a user-defined index.
R*-tree: an R-tree tuned with the Beckmann-Kriegel-Schneider-Seeger heuristics.
Dynamic R-tree spatial index over axis-aligned rectangles.