pub struct ComputedGrid {
grid: Grid,
standard_grid: Option<StandardGrid>,
walkable_nodes: Vec<Point2<i8>>,
coords_to_node: HashMap<Point2<i8>, usize>,
valid_actions: Vec<[bool; 5]>,
distance_matrix: Vec<Vec<Option<u8>>>,
walls: Vec<Wall>,
}
Expand description
A Grid
with precomputed data for faster pathfinding.
This struct is created by ComputedGrid::try_from
.
§Examples
use core_pb::grid::standard_grid::StandardGrid;
let grid = StandardGrid::Blank.compute_grid();
Fields§
§grid: Grid
§standard_grid: Option<StandardGrid>
§walkable_nodes: Vec<Point2<i8>>
§coords_to_node: HashMap<Point2<i8>, usize>
§valid_actions: Vec<[bool; 5]>
walkable, right, left, up, down
distance_matrix: Vec<Vec<Option<u8>>>
note that all walkable nodes might not be reachable from each other
walls: Vec<Wall>
walls represent rectangles with top left corner at the specified point
Implementations§
Source§impl ComputedGrid
impl ComputedGrid
Sourcepub fn standard_grid(&self) -> &Option<StandardGrid>
pub fn standard_grid(&self) -> &Option<StandardGrid>
Returns the underlying StandardGrid
, if one was used to construct it.
Sourcepub fn walkable_nodes(&self) -> &Vec<Point2<i8>>
pub fn walkable_nodes(&self) -> &Vec<Point2<i8>>
Returns the positions of all walkable nodes in the grid.
§Examples
use nalgebra::Point2;
use core_pb::grid::standard_grid::StandardGrid;
let grid = StandardGrid::Blank.compute_grid();
assert_eq!(grid.walkable_nodes()[0], Point2::new(1, 1));
Sourcepub fn coords_to_node(&self, p: &Point2<i8>) -> Option<usize>
pub fn coords_to_node(&self, p: &Point2<i8>) -> Option<usize>
Returns the index of the given position in the walkable_nodes vector, or None
if the
position is not walkable.
§Examples
use nalgebra::Point2;
use core_pb::grid::standard_grid::StandardGrid;
let grid = StandardGrid::Blank.compute_grid();
assert_eq!(grid.coords_to_node(&Point2::new(1, 1)), Some(0));
assert_eq!(grid.coords_to_node(&Point2::new(0, 0)), None);
Sourcepub fn valid_actions(&self, p: Point2<i8>) -> Option<[bool; 5]>
pub fn valid_actions(&self, p: Point2<i8>) -> Option<[bool; 5]>
Returns the valid actions for the given position.
The five values represent:
- whether the node is walkable
- whether the node to the right is walkable
- whether the node to the left is walkable
- whether the node above is walkable
- whether the node below is walkable
§Examples
use nalgebra::Point2;
use core_pb::grid::standard_grid::StandardGrid;
let grid = StandardGrid::Blank.compute_grid();
assert_eq!(grid.valid_actions(Point2::new(1, 1)), Some([true, false, false, false, false]));
Sourcepub fn wall_at(&self, p: &Point2<i8>) -> bool
pub fn wall_at(&self, p: &Point2<i8>) -> bool
Returns whether there is a wall at a given position
§Examples
use nalgebra::Point2;
use core_pb::grid::standard_grid::StandardGrid;
let grid = StandardGrid::Blank.compute_grid();
assert_eq!(grid.wall_at(&Point2::new(0, 0)), true);
assert_eq!(grid.wall_at(&Point2::new(1, 1)), false);
assert_eq!(grid.wall_at(&Point2::new(32, 32)), true);
Sourcepub fn dist(&self, p1: &Point2<i8>, p2: &Point2<i8>) -> Option<u8>
pub fn dist(&self, p1: &Point2<i8>, p2: &Point2<i8>) -> Option<u8>
Returns the distance between two points, or None
if the points are not both walkable.
§Examples
use nalgebra::Point2;
use core_pb::grid::standard_grid::StandardGrid;
let grid = StandardGrid::Pacman.compute_grid();
assert_eq!(grid.dist(&Point2::new(1, 1), &Point2::new(1, 1)), Some(0));
assert_eq!(grid.dist(&Point2::new(1, 1), &Point2::new(1, 2)), Some(1));
Sourcepub fn neighbors(&self, p: &Point2<i8>) -> Vec<Point2<i8>>
pub fn neighbors(&self, p: &Point2<i8>) -> Vec<Point2<i8>>
Returns all the walkable neighbors of the given position.
§Examples
use nalgebra::Point2;
use core_pb::grid::standard_grid::StandardGrid;
let grid = StandardGrid::Pacman.compute_grid();
assert!(grid.neighbors(&Point2::new(1, 1)).contains(&Point2::new(1, 2)));
assert!(grid.neighbors(&Point2::new(1, 1)).contains(&Point2::new(2, 1)));
Sourcepub fn node_nearest(&self, x: f32, y: f32) -> Option<Point2<i8>>
pub fn node_nearest(&self, x: f32, y: f32) -> Option<Point2<i8>>
Return the walkable node from the nodes surrounding this point
Sourcepub fn bfs_path(
&self,
start: Point2<i8>,
finish: Point2<i8>,
) -> Option<Vec<Point2<i8>>>
pub fn bfs_path( &self, start: Point2<i8>, finish: Point2<i8>, ) -> Option<Vec<Point2<i8>>>
Returns the shortest path, if one exists, from start to finish The path includes path the start and the finish
fn compute_open_grid() -> Self
Trait Implementations§
Source§impl Clone for ComputedGrid
impl Clone for ComputedGrid
Source§fn clone(&self) -> ComputedGrid
fn clone(&self) -> ComputedGrid
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for ComputedGrid
impl Debug for ComputedGrid
Source§impl Default for ComputedGrid
impl Default for ComputedGrid
Source§impl<'de> Deserialize<'de> for ComputedGrid
impl<'de> Deserialize<'de> for ComputedGrid
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl From<StandardGrid> for ComputedGrid
impl From<StandardGrid> for ComputedGrid
Source§fn from(value: StandardGrid) -> Self
fn from(value: StandardGrid) -> Self
Source§impl PartialEq for ComputedGrid
impl PartialEq for ComputedGrid
Source§impl Serialize for ComputedGrid
impl Serialize for ComputedGrid
impl StructuralPartialEq for ComputedGrid
Auto Trait Implementations§
impl Freeze for ComputedGrid
impl RefUnwindSafe for ComputedGrid
impl Send for ComputedGrid
impl Sync for ComputedGrid
impl Unpin for ComputedGrid
impl UnwindSafe for ComputedGrid
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
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.