Bench-MR
A Motion Planning Benchmark for Wheeled Mobile Robots
PolygonMaze.h
Go to the documentation of this file.
1#pragma once
2
3#include <collision2d/sat.hpp>
4#include <memory>
5
6#include "base/Environment.h"
9
13class PolygonMaze : public Environment {
14 public:
16
17 std::string name() const override { return _name; }
18
19 const std::vector<Polygon> &obstacles() const { return _obstacles; }
20
21 static std::shared_ptr<PolygonMaze> loadFromSvg(const std::string &filename) {
22 auto maze = std::make_shared<PolygonMaze>();
23 maze->_name += " " + filename;
24 maze->_obstacles = SvgPolygonLoader::load(filename);
25 if (maze->_obstacles.empty()) {
26 OMPL_ERROR(
27 ("Could not find any obstacles in \"" + filename + "\".").c_str());
28 return maze;
29 }
30 for (auto &obstacle : maze->_obstacles)
31 obstacle.scale(global::settings.env.polygon.scaling);
32 auto min = maze->_obstacles[0].min();
33 auto max = maze->_obstacles[0].max();
34 for (const auto &o : maze->_obstacles) {
35 const auto new_min = o.min();
36 const auto new_max = o.max();
37 if (new_min.x < min.x) min.x = new_min.x;
38 if (new_max.x > max.x) max.x = new_max.x;
39 if (new_min.y < min.y) min.y = new_min.y;
40 if (new_max.y > max.y) max.y = new_max.y;
41 }
42 maze->_bounds.setLow(0, min.x);
43 maze->_bounds.setLow(1, min.y);
44 maze->_bounds.setHigh(0, max.x);
45 maze->_bounds.setHigh(1, max.y);
46 OMPL_INFORM(("Loaded polygon maze from \"" + filename + "\".").c_str());
47 OMPL_INFORM("\tBounds: [%.2f %.2f] -- [%.2f %.2f]", min.x, min.y, max.x,
48 max.y);
49 return maze;
50 }
51
52 bool collides(double x, double y) override {
53 int i = 0;
54 for (const auto &poly : _obstacles) {
55 if (collision2d::intersect(collision2d::Point<double>{x, y},
56 (collision2d::Polygon<double>)poly)) {
57 #ifdef DEBUG
58 OMPL_DEBUG("[%.2f %.2f] collides with polygon %d.", x, y, i);
59 #endif
60 return true;
61 }
62 ++i;
63 }
64 return false;
65 }
66 bool collides(const Polygon &polygon) override {
67 for (const auto &poly : _obstacles) {
68 if (collision2d::intersect((collision2d::Polygon<double>)polygon,
69 (collision2d::Polygon<double>)poly)) {
70 // std::cerr << "Intersection between polygons " << polygon.min() << " "
71 // << polygon.max() << " and " << poly.min() << " " <<
72 // poly.max()
73 // << std::endl;
74 return true;
75 }
76 }
77 return false;
78 }
79
80 void to_json(nlohmann::json &j) override {
81 j["type"] = "polygon";
82 j["obstacles"] = obstacles();
83 j["start"] = {start().x, start().y, startTheta()};
84 j["goal"] = {goal().x, goal().y, goalTheta()};
85 j["width"] = width();
86 j["height"] = height();
87 j["min_x"] = _bounds.low[0];
88 j["min_y"] = _bounds.low[1];
89 j["max_x"] = _bounds.high[0];
90 j["max_y"] = _bounds.high[1];
91 j["name"] = name();
92 }
93
94 double unit() const override { return .2; }
95
96 private:
97 std::string _name{"polygon_maze"};
98 std::vector<Polygon> _obstacles;
99};
Definition: Environment.h:8
double goalTheta() const
Definition: Environment.h:111
double width() const
Definition: Environment.h:34
double startTheta() const
Definition: Environment.h:110
ob::RealVectorBounds _bounds
Definition: Environment.h:137
double height() const
Definition: Environment.h:35
const Point & goal() const
Definition: Environment.h:17
const Point & start() const
Definition: Environment.h:14
Implements a maze consisting of convex shapes as obstacles.
Definition: PolygonMaze.h:13
const std::vector< Polygon > & obstacles() const
Definition: PolygonMaze.h:19
void to_json(nlohmann::json &j) override
Definition: PolygonMaze.h:80
bool collides(double x, double y) override
Definition: PolygonMaze.h:52
double unit() const override
Unit, e.g.
Definition: PolygonMaze.h:94
bool collides(const Polygon &polygon) override
Definition: PolygonMaze.h:66
static std::shared_ptr< PolygonMaze > loadFromSvg(const std::string &filename)
Definition: PolygonMaze.h:21
PolygonMaze()
Definition: PolygonMaze.h:15
std::string name() const override
Definition: PolygonMaze.h:17
static std::vector< Polygon > load(const std::string &filename)
Definition: SvgPolygonLoader.hpp:16
double max(const std::vector< double > &values)
Definition: PathStatistics.hpp:79
double min(const std::vector< double > &values)
Definition: PathStatistics.hpp:73
double x
Definition: Primitives.h:43
double y
Definition: Primitives.h:43
Definition: Primitives.h:145
static PlannerSettings::GlobalSettings settings
Definition: PlannerSettings.h:699