Map classes
Planner endpoints, returned paths and plot axes use world coordinates (x, y),
or (x, y, theta) with angles in radians. Distances and obstacle inflation
radii are in world units; parameters explicitly expressed in cells, such as
PRM’s collision_step, retain that meaning.
An occupancy grid’s origin is the world position of cell (column=0, row=0),
and cellsize is the side length of a cell. Arrays are indexed as
[row, column]. Use world_to_grid (w2g) and grid_to_world (g2w)
to convert coordinates. A raw array has unit cells and origin (0, 0), as in
the book examples. DistanceTransformPlanner, DstarPlanner and Bug2 round
endpoints to the nearest cell and return cell centres in world coordinates;
continuous planners retain continuous world coordinates. Distance-transform
fields are in world units, while the standalone distancexform array
algorithm measures distances in cells. D* costs are per world unit, and its
sensor receives and returns positions in world coordinates.
A map draws its own background. An occupancy grid’s plot method accepts an
optional field with the same shape as the grid, plus colorbar,
colorbar_style, labels, label_style, cmap and an inflated
obstacle mask. The existing map argument replaces the image without an
obstacle overlay. A polygon map draws its polygons in world coordinates.
Fields, obstacles and labels share the grid’s coordinate conversion and the supplied axes. Image edges extend half a cell beyond the outer cell centres; labels sit at cell centres, and enabling them shows edge cells in full. Plotting preserves the supplied field and style dictionaries.
Occupancy grid classes
Binary Occupancy grid
- class BinaryOccupancyGrid(grid=None, origin=(0, 0), value=0, cellsize=1, workspace=None, name=None)[source]
Bases:
BaseOccupancyGridBinary occupancy grid
- __init__(grid=None, origin=(0, 0), value=0, cellsize=1, workspace=None, name=None)[source]
Create a binary occupancy grid instance
- Parameters:
grid (
ndarray|BinaryOccupancyGrid|None) – occupancy grid as a NumPy array, or another binary occupancy grid, defaults to Noneorigin (
Union[List[float],Tuple[float,float],ndarray[Tuple[Literal[2]],dtype[floating]]]) – world coordinates of the grid element [0,0], defaults to (0, 0)value (
float|bool) – initial value of cells if the grid is created fromworkspace, defaults to 0cellsize (
float) – cell size in world units, defaults to 1workspace (
Union[float,ndarray,Sequence[float],HasWorkspace,None]) – extent of the grid in world coordinates, used ifgridis not given, see The workspace
- Raises:
ValueError –
gridis not an array or an occupancy grid, or neithergridnorworkspaceis given
The array is kept internally as a bool array. Cells are set to True (occupied) corresponding to input values > 0.
This object supports a user-defined coordinate system and grid size. World coordinates are converted to grid coordinates to lookup the occupancy status.
Example:
>>> from roboticstoolbox import BinaryOccupancyGrid >>> import numpy as np >>> og = BinaryOccupancyGrid(np.zeros((5,5))) >>> print(og) BinaryOccupancyGrid: 5 x 5, cell size=1, x = [0.0, 4.0], y = [0.0, 4.0], 0.0% occupied >>> og = BinaryOccupancyGrid(workspace=[-5,5], cellsize=0.1, value=0) >>> print(og) BinaryOccupancyGrid: 101 x 101, cell size=0.1, x = [-5.0, 5.0], y = [-5.0, 5.0], 0.0% occupied
See also
- __str__()[source]
Compact string description of the occupancy grid, including the percentage of cells that are occupied
- Return type:
- isoccupied(p)[source]
Test if coordinate is occupied
- Parameters:
p (
Union[List[float],Tuple[float,float],ndarray[Tuple[Literal[2]],dtype[floating]]]) – world coordinate (x, y)- Return type:
- Returns:
occupancy status of corresponding grid cell
The grid cell size and offset are used to convert
pto an occupancy grid coordinate. The grid coordinate is rounded and cast to integer value. If the coordinate is outside the bounds of the occupancy grid it is considered to be occupied.See also
- inflate(radius)[source]
Inflate obstacles
- Parameters:
radius (
float) – radius of circular structuring element in world units- Return type:
A circular structuring element is created and used to dilate the stored occupancy grid.
Successive calls to
inflatewill compound the inflation.See also
- g2w(p)
Convert grid coordinate to world coordinate (superclass)
- Parameters:
p (
Union[List[float],Tuple[float,float],ndarray[Tuple[Literal[2]],dtype[floating]]]) – grid coordinate (column, row)- Return type:
- Returns:
world coordinate (x, y)
The grid cell size and offset are used to convert occupancy grid coordinate
pto a world coordinate.g2wis a shorthand for this method.
- property grid: ndarray
Occupancy grid as a NumPy array (superclass)
- Returns:
binary occupancy grid
- Return type:
ndarray(N,M) of bool
If
inflatehas been called, this will return the inflated occupancy grid.
- grid_to_world(p)
Convert grid coordinate to world coordinate (superclass)
- Parameters:
p (
Union[List[float],Tuple[float,float],ndarray[Tuple[Literal[2]],dtype[floating]]]) – grid coordinate (column, row)- Return type:
- Returns:
world coordinate (x, y)
The grid cell size and offset are used to convert occupancy grid coordinate
pto a world coordinate.g2wis a shorthand for this method.
- property height: float | None
Height of the workspace in world units
- Returns:
y-extent of the workspace, None if there is no workspace
- line_w(p1, p2)
Get index of cells along a line segment (superclass)
- Parameters:
- Returns:
index into the raveled grid
- Return type:
Get the indices of cells along a line segment defined by the end points given in world coordinates.
The returned indices can be applied to a raveled view of the grid.
line_wis a shorthand for this method.See also
- line_world(p1, p2)
Get index of cells along a line segment (superclass)
- Parameters:
- Returns:
index into the raveled grid
- Return type:
Get the indices of cells along a line segment defined by the end points given in world coordinates.
The returned indices can be applied to a raveled view of the grid.
line_wis a shorthand for this method.See also
- property maxdim: float
Maximum dimension of grid in world coordinates (superclass)
- Returns:
maximum side length of the occupancy grid
- Return type:
- property name: str | None
Occupancy grid name (superclass)
- Returns:
name of the occupancy grid
- Return type:
- plot(map=None, ax=None, block=None, *, field=None, colorbar=True, colorbar_style=None, labels=False, label_style=None, cmap=None, inflated=None, **kwargs)
Plot the occupancy grid (superclass)
- Parameters:
map (
ndarray|None) – array which is plotted instead of the grid, must be same size as the occupancy grid, defaults to Noneax (
Axes|None) – Matplotlib axes to plot into, defaults to None (the current axes)block (
bool|None) –Noneleaves showing the figure to the environment (noplt.showcall),Falseshows it and returns at once,Trueshows it and waits until it is closed; ignored by notebook backends, defaults to Nonefield (
ndarray|None) – optional scalar field, with the same shape as the grid; obstacles are drawn over it, defaults to Nonecolorbar (
bool|dict[str,Any]) – show a colorbar for the field, defaults to Truecolorbar_style (
dict[str,Any] |None) – colorbar options, with label “Distance” by defaultlabels (
bool) – label finite field values at cell centres, defaults to Falselabel_style (
dict[str,Any] |None) – Matplotlib text options, merged over red centred text with a white outlinecmap (
str|Colormap|None) – colormap for the field or replacementmap. Fields default to gray; replacement images use Matplotlib’s default colormapinflated (
ndarray|None) – inflated obstacle mask, with the same shape as the grid; additional occupied cells are drawn in pink, defaults to Nonekwargs (
Any) – arguments passed toimshow
- Raises:
ValueError – an array has the wrong shape, or both
mapandfieldare supplied- Return type:
The grid is plotted as an image but with axes in world coordinates.
Binary grids show free space in white, obstacles in red and inflated obstacles in pink. A field is drawn beneath the obstacles, with unreachable (infinite) values in blue. General grids and replacement images are drawn using their numeric values. Passing
cmapwithout a field overrides the grid’s colormap.Cell centres use the grid’s origin and cell size; image edges extend half a cell beyond the outer centres.
labelsshows the edge cells whole. The field and supplied style dictionaries are not modified.See also
- property ravel: ndarray
Ravel the grid (superclass)
- Returns:
1D view of the occupancy grid
- Return type:
ndarray(N)
- set(region, value)
Set region of map (superclass)
- Parameters:
- Return type:
The cells that contain the corners of the region, and all those between them, are set.
- property shape: tuple[int, ...]
Shape of the occupancy grid array (superclass)
- Returns:
shape of the occupancy grid array
- Return type:
2-tuple
This is the shape of the NumPy array that holds the occupancy grid.
- w2g(p)
Convert world coordinate to grid coordinate (superclass)
- Parameters:
p (
Union[List[float],Tuple[float,float],ndarray[Tuple[Literal[2]],dtype[floating]]]) – world coordinate (x, y)- Return type:
- Returns:
grid coordinate (column, row)
The grid cell size and offset are used to convert
pto an occupancy grid coordinate. The grid coordinate is rounded and cast to integer value. No check is made on the validity of the coordinate.w2gis a shorthand for this method.
- property width: float | None
Width of the workspace in world units
- Returns:
x-extent of the workspace, None if there is no workspace
- property workspace: ndarray[Tuple[Literal[4]], dtype[floating]]
Bounds of the occupancy grid in world coordinates (superclass)
- Returns:
workspace bounds [xmin, xmax, ymin, ymax]
Returns the bounds of the occupancy grid in world coordinates.
- world_to_grid(p)
Convert world coordinate to grid coordinate (superclass)
- Parameters:
p (
Union[List[float],Tuple[float,float],ndarray[Tuple[Literal[2]],dtype[floating]]]) – world coordinate (x, y)- Return type:
- Returns:
grid coordinate (column, row)
The grid cell size and offset are used to convert
pto an occupancy grid coordinate. The grid coordinate is rounded and cast to integer value. No check is made on the validity of the coordinate.w2gis a shorthand for this method.
- property xmax: float
Maximum x-coordinate of this grid (superclass)
- Returns:
maximum world x-coordinate
- Return type:
- property xmin: float
Minimum x-coordinate of this grid (superclass)
- Returns:
minimum world x-coordinate
- Return type:
Occupancy grid
- class OccupancyGrid(grid=None, origin=(0, 0), value=0, cellsize=1, workspace=None, name=None)[source]
Bases:
BaseOccupancyGridGeneral occupancy grid
The elements of the array are floats and can represent occupancy probability or traversal cost.
Example:
>>> from roboticstoolbox import OccupancyGrid >>> import numpy as np >>> og = OccupancyGrid(np.zeros((5,5))) >>> print(og) OccupancyGrid: 5 x 5, cell size=1, x = [0.0, 4.0], y = [0.0, 4.0], dtype float64, min 0.0, max 0.0, mean 0.0 >>> og = OccupancyGrid(workspace=[-5,5], cellsize=0.1, value=0.5) >>> print(og) OccupancyGrid: 101 x 101, cell size=0.1, x = [-5.0, 5.0], y = [-5.0, 5.0], dtype float64, min 0.5, max 0.5, mean 0.5
See also
- __str__()[source]
Compact string description of the occupancy grid, including the data type and the minimum, maximum and mean of the cell values
- Return type:
- __init__(grid=None, origin=(0, 0), value=0, cellsize=1, workspace=None, name=None)
Occupancy grid (superclass)
- Parameters:
grid (
ndarray|None) – occupancy grid as a NumPy array, one row per y and one column per x, defaults to Noneorigin (
Union[List[float],Tuple[float,float],ndarray[Tuple[Literal[2]],dtype[floating]]]) – world coordinates of the grid element [0,0], defaults to (0, 0)value (
float|bool) – initial value of cells if the grid is created fromworkspace, defaults to 0cellsize (
float) – cell size in world units, defaults to 1workspace (
Union[float,ndarray,Sequence[float],HasWorkspace,None]) – extent of the grid in world coordinates, used ifgridis not given, see The workspace
- Raises:
ValueError – neither
gridnorworkspaceis given
This object supports a user-defined coordinate system and grid size. World coordinates are converted to grid coordinates to lookup the occupancy status.
The grid can be initialized by:
a 2D NumPy array,
grid, withoriginandcellsizespecifying
workspace,cellsizeandvaluearguments
- g2w(p)
Convert grid coordinate to world coordinate (superclass)
- Parameters:
p (
Union[List[float],Tuple[float,float],ndarray[Tuple[Literal[2]],dtype[floating]]]) – grid coordinate (column, row)- Return type:
- Returns:
world coordinate (x, y)
The grid cell size and offset are used to convert occupancy grid coordinate
pto a world coordinate.g2wis a shorthand for this method.
- property grid: ndarray
Occupancy grid as a NumPy array (superclass)
- Returns:
binary occupancy grid
- Return type:
ndarray(N,M) of bool
If
inflatehas been called, this will return the inflated occupancy grid.
- grid_to_world(p)
Convert grid coordinate to world coordinate (superclass)
- Parameters:
p (
Union[List[float],Tuple[float,float],ndarray[Tuple[Literal[2]],dtype[floating]]]) – grid coordinate (column, row)- Return type:
- Returns:
world coordinate (x, y)
The grid cell size and offset are used to convert occupancy grid coordinate
pto a world coordinate.g2wis a shorthand for this method.
- property height: float | None
Height of the workspace in world units
- Returns:
y-extent of the workspace, None if there is no workspace
- line_w(p1, p2)
Get index of cells along a line segment (superclass)
- Parameters:
- Returns:
index into the raveled grid
- Return type:
Get the indices of cells along a line segment defined by the end points given in world coordinates.
The returned indices can be applied to a raveled view of the grid.
line_wis a shorthand for this method.See also
- line_world(p1, p2)
Get index of cells along a line segment (superclass)
- Parameters:
- Returns:
index into the raveled grid
- Return type:
Get the indices of cells along a line segment defined by the end points given in world coordinates.
The returned indices can be applied to a raveled view of the grid.
line_wis a shorthand for this method.See also
- property maxdim: float
Maximum dimension of grid in world coordinates (superclass)
- Returns:
maximum side length of the occupancy grid
- Return type:
- property name: str | None
Occupancy grid name (superclass)
- Returns:
name of the occupancy grid
- Return type:
- plot(map=None, ax=None, block=None, *, field=None, colorbar=True, colorbar_style=None, labels=False, label_style=None, cmap=None, inflated=None, **kwargs)
Plot the occupancy grid (superclass)
- Parameters:
map (
ndarray|None) – array which is plotted instead of the grid, must be same size as the occupancy grid, defaults to Noneax (
Axes|None) – Matplotlib axes to plot into, defaults to None (the current axes)block (
bool|None) –Noneleaves showing the figure to the environment (noplt.showcall),Falseshows it and returns at once,Trueshows it and waits until it is closed; ignored by notebook backends, defaults to Nonefield (
ndarray|None) – optional scalar field, with the same shape as the grid; obstacles are drawn over it, defaults to Nonecolorbar (
bool|dict[str,Any]) – show a colorbar for the field, defaults to Truecolorbar_style (
dict[str,Any] |None) – colorbar options, with label “Distance” by defaultlabels (
bool) – label finite field values at cell centres, defaults to Falselabel_style (
dict[str,Any] |None) – Matplotlib text options, merged over red centred text with a white outlinecmap (
str|Colormap|None) – colormap for the field or replacementmap. Fields default to gray; replacement images use Matplotlib’s default colormapinflated (
ndarray|None) – inflated obstacle mask, with the same shape as the grid; additional occupied cells are drawn in pink, defaults to Nonekwargs (
Any) – arguments passed toimshow
- Raises:
ValueError – an array has the wrong shape, or both
mapandfieldare supplied- Return type:
The grid is plotted as an image but with axes in world coordinates.
Binary grids show free space in white, obstacles in red and inflated obstacles in pink. A field is drawn beneath the obstacles, with unreachable (infinite) values in blue. General grids and replacement images are drawn using their numeric values. Passing
cmapwithout a field overrides the grid’s colormap.Cell centres use the grid’s origin and cell size; image edges extend half a cell beyond the outer centres.
labelsshows the edge cells whole. The field and supplied style dictionaries are not modified.See also
- property ravel: ndarray
Ravel the grid (superclass)
- Returns:
1D view of the occupancy grid
- Return type:
ndarray(N)
- set(region, value)
Set region of map (superclass)
- Parameters:
- Return type:
The cells that contain the corners of the region, and all those between them, are set.
- property shape: tuple[int, ...]
Shape of the occupancy grid array (superclass)
- Returns:
shape of the occupancy grid array
- Return type:
2-tuple
This is the shape of the NumPy array that holds the occupancy grid.
- w2g(p)
Convert world coordinate to grid coordinate (superclass)
- Parameters:
p (
Union[List[float],Tuple[float,float],ndarray[Tuple[Literal[2]],dtype[floating]]]) – world coordinate (x, y)- Return type:
- Returns:
grid coordinate (column, row)
The grid cell size and offset are used to convert
pto an occupancy grid coordinate. The grid coordinate is rounded and cast to integer value. No check is made on the validity of the coordinate.w2gis a shorthand for this method.
- property width: float | None
Width of the workspace in world units
- Returns:
x-extent of the workspace, None if there is no workspace
- property workspace: ndarray[Tuple[Literal[4]], dtype[floating]]
Bounds of the occupancy grid in world coordinates (superclass)
- Returns:
workspace bounds [xmin, xmax, ymin, ymax]
Returns the bounds of the occupancy grid in world coordinates.
- world_to_grid(p)
Convert world coordinate to grid coordinate (superclass)
- Parameters:
p (
Union[List[float],Tuple[float,float],ndarray[Tuple[Literal[2]],dtype[floating]]]) – world coordinate (x, y)- Return type:
- Returns:
grid coordinate (column, row)
The grid cell size and offset are used to convert
pto an occupancy grid coordinate. The grid coordinate is rounded and cast to integer value. No check is made on the validity of the coordinate.w2gis a shorthand for this method.
- property xmax: float
Maximum x-coordinate of this grid (superclass)
- Returns:
maximum world x-coordinate
- Return type:
- property xmin: float
Minimum x-coordinate of this grid (superclass)
- Returns:
minimum world x-coordinate
- Return type:
Polygon map
- class PolygonMap(workspace=None, polygons=None)[source]
Bases:
BaseMapPolygonal obstacle map
- __init__(workspace=None, polygons=None)[source]
Polygonal obstacle map
- Parameters:
The obstacle polygons are specified as instances of
Polygon2. Useadd()to add a polygon given as an ndarray(2,N).The workspace can be specified in several ways:
workspacex-range
y-range
A (scalar)
-A:A
-A:A
[A, B]
A:B
A:B
[A, B, C, D]
A:B
C:D
Workspace is used only to set plot bounds.
- property height: float | None
Height of the workspace in world units
- Returns:
y-extent of the workspace, None if there is no workspace
- property width: float | None
Width of the workspace in world units
- Returns:
x-extent of the workspace, None if there is no workspace
- iscollision(polygon)[source]
Test for collision
- Parameters:
- Return type:
- Returns:
collision
The
polygonis tested against polygons in the map, and returns True on the first collision.
- plot(block=None)[source]
Plot the polygonal obstacles
- Parameters:
block (
bool|None) –Noneleaves showing the figure to the environment (noplt.showcall),Falseshows it and returns at once,Trueshows it and waits until it is closed; ignored by notebook backends, defaults to None- Return type:
The obstacles are plotted in red, in axes that have the limits of the workspace.
- isoccupied(p)[source]
Test if point lies inside an obstacle
- Parameters:
p (
Union[List[float],Tuple[float,float],ndarray[Tuple[Literal[2]],dtype[floating]]]) – a 2D point- Return type:
- Returns:
True if the point lies inside any obstacle
The point is tested for enclosure by polygons in the map, and returns True on the first enclosure.