se2ez
polygon.cpp
Go to the documentation of this file.
1 // #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
2 #include <CGAL/Simple_cartesian.h>
3 #include <CGAL/Partition_traits_2.h>
4 #include <CGAL/Partition_is_valid_traits_2.h>
5 #include <CGAL/polygon_function_objects.h>
6 #include <CGAL/partition_2.h>
7 
8 #include <se2ez/core/geometry.h>
10 
11 using namespace se2ez;
12 
13 namespace cgal
14 {
15  // using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
16  using Kernel = CGAL::Simple_cartesian<double>;
17  using PartitionTraits = CGAL::Partition_traits_2<Kernel>;
18  using ValidityTraits =
19  CGAL::Partition_is_valid_traits_2<PartitionTraits, CGAL::Is_convex_2<PartitionTraits>>;
20 
21  using Point = PartitionTraits::Point_2;
22  using Polygon = PartitionTraits::Polygon_2;
23 } // namespace cgal
24 
25 namespace
26 {
27  void toCGAL(cgal::Polygon &polygon, const Geometry &geometry)
28  {
29  polygon.clear();
30 
31  if (!(geometry.type == Geometry::CONVEX || geometry.type == Geometry::SIMPLE))
32  throw std::runtime_error("Geometry must be a polygon!");
33 
34  for (const auto &point : geometry.points)
35  polygon.push_back(cgal::Point(point[0], point[1]));
36  }
37 
38  void fillPoints(tf::EigenVector<Eigen::Vector2d> &points, const cgal::Polygon &polygon)
39  {
40  for (auto it = polygon.vertices_begin(); it != polygon.vertices_end(); ++it)
41  points.emplace_back(it->x(), it->y());
42  }
43 
44  GeometryPtr toSE2EZConvex(const cgal::Polygon &polygon)
45  {
47  fillPoints(points, polygon);
48 
49  return std::make_shared<Geometry>(Geometry::CONVEX, points, Eigen::Vector3d{0, 0, 0});
50  }
51 
52  bool collinear(const tf::EigenVector<Eigen::Vector2d> &points)
53  {
54  bool collinear = false;
55  for (unsigned int i = 0; i < points.size() - 2 && !collinear; ++i)
56  {
57  cgal::Point p(points[i][0], points[i][1]);
58  cgal::Point q(points[i + 1][0], points[i + 1][1]);
59  cgal::Point r(points[i + 2][0], points[i + 2][1]);
60 
61  collinear |= CGAL::collinear(p, q, r);
62  }
63 
64  return collinear;
65  }
66 } // namespace
67 
68 bool geo::isConvex(const Geometry &geometry)
69 {
70  cgal::Polygon poly;
71  toCGAL(poly, geometry);
72  return poly.is_convex();
73 }
74 
75 bool geo::isSimple(const Geometry &geometry)
76 {
77  cgal::Polygon poly;
78  toCGAL(poly, geometry);
79  return poly.is_simple();
80 }
81 
83 {
84  cgal::Polygon poly;
85  toCGAL(poly, geometry);
86 
87  if (poly.is_counterclockwise_oriented())
88  poly.reverse_orientation();
89 
90  geometry.points.clear();
91  fillPoints(geometry.points, poly);
92 }
93 
95 {
96  cgal::Polygon poly;
97  toCGAL(poly, geometry);
98 
99  if (poly.is_clockwise_oriented())
100  poly.reverse_orientation();
101 
102  geometry.points.clear();
103  fillPoints(geometry.points, poly);
104 }
105 
106 double geo::area(const Geometry &geometry)
107 {
108  cgal::Polygon poly;
109  toCGAL(poly, geometry);
110  return poly.area();
111 }
112 
114 {
115  cgal::Polygon poly;
116  std::vector<cgal::Polygon> partitions;
117 
120 
121  toCGAL(poly, *geometry);
122 
123  CGAL::approx_convex_partition_2(poly.vertices_begin(), poly.vertices_end(),
124  std::back_inserter(partitions), pt);
125 
126  bool valid = CGAL::partition_is_valid_2(poly.vertices_begin(), poly.vertices_end(), //
127  partitions.begin(), partitions.end(), vt);
128 
129  if (!valid)
130  throw std::runtime_error("Invalid convex partition found!");
131 
132  std::vector<GeometryPtr> convert;
133  for (const auto &p : partitions)
134  convert.emplace_back(toSE2EZConvex(p));
135 
136  return convert;
137 }
138 
139 // TODO: Template out
140 
142 {
143  cgal::Polygon poly;
144  std::vector<cgal::Polygon> partitions;
145 
148 
149  toCGAL(poly, *geometry);
150 
151  CGAL::optimal_convex_partition_2(poly.vertices_begin(), poly.vertices_end(),
152  std::back_inserter(partitions), pt);
153 
154  bool valid = CGAL::partition_is_valid_2(poly.vertices_begin(), poly.vertices_end(), //
155  partitions.begin(), partitions.end(), vt);
156 
157  if (!valid)
158  throw std::runtime_error("Invalid convex partition found!");
159 
160  std::vector<GeometryPtr> convert;
161  for (const auto &p : partitions)
162  convert.emplace_back(toSE2EZConvex(p));
163 
164  return convert;
165 }
166 
168 {
169  if (geometry->type != Geometry::CONVEX)
170  throw std::runtime_error("Geometry must be convex!");
171 
172  std::vector<GeometryPtr> triangles;
174  points[0] = geometry->points[0];
175  for (unsigned int i = 1; i < geometry->points.size() - 1; ++i)
176  {
177  points[1] = geometry->points[i];
178  points[2] = geometry->points[i + 1];
179 
180  if (collinear(points))
181  continue;
182 
183  triangles.emplace_back(
184  std::make_shared<Geometry>(Geometry::CONVEX, points, geometry->offset, geometry->color));
185  }
186 
187  return triangles;
188 }
void makeClockwise(Geometry &geometry)
If not clockwise, reorders polygon vertices to be in clockwise orientation.
Definition: polygon.cpp:82
double area(const Geometry &geometry)
Compute the area of a polygon.
Definition: polygon.cpp:106
const Type type
Type of geometry.
Definition: geometry.h:117
bool isSimple(const Geometry &geometry)
Checks if a polygon is simple, that is, it has no holes.
Definition: polygon.cpp:75
A convex polygon.
Definition: geometry.h:35
std::vector< GeometryPtr > shatter(const GeometryPtr &geometry)
"Shatters" a convex polygon into a fan of triangles using the first point as the root for all triangl...
Definition: polygon.cpp:167
CGAL::Partition_is_valid_traits_2< PartitionTraits, CGAL::Is_convex_2< PartitionTraits > > ValidityTraits
Definition: polygon.cpp:19
T end(T... args)
tf::EigenVector< Eigen::Vector2d > points
Point list for polygons.
Definition: geometry.h:119
A shared pointer wrapper for se2ez::Geometry.
std::vector< GeometryPtr > convexifyApproximate(const GeometryPtr &geometry)
Performs an approximate convex decomposition on the geometry.
Definition: polygon.cpp:113
PartitionTraits::Point_2 Point
Definition: polygon.cpp:21
A simple polygon (no holes).
Definition: geometry.h:36
void makeCounterClockwise(Geometry &geometry)
If not counterclockwise, reorders polygon vertices to be in counterclockwise orientation.
Definition: polygon.cpp:94
Definition: polygon.cpp:13
T size(T... args)
CGAL::Simple_cartesian< double > Kernel
Definition: polygon.cpp:16
std::vector< GeometryPtr > convexifyOptimal(const GeometryPtr &geometry)
Performs an optimal convex decomposition on the geometry.
Definition: polygon.cpp:141
T begin(T... args)
T back_inserter(T... args)
bool isConvex(const Geometry &geometry)
Checks if a polygon is convex.
Definition: polygon.cpp:68
Main namespace.
Definition: collision.h:11
PartitionTraits::Polygon_2 Polygon
Definition: polygon.cpp:22
CGAL::Partition_traits_2< Kernel > PartitionTraits
Definition: polygon.cpp:17
T emplace_back(T... args)