Skip to main content

DynamicOccupancyGrid

Struct DynamicOccupancyGrid 

Source
pub struct DynamicOccupancyGrid<T: Numeric + Primal = f64> { /* private fields */ }
Available on crate feature alloc only.
Expand description

A map of square cells sized when the program runs, each free or blocked — what a map read from a file or sized from a sensor needs.

Cells are named row first, as they are stored: cell (row, column) covers the world square starting at origin + [column · resolution, row · resolution].

use core::f64::consts::{FRAC_PI_2, PI};

use multicalc::mapping::{DynamicOccupancyGrid, MutableOccupancyMap, OccupancyMap};

// A 20 m by 15 m warehouse floor at 5 cm cells: 400 across by 300 up, 120,000 cells in all.
let floor_width = 20.0_f64;
let floor_depth = 15.0;
let cell_size = 0.05;
let columns = (floor_width / cell_size) as usize;
let rows = (floor_depth / cell_size) as usize;
let lowest_corner = [0.0, 0.0];
let mut floor = DynamicOccupancyGrid::try_new(columns, rows, cell_size, lowest_corner)?;
assert_eq!(floor.columns(), 400);
assert_eq!(floor.rows(), 300);

// The outer walls, drawn a decimetre inside the edge so the whole loop lands on the map.
let margin = 0.1;
let walls = [
    [margin, margin],
    [floor_width - margin, margin],
    [floor_width - margin, floor_depth - margin],
    [margin, floor_depth - margin],
];
let joined_up = true;
floor.occupy_polyline(&walls, joined_up);

// A run of shelving along the south side, and a roof pillar in the middle of the floor.
let shelving = [[2.0, 2.0], [8.0, 2.0], [8.0, 3.0], [2.0, 3.0]];
floor.occupy_polyline(&shelving, joined_up);
let pillar_centre = [10.0, 7.5];
let pillar_radius = 0.4;
floor.occupy_circle(pillar_centre, pillar_radius);

// A robot parked halfway down the floor, five metres west of the pillar, taking a scan.
let robot = [5.0, 7.5];
let maximum_range = 10.0;

// Anything drawn is a cell thick, so the face a beam meets can sit a cell either side of the
// line that drew it. At 5 cm cells that is what a scan against a map can be trusted to.
let within_a_cell_or_two = 2.0 * cell_size;

// East: the near face of the pillar, at its centre less its radius.
let east = 0.0;
let pillar_face = pillar_centre[0] - pillar_radius - robot[0];
let ahead = floor.cast_ray(robot, east, maximum_range);
assert!(ahead.is_some_and(|met| (met - pillar_face).abs() <= within_a_cell_or_two));

// North: nothing until the far wall, seven and a half metres up.
let north = FRAC_PI_2;
let wall_face = floor_depth - margin - robot[1];
let above = floor.cast_ray(robot, north, maximum_range);
assert!(above.is_some_and(|met| (met - wall_face).abs() <= within_a_cell_or_two));

// South: the shelving stops the beam well before the wall behind it.
let south = -FRAC_PI_2;
let shelving_near_side = 3.0;
let shelving_face = robot[1] - shelving_near_side;
let below = floor.cast_ray(robot, south, maximum_range);
assert!(below.is_some_and(|met| (met - shelving_face).abs() <= within_a_cell_or_two));

// West: open floor all the way to the wall, which is nearer than the beam can see.
let west = PI;
let behind = floor.cast_ray(robot, west, maximum_range);
assert!(behind.is_some_and(|met| met < robot[0] && met > robot[0] - 2.0 * margin));

// A shorter-sighted sensor in the same spot sees nothing at all to the west.
let short_sighted = 4.0;
assert!(floor.cast_ray(robot, west, short_sighted).is_none());

Implementations§

Source§

impl<T: Numeric + Primal> DynamicOccupancyGrid<T>

Source

pub fn try_new( columns: usize, rows: usize, resolution: T, origin: [T; 2], ) -> Result<Self, MappingError>

A map of the given size with every cell free.

resolution is the edge length of one cell and origin is the world corner of cell (0, 0), its lowest x and lowest y.

Returns MappingError::EmptyGrid with no columns or no rows, MappingError::GridTooLarge when the two multiplied out are more cells than can be counted, MappingError::NonFinite if the cell size or origin is not finite, and MappingError::NonPositiveResolution if the cell size is zero or negative.

Trait Implementations§

Source§

impl<T: Clone + Numeric + Primal> Clone for DynamicOccupancyGrid<T>

Source§

fn clone(&self) -> DynamicOccupancyGrid<T>

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<T: Debug + Numeric + Primal> Debug for DynamicOccupancyGrid<T>

Source§

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

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

impl<T: Numeric + Primal> MutableOccupancyMap<T> for DynamicOccupancyGrid<T>

Source§

fn set_cell(&mut self, row: usize, column: usize, occupied: bool)

Marks a cell. An index outside the map does nothing.
Source§

fn clear(&mut self)

Frees every cell.
Source§

fn occupy_point(&mut self, point: [T; 2])

Blocks the cell holding point. A point outside the map does nothing.
Source§

fn occupy_polyline(&mut self, polyline: &[[T; 2]], closed: bool)

Blocks the cells along each edge of a list of points. closed joins the last point back to the first. Each edge is sampled well inside a cell width, so a wall it draws has no gap a beam could slip through. Read more
Source§

fn occupy_circle(&mut self, center: [T; 2], radius: T)

Blocks the cells around a circle’s rim — the outline, not the filled disc. Read more
Source§

impl<T: Numeric + Primal> OccupancyMap<T> for DynamicOccupancyGrid<T>

Source§

fn columns(&self) -> usize

How many cells across.
Source§

fn rows(&self) -> usize

How many cells up.
Source§

fn resolution(&self) -> T

The edge length of one cell.
Source§

fn origin(&self) -> [T; 2]

The world corner of cell (0, 0): its lowest x and lowest y.
Source§

fn is_occupied(&self, row: usize, column: usize) -> bool

Whether the cell is blocked. A cell outside the map reads as free.
Source§

fn cell_of(&self, point: [T; 2]) -> Option<(usize, usize)>

The cell holding point, as (row, column), or None when the point lies outside the map. Read more
Source§

fn cast_ray( &self, start_position: [T; 2], bearing: T, maximum_range: T, ) -> Option<T>

How far a beam fired from start_position travels before it meets a blocked cell, or None when it meets none within maximum_range. Read more
Source§

impl<T: PartialEq + Numeric + Primal> PartialEq for DynamicOccupancyGrid<T>

Source§

fn eq(&self, other: &DynamicOccupancyGrid<T>) -> bool

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

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

Inequality operator !=. Read more
Source§

impl<T: PartialEq + Numeric + Primal> StructuralPartialEq for DynamicOccupancyGrid<T>

Auto Trait Implementations§

§

impl<T> Freeze for DynamicOccupancyGrid<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for DynamicOccupancyGrid<T>
where T: RefUnwindSafe,

§

impl<T> Send for DynamicOccupancyGrid<T>
where T: Send,

§

impl<T> Sync for DynamicOccupancyGrid<T>
where T: Sync,

§

impl<T> Unpin for DynamicOccupancyGrid<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for DynamicOccupancyGrid<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for DynamicOccupancyGrid<T>
where T: UnwindSafe,

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

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.