TinyRobotics
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
GridMap< StateT, T > Class Template Reference

A grid map for spatial representation, navigation, and planning. More...

#include <GridMap.h>

Inheritance diagram for GridMap< StateT, T >:
Inheritance graph
[legend]
Collaboration diagram for GridMap< StateT, T >:
Collaboration graph
[legend]

Classes

struct  Cell
 Cell structure to represent grid cell indices. More...
 

Public Member Functions

 GridMap ()=default
 Default constructor.
 
 GridMap (int xCount, int yCount, DistanceM resolutionM)
 Construct with cell counts and resolution in meters.
 
 GridMap (int xCount, int yCount, Distance resolution)
 Construct with cell counts and Distance object for resolution.
 
bool worldToCell (DistanceM wx, DistanceM wy, Cell &cell) const
 World to cell conversion.
 
void cellToWorld (int cx, int cy, DistanceM &wx, DistanceM &wy) const
 Cell to world (center of cell)
 
Coordinate< DistanceM > toWorld (int cx, int cy) const
 Provide the workd coordinates for the cell.
 
bool getCell (int cx, int cy, StateT &result)
 Provide access to cell state by cell index.
 
bool getCell (int cx, int cy, CellState &result) const
 Provide access to cell state by cell index.
 
bool getCell (Coordinate< T > &coord, CellState &result)
 Provide access to cell state by coordinate.
 
void setCell (Cell &cell, CellState value)
 Provide access to cell state by coordinate.
 
void setCell (int cx, int cy, CellState value)
 Set cell state (for initialization or manual updates)
 
void setCell (Coordinate< T > &coord, CellState value)
 Set cell state by coordinate (converts to cell index internally)
 
std::vector< CellgetNeighborCells (const Coordinate< DistanceM > from) const
 Determine all neighboring cells (8-connected) for a given cell coordinate.
 
std::vector< Coordinate< T > > getNeighbors (Coordinate< T > from) const
 Get world coordinates of neighboring cells (for pathfinding or navigation)
 
void resize (int newXCount, int newYCount)
 Resize the grid to new dimensions.
 
int getXCount () const
 Get the number of cells in the x direction.
 
int getYCount () const
 Get the number of cells in the y direction.
 
float getResolution () const
 Get the cell resolution in meters.
 
void setResolution (float resM)
 Defines the resolution in meters.
 
bool isValid (const Coordinate< T > &coord) const
 Check if a coordinate is within the map bounds.
 
void setCellStateCallback (CellState(*cb)(const StateT &, void *ref))
 Set the callback for converting StateType to CellState.
 
void setValidityCallback (bool(*cb)(int cx, int cy, void *ref))
 Set the callback for cell validity checking.
 
void setReference (void *ref)
 Set the reference pointer passed to callbacks.
 
size_t writeTo (Print &out)
 Write map to output.
 
size_t readFrom (Stream &in)
 Read map from input.
 
virtual int getXCount () const =0
 Get the number of cells in the X direction.
 
virtual int getYCount () const =0
 Get the number of cells in the Y direction.
 
virtual float getResolution () const =0
 Get the map resolution (cell size in meters).
 
virtual bool getCell (int x, int y, CellState &state) const =0
 Get the state of a cell by integer indices.
 
virtual Coordinate< T > toWorld (int x, int y) const =0
 Convert cell indices to world coordinates.
 
virtual std::vector< Coordinate< T > > getNeighbors (Coordinate< T > from) const =0
 Get world coordinates of neighboring cells (for pathfinding or navigation).
 
virtual bool isValid (const Coordinate< T > &coord) const =0
 Check if a coordinate is inside the map bounds.
 

Static Protected Member Functions

static bool isValid (int cx, int cy, void *ref)
 

Protected Attributes

int xCount = 0
 
int yCount = 0
 
float resolution = 0
 
Coordinate< T > origin
 
void * reference = this
 
bool(* is_valid_cb )(int cx, int cy, void *) = isValid
 
CellState(* get_cellstate_cb )(const StateT &, void *ref) = nullptr
 
std::vector< StateT, AllocatorPSRAM< StateT > > data
 
GridMapSerializer< GridMap< StateT >, StateT, T > serializer
 

Detailed Description

template<typename StateT = CellState, typename T = DistanceM>
class tinyrobotics::GridMap< StateT, T >

A grid map for spatial representation, navigation, and planning.

The GridMap class models the environment as a regular grid of cells, each storing a state (e.g., occupancy, height, or other user-defined data). It supports:

Typical use cases include:

Features:

Callback Notes:

Example:

GridMap<CellState> map(100, 100, 0.1f); // 10m x 10m, 10cm resolution
Coordinate<float> pos(1.2, 3.4);
map.setCell(pos, CellState::OCCUPIED);
auto state = map.getCell(5, 5);
// For GridMap<float>, set a callback to convert float to CellState
map.setCellStateCallback([](const float& v, void*) {
return v > 0.5f ? CellState::OCCUPIED : CellState::FREE;
});
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
A grid map for spatial representation, navigation, and planning.
Definition: GridMap.h:71
Template Parameters
StateTypeThe type stored in each cell (e.g., occupancy, height)
TNumeric type for coordinates (default: float)

Member Function Documentation

◆ getCell()

bool getCell ( int  cx,
int  cy,
CellState result 
) const
inlinevirtual

Provide access to cell state by cell index.

Implements IMap< T >.

◆ getNeighbors()

std::vector< Coordinate< T > > getNeighbors ( Coordinate< T >  from) const
inlinevirtual

Get world coordinates of neighboring cells (for pathfinding or navigation)

Implements IMapNeighbors< T >.

◆ getResolution()

float getResolution ( ) const
inlinevirtual

Get the cell resolution in meters.

Implements IMap< T >.

◆ getXCount()

int getXCount ( ) const
inlinevirtual

Get the number of cells in the x direction.

Implements IMap< T >.

◆ getYCount()

int getYCount ( ) const
inlinevirtual

Get the number of cells in the y direction.

Implements IMap< T >.

◆ isValid() [1/2]

bool isValid ( const Coordinate< T > &  coord) const
inlinevirtual

Check if a coordinate is within the map bounds.

Parameters
coordThe coordinate to check.
Returns
true if the coordinate is inside the map, false otherwise.

Implements IMapNeighbors< T >.

◆ isValid() [2/2]

static bool isValid ( int  cx,
int  cy,
void *  ref 
)
inlinestaticprotected

Default validity check: a cell is valid if it's not occupied. This can be overridden with a custom callback for more complex logic (e.g., dynamic obstacles, special terrain, etc.).

◆ toWorld()

Coordinate< DistanceM > toWorld ( int  cx,
int  cy 
) const
inlinevirtual

Provide the workd coordinates for the cell.

Implements IMap< T >.


The documentation for this class was generated from the following file: