3#include <ompl/base/State.h>
4#include <ompl/base/spaces/SE2StateSpace.h>
5#include <ompl/control/PathControl.h>
6#include <ompl/geometric/PathGeometric.h>
8#include <nlohmann/json.hpp>
10namespace ob = ompl::base;
12typedef ob::SE2StateSpace::StateType
State;
30 inline double width()
const {
return std::abs(
x1 -
x2); }
31 inline double height()
const {
return std::abs(
y1 -
y2); }
48 if (state ==
nullptr) {
49 OMPL_WARN(
"Cannot create point from NULL state.");
54 x = state->as<
State>()->getX();
55 y = state->as<
State>()->getY();
59 static std::vector<Point>
fromPath(
const ompl::geometric::PathGeometric &p,
60 bool interpolate =
false);
62 static std::vector<Point>
fromPath(
const ompl::control::PathControl &p,
63 bool interpolate =
false);
67 for (
auto &p : points) {
71 return {
x / points.size(),
y / points.size()};
75 const double dx = p.
x -
x;
76 const double dy = p.
y -
y;
77 return std::sqrt(dx * dx + dy * dy);
81 const double dx = this->x -
x;
82 const double dy = this->y -
y;
83 return std::sqrt(dx * dx + dy * dy);
87 const double dx = p.
x -
x;
88 const double dy = p.
y -
y;
89 return dx * dx + dy * dy;
93 const double dx = this->x -
x;
94 const double dy = this->y -
y;
95 return dx * dx + dy * dy;
98 operator Eigen::Matrix<double, 2, 1>()
const {
return {
x,
y}; }
135 return stream <<
'[' << p.
x <<
' ' << p.
y <<
']';
148 operator std::vector<Point> &() {
return points; }
149 operator const std::vector<Point> &()
const {
return points; }
155 for (
const auto &p :
points) c += p;
165 for (
auto &p :
points) p -= c;
169 for (
auto &p :
points) p *= scaling;
174 for (
auto &p : poly.
points) p *= scaling;
182 std::replace(path_str.begin(), path_str.end(),
',',
' ');
183 std::vector<std::string> tokens;
184 std::stringstream ss(path_str);
187 while (ss >> word) tokens.emplace_back(word);
188 std::vector<Point>
points;
191 for (
size_t i = 0; i < tokens.size(); i++) {
192 const auto &token = tokens[i];
193 if (token ==
"m" || token ==
"l") {
194 for (
size_t j = i + 1; j < tokens.size() - 1; j += 2) {
195 if (!is_number(tokens[j]))
break;
196 point += {stod(tokens[j]), -stod(tokens[j + 1])};
197 points.emplace_back(point);
201 if (token ==
"M" || token ==
"L") {
202 for (
size_t j = i + 1; j < tokens.size() - 1; j += 2) {
203 if (!is_number(tokens[j]))
break;
204 point =
Point(stod(tokens[j]), -stod(tokens[j + 1]));
205 points.emplace_back(point);
209 if (token ==
"h" || token ==
"v") {
210 for (
size_t j = i + 1; j < tokens.size(); j++) {
211 if (!is_number(tokens[j]))
break;
213 point.
x += stod(tokens[j]);
215 point.
y -= stod(tokens[j]);
216 points.emplace_back(point);
220 if (token ==
"H" || token ==
"V") {
221 for (
size_t j = i + 1; j < tokens.size(); j++) {
222 if (!is_number(tokens[j]))
break;
224 point.
x = stod(tokens[j]);
226 point.
y = -stod(tokens[j]);
227 points.emplace_back(point);
236 for (
auto &p :
points) p += t;
243 double c = std::cos(angle), s = std::sin(angle);
246 p.x = temp.
x * c - temp.
y * s;
247 p.y = temp.
x * s + temp.
y * c;
268 for (
const auto &p :
points) {
269 if (p.x < m.
x) m.
x = p.x;
270 if (p.y < m.
y) m.
y = p.y;
279 for (
const auto &p :
points) {
280 if (p.x > m.
x) m.
x = p.x;
281 if (p.y > m.
y) m.
y = p.y;
293 operator std::vector<Eigen::Matrix<double, 2, 1>>()
const {
294 std::vector<Eigen::Matrix<double, 2, 1>> v;
295 for (
const auto &p :
points)
296 v.emplace_back(Eigen::Matrix<double, 2, 1>{p.x, p.y});
301 for (
const auto &point : p.
points) stream << point <<
' ';
306 static bool is_number(
const std::string &s) {
319 for (
const auto &element : j) p.
points.emplace_back(element);
bool replace(std::string &str, const std::string &from, const std::string &to)
Definition: Log.cpp:49
void from_json(const nlohmann::json &j, Point &p)
Definition: Primitives.h:140
ob::SE2StateSpace::StateType State
Definition: Primitives.h:12
void to_json(nlohmann::json &j, const Point &p)
Definition: Primitives.h:139
Definition: Primitives.h:14
ob::State * StateFromXY(double x, double y)
Definition: Primitives.cpp:14
ob::State * StateFromXYT(double x, double y, double theta)
Definition: Primitives.cpp:6
double min(const std::vector< double > &values)
Definition: PathStatistics.hpp:73
Definition: Primitives.h:42
Point operator/(double d) const
Definition: Primitives.h:130
Point & operator-=(const Point &p)
Definition: Primitives.h:111
Point(const ob::State *state)
Definition: Primitives.h:47
double distance(double x, double y) const
Definition: Primitives.h:80
Point operator+(const Point &p) const
Definition: Primitives.h:110
double distanceSquared(double x, double y) const
Definition: Primitives.h:92
static Point centroid(const std::vector< Point > &points)
Definition: Primitives.h:65
Point & operator/=(double d)
Definition: Primitives.h:125
Point(double x, double y)
Definition: Primitives.h:46
Point & operator=(const Eigen::Matrix< double, 2, 1 > &p)
Definition: Primitives.h:99
double distance(const Point &p) const
Definition: Primitives.h:74
Point operator*(double d) const
Definition: Primitives.h:123
static std::vector< Point > fromPath(const ompl::geometric::PathGeometric &p, bool interpolate=false)
Definition: Primitives.cpp:25
double x
Definition: Primitives.h:43
Point & operator*=(double d)
Definition: Primitives.h:118
double distanceSquared(const Point &p) const
Definition: Primitives.h:86
Point & operator+=(const Point &p)
Definition: Primitives.h:105
ompl::base::State * toState(double theta=0) const
Definition: Primitives.cpp:21
Point operator-(const Point &p) const
Definition: Primitives.h:116
double y
Definition: Primitives.h:43
friend std::ostream & operator<<(std::ostream &stream, const Point &p)
Definition: Primitives.h:134
Definition: Primitives.h:145
Polygon convexHull() const
Definition: Primitives.cpp:106
void center()
Translates points such that the centroid becomes (0, 0).
Definition: Primitives.h:163
friend std::ostream & operator<<(std::ostream &stream, const Polygon &p)
Definition: Primitives.h:300
Polygon scaled(double scaling)
Definition: Primitives.h:172
Polygon(const std::vector< Point > &points={})
Definition: Primitives.h:151
Polygon transformed(const ompl::base::State *state) const
Definition: Primitives.h:251
Point centroid() const
Definition: Primitives.h:153
std::vector< Point > points
Definition: Primitives.h:146
void translate(const Point &t)
Definition: Primitives.h:235
void scale(double scaling)
Definition: Primitives.h:168
bool isConvex() const
Definition: Primitives.cpp:69
Point max() const
Maximum x/y coordinates of all points.
Definition: Primitives.h:277
Point min() const
Minimum x/y coordinates of all points.
Definition: Primitives.h:266
void rotate(double angle)
Rotates polygon counterclockwise about the origin.
Definition: Primitives.h:242
static Polygon loadFromSvgPathStr(std::string path_str)
Loads polygon from a path tag inside an SVG file.
Definition: Primitives.h:181
Definition: Primitives.h:19
double width() const
Definition: Primitives.h:30
void correct()
Ensures that x1, y1 is the bottom-left corner (minimum coordinates).
Definition: Primitives.h:36
double x() const
Definition: Primitives.h:27
double x1
Definition: Primitives.h:20
Rectangle(double x1, double y1, double x2, double y2)
Definition: Primitives.h:24
double y1
Definition: Primitives.h:20
double y() const
Definition: Primitives.h:28
double x2
Definition: Primitives.h:21
double y2
Definition: Primitives.h:21
double height() const
Definition: Primitives.h:31