pub struct SpatialHash { /* private fields */ }Expand description
A uniform-grid cell list for fixed-radius neighbour queries (boids, molecular
dynamics, SPH) over [0, width) × [0, height) (CORE-R54).
rebuild counting-sorts the point indices by bucket into one flat
array (CSR layout: starts[b]..starts[b + 1] is bucket b), so a query reads a few
contiguous runs instead of chasing per-bucket Vecs. With the bucket side at least
the query radius, a query touches ≤ 9 buckets and the total cost is O(n) per frame
instead of O(n²).
Implementations§
Source§impl SpatialHash
impl SpatialHash
Sourcepub const MAX_AXIS: usize = 1024
pub const MAX_AXIS: usize = 1024
Upper bound on buckets per axis, so a tiny cell cannot allocate without bound.
Sourcepub fn new(width: f64, height: f64, cell: f64) -> Self
pub fn new(width: f64, height: f64, cell: f64) -> Self
A cell list over [0, width) × [0, height) with square buckets of side cell
(rounded so they tile the domain; at most MAX_AXIS per
axis). A non-finite or non-positive width/height is taken as 1, and such a
cell as the whole domain (one bucket).
Sourcepub fn resize(&mut self, width: f64, height: f64, cell: f64)
pub fn resize(&mut self, width: f64, height: f64, cell: f64)
Change the domain and bucket size (see new) and forget the
points; allocates only when the bucket count grows.
Sourcepub fn rebuild(&mut self, points: &[[f64; 2]])
pub fn rebuild(&mut self, points: &[[f64; 2]])
Re-bucket points (by index) in one counting sort: O(n + buckets), stable
within a bucket, allocation-free unless the point count grows. Points outside
the domain or non-finite clamp to the border bucket. Indices beyond u32::MAX
are ignored.
pub fn is_empty(&self) -> bool
Sourcepub fn for_each_within(&self, p: [f64; 2], r: f64, f: impl FnMut(usize))
pub fn for_each_within(&self, p: [f64; 2], r: f64, f: impl FnMut(usize))
Call f(i) for every point index i whose point lies within distance r of
p, and possibly for others in the buckets the query disc overlaps, each exactly
once, bucket rows top to bottom and ascending index within a bucket. Callers
filter by exact distance. A non-finite p or a negative or NaN r visits
nothing; r = ∞ visits every point.