5#include "TinyRobotics/coordinates/Coordinate.h"
6#include "TinyRobotics/utils/AllocatorPSRAM.h"
7#include "TinyRobotics/utils/Common.h"
8#include <TinyRobotics/serialize/MapSerializer.h>
10namespace tinyrobotics {
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
29template <
typename T = DistanceM>
37 GridBitMap() =
default;
38 GridBitMap(
int xCount,
int yCount,
float resolutionM)
39 : xCount(xCount), yCount(yCount), resolution(resolutionM) {
40 resize(xCount, yCount);
42 GridBitMap(
int xCount,
int yCount,
Distance resolution)
43 : xCount(xCount), yCount(yCount) {
45 resize(xCount, yCount);
48 void resize(
int newXCount,
int newYCount) {
51 occupied.resize(xCount * yCount,
false);
52 free.resize(xCount * yCount,
false);
62 bool worldToCell(
float wx,
float wy,
Cell& cell)
const {
63 cell.cx =
static_cast<
int>((wx - origin.x) / resolution);
64 cell.cy =
static_cast<
int>((wy - origin.y) / resolution);
65 return (cell.cx >= 0 && cell.cx < xCount && cell.cy >= 0 &&
70 void cellToWorld(
int cx,
int cy,
float& wx,
float& wy)
const {
71 wx = origin.x + (cx + 0.5f) * resolution;
72 wy = origin.y + (cy + 0.5f) * resolution;
77 if (cx < 0 || cx >= xCount || cy < 0 || cy >= yCount)
return false;
78 size_t idx = cy * xCount + cx;
81 }
else if (free[idx]) {
90 void setCell(
int cx,
int cy,
CellState value) {
91 if (cx < 0 || cx >= xCount || cy < 0 || cy >= yCount)
return;
92 size_t idx = cy * xCount + cx;
95 occupied[idx] =
false;
98 case CellState::OCCUPIED:
102 case CellState::UNKNOWN:
103 occupied[idx] =
false;
112 if (worldToCell(coord.x, coord.y, cell)) {
113 return getCell(cell.cx, cell.cy, result);
121 if (worldToCell(coord.x, coord.y, cell)) {
122 setCell(cell.cx, cell.cy, value);
127
128
129
130
133 cx =
static_cast<
int>((coord.x - origin.x) / resolution);
134 cy =
static_cast<
int>((coord.y - origin.y) / resolution);
135 return cx >= 0 && cx < xCount && cy >= 0 && cy < yCount;
140 std::vector<Coordinate<T>> neighbors;
141 for (
auto& cell : getNeighborCells(from)) {
142 Coordinate<T> neighbor;
143 cellToWorld(cell.cx, cell.cy, neighbor.x, neighbor.y);
144 neighbors.push_back(neighbor);
152 cellToWorld(cx, cy, wx, wy);
158 return serializer.write(*
this, out);
163 return serializer.read(*
this, in);
170 float resolution = 0.1f;
172 std::vector<
bool> occupied;
173 std::vector<
bool> free;
180 worldToCell(from.x, from.y, cell);
181 int cx =
static_cast<
int>(cell.cx);
182 int cy =
static_cast<
int>(cell.cy);
183 std::vector<Cell> neighbors;
186 {
static_cast<size_t>(cx + 1),
static_cast<size_t>(cy)});
189 {
static_cast<size_t>(cx - 1),
static_cast<size_t>(cy)});
192 {
static_cast<size_t>(cx),
static_cast<size_t>(cy + 1)});
195 {
static_cast<size_t>(cx),
static_cast<size_t>(cy - 1)});
197 if (cx < xCount - 1 && cy < yCount - 1)
199 {
static_cast<size_t>(cx + 1),
static_cast<size_t>(cy + 1)});
200 if (cx > 0 && cy < yCount - 1)
202 {
static_cast<size_t>(cx - 1),
static_cast<size_t>(cy + 1)});
203 if (cx < xCount - 1 && cy > 0)
205 {
static_cast<size_t>(cx + 1),
static_cast<size_t>(cy - 1)});
206 if (cx > 0 && cy > 0)
208 {
static_cast<size_t>(cx - 1),
static_cast<size_t>(cy - 1)});
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 using two bit vectors to represent CellState efficiently.
Definition: GridBitMap.h:30
size_t readFrom(Stream &in)
Read map from input.
Definition: GridBitMap.h:162
size_t writeTo(Print &out)
Write map to output.
Definition: GridBitMap.h:157
Coordinate< T > toWorld(int cx, int cy) const override
Convert cell indices to world coordinates (returns Coordinate<T>)
Definition: GridBitMap.h:150
int getXCount() const
Get the number of cells in the X direction.
Definition: GridBitMap.h:55
std::vector< Coordinate<> > getNeighbors(Coordinate< T > from) const
Get world coordinates of neighboring cells (for pathfinding or navigation)
Definition: GridBitMap.h:139
bool isValid(const Coordinate< T > &coord) const
Check if a coordinate is within the map bounds.
Definition: GridBitMap.h:131
int getYCount() const
Get the number of cells in the Y direction.
Definition: GridBitMap.h:57
bool getCell(int cx, int cy, CellState &result) const
Get the state of a cell by integer indices.
Definition: GridBitMap.h:76
std::vector< Cell > getNeighborCells(const Coordinate< T > from) const
Determine all neighboring cells (8-connected) for a given cell coordinate.
Definition: GridBitMap.h:178
float getResolution() const
Get the map resolution (cell size in meters).
Definition: GridBitMap.h:59
Utility class for serializing and deserializing grid maps in CSV format.
Definition: MapSerializer.h:42
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
Definition: GridBitMap.h:32