pub struct SpatialGrid { /* private fields */ }Expand description
Spatial hash grid for O(n) neighbor queries in particle simulations.
Divides space into a uniform 3D grid of grid_dim^3 cells. Uses a
CSR (Compressed Sparse Row) flat-buffer layout: indices holds all
particle indices packed contiguously, and cell_offsets[h]..cell_offsets[h+1]
gives the slice for cell h. This eliminates per-cell heap allocations
and improves cache locality for neighbor queries.
§Build flow
- Call
clear()to reset counts. - Call
insert()for each particle. - Call
build()to finalize the CSR layout. - Call
query_neighbors_into()for lookups.
Implementations§
Source§impl SpatialGrid
impl SpatialGrid
Sourcepub fn new(cell_size: Fix128, grid_dim: usize) -> Self
pub fn new(cell_size: Fix128, grid_dim: usize) -> Self
Create a new spatial grid.
cell_size: Side length of each cubic cell (should match the interaction radius for best performance).grid_dim: Number of cells along each axis. Total cells =grid_dim^3.
Sourcepub fn hash(&self, pos: Vec3Fix) -> usize
pub fn hash(&self, pos: Vec3Fix) -> usize
Compute the cell index for a given position.
Each axis is clamp(floor(x / cell_size) + grid_dim / 2, 0, grid_dim - 1)
for every Fix128 input: positions far outside the grid (up to the
ends of the Fix128 range, or with a cell size down to 2^-64) land
in the border cell on their side, never panic and never wrap, in both
debug and release builds.
A grid built with grid_dim = 0 has no cells; every position hashes
to 0, and callers already guard h < total_cells (which is also
0) before using the result.
Sourcepub fn insert(&mut self, idx: usize, pos: Vec3Fix)
pub fn insert(&mut self, idx: usize, pos: Vec3Fix)
Record a particle insertion (pass 1: count only).
Call build() after all insertions to finalize the CSR layout.
Auto Trait Implementations§
impl Freeze for SpatialGrid
impl RefUnwindSafe for SpatialGrid
impl Send for SpatialGrid
impl Sync for SpatialGrid
impl Unpin for SpatialGrid
impl UnsafeUnpin for SpatialGrid
impl UnwindSafe for SpatialGrid
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more