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
impl SpatialHashGrid
pub fn new(cell_size: f32) -> Self
Sourcepub fn set_cell_size(&mut self, size: f32)
pub fn set_cell_size(&mut self, size: f32)
Update cell size. Call before populate() when body density changes.
Sourcepub fn inv_cell_size(&self) -> f32
pub fn inv_cell_size(&self) -> f32
Inverse cell size (for wave coherence broadphase access).
Sourcepub fn entries(&self) -> &[(u64, usize)]
pub fn entries(&self) -> &[(u64, usize)]
Sorted entries (for wave coherence per-body regeneration).
Sourcepub fn populate(&mut self, bodies: &[RigidBody])
pub fn populate(&mut self, bodies: &[RigidBody])
Populate the grid from body positions. Sorts entries by hash for fast lookup.
Sourcepub fn query_pairs(&self, bodies: &[RigidBody]) -> Vec<(usize, usize)>
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
impl Clone for SpatialHashGrid
Source§fn clone(&self) -> SpatialHashGrid
fn clone(&self) -> SpatialHashGrid
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more