A Rust library providing efficient Hilbert R-tree implementation for spatial queries on axis-aligned bounding boxes (AABBs).
- Hilbert Curve Ordering: Uses Hilbert space-filling curve for improved spatial locality (inspired by Flatbush algorithm)
- AABB Intersection Queries: Fast rectangular bounding box intersection testing
- Zero-Copy: Single contiguous buffer layout - safe for parallel queries with no allocations per query
- Simple API: Easy to use with minimal setup
- Static Optimization: Efficient for static or infrequently-modified spatial data. Changed data is handled by rebuilding the index (see Changing Data)
Add this to your Cargo.toml:
[dependencies]
aabb = "0.8"use aabb::prelude::*;
fn main() {
let mut tree = AABB::with_capacity(3);
// Add bounding boxes (min_x, min_y, max_x, max_y)
tree.add(0.0, 0.0, 1.0, 1.0);
tree.add(0.5, 0.5, 1.5, 1.5);
tree.add(2.0, 2.0, 3.0, 3.0);
// Build the spatial index
tree.build();
// Query for intersecting boxes
let mut results = Vec::new();
// bbox: xmin, ymin, xmax, ymax
tree.query_intersecting(0.7, 0.7, 1.3, 1.3, &mut results);
println!("Found {} intersecting boxes", results.len());
// Results contains indices of boxes that intersect the query
}use aabb::prelude::*;
fn main() {
let mut tree = AABB::with_capacity(4);
// Add points using the convenient add_point() method
tree.add_point(0.0, 0.0);
tree.add_point(1.0, 1.0);
tree.add_point(2.0, 2.0);
tree.add_point(5.0, 5.0);
// Build the spatial index
tree.build();
// Query for points within a circular region (optimized for point data)
let mut results = Vec::new();
tree.query_circle_points(0.0, 0.0, 2.5, &mut results);
println!("Found {} points within radius 2.5", results.len());
// Find K nearest points
let mut results = Vec::new();
tree.query_nearest_k_points(0.0, 0.0, 2, &mut results);
println!("Found {} nearest points", results.len());
}The Hilbert R-tree stores bounding boxes in a flat array and sorts them by their Hilbert curve index (computed from box centers). This provides good spatial locality for most spatial queries while maintaining a simple, cache-friendly data structure.
The Hilbert space-filling curve is a continuous fractal curve that visits every cell in a 2D grid exactly once, maintaining proximity in space:
The curve preserves spatial locality - points close to each other in 2D space tend to be close along the Hilbert curve order. This property makes the flat array layout extremely cache-friendly for spatial queries.
The index is built in one pass and there is no way to move or remove a single item. When your data changes, rebuild the index. A full rebuild takes about 5 ms for 100,000 items.
This works the same for AABB (f64) and AABBI32:
- Add and rebuild: call
add()for the new items, thenbuild()again. Every item keeps the idadd()gave it. Queries return no results betweenadd()and the nextbuild(). - Clear and refill: call
clear(), add the current items andbuild(). This reuses the tree's memory, which suits data rebuilt every frame. Ids start from 0 again afterclear().
use aabb::prelude::*;
fn main() {
let mut tree = AABB::with_capacity(100);
for frame in 0..3 {
// Rebuild the index from the current positions
tree.clear();
for i in 0..100 {
let x = i as f64 + frame as f64; // objects move each frame
tree.add(x, 0.0, x + 1.0, 1.0); // id == i, every frame
}
tree.build();
let mut results = Vec::new();
tree.query_intersecting(0.0, 0.0, 5.0, 1.0, &mut results);
println!("frame {frame}: {} boxes near the origin", results.len());
}
}build() on a tree that has not changed since the last build does nothing. get(id) builds a small id lookup table the first time it is called after a build (4 bytes per item), so repeated calls are fast.
HilbertRTree::new()orAABB::new()- Create a new empty treeHilbertRTree::with_capacity(capacity)orAABB::with_capacity(capacity)- Create a new tree with preallocated capacityHilbertRTreeI32::new()orAABBI32::new()- Create a new empty treeHilbertRTreeI32::with_capacity(capacity)orAABBI32::with_capacity(capacity)- Create a new tree with preallocated capacityadd(min_x, min_y, max_x, max_y)-(f64, i32)Add a bounding boxadd_point(x, y)-(f64)Add a point (convenience method - internally stores as (x, y, x, y))build()-(f64, i32)Build the spatial index (required before querying). It can be called again after moreadd()calls; items keep their idsclear()-(f64, i32)Remove all items but keep the allocated memory, so the tree can be refilled and rebuilt. Ids start from 0 againget(item_id)-(f64, i32)Retrieve the bounding box for an item by its IDget_point(item_id)-(f64)Retrieve a point as (x, y) for items added withadd_point()save(path)-(f64, i32)Save the built tree to a file for fast loading laterload(path)-(f64, i32)Load a previously saved tree from a file
query_intersecting(min_x, min_y, max_x, max_y, results)(f64, i32)- Find boxes that intersect a rectanglequery_intersecting_id(item_id, results)(f64, i32)- Find boxes that intersect with a specific item already in the tree (by ID), excluding the item itselfquery_intersecting_k(min_x, min_y, max_x, max_y, k, results)(f64, i32)- Find first K intersecting boxesquery_point(x, y, results)(f64, i32)- Find boxes that contain a pointquery_contain(min_x, min_y, max_x, max_y, results)(f64, i32)- Find boxes that contain a rectanglequery_contained_within(min_x, min_y, max_x, max_y, results)(f64, i32)- Find boxes contained within a rectangle
query_nearest_k(x, y, k, results)(f64)- Find K nearest boxes to a pointquery_circle(center_x, center_y, radius, results)(f64)- Find boxes intersecting a circular region
query_nearest_k_points(x, y, k, results)(f64)- Find K nearest points (stored as (x, x, y, y)), sorted by distancequery_circle_points(center_x, center_y, radius, results)(f64)- Find points within a circular region (optimized for point data)
Note: Point-specific methods assume all items in the tree are stored as degenerate boxes (points) where min_x == max_x and min_y == max_y. For mixed data (both points and boxes), use the general methods instead.
query_in_direction(rect_min_x, rect_min_y, rect_max_x, rect_max_y, direction_x, direction_y, distance, results)(f64)- Find boxes intersecting a rectangle's movement pathquery_in_direction_k(rect_min_x, rect_min_y, rect_max_x, rect_max_y, direction_x, direction_y, k, distance, results)(f64)- Find K nearest boxes intersecting a rectangle's movement path
Minimal examples for each query method are available in the examples/ directory:
query_intersecting- Find boxes intersecting a rectanglequery_intersecting_id- Find boxes intersecting a specific item already in the tree (by ID)query_intersecting_k- Find K first intersecting boxesquery_point- Find boxes containing a pointquery_contain- Find boxes containing a rectanglequery_contained_within- Find boxes inside a rectanglequery_nearest_k- Find K nearest boxesquery_circle- Find boxes in a circular regionquery_circle_points- Find points in a circular region (optimized)query_nearest_k_points- Find K nearest points (optimized)query_in_direction- Find boxes in a movement pathquery_in_direction_k- Find K nearest in a movement path
Run any example with:
cargo run --example query_point- Cache-Friendly: Flat array storage with Hilbert curve ordering for good spatial locality
- Static Optimization: Optimized for static or infrequently-modified spatial data. Rebuilding is cheap (about 5 ms for 100,000 items), see Changing Data
Environment:
- OS: Ubuntu 26.04.1 LTS
- Processor: Intel Core i5-1240P
- Kernel: Linux 7.0.0-43-generic
- Rust: 1.100.0
- Results: median of 3 runs, 1,000,000 items, fixed seed
> cargo bench --bench profile_bench
> cargo bench --bench profile_bench_i32
Box Tree Queries (f64)
================
build box tree 1000000 items: 73.31ms
query_intersecting (100% coverage) - 1000 queries: 2254.808µs/query
query_intersecting (50% coverage) - 1000 queries: 355.786µs/query
query_intersecting (10% coverage) - 1000 queries: 82.899µs/query
query_intersecting (1% coverage) - 1000 queries: 14.450µs/query
query_intersecting (0.01% coverage)- 1000 queries: 2.017µs/query
query_nearest_k (1000 searches of 100 neighbors): 12.897µs/query
query_nearest_k (1 search of 1000000 neighbors): 108.665ms
query_nearest_k (100000 searches of 1 neighbor): 5.169µs/query
query_nearest_k k=1 - 1000 queries: 5.164µs/query
query_nearest_k k=10 - 1000 queries: 5.833µs/query
query_nearest_k k=100 - 1000 queries: 12.792µs/query
query_nearest_k k=1000 - 100 queries: 77.114µs/query
query_point - 10000 queries: 1.005µs/query
query_intersecting_k k=100 - 10000 queries: 1.623µs/query
query_contain - 1000 queries: 1.151µs/query
query_contained_within - 1000 queries: 2.307µs/query
query_circle - 1000 queries (radius=5): 48.348µs/query
query_in_direction - 1000 queries: 13.101µs/query
query_in_direction_k k=50 - 1000 queries: 20.873µs/query
Point Cloud Queries
===================
build point cloud 1000000 points: 69.91ms
query_circle_points - 1000 queries (radius=5): 29.411µs/query
query_nearest_k_points k=1 - 1000 queries: 2.479µs/query
query_nearest_k_points k=10 - 1000 queries: 3.352µs/query
query_nearest_k_points k=100 - 1000 queries: 10.318µs/query
query_nearest_k_points k=1000 - 100 queries: 72.921µs/query
Box Tree Queries (i32)
================
build box tree 1000000 items: 55.45ms
query_intersecting (100% coverage) - 1000 queries: 1464.778µs/query
query_intersecting (50% coverage) - 1000 queries: 265.412µs/query
query_intersecting (10% coverage) - 1000 queries: 57.433µs/query
query_intersecting (1% coverage) - 1000 queries: 7.472µs/query
query_intersecting (0.01% coverage)- 1000 queries: 0.581µs/query
query_intersecting_k k=100 - 10000 queries: 0.552µs/query
query_point - 10000 queries: 0.493µs/query
query_contain - 1000 queries: 0.112µs/query
query_contained_within - 1000 queries: 1.635µs/query
get() first call, builds id table - 1 call: 4.680ms
query_intersecting_id - 1000 queries: 0.735µs/query