A Rust library providing a simple and 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
- AABB Intersection Queries: Fast rectangular bounding box intersection testing
- Simple API: Easy to use with minimal setup
- Static Optimization: Efficient for static or infrequently-modified spatial data
Add this to your Cargo.toml:
[dependencies]
aabb = "0.2"use aabb::prelude::*;
fn main() {
let mut tree = HilbertRTree::new();
// 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();
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
}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.
HilbertRTree::new()- Create a new empty treeadd(min_x, min_y, max_x, max_y)- Add a bounding boxbuild()- Build the spatial index (required before querying)
query_intersecting(min_x, min_y, max_x, max_y, results)- Find boxes that intersect a rectanglequery_intersecting_k(min_x, min_y, max_x, max_y, k, results)- Find first K intersecting boxesquery_point(x, y, results)- Find boxes that contain a pointquery_containing(min_x, min_y, max_x, max_y, results)- Find boxes that contain a rectanglequery_contained_by(min_x, min_y, max_x, max_y, results)- Find boxes contained within a rectangle
query_nearest_k(x, y, k, results)- Find K nearest boxes to a pointquery_nearest(x, y) -> Option<usize>- Find the single nearest box to a pointquery_within_distance(x, y, max_distance, results)- Find boxes within distance of a pointquery_circle(center_x, center_y, radius, results)- Find boxes intersecting a circular region
query_in_direction(rect_min_x, rect_min_y, rect_max_x, rect_max_y, direction_x, direction_y, distance, results)- 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)- Find K nearest boxes intersecting a rectangle's movement path
- Cache-Friendly: Flat array storage with Hilbert curve ordering for good spatial locality
- Static Optimization: Optimized for static or infrequently-modified spatial data
This project is licensed under the MIT License.