Skip to main content

Grid

Struct Grid 

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

A rectangular 4-connected grid. Cells are vertices in row-major order, y * width + x; blocked cells are vertices without edges.

Implementations§

Source§

impl Grid

Source

pub fn new(width: usize, height: usize) -> Self

Creates a grid of width by height free cells.

§Panics

Panics if width or height is zero.

Source

pub fn parse(rows: &[&str]) -> Self

Parses a grid from rows of text: . is free, @ is blocked.

All rows must have the same length. Moving AI maps use more symbols; read them with parse_map.

§Panics

Panics if there are no rows, rows differ in length, or a row has another character.

Source

pub fn block(&mut self, x: usize, y: usize)

Marks the cell at (x, y) as an obstacle.

§Panics

Panics if (x, y) is outside the grid.

Source

pub fn width(&self) -> usize

The number of columns.

Source

pub fn height(&self) -> usize

The number of rows.

Source

pub fn vertex(&self, x: usize, y: usize) -> Vertex

The vertex of cell (x, y).

§Panics

Panics if (x, y) is outside the grid.

Source

pub fn cell(&self, vertex: Vertex) -> (usize, usize)

The cell (x, y) of vertex.

Source

pub fn is_blocked(&self, vertex: Vertex) -> bool

Returns true if vertex is an obstacle.

Source§

impl Grid

Source

pub const EAST: Heading = 0

The heading towards larger x.

Source

pub const NORTH: Heading = 1

The heading towards smaller y, the row above.

Source

pub const WEST: Heading = 2

The heading towards smaller x.

Source

pub const SOUTH: Heading = 3

The heading towards larger y, the row below.

Trait Implementations§

Source§

impl Clone for Grid

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Grid

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Grid

Source§

impl Graph for Grid

Source§

fn vertex_count(&self) -> usize

The number of vertices. Vertex ids are 0..vertex_count().
Source§

fn neighbors(&self, vertex: Vertex, emit: impl FnMut(Vertex))

Calls emit once for every out-neighbour of vertex. Read more
Source§

impl OrientedGraph for Grid

A grid as an OrientedGraph: four headings (Grid::EAST, Grid::NORTH, Grid::WEST, Grid::SOUTH, counter-clockwise), and a turn is a quarter turn either way, counter-clockwise first.

§Examples

use mapf_search::{Grid, OrientedGraph};

let grid = Grid::parse(&["..", "@."]);
assert_eq!(grid.forward(grid.vertex(0, 0), Grid::EAST), Some(grid.vertex(1, 0)));
assert_eq!(grid.forward(grid.vertex(0, 0), Grid::SOUTH), None);

let mut turns = Vec::new();
grid.turns(Grid::EAST, |heading| turns.push(heading));
assert_eq!(turns, [Grid::NORTH, Grid::SOUTH]);
Source§

fn heading_count(&self) -> usize

The number of headings. Headings are 0..heading_count(), the same at every vertex.
Source§

fn forward(&self, vertex: Vertex, heading: Heading) -> Option<Vertex>

The vertex an agent at vertex facing heading reaches by moving forward, or None if there is no edge that way.
Source§

fn turns(&self, heading: Heading, emit: impl FnMut(Heading))

Calls emit once for every heading one turn away from heading. Turning must be able to reach every heading from every other.
Source§

impl PartialEq for Grid

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Grid

Auto Trait Implementations§

§

impl Forget for Grid

§

impl Freeze for Grid

§

impl Move for Grid

§

impl RefUnwindSafe for Grid

§

impl Send for Grid

§

impl Sync for Grid

§

impl Unpin for Grid

§

impl UnsafeUnpin for Grid

§

impl UnwindSafe for Grid

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.