| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A Rust library providing efficient Hilbert R-tree implementation for spatial queries on axis-aligned bounding boxes (AABBs).
Add this to your Cargo.toml:
[dependencies]
aabb = "0.7"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.
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.
Minimal examples for each query method are available in the examples/ directory:
Run any example with:
cargo run --example query_pointEnvironment: - OS: Ubuntu 24.04.3 LTS - Processor: Intel Core i5-1240P - Kernel: Linux 6.8.0-86-generic - CPU Frequency: ~1773-3500 MHz > cargo bench --bench profile_bench > cargo bench --bench profile_bench_i32 Box Tree Queries (f64) ================ build box tree 1000000 items: 76.65ms query_intersecting (100% coverage) - 1000 queries: 2390.735µs/query query_intersecting (50% coverage) - 1000 queries: 436.450µs/query query_intersecting (10% coverage) - 1000 queries: 98.460µs/query query_intersecting (1% coverage) - 1000 queries: 17.832µs/query query_intersecting (0.01% coverage)- 1000 queries: 2.668µs/query query_nearest_k (1000 searches of 100 neighbors): 13.868µs/query query_nearest_k (1 search of 1000000 neighbors): 110.456ms/query query_nearest_k (100000 searches of 1 neighbor): 5.297µs/query query_nearest_k k=1 - 1000 queries: 6.315µs/query query_nearest_k k=10 - 1000 queries: 6.580µs/query query_nearest_k k=100 - 1000 queries: 13.783µs/query query_nearest_k k=1000 - 100 queries: 86.150µs/query query_point - 10000 queries: 1.083µs/query query_intersecting_k k=100 - 10000 queries: 1.886µs/query query_contain - 1000 queries: 2.027µs/query query_contained_within - 1000 queries: 2.947µs/query query_circle - 1000 queries (radius=5): 49.666µs/query query_in_direction - 1000 queries: 13.243µs/query query_in_direction_k k=50 - 1000 queries: 20.865µs/query Point Cloud Queries =================== build point cloud 1000000 points: 71.33ms query_circle_points - 1000 queries (radius=5): 30.001µs/query query_nearest_k_points k=1 - 1000 queries: 2.955µs/query query_nearest_k_points k=10 - 1000 queries: 4.333µs/query query_nearest_k_points k=100 - 1000 queries: 12.038µs/query query_nearest_k_points k=1000 - 100 queries: 76.387µs/query Box Tree Queries (i32) ================ build box tree 1000000 items: 55.19ms query_intersecting (100% coverage) - 1000 queries: 1676.877µs/query query_intersecting (50% coverage) - 1000 queries: 262.290µs/query query_intersecting (50% coverage) - 1000 queries: 55.554µs/query query_intersecting (1% coverage) - 1000 queries: 7.253µs/query query_intersecting (0.01% coverage)- 1000 queries: 0.768µs/query query_intersecting_k k=100 - 10000 queries: 0.564µs/query query_point - 10000 queries: 0.449µs/query query_contain - 1000 queries: 0.279µs/query query_contained_within - 1000 queries: 1.604µs/query query_intersecting_id - 1000 queries: 0.307µs/query
AABB is part of the open-sourced Nest2D projects collection.
| Back | FazBrowse Home | New Git URL |