Bench-MR
A Motion Planning Benchmark for Wheeled Mobile Robots
GridMaze.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <ctime>
6#include <iostream>
7#include <nlohmann/json.hpp>
8#include <vector>
9
10#include "base/Environment.h"
12
13#define ROS_SUPPORT 0
14#define XML_SUPPORT 0
15
16#if ROS_SUPPORT
17#include <ros/ros.h>
18#endif
19
20class GridMaze : public Environment {
21 public:
22 GridMaze() = default;
23 GridMaze(const GridMaze &environment);
24 GridMaze(unsigned int seed, unsigned int width, unsigned int height,
25 double voxelSize = 1.);
26
27 ~GridMaze();
28
29 static inline const unsigned int DefaultWidth = 50;
30 static inline const unsigned int DefaultHeight = 50;
31
32 unsigned int seed() const { return _seed; }
33
34 bool empty() const { return _empty; }
35
36 double voxelSize() const { return _voxelSize; }
37
38 inline bool occupied(unsigned int index) const { return _grid[index]; }
39 inline bool occupied(double x, double y) {
41 // std::cout << "occupied ? " << x << "\t" << y << std::endl;
42 if (x < 0 || y < 0 || x > width() || y > height()) {
44 return true;
45 }
46 // if (!fastCollisionCheck) {
47 //#if QT_SUPPORT
48 // // bool o = bilinearDistance(x, y) <= 0.05;
49 // // QtVisualizer::drawNode(x, y, o ? Qt::red :
50 // Qt::darkGreen);
51 //#endif
52 // return _grid[coord2key(x, y)] || bilinearDistance(x, y) <= 0.1;
53 // }
54 bool c = _grid[coord2key(x, y)] || _grid[coord2key(x + .15, y)] ||
55 _grid[coord2key(x, y + .15)] ||
56 _grid[coord2key(x + .15, y + .15)] ||
57 _grid[coord2key(x - .15, y)] || _grid[coord2key(x, y - .15)] ||
58 _grid[coord2key(x - .15, y - .15)];
59
61 return c;
62 }
63
64 inline bool occupiedCell(unsigned int xi, unsigned int yi) const {
65 return _grid[yi * _voxels_x + xi];
66 }
67
72 inline double distance(double x, double y) override {
73 if (_distances == nullptr) computeDistances();
74 return _distances[coord2key(x, y)];
75 }
76
81 inline double distance(unsigned int index) {
82 if (_distances == nullptr) computeDistances();
83 return _distances[index];
84 }
85
90 inline double distance(unsigned int xi, unsigned int yi) {
91 if (_distances == nullptr) computeDistances();
92 return _distances[yi * _voxels_x + xi];
93 }
94
95 friend std::ostream &operator<<(std::ostream &stream, const GridMaze &m) {
96 for (unsigned int y = 0; y < m._voxels_y; ++y) {
97 for (unsigned int x = 0; x < m._voxels_x; ++x) {
98 if (std::round(m._goal.x) == x && std::round(m._goal.y) == y)
99 stream << 'G';
100 else if (std::round(m._start.x) == x && std::round(m._start.y) == y)
101 stream << 'S';
102 else if (m.occupiedCell(x, y))
103 stream << '#';
104 else
105 stream << ' ';
106 }
107 stream << std::endl;
108 }
109 return stream;
110 }
111
117 bool saveSbplConfigFile(const std::string &filename) const;
118
119 void mapData(unsigned char *data, double resolution = 1.);
120 std::string mapString() const;
121 std::vector<double> mapDistances();
122
123 std::vector<Rectangle> obstacles() const;
124 std::vector<Rectangle> obstacles(double x1, double y1, double x2,
125 double y2) const;
126
127 bool collides(double x, double y) override;
128 bool collides(const Polygon &polygon) override;
129
130 static std::shared_ptr<GridMaze> createRandom(
131 unsigned int width = DefaultWidth, unsigned int height = DefaultHeight,
132 double obsRatio = 0.3, unsigned int seed = (unsigned int)time(nullptr),
133 int borderSize = 1);
134 static std::shared_ptr<GridMaze> createRandomCorridor(
135 unsigned int width = DefaultWidth, unsigned int height = DefaultHeight,
136 double radius = 2, int branches = 30,
137 unsigned int seed = (unsigned int)time(nullptr), int borderSize = 1);
138 static std::shared_ptr<GridMaze> createFromObstacles(
139 const std::vector<Rectangle> &obstacles,
140 unsigned int width = DefaultWidth, unsigned int height = DefaultHeight,
141 int borderSize = 1);
142 static std::shared_ptr<GridMaze> createSimple();
143
144 static std::shared_ptr<GridMaze> createFromMovingAiScenario(
145 Scenario &scenario);
146
152 static std::shared_ptr<GridMaze> createFromImage(
153 const std::string &filename, double occupancy_threshold = 0.5,
154 int width = 0, int height = 0);
155
156#if XML_SUPPORT
157 static GridMaze *loadFromXml(std::string filename);
158#endif
159#if ROS_SUPPORT
160 void publish(ros::NodeHandle &nodeHandle) const;
161#endif
162
163 double obstacleRatio() const;
164 std::string generatorType() const;
165
166 unsigned int cells() const { return _voxels_x * _voxels_y; }
167
168 unsigned int voxels_x() const { return _voxels_x; }
169 unsigned int voxels_y() const { return _voxels_y; }
170
171 std::string name() const override { return _name; }
172 std::string &name() { return _name; }
173
179 void computeDistances();
180
181 void to_json(nlohmann::json &j) override {
182 j["type"] = "grid";
183 j["generator"] = generatorType();
184 j["width"] = voxels_x();
185 j["height"] = voxels_y();
186 j["obstacleRatio"] = obstacleRatio();
187 j["seed"] = seed();
188 j["start"] = {start().x, start().y, startTheta()};
189 j["goal"] = {goal().x, goal().y, goalTheta()};
190 j["map"] = mapString();
191 j["name"] = name();
192 if (global::settings.log_env_distances) j["distances"] = mapDistances();
193 j["distance_computation_method"] =
195 }
196
197 protected:
199
200 inline unsigned int coord2key(double x, double y) const {
201 return (unsigned int)std::max(
202 0., std::min(std::round(y), _voxels_y - 1.) * _voxels_x +
203 std::min(std::round(x), _voxels_x - 1.));
204 }
205 void fill(double x, double y, bool value);
206 void fill(const Rectangle &r, bool value);
207 void fillBorder(bool value, int size = 1);
208
210 if (global::settings.auto_choose_distance_computation_method) {
211 if (cells() > global::settings.fast_odf_threshold)
214 }
216 }
217
218 private:
219 // true means occupied
220 bool *_grid{nullptr};
221
222 unsigned int _voxels_x{0};
223 unsigned int _voxels_y{0};
224
225 double *_distances{nullptr};
226 double _voxelSize{1.0};
227 bool _empty{true};
228 unsigned int _seed{0};
229 std::string _type{"undefined"};
230 std::string _name{"grid"};
231};
Definition: Environment.h:8
double goalTheta() const
Definition: Environment.h:111
double width() const
Definition: Environment.h:34
Stopwatch _collision_timer
Definition: Environment.h:139
double startTheta() const
Definition: Environment.h:110
double height() const
Definition: Environment.h:35
Point _goal
Definition: Environment.h:132
Point _start
Definition: Environment.h:131
const Point & goal() const
Definition: Environment.h:17
const Point & start() const
Definition: Environment.h:14
Definition: GridMaze.h:20
distance_computation::Method distanceComputationMethod() const
Definition: GridMaze.h:209
void fillBorder(bool value, int size=1)
Definition: GridMaze.cpp:89
std::string mapString() const
Definition: GridMaze.cpp:707
static std::shared_ptr< GridMaze > createSimple()
Definition: GridMaze.cpp:565
friend std::ostream & operator<<(std::ostream &stream, const GridMaze &m)
Definition: GridMaze.h:95
unsigned int coord2key(double x, double y) const
Definition: GridMaze.h:200
Stopwatch _collision_timer
Definition: Environment.h:139
bool saveSbplConfigFile(const std::string &filename) const
Writes cfg file for planners based on sbpl.
Definition: GridMaze.cpp:667
bool collides(double x, double y) override
Definition: GridMaze.cpp:355
unsigned int voxels_y() const
Definition: GridMaze.h:169
GridMaze()=default
std::vector< double > mapDistances()
Definition: GridMaze.cpp:716
static std::shared_ptr< GridMaze > createRandomCorridor(unsigned int width=DefaultWidth, unsigned int height=DefaultHeight, double radius=2, int branches=30, unsigned int seed=(unsigned int) time(nullptr), int borderSize=1)
Definition: GridMaze.cpp:97
std::vector< Rectangle > obstacles() const
Definition: GridMaze.cpp:399
bool empty() const
Definition: GridMaze.h:34
void fill(double x, double y, bool value)
Definition: GridMaze.cpp:75
std::string generatorType() const
Definition: GridMaze.cpp:665
double distance(double x, double y) override
Computes distances field if necessary, and returns the distance to the nearest obstacle.
Definition: GridMaze.h:72
bool occupied(double x, double y)
Definition: GridMaze.h:39
static std::shared_ptr< GridMaze > createRandom(unsigned int width=DefaultWidth, unsigned int height=DefaultHeight, double obsRatio=0.3, unsigned int seed=(unsigned int) time(nullptr), int borderSize=1)
Definition: GridMaze.cpp:182
std::string & name()
Definition: GridMaze.h:172
double voxelSize() const
Definition: GridMaze.h:36
std::string name() const override
Definition: GridMaze.h:171
unsigned int seed() const
Definition: GridMaze.h:32
unsigned int voxels_x() const
Definition: GridMaze.h:168
static std::shared_ptr< GridMaze > createFromMovingAiScenario(Scenario &scenario)
Definition: GridMaze.cpp:575
bool occupiedCell(unsigned int xi, unsigned int yi) const
Definition: GridMaze.h:64
void computeDistances()
Brute-force, quadratic in the number of cells, algorithm to compute the distance field,...
Definition: GridMaze.cpp:422
~GridMaze()
Definition: GridMaze.cpp:70
static std::shared_ptr< GridMaze > createFromObstacles(const std::vector< Rectangle > &obstacles, unsigned int width=DefaultWidth, unsigned int height=DefaultHeight, int borderSize=1)
Definition: GridMaze.cpp:216
void mapData(unsigned char *data, double resolution=1.)
Definition: GridMaze.cpp:688
static std::shared_ptr< GridMaze > createFromImage(const std::string &filename, double occupancy_threshold=0.5, int width=0, int height=0)
Creates a map from a grayscale image where tones below the occupancy threshold are considered obstacl...
Definition: GridMaze.cpp:605
double obstacleRatio() const
Definition: GridMaze.cpp:657
double distance(unsigned int index)
Computes distances field if necessary, and returns the distance to the nearest obstacle.
Definition: GridMaze.h:81
double distance(unsigned int xi, unsigned int yi)
Computes distances field if necessary, and returns the distance to the nearest obstacle.
Definition: GridMaze.h:90
unsigned int cells() const
Definition: GridMaze.h:166
bool occupied(unsigned int index) const
Definition: GridMaze.h:38
static const unsigned int DefaultHeight
Definition: GridMaze.h:30
void to_json(nlohmann::json &j) override
Definition: GridMaze.h:181
static const unsigned int DefaultWidth
Definition: GridMaze.h:29
double stop()
Stops the timer.
Definition: Stopwatch.hpp:32
void resume()
Continues the stop watch from when it was stopped.
Definition: Stopwatch.hpp:51
std::string to_string(Method m)
Definition: PlannerSettings.h:32
Method
Definition: PlannerSettings.h:31
@ BRUTE_FORCE
Definition: PlannerSettings.h:31
@ DEAD_RECKONING
Definition: PlannerSettings.h:31
double max(const std::vector< double > &values)
Definition: PathStatistics.hpp:79
double min(const std::vector< double > &values)
Definition: PathStatistics.hpp:73
Property< distance_computation::Method > distance_computation_method
Definition: PlannerSettings.h:235
double x
Definition: Primitives.h:43
double y
Definition: Primitives.h:43
Definition: Primitives.h:145
Definition: Primitives.h:19
Load MovingAI scenario files.
Definition: ScenarioLoader.h:11
static PlannerSettings::GlobalSettings settings
Definition: PlannerSettings.h:699