Bench-MR
A Motion Planning Benchmark for Wheeled Mobile Robots
SvgPolygonLoader.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <fstream>
5#include <sstream>
6
7#include "base/Primitives.h"
8
15 public:
16 static std::vector<Polygon> load(const std::string &filename) {
17 std::ifstream input_file(filename);
18 std::string line;
19 std::vector<Polygon> polygons;
20 if (input_file.fail()) {
21 std::cerr << "Cannot load SVG file from " << filename
22 << ". Make sure the file exists." << std::endl;
23 assert(0);
24 return polygons;
25 }
26 double offset_x = 0, offset_y = 0;
27 while (getline(input_file, line)) {
28 trim(line);
29 if (line.substr(0, 21) == "transform=\"translate(") {
30 std::stringstream ss(line.substr(21, line.length() - 21));
31 ss >> offset_x;
32 char c;
33 ss >> c;
34 ss >> offset_y;
35 std::cout << "SVG group: using offset_x=" << offset_x
36 << " offset_y=" << offset_y << std::endl;
37 } else if (line.substr(0, 2) == "d=") {
38 Polygon poly =
39 Polygon::loadFromSvgPathStr(line.substr(3, line.length() - 3));
40 poly.translate(Point(offset_x, -offset_y)); // flip offset y
41 if (!poly.isConvex()) {
42 std::cerr << "Warning: found nonconvex polygon in " << filename
43 << ". Using its convex hull instead." << std::endl;
44 poly = poly.convexHull();
45 // std::cout << poly << std::endl;
46 std::cerr << std::boolalpha << "Hull is convex? " << poly.isConvex()
47 << std::endl;
48 assert(poly.isConvex());
49 }
50 polygons.emplace_back(poly);
51 }
52 }
53 return polygons;
54 }
55
56 private:
57 static inline void trim(std::string &s) {
58 s.erase(s.begin(), std::find_if(s.begin(), s.end(),
59 [](int ch) { return !std::isspace(ch); }));
60 s.erase(std::find_if(s.rbegin(), s.rend(),
61 [](int ch) { return !std::isspace(ch); })
62 .base(),
63 s.end());
64 }
65};
This class loads polygons from an SVG file.
Definition: SvgPolygonLoader.hpp:14
static std::vector< Polygon > load(const std::string &filename)
Definition: SvgPolygonLoader.hpp:16
bool line(double x0, double y0, double y1, double x1)
Definition: gnode_base.cpp:23
Definition: Primitives.h:42
Definition: Primitives.h:145
Polygon convexHull() const
Definition: Primitives.cpp:106
void translate(const Point &t)
Definition: Primitives.h:235
bool isConvex() const
Definition: Primitives.cpp:69
static Polygon loadFromSvgPathStr(std::string path_str)
Loads polygon from a path tag inside an SVG file.
Definition: Primitives.h:181