Skip to main content

SpatialGrid

Struct SpatialGrid 

Source
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

  1. Call clear() to reset counts.
  2. Call insert() for each particle.
  3. Call build() to finalize the CSR layout.
  4. Call query_neighbors_into() for lookups.

Implementations§

Source§

impl SpatialGrid

Source

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.
Source

pub fn clear(&mut self)

Reset the grid for a fresh build pass.

Source

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.

Source

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.

Source

pub fn build(&mut self)

Finalize the CSR layout after all insert() calls.

Converts the temporary (cell, particle) pairs into a proper prefix-sum offset table and sorted flat index buffer.

Source

pub fn query_neighbors_into( &self, pos: Vec3Fix, _radius_sq: Fix128, neighbors: &mut Vec<usize>, )

Collect all particle indices in the 3x3x3 neighborhood of pos.

Results are appended to neighbors (which is cleared first). _radius_sq is reserved for future distance filtering.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.