StaticAABB2DIndex
Summary
Fast static spatial index data structure for 2D axis aligned bounding boxes utilizing hilbert curve
spatial ordering. This is a rust port of the excellent
flatbush javascript library.
By default no unsafe code is used (#![forbid(unsafe_code)]
is applied). Some unsafe optimizations
can be enabled by toggling on the unsafe_optimizations
flag.
Quick Links
Crate
Documentation
Examples
Quick Code Example
```rust
use staticaabb2dindex::*;
// create builder for index containing 4 axis aligned bounding boxes
// index also supports integers and custom types that implement the IndexableNum trait
let mut builder: StaticAABB2DIndexBuilder = StaticAABB2DIndexBuilder::new(4);
// add bounding boxes to the index
// add takes in (minx, miny, maxx, maxy) of the bounding box
builder.add(0.0, 0.0, 2.0, 2.0);
builder.add(-1.0, -1.0, 3.0, 3.0);
builder.add(0.0, 0.0, 1.0, 3.0);
builder.add(4.0, 2.0, 16.0, 8.0);
// note build may return an error if the number of added boxes does not equal the static size
// given at the time the builder was created or the type used fails to cast to/from a u16
let index: StaticAABB2DIndex = builder.build().unwrap();
// query the created index (minx, miny, maxx, maxy)
let queryresults = index.query(-1.0, -1.0, -0.5, -0.5);
// queryresults holds the index positions of the boxes that overlap with the box given
// (positions are according to the order boxes were added the index builder)
asserteq!(queryresults, vec![1]);
// the query may also be done with a visiting function that can stop the query early
let mut visitedresults: Vec = Vec::new();
let mut visitor = |boxaddedpos: usize| -> Control<()> {
visitedresults.push(boxaddedpos);
// return continue to continue visiting results, break to stop early
Control::Continue
};
index.visitquery(-1.0, -1.0, -0.5, -0.5, &mut visitor);
asserteq!(visited_results, vec![1]);
```