Collision Checker
boost_object_polygon.cc
Go to the documentation of this file.
1 #include <algorithm>
2 
3 #include <boost/polygon/polygon.hpp>
5 
6 namespace collision {
7 namespace solvers {
8 namespace solverBoost {
13  bg_polygon_type hull;
14  boost::geometry::convex_hull(m_boost_polygon, hull);
15  return std::make_shared<const BoostPolygon>(hull);
16 }
17 
21 int BoostPolygon::buffer(std::vector<BoostPolygon> &output,
22  double radius) const {
23  typedef double coordinate_type;
24  const double buffer_distance = radius;
25  const int points_per_circle = 5;
26  boost::geometry::strategy::buffer::distance_symmetric<coordinate_type>
27  distance_strategy(buffer_distance);
28  boost::geometry::strategy::buffer::join_round join_strategy(
29  points_per_circle);
30  boost::geometry::strategy::buffer::end_round end_strategy(points_per_circle);
31  boost::geometry::strategy::buffer::point_circle circle_strategy(
32  points_per_circle);
33  boost::geometry::strategy::buffer::side_straight side_strategy;
34  boost::geometry::model::multi_polygon<bg_polygon_type> mpol;
35  mpol.emplace_back(m_boost_polygon);
36  boost::geometry::model::multi_polygon<bg_polygon_type> bufres;
37 
38  boost::geometry::buffer(mpol, bufres, distance_strategy, side_strategy,
39  join_strategy, end_strategy, circle_strategy);
40  for (auto el : bufres) {
41  output.push_back(BoostPolygon(el));
42  }
43 
44  return 0;
45 }
46 
47 int BoostPolygon::extend(std::vector<BoostPolygon> &output,
48  double radius) const {
49  typedef double coordinate_type;
50  const double buffer_distance = radius;
51  const int points_per_circle = 5;
52  boost::geometry::strategy::buffer::distance_symmetric<coordinate_type>
53  distance_strategy(buffer_distance);
54  boost::geometry::strategy::buffer::join_miter join_strategy;
55  boost::geometry::strategy::buffer::end_flat end_strategy;
56  boost::geometry::strategy::buffer::point_circle circle_strategy(
57  points_per_circle);
58  boost::geometry::strategy::buffer::side_straight side_strategy;
59  boost::geometry::model::multi_polygon<bg_polygon_type> mpol;
60  mpol.emplace_back(m_boost_polygon);
61  boost::geometry::model::multi_polygon<bg_polygon_type> bufres;
62 
63  boost::geometry::buffer(mpol, bufres, distance_strategy, side_strategy,
64  join_strategy, end_strategy, circle_strategy);
65  for (auto el : bufres) {
66  output.push_back(BoostPolygon(el));
67  }
68 
69  return 0;
70 }
71 
76  triangulation::TriangulationQuality qual) const {
77 #if ENABLE_POLYGON_VALIDITY_CHECKS
78  boost::geometry::validity_failure_type failure;
79  bool valid = boost::geometry::is_valid(m_boost_polygon, failure);
80  if (!valid) {
81  std::cout << "COLLISION_BOOST_POLYGON: the boost polygon is invalid"
82  << std::endl;
83  }
84 #endif
85  // must be a closed polygon
86  std::vector<Eigen::Vector2d> vertices;
87  for (auto it = boost::begin(boost::geometry::exterior_ring(m_boost_polygon));
88  (it != boost::end(boost::geometry::exterior_ring(m_boost_polygon))) &&
89  (it + 1 != boost::end(boost::geometry::exterior_ring(m_boost_polygon)));
90  ++it) {
91  double x = boost::geometry::get<0>(*it);
92  double y = boost::geometry::get<1>(*it);
93  vertices.emplace_back(x, y);
94  }
95 
96  std::reverse(std::begin(vertices),
97  std::end(vertices)); // produce a CCW polygon, not closed
98 
99  int num_holes = boost::geometry::interior_rings(m_boost_polygon).size();
100 
101  std::vector<std::vector<Eigen::Vector2d>> hole_vertices;
102 
103  for (int hole_ind = 0; hole_ind < num_holes; hole_ind++) {
104  auto cur_ring = boost::geometry::interior_rings(m_boost_polygon)[hole_ind];
105  std::vector<Eigen::Vector2d> vertices_hole;
106  for (auto it = boost::begin(cur_ring);
107  (it != boost::end(cur_ring)) && (it + 1 != boost::end(cur_ring));
108  ++it) {
109  double x = boost::geometry::get<0>(*it);
110  double y = boost::geometry::get<1>(*it);
111  vertices_hole.emplace_back(x, y);
112  }
113  hole_vertices.push_back(vertices_hole);
114  }
115 
117  std::make_shared<const collision::Polygon>(vertices, hole_vertices, 0, qual);
118 
119  return ret;
120 }
121 
122 } // namespace solverBoost
123 } // namespace solvers
124 } // namespace collision
BoostPolygon(void)
Constructs a BoostPolygon from Polygon.
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.
int extend(std::vector< BoostPolygon > &output, double radius) const
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
int buffer(std::vector< BoostPolygon > &output, double radius) const
Buffers (enlargens) the polygon with a given radius.