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>
impl<T: Numeric + Primal> DynamicOccupancyGrid<T>
Sourcepub fn try_new(
columns: usize,
rows: usize,
resolution: T,
origin: [T; 2],
) -> Result<Self, MappingError>
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>
impl<T: Clone + Numeric + Primal> Clone for DynamicOccupancyGrid<T>
Source§fn clone(&self) -> DynamicOccupancyGrid<T>
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: Numeric + Primal> MutableOccupancyMap<T> for DynamicOccupancyGrid<T>
impl<T: Numeric + Primal> MutableOccupancyMap<T> for DynamicOccupancyGrid<T>
Source§fn set_cell(&mut self, row: usize, column: usize, occupied: bool)
fn set_cell(&mut self, row: usize, column: usize, occupied: bool)
Marks a cell. An index outside the map does nothing.
Source§fn occupy_point(&mut self, point: [T; 2])
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)
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 moreSource§fn occupy_circle(&mut self, center: [T; 2], radius: T)
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>
impl<T: Numeric + Primal> OccupancyMap<T> for DynamicOccupancyGrid<T>
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> 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
Mutably borrows from an owned value. Read more