Skip to main content

DistanceField

Struct DistanceField 

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

Cost from every cell to the nearest of one or more goals (a Dijkstra map).

One computation serves any number of agents chasing the same target: each agent calls next_step for its cell. Distances use the same costs and corner rules as PathFinder, so a chain of next steps follows an optimal path. Unlike PathFinder, which lets an agent leave a blocked start cell, blocked cells have no distance and no next step. Buffers are reused; computing again for a grid no larger than before does not allocate, apart from the queue growing past its previous peak.

use rayengine_core::glam::UVec2;
use rayengine_core::pathfinding::{CostGrid, DistanceField, Neighborhood};

let mut grid = CostGrid::new(UVec2::new(3, 3), 1.0);
grid.block_rect(UVec2::new(1, 0), UVec2::new(1, 1));
let mut field = DistanceField::new();
field.compute(&grid, [UVec2::new(2, 0)], Neighborhood::Four).unwrap();
assert_eq!(field.distance(UVec2::new(0, 0)), Some(6.0));
assert_eq!(field.next_step(&grid, UVec2::new(0, 0)), Some(UVec2::new(0, 1)));
assert_eq!(field.distance(UVec2::new(1, 0)), None);

Implementations§

Source§

impl DistanceField

Source

pub fn new() -> Self

Creates an empty field with no buffers allocated.

Source

pub fn compute<G: NavGrid + ?Sized>( &mut self, grid: &G, goals: impl IntoIterator<Item = UVec2>, neighborhood: Neighborhood, ) -> Result<(), PathError>

Recomputes distances to the nearest walkable goal.

Blocked goals are ignored; with no walkable goals every cell is unreachable. Costs must be finite and positive. On error the field is left empty, as if computed for a zero-sized grid.

Source

pub fn size(&self) -> UVec2

Size of the grid the field was computed for.

Source

pub fn distance(&self, cell: UVec2) -> Option<f32>

Cost from cell to the nearest goal, or None when the cell is blocked, unreachable, or outside the computed grid.

Source

pub fn next_step<G: NavGrid + ?Sized>( &self, grid: &G, cell: UVec2, ) -> Option<UVec2>

The neighbor to move to from cell along an optimal path, or None at a goal, from an unreachable cell, or for a grid of another size.

Pass the grid the field was computed from. Exact ties prefer orthogonal moves in the order +x, +y, -x, -y.

Trait Implementations§

Source§

impl Clone for DistanceField

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 DistanceField

Source§

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

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

impl Default for DistanceField

Source§

fn default() -> Self

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

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> 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<T> Component for T
where T: Send + Sync + 'static,

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.