2#include "TinyRobotics/coordinates/Coordinate.h"
3#include "TinyRobotics/maps/IMap.h"
6namespace tinyrobotics {
9
10
11
12
13
26
27
28
29
30
31
32
33
34template <
typename T=DistanceM>
38
39
40
44
45
46
50
51
52
56
57
58
62 switchFirstLast =
true;
64 switchFirstLast =
false;
69
70
71
78
79
80
81
82
86 record_count_ = frontiers.size();
87 if (frontiers.empty())
return false;
90 if (switchFirstLast) {
99 return selectRandom(nextCell);
101 return selectNearest(nextCell);
103 return selectFarthest(nextCell);
105 return selectFirst(nextCell);
107 return selectLast(nextCell);
109 return selectCustom(nextCell);
120 bool switchFirstLast =
false;
121 size_t record_count_ = 0;
123 std::vector<Coordinate<T>> frontiers;
125 int (*select_callback)(std::vector<Coordinate<T>>& frontiers,
126 void* ref) =
nullptr;
130 int idx = random(0, frontiers.size());
131 nextCell = frontiers[idx];
138 float distance = current_pos.distance(result);
139 for (
int i = 1; i < frontiers.size(); ++i) {
140 Coordinate<T>& tmp = frontiers[i];
141 float tmp_distance = current_pos.distance(tmp);
142 if (tmp_distance < distance) {
144 distance = tmp_distance;
152 bool selectFarthest(
Coordinate<T>& nextCell) {
154 float distance = current_pos.distance(result);
155 for (
int i = 1; i < frontiers.size(); ++i) {
156 Coordinate<T>& tmp = frontiers[i];
157 float tmp_distance = current_pos.distance(tmp);
158 if (tmp_distance > distance) {
160 distance = tmp_distance;
169 nextCell = frontiers.front();
175 nextCell = frontiers.back();
181 if (select_callback) {
182 int idx = select_callback(frontiers, ref);
183 if (idx >= 0 && idx < frontiers.size()) {
184 nextCell = frontiers[idx];
194
195
196
197
200 void* ref =
nullptr) {
201 select_callback = cb;
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
228 for (
int y = 0; y < map_.getYCount(); ++y) {
229 for (
int x = 0; x < map_.getXCount(); ++x) {
231 if (!map_.getCell(x, y, state) || state !=
CellState::FREE)
continue;
234 for (
int dy = -1; dy <= 1 && !found; ++dy) {
235 for (
int dx = -1; dx <= 1 && !found; ++dx) {
236 if (dx == 0 && dy == 0)
continue;
238 if (map_.getCell(x + dx, y + dy, neighborState) &&
241 frontiers.push_back(map_.toWorld(x, y));
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
Generic exploration and frontier-based SLAM utility for grid or occupancy maps.
Definition: FrontierExplorer.h:35
FrontierExplorer(IMap< T > &map)
Construct a FrontierExplorer with a given map (by reference).
Definition: FrontierExplorer.h:41
void collectFrontiers()
Find all frontier cells in the map (cells adjacent to unknown space).
Definition: FrontierExplorer.h:226
size_t size() const
Provides the number of potential frontier cells found in the last search.
Definition: FrontierExplorer.h:115
bool getNextFrontier(Coordinate< T > &nextCell)
Provides the next frontier cell to explore based on the current strategy.
Definition: FrontierExplorer.h:83
void setSelectCallback(int(*cb)(std::vector< Coordinate< T > > &frontiers, void *ref), void *ref=nullptr)
Set a custom callback for selecting the next frontier cell. You can implement your own optimization s...
Definition: FrontierExplorer.h:198
Coordinate< T > getCurrentPosition() const
Get the current position of the explorer.
Definition: FrontierExplorer.h:53
void setCurrentPosition(const Coordinate< T > &pos)
Set the current position of the explorer.
Definition: FrontierExplorer.h:47
void setStrategy(FrontierSelectionStrategy strategy)
Set the strategy for selecting the next frontier cell.
Definition: FrontierExplorer.h:59
FrontierSelectionStrategy getStrategy() const
Get the current frontier selection strategy.
Definition: FrontierExplorer.h:72
Interface for frontier-based exploration utilities.
Definition: IFrontierExplorer.h:18
Abstract interface for 2D grid maps and occupancy maps in TinyRobotics.
Definition: IMap.h:79
FrontierSelectionStrategy
Strategy for selecting the next frontier cell to explore. Used by FrontierExplorer to determine how t...
Definition: FrontierExplorer.h:14
@ RANDOM
Randomly select a frontier cell.
@ FIRST
Select the first frontier cell found (e.g., in scan order)
@ CUSTOM
Use a user-defined callback to select the frontier.
@ NEAREST
Select the frontier cell closest to the robot.
@ SWITCH_FIRST_LAST
Alternate between first and last on each call.
@ FARTHEST
Select the frontier cell farthest from the robot.
@ LAST
Select the last frontier cell found.
CellState
Cell state for occupancy grid mapping (e.g., UNKNOWN, FREE, OCCUPIED).
Definition: Common.h:32