pub struct SpatialIndexN<const D: usize, T> { /* private fields */ }Expand description
A spatial index backed by an R-tree for efficient region and
nearest-neighbor queries in D-dimensional space.
Implementations§
Source§impl<const D: usize, T> SpatialIndexN<D, T>
impl<const D: usize, T> SpatialIndexN<D, T>
Sourcepub const fn with_config(config: SpatialConfig) -> Self
pub const fn with_config(config: SpatialConfig) -> Self
Creates a new, empty spatial index with the given configuration.
Sourcepub const fn config(&self) -> SpatialConfig
pub const fn config(&self) -> SpatialConfig
Returns the configuration used by this index.
Sourcepub fn insert(&mut self, entry: SpatialEntryN<D, T>)
pub fn insert(&mut self, entry: SpatialEntryN<D, T>)
Inserts an entry into the index.
Sourcepub fn remove<F>(
&mut self,
region: BoundingBoxN<D>,
pred: F,
) -> Result<(), SpatialError>
pub fn remove<F>( &mut self, region: BoundingBoxN<D>, pred: F, ) -> Result<(), SpatialError>
Removes the first entry whose bounding box and data match the predicate.
The region hint narrows the search to nodes overlapping it. After
removal, underflowing nodes are condensed and their entries reinserted
to maintain tree quality (Guttman’s condense_tree).
§Errors
Returns SpatialError::NotFound if no matching entry exists.
Sourcepub fn query_region(&self, region: BoundingBoxN<D>) -> Vec<&SpatialEntryN<D, T>>
pub fn query_region(&self, region: BoundingBoxN<D>) -> Vec<&SpatialEntryN<D, T>>
Returns all entries whose bounding box intersects region.
Sourcepub fn query_nearest_nd(
&self,
point: [f32; D],
k: usize,
) -> Vec<&SpatialEntryN<D, T>>
pub fn query_nearest_nd( &self, point: [f32; D], k: usize, ) -> Vec<&SpatialEntryN<D, T>>
Returns the k entries nearest to point, ordered nearest-first.
Distance is measured from the query point to the nearest edge of each
entry’s bounding box. If fewer than k entries exist, all are returned.
Sourcepub fn query_nearest_by_centroid_nd(
&self,
point: [f32; D],
k: usize,
) -> Vec<&SpatialEntryN<D, T>>
pub fn query_nearest_by_centroid_nd( &self, point: [f32; D], k: usize, ) -> Vec<&SpatialEntryN<D, T>>
Returns the k entries whose bounding-box center is nearest
to point, ordered nearest-first.
Unlike query_nearest_nd, which measures to
the bbox edge (returning 0 for elements containing the point), this
measures to each element’s centroid. Large containers whose center is
far from the query point are naturally deprioritized.
Sourcepub fn query_within_radius_nd(
&self,
point: [f32; D],
r: f32,
) -> Vec<&SpatialEntryN<D, T>>
pub fn query_within_radius_nd( &self, point: [f32; D], r: f32, ) -> Vec<&SpatialEntryN<D, T>>
Returns all entries within r of point, sorted nearest-first.
Returns an empty vector if r < 0.0.
Sourcepub fn query_within_radius_with_distances_nd(
&self,
point: [f32; D],
r: f32,
) -> Vec<(&SpatialEntryN<D, T>, f32)>
pub fn query_within_radius_with_distances_nd( &self, point: [f32; D], r: f32, ) -> Vec<(&SpatialEntryN<D, T>, f32)>
Returns all entries within r of point with their distances, sorted
nearest-first.
Each tuple contains (entry, distance) where distance is measured from
the query point to the nearest edge of the bounding box (0 when inside).
Returns an empty vector if r < 0.0.
Sourcepub fn bulk_load(entries: Vec<SpatialEntryN<D, T>>) -> Self
pub fn bulk_load(entries: Vec<SpatialEntryN<D, T>>) -> Self
Builds a spatial index from a pre-collected set of entries using the Sort-Tile-Recursive (STR) bulk-loading algorithm.
The resulting tree has tighter bounding boxes and less overlap than
one-at-a-time insertion, yielding better query performance for static
datasets. The index can still be modified with insert
and remove afterwards.
Sourcepub fn bulk_load_with_config(
entries: Vec<SpatialEntryN<D, T>>,
config: SpatialConfig,
) -> Self
pub fn bulk_load_with_config( entries: Vec<SpatialEntryN<D, T>>, config: SpatialConfig, ) -> Self
Builds a spatial index with the given configuration using the Sort-Tile-Recursive (STR) bulk-loading algorithm.
Sourcepub fn iter(&self) -> SpatialIterN<'_, D, T> ⓘ
pub fn iter(&self) -> SpatialIterN<'_, D, T> ⓘ
Returns an iterator over references to all entries in the index.
Source§impl<T> SpatialIndexN<2, T>
impl<T> SpatialIndexN<2, T>
Sourcepub fn query_nearest(
&self,
x: f32,
y: f32,
k: usize,
) -> Vec<&SpatialEntryN<2, T>>
pub fn query_nearest( &self, x: f32, y: f32, k: usize, ) -> Vec<&SpatialEntryN<2, T>>
Returns the k entries nearest to the point (x, y), ordered from
nearest to farthest.
Distance is measured from the query point to the nearest edge of each
entry’s bounding box. Elements containing the query point have
distance 0. If fewer than k entries exist, all entries are returned.
Sourcepub fn query_nearest_by_centroid(
&self,
x: f32,
y: f32,
k: usize,
) -> Vec<&SpatialEntryN<2, T>>
pub fn query_nearest_by_centroid( &self, x: f32, y: f32, k: usize, ) -> Vec<&SpatialEntryN<2, T>>
Returns the k entries whose bounding-box center is nearest to the
point (x, y), ordered nearest-first.
Unlike query_nearest, which measures to the
bbox edge, this measures to each element’s centroid so that large
containers are naturally deprioritized.
Sourcepub fn query_within_radius(
&self,
x: f32,
y: f32,
r: f32,
) -> Vec<&SpatialEntryN<2, T>>
pub fn query_within_radius( &self, x: f32, y: f32, r: f32, ) -> Vec<&SpatialEntryN<2, T>>
Returns all entries within r pixels of the point (x, y), sorted
nearest-first by edge distance.
Distance is measured from the query point to the nearest edge of each
entry’s bounding box. Elements containing the query point have
distance 0. Returns an empty vector if r < 0.0.
Sourcepub fn query_within_radius_with_distances(
&self,
x: f32,
y: f32,
r: f32,
) -> Vec<(&SpatialEntryN<2, T>, f32)>
pub fn query_within_radius_with_distances( &self, x: f32, y: f32, r: f32, ) -> Vec<(&SpatialEntryN<2, T>, f32)>
Returns all entries within r pixels of the point (x, y) with their
distances, sorted nearest-first.
Each tuple contains (entry, distance) where distance is measured from
the query point to the nearest edge of the bounding box (0 when inside).
Returns an empty vector if r < 0.0.
Source§impl<T> SpatialIndexN<3, T>
impl<T> SpatialIndexN<3, T>
Sourcepub fn query_nearest(
&self,
x: f32,
y: f32,
z: f32,
k: usize,
) -> Vec<&SpatialEntryN<3, T>>
pub fn query_nearest( &self, x: f32, y: f32, z: f32, k: usize, ) -> Vec<&SpatialEntryN<3, T>>
Returns the k entries nearest to the point (x, y, z), ordered from
nearest to farthest.
Distance is measured from the query point to the nearest edge of each
entry’s bounding box. Elements containing the query point have
distance 0. If fewer than k entries exist, all entries are returned.
Sourcepub fn query_nearest_by_centroid(
&self,
x: f32,
y: f32,
z: f32,
k: usize,
) -> Vec<&SpatialEntryN<3, T>>
pub fn query_nearest_by_centroid( &self, x: f32, y: f32, z: f32, k: usize, ) -> Vec<&SpatialEntryN<3, T>>
Returns the k entries whose bounding-box center is nearest to the
point (x, y, z), ordered nearest-first.
Unlike query_nearest, which measures to the
bbox edge, this measures to each element’s centroid so that large
containers are naturally deprioritized.
Sourcepub fn query_within_radius(
&self,
x: f32,
y: f32,
z: f32,
r: f32,
) -> Vec<&SpatialEntryN<3, T>>
pub fn query_within_radius( &self, x: f32, y: f32, z: f32, r: f32, ) -> Vec<&SpatialEntryN<3, T>>
Returns all entries within r units of the point (x, y, z), sorted
nearest-first by edge distance.
Distance is measured from the query point to the nearest edge of each
entry’s bounding box. Elements containing the query point have
distance 0. Returns an empty vector if r < 0.0.
Sourcepub fn query_within_radius_with_distances(
&self,
x: f32,
y: f32,
z: f32,
r: f32,
) -> Vec<(&SpatialEntryN<3, T>, f32)>
pub fn query_within_radius_with_distances( &self, x: f32, y: f32, z: f32, r: f32, ) -> Vec<(&SpatialEntryN<3, T>, f32)>
Returns all entries within r units of the point (x, y, z) with their
distances, sorted nearest-first.
Each tuple contains (entry, distance) where distance is measured from
the query point to the nearest edge of the bounding box (0 when inside).
Returns an empty vector if r < 0.0.