15 #ifndef NAV2_COLLISION_MONITOR__POLYGON_UTILS_HPP_
16 #define NAV2_COLLISION_MONITOR__POLYGON_UTILS_HPP_
23 #include "tf2/LinearMath/Transform.hpp"
24 #include "tf2/LinearMath/Vector3.hpp"
26 #include "nav2_util/array_parser.hpp"
28 #include "nav2_collision_monitor/types.hpp"
30 namespace nav2_collision_monitor
49 inline bool parsePolygonPoints(
50 const std::string & points_str,
51 std::size_t min_vertices,
52 std::vector<Point> & out,
53 std::string & error_msg)
55 std::string parse_error;
56 const std::vector<std::vector<float>> vvf = nav2_util::parseVVF(points_str, parse_error);
57 if (!parse_error.empty()) {
58 error_msg =
"error parsing points '" + points_str +
"': " + parse_error;
61 if (vvf.size() < min_vertices) {
62 error_msg =
"polygon must have at least " + std::to_string(min_vertices) +
" vertices";
66 std::vector<Point> parsed;
67 parsed.reserve(vvf.size());
68 for (
const std::vector<float> & v : vvf) {
70 error_msg =
"each point must be a pair of numbers [x, y]";
76 parsed.push_back(point);
79 out = std::move(parsed);
89 inline void transformPolygonPoints(
90 const tf2::Transform & tf,
91 const std::vector<Point> & in,
92 std::vector<Point> & out)
94 out.resize(in.size());
95 for (std::size_t i = 0; i < in.size(); ++i) {
96 const tf2::Vector3 p_b = tf * tf2::Vector3(in[i].x, in[i].y, 0.0);
108 inline std::vector<Point> circleToPolygon(
double radius,
int edges = 16)
110 std::vector<Point> poly;
111 poly.reserve(
static_cast<std::size_t
>(edges));
112 const double angle_increment = 2.0 * M_PI / edges;
113 for (
int i = 0; i < edges; ++i) {
114 const double angle = angle_increment * i;
116 p.x = radius * std::cos(angle);
117 p.y = radius * std::sin(angle);