pub struct Grid { /* private fields */ }Expand description
Dense row-major rectangular map: walkability, traversal_cost, optional reachability.
Shared substrate for online crate::Pathfinder algorithms (4-connected). Open cells
default to unit cost; blocked cells are excluded from neighbors. Cached
reachability::GridReachabilityIndex is optional and cleared on walkability edits.
Implementations§
Source§impl Grid
impl Grid
Sourcepub fn new(width: usize, height: usize) -> Result<Self, GridBuildError>
pub fn new(width: usize, height: usize) -> Result<Self, GridBuildError>
Creates a grid with non-zero dimensions.
§Errors
Returns GridBuildError when either dimension is zero, the cell count
overflows, or backing storage cannot be reserved.
Sourcepub const fn builder(width: usize, height: usize) -> GridBuilder
pub const fn builder(width: usize, height: usize) -> GridBuilder
Starts a builder for a grid with the supplied dimensions.
Sourcepub fn try_from_rows<I, S>(rows: I) -> Result<Self, GridBuildError>
pub fn try_from_rows<I, S>(rows: I) -> Result<Self, GridBuildError>
Parses a rectangular grid from textual rows.
. creates an open cell with cost 1, # creates a blocked cell, and
1 through 9 create open cells with the corresponding traversal cost.
§Errors
Returns GridBuildError for empty, zero-width, ragged, or invalid
input, or when the resulting grid cannot be allocated.
Sourcepub fn index_reachability(&mut self)
pub fn index_reachability(&mut self)
Builds or rebuilds the cached reachability index from the current cells.
Sourcepub fn is_reachable(&self, start: Point, goal: Point) -> bool
pub fn is_reachable(&self, start: Point, goal: Point) -> bool
Returns whether start and goal share a walkable 4-way component.
Without a prior Self::index_reachability call, always returns true
so search is not short-circuited.
Sourcepub fn cell_count(&self) -> usize
pub fn cell_count(&self) -> usize
Total cells (width * height).
Sourcepub fn contains(&self, point: Point) -> bool
pub fn contains(&self, point: Point) -> bool
Whether point lies inside the grid bounds (walkability is separate).
Sourcepub fn cell(&self, point: Point) -> Option<Cell>
pub fn cell(&self, point: Point) -> Option<Cell>
Cell state at point, or None when out of bounds.
Sourcepub fn set_cell(
&mut self,
point: Point,
cell: Cell,
) -> Result<(), GridEditError>
pub fn set_cell( &mut self, point: Point, cell: Cell, ) -> Result<(), GridEditError>
Sets walkability; clears the cached reachability index when the value changes.
Blocking an open cell hides its traversal cost from queries but retains the stored cost so reopening restores it.
§Errors
Returns GridEditError::OutOfBounds if point is outside the grid.
Sourcepub fn is_walkable(&self, point: Point) -> bool
pub fn is_walkable(&self, point: Point) -> bool
In-bounds and Cell::Open; out-of-bounds and blocked cells are not walkable.
Sourcepub fn path_is_walkable(&self, points: &[Point]) -> bool
pub fn path_is_walkable(&self, points: &[Point]) -> bool
Checks that every point is open and every consecutive pair is an orthogonal unit step.
Sourcepub fn segment_is_walkable(&self, start: Point, end: Point) -> bool
pub fn segment_is_walkable(&self, start: Point, end: Point) -> bool
Returns whether start and end form a single orthogonal unit edge between open cells.
Sourcepub fn traversal_cost(&self, point: Point) -> Option<usize>
pub fn traversal_cost(&self, point: Point) -> Option<usize>
Returns the traversal cost for an open cell, or None if blocked or out of bounds.
Sourcepub fn set_traversal_cost(
&mut self,
point: Point,
cost: usize,
) -> Result<(), GridEditError>
pub fn set_traversal_cost( &mut self, point: Point, cost: usize, ) -> Result<(), GridEditError>
Sets a positive traversal cost for a walkable cell.
§Errors
Returns GridEditError if the cost is zero, the point is outside the
grid, or the cell is blocked.
Sourcepub fn neighbors4(&self, point: Point) -> Vec<Point>
pub fn neighbors4(&self, point: Point) -> Vec<Point>
Walkable 4-connected neighbors of point, in fixed axis order.