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: bool

Implementations§

source§

impl PathingGrid

source

pub fn get_component(&self, point: &Point) -> usize

Retrieves the component id a given Point belongs to.

source

pub fn unreachable(&self, start: &Point, goal: &Point) -> bool

Checks if start and goal are on the same component.

source

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.

source

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?
examples/simple.rs (line 24)
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
Hide additional examples
examples/paths_and_waypoints.rs (line 41)
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);
    }
}
source

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?
examples/multiple_goals.rs (line 26)
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);
    }
}
source

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.

source

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?
examples/paths_and_waypoints.rs (line 29)
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);
    }
}
source

pub fn update(&mut self)

Regenerates the components if they are marked as dirty.

source

pub fn generate_components(&mut self)

Generates a new UnionFind structure and links up grid neighbours to the same components.

Examples found in repository?
examples/simple.rs (line 19)
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
Hide additional examples
examples/multiple_goals.rs (line 20)
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);
    }
}
examples/paths_and_waypoints.rs (line 25)
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

source§

fn clone(&self) -> PathingGrid

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for PathingGrid

source§

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

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

impl Default for PathingGrid

source§

fn default() -> PathingGrid

Returns the “default value” for a type. Read more
source§

impl Display for PathingGrid

source§

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

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

impl Grid<bool> for PathingGrid

source§

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.

source§

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

source§

fn get(&self, x: usize, y: usize) -> bool

source§

fn width(&self) -> usize

source§

fn height(&self) -> usize

source§

fn get_point(&self, point: Point) -> T

source§

fn set_point(&mut self, point: Point, value: T)

source§

fn get_ix(&self, x: usize, y: usize) -> usize

Gets the index corresponding to a coordinate, which is row-wise.
source§

fn get_ix_point(&self, point: &Point) -> usize

source§

fn point_in_bounds(&self, point: Point) -> bool

Tests whether a point is in bounds.
source§

fn index_in_bounds(&self, x: usize, y: usize) -> bool

Tests whether an index is in bounds.
source§

fn set_rectangle(&mut self, rect: &Rect, value: T)

Sets a given rectangle on the grid to the value.
source§

fn rect(&self) -> Rect

Retrieves the rectangle corresponding to the grid dimensions at the origin.
source§

fn get_rect(&self, rect: Rect) -> Vec<T>

Retrieves a column-wise vector of grid values in the given rectangle.

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

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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>,

§

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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V