Skip to main content

SpatialGrid

Struct SpatialGrid 

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

In-memory spatial grid mapping cell coordinates to entity ID lists.

Intended lifetime: one tick. Build with SpatialGrid::new(), populate with insert, query with query_cell/query_neighbors, then drop.

Implementations§

Source§

impl SpatialGrid

Source

pub fn new() -> Self

Create an empty spatial grid.

Source

pub fn with_capacity(capacity: usize) -> Self

Create a grid pre-allocated for at least capacity entities.

Source

pub fn insert(&mut self, entity_id: &str, x: i32, y: i32)

Insert an entity at world position (x, y).

Computes the cell coordinate and appends entity_id to that cell’s list. An entity may appear in multiple calls (e.g. once per owned component), but callers are responsible for deduplication if required.

Source

pub fn remove(&mut self, entity_id: &str, x: i32, y: i32)

Remove an entity from its current cell at world position (x, y).

Removes the first occurrence of entity_id in the cell. No-ops if the entity is not present.

Source

pub fn query_cell(&self, x: i32, y: i32) -> &[String]

Return all entity IDs in the cell containing world position (x, y).

Returns an empty slice if the cell is unpopulated.

Source

pub fn query_neighbors(&self, cx: i32, cy: i32) -> Vec<&str>

Return all entity IDs in the 3x3 neighborhood of cell (cx, cy).

Results are unsorted and may contain duplicates if an entity spans cells. Allocates a Vec bounded by 9 * max_entities_per_cell.

Source

pub fn query_radius(&self, x: i32, y: i32, range: i32) -> Vec<(&str, i32, i32)>

Return all entity IDs within range of world position (x, y).

Queries the 3x3 cell neighborhood and excludes cells whose closest point to (x, y) exceeds range. Because the grid stores cell assignment — not exact entity positions — this is cell-granularity filtering. Callers with exact entity coordinates should apply a final distance_squared check for precise results.

Each returned tuple is (entity_id, cell_origin_x, cell_origin_y).

Source

pub fn cell_count(&self) -> usize

Return the number of occupied cells.

Source

pub fn entity_count(&self) -> usize

Return the total number of entity references across all cells.

Source

pub fn clear(&mut self)

Clear all cells. Retains allocated capacity.

Trait Implementations§

Source§

impl Default for SpatialGrid

Source§

fn default() -> SpatialGrid

Returns the “default value” for a type. 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> 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, 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.