Struct grid_pathfinding::PathingGrid
source · pub struct PathingGrid {
pub grid: BoolGrid,
pub neighbours: SimpleGrid<u8>,
pub components: UnionFind<usize>,
pub components_dirty: bool,
}Expand description
PathingGrid maintains information about components using a UnionFind structure in addition to the raw bool grid values in the BoolGrid that determine whether a space is occupied (true) or empty (false). It also records neighbours in u8 format for fast lookups during search. Implements Grid by building on BoolGrid.
Fields§
§grid: BoolGrid§neighbours: SimpleGrid<u8>§components: UnionFind<usize>§components_dirty: boolImplementations§
source§impl PathingGrid
impl PathingGrid
sourcepub fn get_component(&self, point: &Point) -> usize
pub fn get_component(&self, point: &Point) -> usize
Retrieves the component id a given Point belongs to.
sourcepub fn unreachable(&self, start: &Point, goal: &Point) -> bool
pub fn unreachable(&self, start: &Point, goal: &Point) -> bool
Checks if start and goal are on the same component.
sourcepub fn neighbours_unreachable(&self, start: &Point, goal: &Point) -> bool
pub fn neighbours_unreachable(&self, start: &Point, goal: &Point) -> bool
Checks if any neighbour of the goal is on the same component as the start.
sourcepub fn get_path_single_goal(
&self,
start: Point,
goal: Point,
approximate: bool
) -> Option<Vec<Point>>
pub fn get_path_single_goal( &self, start: Point, goal: Point, approximate: bool ) -> Option<Vec<Point>>
Computes a path from start to goal using JPS. If approximate is true, then it will path to one of the neighbours of the goal, which is useful if goal itself is blocked. The heuristic used is the Chebyshev distance.
Examples found in repository?
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let mut pathing_grid: PathingGrid = PathingGrid::new(3, 3, false);
pathing_grid.set(1, 1, true);
pathing_grid.generate_components();
println!("{}", pathing_grid);
let start = Point::new(0, 0);
let end = Point::new(2, 2);
let path = pathing_grid
.get_path_single_goal(start, end, false)
.unwrap();
println!("Path:");
for p in path {
println!("{:?}", p);
}
}More examples
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
let mut pathing_grid: PathingGrid = PathingGrid::new(5, 5, false);
pathing_grid.set(1, 1, true);
pathing_grid.generate_components();
println!("{}", pathing_grid);
let start = Point::new(0, 0);
let end = Point::new(4, 4);
if let Some(path) = pathing_grid.get_waypoints_single_goal(start, end, false) {
println!("Waypoints:");
for p in &path {
println!("{:?}", p);
}
println!("\nPath generated from waypoints:");
for p in waypoints_to_path(path) {
println!("{:?}", p);
}
}
println!("\nDirectly computed path");
let expanded_path = pathing_grid
.get_path_single_goal(start, end, false)
.unwrap();
for p in expanded_path {
println!("{:?}", p);
}
}sourcepub fn get_path_multiple_goals(
&self,
start: Point,
goals: Vec<&Point>
) -> Option<(Point, Vec<Point>)>
pub fn get_path_multiple_goals( &self, start: Point, goals: Vec<&Point> ) -> Option<(Point, Vec<Point>)>
Computes a path from start to one of the given goals. This is done by taking the Chebyshev distance to the closest goal as heuristic value.
Examples found in repository?
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() {
let mut pathing_grid: PathingGrid = PathingGrid::new(3, 3, false);
pathing_grid.set(1, 1, true);
pathing_grid.generate_components();
println!("{}", pathing_grid);
let start = Point::new(0, 0);
let goal_1 = Point::new(2, 0);
let goal_2 = Point::new(2, 2);
let goals = vec![&goal_1, &goal_2];
let (selected_goal, path) = pathing_grid.get_path_multiple_goals(start, goals).unwrap();
println!("Selected goal: {:?}\n", selected_goal);
println!("Path:");
for p in path {
println!("{:?}", p);
}
}sourcepub fn get_waypoints_multiple_goals(
&self,
start: Point,
goals: Vec<&Point>
) -> Option<(Point, Vec<Point>)>
pub fn get_waypoints_multiple_goals( &self, start: Point, goals: Vec<&Point> ) -> Option<(Point, Vec<Point>)>
The raw waypoints (jump points) from which get_path_multiple_goals makes a path.
sourcepub fn get_waypoints_single_goal(
&self,
start: Point,
goal: Point,
approximate: bool
) -> Option<Vec<Point>>
pub fn get_waypoints_single_goal( &self, start: Point, goal: Point, approximate: bool ) -> Option<Vec<Point>>
The raw waypoints (jump points) from which get_path_single_goal makes a path.
Examples found in repository?
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
let mut pathing_grid: PathingGrid = PathingGrid::new(5, 5, false);
pathing_grid.set(1, 1, true);
pathing_grid.generate_components();
println!("{}", pathing_grid);
let start = Point::new(0, 0);
let end = Point::new(4, 4);
if let Some(path) = pathing_grid.get_waypoints_single_goal(start, end, false) {
println!("Waypoints:");
for p in &path {
println!("{:?}", p);
}
println!("\nPath generated from waypoints:");
for p in waypoints_to_path(path) {
println!("{:?}", p);
}
}
println!("\nDirectly computed path");
let expanded_path = pathing_grid
.get_path_single_goal(start, end, false)
.unwrap();
for p in expanded_path {
println!("{:?}", p);
}
}sourcepub fn generate_components(&mut self)
pub fn generate_components(&mut self)
Generates a new UnionFind structure and links up grid neighbours to the same components.
Examples found in repository?
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let mut pathing_grid: PathingGrid = PathingGrid::new(3, 3, false);
pathing_grid.set(1, 1, true);
pathing_grid.generate_components();
println!("{}", pathing_grid);
let start = Point::new(0, 0);
let end = Point::new(2, 2);
let path = pathing_grid
.get_path_single_goal(start, end, false)
.unwrap();
println!("Path:");
for p in path {
println!("{:?}", p);
}
}More examples
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() {
let mut pathing_grid: PathingGrid = PathingGrid::new(3, 3, false);
pathing_grid.set(1, 1, true);
pathing_grid.generate_components();
println!("{}", pathing_grid);
let start = Point::new(0, 0);
let goal_1 = Point::new(2, 0);
let goal_2 = Point::new(2, 2);
let goals = vec![&goal_1, &goal_2];
let (selected_goal, path) = pathing_grid.get_path_multiple_goals(start, goals).unwrap();
println!("Selected goal: {:?}\n", selected_goal);
println!("Path:");
for p in path {
println!("{:?}", p);
}
}22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
let mut pathing_grid: PathingGrid = PathingGrid::new(5, 5, false);
pathing_grid.set(1, 1, true);
pathing_grid.generate_components();
println!("{}", pathing_grid);
let start = Point::new(0, 0);
let end = Point::new(4, 4);
if let Some(path) = pathing_grid.get_waypoints_single_goal(start, end, false) {
println!("Waypoints:");
for p in &path {
println!("{:?}", p);
}
println!("\nPath generated from waypoints:");
for p in waypoints_to_path(path) {
println!("{:?}", p);
}
}
println!("\nDirectly computed path");
let expanded_path = pathing_grid
.get_path_single_goal(start, end, false)
.unwrap();
for p in expanded_path {
println!("{:?}", p);
}
}Trait Implementations§
source§impl Clone for PathingGrid
impl Clone for PathingGrid
source§fn clone(&self) -> PathingGrid
fn clone(&self) -> PathingGrid
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Debug for PathingGrid
impl Debug for PathingGrid
source§impl Default for PathingGrid
impl Default for PathingGrid
source§fn default() -> PathingGrid
fn default() -> PathingGrid
source§impl Display for PathingGrid
impl Display for PathingGrid
source§impl Grid<bool> for PathingGrid
impl Grid<bool> for PathingGrid
source§fn set(&mut self, x: usize, y: usize, blocked: bool)
fn set(&mut self, x: usize, y: usize, blocked: bool)
Updates a position on the grid. Joins newly connected components and flags the components as dirty if components are (potentially) broken apart into multiple.