pub struct SpatialHash<T> { /* private fields */ }Expand description
A uniform-grid spatial hash map keyed by (cell_x, cell_y).
T must be Clone because a single large item may be registered in
multiple cells simultaneously.
Implementations§
Source§impl<T: Clone> SpatialHash<T>
impl<T: Clone> SpatialHash<T>
Sourcepub fn new(cell_size: f32) -> Option<Self>
pub fn new(cell_size: f32) -> Option<Self>
Create an empty spatial hash with the given cell size.
cell_size should be chosen to match the typical object size: too small
wastes memory, too large reduces query selectivity. 64.0 px is a good
default for UI graphs; use smaller values for dense physics simulations.
Sourcepub fn insert(&mut self, rect: Rect, item: T)
pub fn insert(&mut self, rect: Rect, item: T)
Insert item into every cell that rect overlaps.
The item is cloned once per overlapping cell. For items that span many
cells this can be expensive — prefer large cell_size for large objects.
Sourcepub fn query(&self, rect: Rect) -> Vec<T>
pub fn query(&self, rect: Rect) -> Vec<T>
Return all items registered in cells that overlap rect.
The returned list may contain duplicates if an item spans multiple cells that all overlap the query rect. Callers must deduplicate if needed. This is intentionally a broad-phase query — exact AABB testing is the caller’s responsibility.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Remove all items from every cell, resetting to an empty grid.
This does NOT free the backing HashMap’s allocated memory; call this at the start of each frame before re-inserting the current object set.
Auto Trait Implementations§
impl<T> Freeze for SpatialHash<T>
impl<T> RefUnwindSafe for SpatialHash<T>where
T: RefUnwindSafe,
impl<T> Send for SpatialHash<T>where
T: Send,
impl<T> Sync for SpatialHash<T>where
T: Sync,
impl<T> Unpin for SpatialHash<T>where
T: Unpin,
impl<T> UnsafeUnpin for SpatialHash<T>
impl<T> UnwindSafe for SpatialHash<T>where
T: UnwindSafe,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.