Collision Checker
boost_object_polygon.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include <memory>
9 
10 #include <boost/foreach.hpp>
11 #include <boost/geometry.hpp>
12 #include <boost/geometry/geometries/multi_polygon.hpp>
13 #include <boost/geometry/geometries/point_xy.hpp>
14 #include <boost/geometry/geometries/polygon.hpp>
15 
16 #include <boost/geometry/algorithms/convex_hull.hpp>
17 
18 namespace collision {
19 namespace solvers {
20 namespace solverBoost {
21 
22 typedef boost::geometry::model::d2::point_xy<double> bg_point_type;
23 typedef boost::geometry::model::polygon<bg_point_type> bg_polygon_type;
24 
26 
27 typedef std::shared_ptr<BoostPolygon> BoostPolygonPtr;
28 typedef std::shared_ptr<const BoostPolygon> BoostPolygonConstPtr;
29 
40  public:
50  BoostPolygon(void) {}
51  BoostPolygon(const BoostPolygon &other) {
52  this->m_boost_polygon = other.m_boost_polygon;
53 #if ENABLE_POLYGON_VALIDITY_CHECKS
54  boost::geometry::validity_failure_type failure;
55  bool valid = boost::geometry::is_valid(m_boost_polygon, failure);
56  if (!valid) {
57  std::cout << "COLLISION_BOOST_POLYGON: the boost polygon is invalid"
58  << std::endl;
59  }
60 #endif
61  }
62 
64  auto vertices = obj->getVertices();
65  for (auto vert = vertices.rbegin(); vert < vertices.rend(); vert++) {
66  boost::geometry::append(m_boost_polygon,
67  bg_point_type((*vert)[0], (*vert)[1]));
68  }
69  auto vert = *(obj->getVertices().rbegin());
70  boost::geometry::append(m_boost_polygon, bg_point_type(vert[0], vert[1]));
71 
72  int num_holes = 0;
73  if ((num_holes = obj->getHoleVertices().size()) > 0) {
74  boost::geometry::interior_rings(m_boost_polygon).resize(num_holes);
75  int hole_ind = 0;
76  for (const auto &hole_contour : obj->getHoleVertices()) {
77  for (const auto &vert : hole_contour) {
78  boost::geometry::append(m_boost_polygon,
79  bg_point_type(vert[0], vert[1]), hole_ind);
80  }
81  auto vert = hole_contour.front();
82  boost::geometry::append(m_boost_polygon,
83  bg_point_type(vert[0], vert[1]), hole_ind);
84  hole_ind++;
85  }
86  }
87 
88 #if ENABLE_POLYGON_VALIDITY_CHECKS
89  boost::geometry::validity_failure_type failure;
90  bool valid = boost::geometry::is_valid(m_boost_polygon, failure);
91  if (!valid) {
92  std::cout << "COLLISION_BOOST_POLYGON: the boost polygon is invalid"
93  << std::endl;
94  }
95 #endif
96  }
101  for (int i = 0; i < 4; i++) {
102  double x = obj->segments()[i].point1().x;
103  double y = obj->segments()[i].point1().y;
104  boost::geometry::append(m_boost_polygon, bg_point_type(x, y));
105  }
106  double x = obj->segments()[0].point1().x;
107  double y = obj->segments()[0].point1().y;
108  boost::geometry::append(m_boost_polygon, bg_point_type(x, y));
109 #if ENABLE_POLYGON_VALIDITY_CHECKS
110  boost::geometry::validity_failure_type failure;
111  bool valid = boost::geometry::is_valid(m_boost_polygon, failure);
112  if (!valid) {
113  std::cout << "COLLISION_BOOST_POLYGON: the boost polygon is invalid"
114  << std::endl;
115  }
116 #endif
117  }
118 
123  for (int i = 0; i < 4; i++) {
124  double x = obj->segments()[i].point1().x;
125  double y = obj->segments()[i].point1().y;
126  boost::geometry::append(m_boost_polygon, bg_point_type(x, y));
127  }
128  double x = obj->segments()[0].point1().x;
129  double y = obj->segments()[0].point1().y;
130  boost::geometry::append(m_boost_polygon, bg_point_type(x, y));
131 
132 #if ENABLE_POLYGON_VALIDITY_CHECKS
133  bool valid = boost::geometry::is_valid(m_boost_polygon);
134 #endif
135  }
136 
140  BoostPolygon(const bg_polygon_type polygon) {
141 #if ENABLE_POLYGON_VALIDITY_CHECKS
142  boost::geometry::validity_failure_type failure;
143  bool valid = boost::geometry::is_valid(polygon, failure);
144  if (!valid) {
145  std::cout
146  << "COLLISION_BOOST_POLYGON: the constructed boost polygon is invalid"
147  << std::endl;
148  }
149 #endif
150 
151  m_boost_polygon = polygon;
152  }
153 
154  virtual ~BoostPolygon(void) {}
155 
156  int extend(std::vector<BoostPolygon> &output, double radius) const;
157 
158  int buffer(std::vector<BoostPolygon> &output, double radius) const;
159 
160  BoostPolygonConstPtr convexHull(void) const;
161 
163  triangulation::TriangulationQuality qual =
164  triangulation::TriangulationQuality()) const;
165 
166  int getTrapezoids(std::vector<BoostPolygon> &output, int axis = 0) const;
167 
172  bool within(const BoostPolygon &other) const {
173  return boost::geometry::within(m_boost_polygon, other.m_boost_polygon);
174  }
178  bg_polygon_type *getInternal(void) const { return &m_boost_polygon; }
179 
180  private:
181  mutable bg_polygon_type m_boost_polygon;
182 };
183 
184 } // namespace solverBoost
185 } // namespace solvers
186 } // namespace collision
Oriented rectangle.
Definition: rectangle_obb.h:19
BoostPolygon(void)
Constructs a BoostPolygon from Polygon.
std::vector< Eigen::Vector2d > getVertices() const
Definition: polygon.cc:153
collision::PolygonConstPtr toPolygon(triangulation::TriangulationQuality qual=triangulation::TriangulationQuality()) const
Converts the BoostPolygon into a collision Polygon.
BoostPolygonConstPtr convexHull(void) const
Computes a convex hull of the given BoostPolygon.
std::shared_ptr< BoostPolygon > BoostPolygonPtr
BoostPolygon(const collision::RectangleAABB *obj)
Constructs a BoostPolygon from RectangleAABB.
int extend(std::vector< BoostPolygon > &output, double radius) const
int getTrapezoids(std::vector< BoostPolygon > &output, int axis=0) const
boost::geometry::model::d2::point_xy< double > bg_point_type
boost::geometry::model::polygon< bg_point_type > bg_polygon_type
std::shared_ptr< const BoostPolygon > BoostPolygonConstPtr
std::shared_ptr< const Polygon > PolygonConstPtr
Definition: polygon.h:23
std::vector< std::vector< Eigen::Vector2d > > getHoleVertices() const
Definition: polygon.cc:155
bool within(const BoostPolygon &other) const
Returns true if the BoostPolygon is completely contained within another BoostPolygon object...
BoostPolygon(const collision::RectangleOBB *obj)
Constructs a BoostPolygon from RectangleOBB.
int buffer(std::vector< BoostPolygon > &output, double radius) const
Buffers (enlargens) the polygon with a given radius.
bg_polygon_type * getInternal(void) const
Returns the contained boost.Geometry polygon object.
Axis-aligned rectangle.
BoostPolygon(const bg_polygon_type polygon)
Constructs a BoostPolygon from Boost.Geometry polygon.
std::vector< LineSegment > segments(void) const
std::vector< LineSegment > segments(void) const
Polygon contains Triangles and Vertices.
Definition: polygon.h:29
BoostPolygon allows to use boost.Geometry functions for Polygon objects.