TinyRobotics
Loading...
Searching...
No Matches
CallbackMap.h
1#pragma once
2#include <cmath>
3#include <cstdint>
4#include <vector>
5
6#include "TinyRobotics/maps/IMap.h"
7#include "TinyRobotics/utils/Common.h"
8
9namespace tinyrobotics {
10
11/**
12 * @class CallbackMap
13 * @brief A map-like utility for generating and validating neighbor coordinates
14 * in a configurable pattern.
15 *
16 * CallbackMap generates neighbor coordinates around a given point at a
17 * specified distance and angular resolution. It supports a user-provided
18 * callback to determine if a coordinate is valid (e.g., obstacle-free).
19 *
20 * Typical use: pathfinding, navigation, or virtual occupancy grids where
21 * neighbors are not stored but computed on demand.
22 *
23 * @tparam T Numeric type for coordinates (default: DistanceM)
24 *
25 * Example usage:
26 * @code
27 * CallbackMap<> map(1.0f, 16); // 1m distance, 16 directions
28 * map.setIsValidCallback([](Coordinate<DistanceM> c) { return c.x > 0; });
29 * auto neighbors = map.getNeighbors(Coordinate<DistanceM>(0,0));
30 * @endcode
31 */
32
33template <typename T = DistanceM>
34class CallbackMap : public IMap<T> {
35 public:
36 /// @brief Default constructor.
37 CallbackMap() = default;
38
39 /// @brief Construct a CallbackMap with extents, resolution, and neighbor
40 /// count.
41 /// @param distanceX Map extent in X direction (meters)
42 /// @param distanceY Map extent in Y direction (meters)
43 /// @param resolutionM Grid resolution (meters)
44 /// @param neighborCount Number of neighbors to generate
45 CallbackMap(float distanceX, float distanceY, float resolutionM,
46 int neighborCount)
47 : defaultResolutionM(resolutionM),
48 defaultNeighborCount(neighborCount),
49 distanceX(distanceX),
50 distanceY(distanceY) {}
51
52 // --- IMap interface implementations ---
53
54 /// @brief Get the number of cells in the X direction.
55 int getXCount() const override {
56 return (defaultResolutionM > 0 && distanceX > 0)
57 ? static_cast<int>(distanceX / defaultResolutionM)
58 : 0;
59 }
60
61 /// @brief Get the number of cells in the Y direction.
62 int getYCount() const override {
63 return (defaultResolutionM > 0 && distanceY > 0)
64 ? static_cast<int>(distanceY / defaultResolutionM)
65 : 0;
66 }
67
68 /// @brief Get the map resolution in meters.
69 float getResolution() const override { return defaultResolutionM; }
70
71 /// @brief Generate valid neighbor coordinates around a given point.
72 /// @param from Center coordinate
73 /// @return Vector of valid neighbor coordinates
74 std::vector<Coordinate<T>> getNeighbors(Coordinate<T> from) const override {
75 std::vector<Coordinate<T>> neighbors;
76 for (int i = 0; i < defaultNeighborCount; ++i) {
77 float angle = 2.0f * static_cast<float>(M_PI) * i / defaultNeighborCount;
78 float dx = defaultResolutionM * std::cos(angle);
79 float dy = defaultResolutionM * std::sin(angle);
80 Coordinate<T> neighbor(from.x + dx, from.y + dy);
81 if (isValid(neighbor)) {
82 neighbors.push_back(neighbor);
83 }
84 }
85 return neighbors;
86 }
87
88 /// @brief Check if a coordinate is valid (not occupied).
89 /// @param coord Coordinate to check
90 /// @return True if valid (not OCCUPIED), false otherwise
91 bool isValid(const Coordinate<T>& coord) const override {
92 if (isValidCB) {
93 return isValidCB(coord) != CellState::OCCUPIED;
94 }
95 // If no callback is set, treat all cells as valid
96 return true;
97 }
98
99 /// @brief Get the cell state at grid coordinates (x, y).
100 /// @param x X grid index
101 /// @param y Y grid index
102 /// @param state Output cell state
103 /// @return True if cell state is available
104 bool getCell(int x, int y, CellState& state) const override {
105 Coordinate<T> c = toWorld(x, y);
106 if (isValidCB) {
107 state = isValidCB(c);
108 return true;
109 }
110 return false;
111 }
112
113 /// @brief Convert grid indices to world coordinates.
114 /// @param x X grid index
115 /// @param y Y grid index
116 /// @return World coordinate
117 Coordinate<T> toWorld(int x, int y) const override {
118 return Coordinate<T>(x * defaultResolutionM, y * defaultResolutionM);
119 }
120
121 /// @brief Set the callback for cell validity checking.
122 /// @param callback Function pointer taking a coordinate and returning
123 /// CellState
124 void setIsValidCallback(CellState (*callback)(Coordinate<T>)) {
125 isValidCB = callback;
126 }
127
128 /// @brief Set the map resolution in meters.
129 /// @param resolutionM Grid resolution
130 void setResolution(float resolutionM) { defaultResolutionM = resolutionM; }
131
132 /// @brief Set the number of neighbors to generate.
133 /// @param neighborCount Number of neighbors
134 void setNeighborCount(int neighborCount) {
135 defaultNeighborCount = neighborCount;
136 }
137
138 /// @brief Set the map extent in X direction.
139 /// @param dx Extent in meters
140 void setDistanceX(float dx) { distanceX = dx; }
141
142 /// @brief Set the map extent in Y direction.
143 /// @param dy Extent in meters
144 void setDistanceY(float dy) { distanceY = dy; }
145
146 protected:
147 int defaultNeighborCount = 36;
148 float defaultResolutionM = 1.0f;
149 float distanceX = 0;
150 float distanceY = 0;
151 CellState (*isValidCB)(Coordinate<T> coord) = nullptr;
152};
153
154} // namespace tinyrobotics
A map-like utility for generating and validating neighbor coordinates in a configurable pattern.
Definition: CallbackMap.h:34
void setDistanceY(float dy)
Set the map extent in Y direction.
Definition: CallbackMap.h:144
void setNeighborCount(int neighborCount)
Set the number of neighbors to generate.
Definition: CallbackMap.h:134
CallbackMap()=default
Default constructor.
std::vector< Coordinate< T > > getNeighbors(Coordinate< T > from) const override
Generate valid neighbor coordinates around a given point.
Definition: CallbackMap.h:74
void setIsValidCallback(CellState(*callback)(Coordinate< T >))
Set the callback for cell validity checking.
Definition: CallbackMap.h:124
bool isValid(const Coordinate< T > &coord) const override
Check if a coordinate is valid (not occupied).
Definition: CallbackMap.h:91
int getXCount() const override
Get the number of cells in the X direction.
Definition: CallbackMap.h:55
int getYCount() const override
Get the number of cells in the Y direction.
Definition: CallbackMap.h:62
CallbackMap(float distanceX, float distanceY, float resolutionM, int neighborCount)
Construct a CallbackMap with extents, resolution, and neighbor count.
Definition: CallbackMap.h:45
Coordinate< T > toWorld(int x, int y) const override
Convert grid indices to world coordinates.
Definition: CallbackMap.h:117
float getResolution() const override
Get the map resolution in meters.
Definition: CallbackMap.h:69
void setResolution(float resolutionM)
Set the map resolution in meters.
Definition: CallbackMap.h:130
void setDistanceX(float dx)
Set the map extent in X direction.
Definition: CallbackMap.h:140
bool getCell(int x, int y, CellState &state) const override
Get the cell state at grid coordinates (x, y).
Definition: CallbackMap.h:104
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
Abstract interface for 2D grid maps and occupancy maps in TinyRobotics.
Definition: IMap.h:79
CellState
Cell state for occupancy grid mapping (e.g., UNKNOWN, FREE, OCCUPIED).
Definition: Common.h:32