Skip to main content

SpatialHashGrid

Struct SpatialHashGrid 

Source
pub struct SpatialHashGrid { /* private fields */ }
Expand description

Spatial hash grid for broadphase pair generation. Replaces O(n^2) all-pairs with O(n) expected-case neighbour lookups.

Implementations§

Source§

impl SpatialHashGrid

Source

pub fn new(cell_size: f32) -> Self

Source

pub fn cell_size(&self) -> f32

Cell size used for spatial hashing (debug/query introspection).

Source

pub fn set_cell_size(&mut self, size: f32)

Update cell size. Call before populate() when body density changes.

Source

pub fn inv_cell_size(&self) -> f32

Inverse cell size (for wave coherence broadphase access).

Source

pub fn entries(&self) -> &[(u64, usize)]

Sorted entries (for wave coherence per-body regeneration).

Source

pub fn populate(&mut self, bodies: &[RigidBody])

Populate the grid from body positions. Sorts entries by hash for fast lookup.

Source

pub fn query_pairs(&self, bodies: &[RigidBody]) -> Vec<(usize, usize)>

Generate candidate pairs using half-neighborhood search.

Instead of querying all 27 neighbor cells (which produces duplicate pairs requiring O(P log P) sort + O(P) dedup), we query only the 14 “forward” cells: the self-cell plus 13 cells that are lexicographically greater in (dx, dy, dz) order. This guarantees each pair is discovered exactly once.

Mathematical proof: For bodies A in cell C_A and B in cell C_B where C_A and C_B are adjacent (differ by at most 1 on each axis):

  • If C_A < C_B lexicographically: A’s forward search includes C_B → pair found by A
  • If C_A > C_B lexicographically: B’s forward search includes C_A → pair found by B
  • If C_A == C_B: same cell, j > i index guard → pair found once

This is the 3D analog of the causal attention mask: compute only the upper triangle of the spatial relevance matrix. Zero duplicates by construction. No sort. No dedup. O(N × 14 × log N) vs O(N × 27 × log N + P log P).

Reference: Allen & Tildesley, “Computer Simulation of Liquids” (1987), half-neighbor Verlet list construction.

Trait Implementations§

Source§

impl Clone for SpatialHashGrid

Source§

fn clone(&self) -> SpatialHashGrid

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SpatialHashGrid

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.