Expand description
Packed static spatial index for 2D and 3D axis-aligned bounding boxes.
The canonical flow is Index2DBuilder -> Index2D -> Index2D::search.
With the simd feature, Index2DBuilder::finish_simd and
Index3DBuilder::finish_simd build SimdIndex2D and SimdIndex3D, which
share the same query API backed by SoA layouts and SIMD traversal.
Indexes also serialize with to_bytes and load back as owned indexes or as
zero-copy views (Index2DView, Index3DView).
§Queries
Every query is a method on the index types (Index2D / Index3D, the
SIMD indexes, and the zero-copy views), so docs.rs lists them on each type’s
page (e.g. Index2D); the only crate-level free items are small distance
helpers such as haversine_distance_2d. Range and ray results are item
indices in insertion order.
- Range / intersection —
search(plussearch_into/search_with/ lazysearch_iter),any,first,visit. - Nearest neighbors — from a point
neighbors(plus_within/_into/_with/visit_neighbors) or from a boxneighbors_of_boxand its variants; or under a custom distance metric withneighbors_metric(e.g. great-circle distance viahaversine_distance_2d). - Ray segment —
raycast(all hits),raycast_closest(nearest box entered), andvisit_raycast. - Spatial join —
join/join_withbetween two indexes,self_join/self_join_withwithin one. - Region / culling — prune to a non-box shape instead of its bounding box:
2D triangle
search_triangleand convex polygonsearch_polygononIndex2D, 3D view frustumsearch_frustumonIndex3D(each withany_*/visit_*/_into, and on the zero-copyIndex2DView/Index3DView).
§Quick Start
use packed_spatial_index::{Index2DBuilder, Box2D};
let mut builder = Index2DBuilder::new(2);
builder.add(Box2D::new(0.0, 0.0, 1.0, 1.0));
builder.add(Box2D::new(5.0, 5.0, 6.0, 6.0));
let index = builder.finish().unwrap();
assert_eq!(index.search(Box2D::new(0.0, 0.0, 2.0, 2.0)), vec![0]);§Cargo Features
parallel(default): adaptive parallel builds through rayon.simd(default): SIMD search/raycast throughwideand x86-64 AVX-512.f32-storage: compact f32-storage SIMD indexes.stream: query a serialized index over aRangeReader(local file or remote object) without loading the whole file. No extra dependencies.async: query over anAsyncRangeReaderfor async I/O sources (browser / edge worker over HTTP range or object storage). Impliesstream.
Structs§
- Box2D
- Axis-aligned 2D box stored as
(min_x, min_y, max_x, max_y). - Box3D
- Axis-aligned 3D box stored as
(min_x, min_y, min_z, max_x, max_y, max_z). - Convex
Polygon2D - A convex polygon in 2D, given as vertices in order (CW or CCW). The query predicates assume convexity; a non-convex polygon yields unspecified results.
- File
Metadata - Descriptive metadata read from a file’s
METAchunk. Every field is an opaque string the writer supplied; this crate does not interpret them (e.g. the CRS is whatever identifier the producer chose, such as"EPSG:4326"). - File
Reader streamand (Unix or Windows) - A
RangeReaderover a local file using positioned reads. - Frustum3D
- A 3D frustum as six inward-pointing half-space planes.
- Index2D
- Finished static read-only index.
- Index2D
Builder - Builder for
Index2Dand, with thesimdfeature,SimdIndex2D. - Index2D
F32 f32-storage - Compact scalar f32-storage 2D index: the same outward-rounded
f32boxes asSimdIndex2DF32(half the box memory ofIndex2D), queried without SIMD. Built natively inf32(no transientf64tree). Range and ray results are a conservative superset of the f64 index (every exact hit, plus a few near-boundary false positives from outward rounding). - Index2D
View - Zero-copy read-only view over bytes produced by
Index2D::to_bytes. - Index3D
- Finished static read-only 3D index.
- Index3D
Builder - Builder for
Index3Dand, with thesimdfeature,SimdIndex3D. - Index3D
F32 f32-storage - Compact scalar f32-storage 3D index: the same outward-rounded
f32boxes asSimdIndex3DF32(half the box memory ofIndex3D), queried without SIMD. Built natively inf32(no transientf64tree), so peak build memory is the f32 tree, not f64. Range and ray results are a conservative superset of the f64 index (every exact hit, plus a few near-boundary false positives from the outward rounding). - Index3D
View - Zero-copy read-only view over bytes produced by
Index3D::to_bytes. - Neighbor
Workspace - Reusable buffers for allocation-free repeated nearest-neighbor searches.
- Point2D
- 2D point used by nearest-neighbor searches.
- Point3D
- 3D point used by nearest-neighbor searches.
- Ray2D
- Finite 2D ray segment used by
raycastsearches. - Ray3D
- Finite 3D ray segment used by
raycastsearches. - Search2D
Iter - Lazy iterator over the items intersecting a query box, returned by
Index2D::search_iter. - Search3D
Iter - Lazy iterator over the items intersecting a query box, returned by
Index3D::search_iter. - Search
Workspace - Reusable buffers for allocation-free repeated searches.
- Serializer2D
- Builder for
Index2Dserialization, created byIndex2D::serialize. - Serializer2D
F32 f32-storage - Serialization builder for
Index2DF32, created byIndex2DF32::serialize. Writes f32 box records plus an optional per-item payload and descriptive metadata; the f32-box counterpart ofSerializer2D. - Serializer3D
- Builder for
Index3Dserialization, created byIndex3D::serialize. The 3D counterpart ofSerializer2D: optional per-item payloads, the streaming-tuned interleaved layout, and descriptive metadata (CRS / content type / attribution), read back withread_metadata. - Serializer3D
F32 f32-storage - Serialization builder for
Index3DF32, created byIndex3DF32::serialize. Writes f32 box records plus an optional per-item payload and descriptive metadata; the f32-box counterpart ofSerializer3D. - Simd
Index2D simd - Finished read-only SIMD index.
- Simd
Index2D F32 f32-storageandsimd - Finished read-only f32-storage SIMD index.
- Simd
Index2D F32View f32-storageandsimd - Zero-copy read-only view over bytes produced by
SimdIndex2DF32::to_bytes. - Simd
Index2D View simd - Zero-copy SIMD view over bytes produced by
SimdIndex2D::to_bytesorIndex2D::to_bytes. - Simd
Index3D simd - Finished read-only SIMD 3D index.
- Simd
Index3D F32 f32-storageandsimd - Finished read-only f32-storage 3D SIMD index.
- Simd
Index3D F32View f32-storageandsimd - Zero-copy read-only view over bytes produced by
SimdIndex3DF32::to_bytes. - Simd
Index3D View simd - Zero-copy SIMD view over bytes produced by
SimdIndex3D::to_bytesorIndex3D::to_bytes. - Slice
Reader stream - A
RangeReaderover an in-memory byte buffer (&[u8],Vec<u8>, a memory map, …). Reads are bounds-checked copies out of the buffer. - Stream
Directory stream - A reusable, reader-independent streaming directory.
- Stream
Index2D stream - Streaming reader for a 2D
f64packed spatial index. - Stream
Index2D F32 stream - Streaming reader for a compact
f322D index — the bytes ofSimdIndex2DF32/Index2DF32. Half the box bytes on the wire ofStreamIndex2D; results are a conservative superset (the stored f32 boxes are rounded outward). Behind thestreamfeature. - Stream
Index3D stream - Streaming reader for a 3D
f64packed spatial index. - Stream
Index3D F32 stream - Streaming reader for a compact
f323D index. The 3D counterpart ofStreamIndex2DF32(24-byte box record). Behind thestreamfeature. - Stream
Limits stream - Per-query cost limits for a streaming index.
- Triangle2D
- A 2D triangle: three vertices
[x, y].repr(C), unpadded, so a slice casts to and from the on-disk fixed-width payload with no copy. - Triangle2D
F32 - A 2D triangle: three vertices
[x, y].repr(C), unpadded, so a slice casts to and from the on-disk fixed-width payload with no copy. - Triangle3D
- A 3D triangle: three vertices
[x, y, z].repr(C), unpadded, so a slice casts to and from the on-disk fixed-width payload with no copy. - Triangle3D
F32 - A 3D triangle: three vertices
[x, y, z].repr(C), unpadded, so a slice casts to and from the on-disk fixed-width payload with no copy. - Triangle
Hit - A ray-triangle intersection: the
indexof the hit triangle in the queried slice, and the ray parametertin direction-length units (the sametconvention ascrate::Ray3D). Returned bycrate::Ray3D::closest_triangle.
Enums§
- Bounds
Error - Error returned by
Box2D::try_newandBox3D::try_newfor invalid coordinate bounds. - Build
Error - Build error for finishing an index.
- Clip
SpaceZ - The normalized-device-coordinate depth range a projection matrix targets, for
Frustum3D::from_view_projection. D3D12, Vulkan, Metal and WebGPU clipzto[0, 1](the modern default); OpenGL and WebGL clip it to[-1, 1]. Only the near plane differs between the two conventions. There is deliberately no silent default on the constructor — the convention is not recoverable from the matrix, so every caller states it — butClipSpaceZ::default()is the modernZeroToOneif you need one. - Load
Error - Error returned when loading an index from bytes.
- Payload
Error - Error returned when serializing an index together with item payloads.
- Sort
Key2D - Which key to use when sorting boxes before packing the tree.
- Sort
Key3D - Which key to use when sorting 3D boxes before packing the tree.
- Stream
Error stream - Error returned by the streaming reader.
Constants§
- DEFAULT_
NODE_ SIZE - Default maximum number of children per tree node.
- DEFAULT_
PARALLEL_ MIN_ ITEMS parallel - Minimum index size at which
parallel(true)enables rayon. - EARTH_
RADIUS_ M - Mean Earth radius in meters (IUGG), a reasonable default for
haversine_distance_2d.
Traits§
- Async
Range Reader asyncandstream - Async counterpart of
RangeReader: read a byte range, returning a future. - Range
Reader stream - A source of bytes addressable by absolute offset.
- Triangle2
- A 2D triangle record (
f64orf32vertices). Implemented byTriangle2DandTriangle2DF32; sealed, so the set of record types is fixed. - Triangle3
- A 3D triangle record (
f64orf32vertices). Implemented byTriangle3DandTriangle3DF32; sealed, so the set of record types is fixed.
Functions§
- haversine_
distance_ 2d - Great-circle (haversine) distance in meters from a
(lon, lat)query point (degrees) to the closest point ofbounds, a lon/lat AABB in degrees. - read_
metadata - Read the optional descriptive metadata from a serialized index, without
loading the index. Returns an empty
FileMetadatawhen there is noMETAchunk.