pub struct SpatialHash { /* private fields */ }Expand description
Flat counting-sort grid over agent positions, rebuilt every tick.
Implementations§
Source§impl SpatialHash
impl SpatialHash
Sourcepub fn new(cell_size: f32, world_w: f32, world_h: f32) -> Self
pub fn new(cell_size: f32, world_w: f32, world_h: f32) -> Self
Creates an empty hash over a world_w by world_h world, with cells about cell_size wide.
Sourcepub fn cell_index(&self, x: f32, y: f32) -> u32
pub fn cell_index(&self, x: f32, y: f32) -> u32
Returns the flat index of the cell holding (x, y).
The position wraps, so a position outside the world still falls in a cell.
Sourcepub fn build(&mut self, pos_x: &[f32], pos_y: &[f32])
pub fn build(&mut self, pos_x: &[f32], pos_y: &[f32])
Sorts the agents at pos_x and pos_y into their cells.
Sourcepub fn build_where(
&mut self,
pos_x: &[f32],
pos_y: &[f32],
include: impl Fn(usize) -> bool,
)
pub fn build_where( &mut self, pos_x: &[f32], pos_y: &[f32], include: impl Fn(usize) -> bool, )
Like Self::build, but skips agents that include rejects.
Skipped agents are not returned by any query.
Sourcepub fn query_radius(
&self,
x: f32,
y: f32,
r: f32,
pos_x: &[f32],
pos_y: &[f32],
result: &mut Vec<u32>,
)
pub fn query_radius( &self, x: f32, y: f32, r: f32, pos_x: &[f32], pos_y: &[f32], result: &mut Vec<u32>, )
Fills result with every agent within r of (x, y), clearing it first.
Distances wrap at the world’s edges, as for Self::for_each_within.
Sourcepub fn for_each_within<F: FnMut(u32, f32, f32, f32)>(
&self,
x: f32,
y: f32,
r: f32,
pos_x: &[f32],
pos_y: &[f32],
f: F,
)
pub fn for_each_within<F: FnMut(u32, f32, f32, f32)>( &self, x: f32, y: f32, r: f32, pos_x: &[f32], pos_y: &[f32], f: F, )
Visits every agent within r of (x, y), passing the callback its index, the toroidal
deltas to it and their squared length.
The deltas come out of the range test either way. A kernel that needs them uses this method and computes each delta once, whereas a list of indices makes it recompute them all.
Sourcepub fn cell_size_is(&self, cell_size: f32) -> bool
pub fn cell_size_is(&self, cell_size: f32) -> bool
Returns whether this hash was built with cell_size.
Sourcepub fn world_is(&self, world_w: f32, world_h: f32) -> bool
pub fn world_is(&self, world_w: f32, world_h: f32) -> bool
Returns whether this hash was built for a world_w by world_h world.
Sourcepub fn grid_dims(&self) -> (u32, u32)
pub fn grid_dims(&self) -> (u32, u32)
Cells along each axis, fitted to the world from the requested cell size.
Sourcepub fn cell_extents(&self) -> (f32, f32)
pub fn cell_extents(&self) -> (f32, f32)
World distance one cell spans on each axis.
Sourcepub fn buckets(&self) -> (&[u32], &[u32])
pub fn buckets(&self) -> (&[u32], &[u32])
Returns (cell_start, sorted_agents), where cell c owns
sorted_agents[cell_start[c]..cell_start[c + 1]].
Sourcepub fn rebuild_with_cell_size(
&mut self,
new_cell_size: f32,
pos_x: &[f32],
pos_y: &[f32],
)
pub fn rebuild_with_cell_size( &mut self, new_cell_size: f32, pos_x: &[f32], pos_y: &[f32], )
Rebuilds the hash for new_cell_size and sorts the agents again, if the cell size changed.
Sourcepub fn heap_bytes(&self) -> usize
pub fn heap_bytes(&self) -> usize
Heap memory held by the hash, in bytes.
Trait Implementations§
Source§impl Debug for SpatialHash
Prints the cell geometry and the agent count, not the sorted agents.
impl Debug for SpatialHash
Prints the cell geometry and the agent count, not the sorted agents.
Source§impl NeighborIndex for SpatialHash
impl NeighborIndex for SpatialHash
Source§fn new(extent: Extent, cell_size: f32) -> Self
fn new(extent: Extent, cell_size: f32) -> Self
extent, with cells cell_size wide.Source§fn rebuild(&mut self, pos_x: &[f32], pos_y: &[f32], cell_size: f32)
fn rebuild(&mut self, pos_x: &[f32], pos_y: &[f32], cell_size: f32)
cell_size wide.