aabb/lib.rs
1//! # AABB - Hilbert R-tree Spatial Index
2//!
3//! A Rust library providing a simple and efficient Hilbert R-tree implementation
4//! for spatial queries on axis-aligned bounding boxes (AABBs).
5//!
6//! ## Features
7//!
8//! - **Hilbert Curve Ordering**: Uses Hilbert space-filling curve for improved spatial locality
9//! - **AABB Intersection Queries**: Fast rectangular bounding box intersection testing
10//! - **Simple API**: Easy to use with minimal setup
11//! - **Static Optimization**: Efficient for static or infrequently-modified spatial data
12//!
13//!
14//! ## Quick Start
15//!
16//! ```rust
17//! use aabb::prelude::*;
18//!
19//! // Create a new spatial index
20//! let mut tree = HilbertRTree::new();
21//!
22//! // Add some bounding boxes (min_x, min_y, max_x, max_y)
23//! tree.add(0.0, 0.0, 2.0, 2.0); // Box 0: large box
24//! tree.add(1.0, 1.0, 3.0, 3.0); // Box 1: overlapping box
25//! tree.add(5.0, 5.0, 6.0, 6.0); // Box 2: distant box
26//! tree.add(1.5, 1.5, 2.5, 2.5); // Box 3: small box inside others
27//!
28//! // Build the spatial index (required before querying)
29//! tree.build();
30//!
31//! // Query for boxes intersecting a region
32//! let mut results = Vec::new();
33//! // minx, miny, maxx, maxy
34//! tree.query_intersecting(1.2, 1.2, 2.8, 2.8, &mut results);
35//!
36//! // Results contains indices of intersecting boxes
37//! println!("Found {} intersecting boxes: {:?}", results.len(), results);
38//! // Output: Found 3 intersecting boxes: [0, 1, 3]
39//!
40//! // You can reuse the results vector for multiple queries
41//! results.clear();
42//! tree.query_intersecting(4.0, 4.0, 7.0, 7.0, &mut results);
43//! println!("Found {} boxes in distant region: {:?}", results.len(), results);
44//! // Output: Found 1 boxes in distant region: [2]
45//!
46//! // Point queries - find boxes containing a specific point
47//! results.clear();
48//! tree.query_point(1.8, 1.8, &mut results);
49//! println!("Point (1.8, 1.8) is in boxes: {:?}", results);
50//!
51//! // Containment queries - find boxes that completely contain a rectangle
52//! results.clear();
53//! tree.query_containing(1.2, 1.2, 1.8, 1.8, &mut results);
54//! println!("Boxes containing rectangle: {:?}", results);
55//!
56//! // K-limited queries - find first K intersecting boxes for performance
57//! results.clear();
58//! tree.query_intersecting_k(0.0, 0.0, 3.0, 3.0, 2, &mut results);
59//! println!("First 2 intersecting boxes: {:?}", results);
60//!
61//! // Nearest neighbor queries
62//! let nearest = tree.query_nearest(2.0, 2.0);
63//! println!("Nearest box to (2.0, 2.0): {:?}", nearest);
64//!
65//! // Distance-based queries - find boxes within 1.5 units
66//! results.clear();
67//! tree.query_within_distance(1.0, 1.0, 1.5, &mut results);
68//! println!("Boxes within distance 1.5: {:?}", results);
69//!
70//! // Circular region queries
71//! results.clear();
72//! tree.query_circle(1.5, 1.5, 2.0, &mut results);
73//! println!("Boxes in circle: {:?}", results);
74//!
75//! // K-nearest neighbor queries
76//! results.clear();
77//! tree.query_nearest_k(2.0, 2.0, 2, &mut results);
78//! println!("2 nearest boxes to (2.0, 2.0): {:?}", results);
79//!
80//! // Directional queries - find boxes in rectangle's movement path
81//! results.clear();
82//! // Start with rectangle (1.5, 1.5) x (2.5, 2.5) and move right by distance 3
83//! tree.query_in_direction(1.5, 1.5, 2.5, 2.5, 1.0, 0.0, 3.0, &mut results);
84//! println!("Boxes intersecting movement path: {:?}", results);
85//!
86//! // Directional K-nearest queries - find K nearest boxes in movement path
87//! results.clear();
88//! tree.query_in_direction_k(1.5, 1.5, 2.5, 2.5, 1.0, 0.0, 2, 3.0, &mut results);
89//! println!("2 nearest boxes in movement path: {:?}", results);
90//!
91//! ```
92//!
93//! ## Available Query Methods
94//!
95//! ### Basic Spatial Queries
96//! - [`query_intersecting`] - Find boxes that intersect a rectangle
97//! - [`query_intersecting_k`] - Find first K intersecting boxes
98//! - [`query_point`] - Find boxes that contain a point
99//! - [`query_containing`] - Find boxes that contain a rectangle
100//! - [`query_contained_by`] - Find boxes contained within a rectangle
101//!
102//! ### Distance-Based Queries
103//! - [`query_nearest`] - Find the single nearest box to a point
104//! - [`query_nearest_k`] - Find K nearest boxes to a point
105//! - [`query_within_distance`] - Find boxes within distance of a point
106//! - [`query_circle`] - Find boxes intersecting a circular region
107//!
108//! ### Directional Queries
109//! - [`query_in_direction`] - Find boxes intersecting a rectangle's movement path
110//! - [`query_in_direction_k`] - Find K nearest boxes intersecting a rectangle's movement path
111//!
112//! [`query_intersecting`]: HilbertRTree::query_intersecting
113//! [`query_intersecting_k`]: HilbertRTree::query_intersecting_k
114//! [`query_point`]: HilbertRTree::query_point
115//! [`query_containing`]: HilbertRTree::query_containing
116//! [`query_contained_by`]: HilbertRTree::query_contained_by
117//! [`query_nearest`]: HilbertRTree::query_nearest
118//! [`query_nearest_k`]: HilbertRTree::query_nearest_k
119//! [`query_within_distance`]: HilbertRTree::query_within_distance
120//! [`query_circle`]: HilbertRTree::query_circle
121//! [`query_in_direction`]: HilbertRTree::query_in_direction
122//! [`query_in_direction_k`]: HilbertRTree::query_in_direction_k
123//!
124//! ## How It Works
125//!
126//! The Hilbert R-tree stores bounding boxes in a flat array and sorts them by their
127//! Hilbert curve index (computed from box centers). This provides good spatial locality
128//! for most spatial queries while maintaining a simple, cache-friendly data structure.
129//!
130//! The Hilbert curve is a space-filling curve that maps 2D coordinates to a 1D sequence
131//! while preserving spatial locality - points that are close in 2D space tend to be
132//! close in the 1D Hilbert ordering.
133
134/// Core Hilbert R-tree spatial index data structure (flat sorted version)
135#[doc(hidden)]
136pub mod hilbert_rtree_leg;
137/// Hierarchical Hilbert R-tree spatial index (flatbush-inspired)
138pub mod hilbert_rtree;
139/// Spatial query implementations for HilbertRTreeLeg (legacy reference implementation)
140#[doc(hidden)]
141pub mod queries;
142/// Integration tests for the library
143#[doc(hidden)]
144pub mod integration_test;
145/// Comparison tests between both implementations
146#[doc(hidden)]
147pub mod comparison_tests;
148/// Component tests for granular method testing
149#[doc(hidden)]
150pub mod component_tests;
151/// Legacy query tests
152#[doc(hidden)]
153pub mod test_legacy_query;
154/// Prelude for convenient imports
155pub mod prelude;
156
157#[doc(hidden)]
158pub use hilbert_rtree_leg::HilbertRTreeLeg;
159pub use hilbert_rtree::HilbertRTree;
160
161/// Add two unsigned 64-bit integers
162pub fn add(left: u64, right: u64) -> u64 {
163 left + right
164}
165
166#[cfg(test)]
167mod tests {
168 use super::*;
169
170 #[test]
171 fn it_works() {
172 let result = add(2, 2);
173 assert_eq!(result, 4);
174 }
175}