7#include "TinyRobotics/utils/AllocatorPSRAM.h"
8#include "TinyRobotics/utils/Common.h"
9#include <TinyRobotics/serialize/MapSerializer.h>
11namespace tinyrobotics {
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
70template <
typename StateT =
CellState,
typename T = DistanceM>
82 GridMap(
int xCount,
int yCount, DistanceM resolutionM)
83 : resolution(resolutionM) {
88 : xCount(xCount), yCount(yCount) {
95 cell.cx =
static_cast<
int>(std::floor((wx - origin.x) / resolution));
96 cell.cy =
static_cast<
int>(std::floor((wy - origin.y) / resolution));
97 return (cell.cx >= 0 && cell.cx < xCount && cell.cy >= 0 &&
102 void cellToWorld(
int cx,
int cy, DistanceM& wx, DistanceM& wy)
const {
103 wx = origin.x + (
static_cast<
float>(cx) + 0.5f) * resolution;
104 wy = origin.y + (
static_cast<
float>(cy) + 0.5f) * resolution;
110 cellToWorld(cx, cy, coord.x, coord.y);
115 bool getCell(
int cx,
int cy, StateT& result) {
116 if (cx < 0 || cx >= xCount || cy < 0 || cy >= yCount)
118 result = data[cy * xCount + cx];
124 if (cx < 0 || cx >= xCount || cy < 0 || cy >= yCount)
126 if constexpr (std::is_same<StateT,
CellState>::value) {
127 result = data[cy * xCount + cx];
130 if (get_cellstate_cb ==
nullptr) {
133 result = get_cellstate_cb(data[cy * xCount + cx], reference);
142 return getCell(cell.cx, cell.cy);
149 if (cell.cx >= 0 && cell.cx < xCount && cell.cy >= 0 && cell.cy < yCount)
150 data[cell.cy * xCount + cell.cx] = value;
155 if (cx >= 0 && cx < xCount && cy >= 0 && cy < yCount)
156 data[cy * xCount + cx] = value;
163 setCell(cell.cx, cell.cy, value);
170 worldToCell(from.x, from.y, cell);
171 int cx =
static_cast<
int>(cell.cx);
172 int cy =
static_cast<
int>(cell.cy);
173 std::vector<Cell> neighbors;
176 {
static_cast<size_t>(cx + 1),
static_cast<size_t>(cy)});
179 {
static_cast<size_t>(cx - 1),
static_cast<size_t>(cy)});
182 {
static_cast<size_t>(cx),
static_cast<size_t>(cy + 1)});
185 {
static_cast<size_t>(cx),
static_cast<size_t>(cy - 1)});
187 if (cx < xCount - 1 && cy < yCount - 1)
189 {
static_cast<size_t>(cx + 1),
static_cast<size_t>(cy + 1)});
190 if (cx > 0 && cy < yCount - 1)
192 {
static_cast<size_t>(cx - 1),
static_cast<size_t>(cy + 1)});
193 if (cx < xCount - 1 && cy > 0)
195 {
static_cast<size_t>(cx + 1),
static_cast<size_t>(cy - 1)});
196 if (cx > 0 && cy > 0)
198 {
static_cast<size_t>(cx - 1),
static_cast<size_t>(cy - 1)});
204 std::vector<Coordinate<T>> neighbors;
205 for (
auto& cell : getNeighborCells(from)) {
206 Coordinate<T> neighbor;
207 cellToWorld(cell.cx, cell.cy, neighbor.x, neighbor.y);
208 neighbors.push_back(neighbor);
214 void resize(
int newXCount,
int newYCount) {
217 data.resize(xCount * yCount);
233
234
235
236
239 cx =
static_cast<
int>((coord.x - origin.x) / resolution);
240 cy =
static_cast<
int>((coord.y - origin.y) / resolution);
241 bool in_range = cx >= 0 && cx < xCount && cy >= 0 && cy < yCount;
242 if (!in_range)
return false;
243 return is_valid_cb(cx, cy, reference);
248 get_cellstate_cb = cb;
253 is_valid_cb = cb !=
nullptr ? cb : isValid;
261 return serializer.write(*
this, out);
266 return serializer.read(*
this, in);
273 float resolution = 0;
275 void* reference =
this;
276 bool (*is_valid_cb)(
int cx,
int cy,
void*) = isValid;
277 CellState (*get_cellstate_cb)(
const StateT&,
void* ref) =
nullptr;
282 GridMapSerializer<GridMap<StateT>,StateT, T> serializer;
288 static bool isValid(
int cx,
int cy,
void* ref) {
291 if (!self->getCell(cx, cy, result))
return false;
292 if constexpr (std::is_same<StateT,
CellState>::value) {
Custom allocator that uses ESP32's PSRAM for memory allocation.
Definition: AllocatorPSRAM.h:21
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
Represents a distance measurement with unit conversion support.
Definition: Distance.h:40
A grid map for spatial representation, navigation, and planning.
Definition: GridMap.h:71
size_t readFrom(Stream &in)
Read map from input.
Definition: GridMap.h:265
void resize(int newXCount, int newYCount)
Resize the grid to new dimensions.
Definition: GridMap.h:214
void cellToWorld(int cx, int cy, DistanceM &wx, DistanceM &wy) const
Cell to world (center of cell)
Definition: GridMap.h:102
void setValidityCallback(bool(*cb)(int cx, int cy, void *ref))
Set the callback for cell validity checking.
Definition: GridMap.h:252
GridMap(int xCount, int yCount, DistanceM resolutionM)
Construct with cell counts and resolution in meters.
Definition: GridMap.h:82
Coordinate< DistanceM > toWorld(int cx, int cy) const
Provide the workd coordinates for the cell.
Definition: GridMap.h:108
size_t writeTo(Print &out)
Write map to output.
Definition: GridMap.h:260
bool getCell(int cx, int cy, StateT &result)
Provide access to cell state by cell index.
Definition: GridMap.h:115
GridMap(int xCount, int yCount, Distance resolution)
Construct with cell counts and Distance object for resolution.
Definition: GridMap.h:87
std::vector< Coordinate< T > > getNeighbors(Coordinate< T > from) const
Get world coordinates of neighboring cells (for pathfinding or navigation)
Definition: GridMap.h:203
int getXCount() const
Get the number of cells in the x direction.
Definition: GridMap.h:221
GridMap()=default
Default constructor.
void setResolution(float resM)
Defines the resolution in meters.
Definition: GridMap.h:230
void setCell(Cell &cell, CellState value)
Provide access to cell state by coordinate.
Definition: GridMap.h:148
void setCellStateCallback(CellState(*cb)(const StateT &, void *ref))
Set the callback for converting StateType to CellState.
Definition: GridMap.h:247
std::vector< Cell > getNeighborCells(const Coordinate< DistanceM > from) const
Determine all neighboring cells (8-connected) for a given cell coordinate.
Definition: GridMap.h:168
void setReference(void *ref)
Set the reference pointer passed to callbacks.
Definition: GridMap.h:257
bool isValid(const Coordinate< T > &coord) const
Check if a coordinate is within the map bounds.
Definition: GridMap.h:237
void setCell(Coordinate< T > &coord, CellState value)
Set cell state by coordinate (converts to cell index internally)
Definition: GridMap.h:160
int getYCount() const
Get the number of cells in the y direction.
Definition: GridMap.h:224
bool getCell(int cx, int cy, CellState &result) const
Provide access to cell state by cell index.
Definition: GridMap.h:123
bool worldToCell(DistanceM wx, DistanceM wy, Cell &cell) const
World to cell conversion.
Definition: GridMap.h:94
float getResolution() const
Get the cell resolution in meters.
Definition: GridMap.h:227
static bool isValid(int cx, int cy, void *ref)
Definition: GridMap.h:288
void setCell(int cx, int cy, CellState value)
Set cell state (for initialization or manual updates)
Definition: GridMap.h:154
bool getCell(Coordinate< T > &coord, CellState &result)
Provide access to cell state by coordinate.
Definition: GridMap.h:139
Abstract interface for 2D grid maps and occupancy maps in TinyRobotics.
Definition: IMap.h:79
DistanceUnit
Supported distance units for conversion and representation.
Definition: Distance.h:10
CellState
Cell state for occupancy grid mapping (e.g., UNKNOWN, FREE, OCCUPIED).
Definition: Common.h:32
Cell structure to represent grid cell indices.
Definition: GridMap.h:74