Coverage Control Library
Loading...
Searching...
No Matches
polygon_utils.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the CoverageControl library
3 *
4 * Author: Saurav Agarwal
5 * Contact: sauravag@seas.upenn.edu, agr.saurav1@gmail.com
6 * Repository: https://github.com/KumarRobotics/CoverageControl
7 *
8 * Copyright (c) 2024, Saurav Agarwal
9 *
10 * The CoverageControl library is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or (at your
13 * option) any later version.
14 *
15 * The CoverageControl library is distributed in the hope that it will be
16 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18 * Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * CoverageControl library. If not, see <https://www.gnu.org/licenses/>.
22 */
23
29#include <list>
30#include <vector>
31
36
37namespace CoverageControl {
39 std::vector<PointVector> &new_polys) {
40 // Transform general polygon to CGAL //
41 Partition_traits_2::Polygon_2 cgal_poly;
42 for (auto const &pt : poly) {
43 cgal_poly.push_back(Partition_traits_2::Point_2(pt.x(), pt.y()));
44 }
45
46 /* std::cout << "Is simple: " << cgal_poly.is_simple() << std::endl; */
47 /* std::cout << "Is orientation: " << cgal_poly.orientation() << std::endl; */
48 if (cgal_poly.orientation() == CGAL::CLOCKWISE) {
49 cgal_poly.reverse_orientation();
50 }
51 // Obtain partition //
52 std::list<Partition_traits_2::Polygon_2> partition_polys;
53 CGAL::y_monotone_partition_2(cgal_poly.begin(), cgal_poly.end(),
54 std::back_inserter(partition_polys));
55
56 // Transform to coveragecontrol data type
57
58 new_polys.reserve(new_polys.size() + partition_polys.size());
59 for (auto const &p : partition_polys) {
60 PointVector new_p;
61 new_p.reserve(p.size());
62 std::transform(p.vertices_begin(), p.vertices_end(),
63 std::back_inserter(new_p),
64 [](CGAL_Point2 const &pt) { return CGALtoCC(pt); });
65 new_polys.push_back(new_p);
66 }
67}
68
69void GenerateRandomPolygons(int const num_polygons, int const max_vertices,
70 double const half_width, double const world_size,
71 std::vector<PointVector> &polygons) {
72 polygons.clear();
73 polygons.reserve(num_polygons);
74 CGAL::Random rand;
75 for (int i = 0; i < num_polygons; ++i) {
76 int num_vertices = rand.get_int(4, max_vertices);
77 PointVector poly;
78 poly.reserve(num_vertices);
79 std::list<CGAL_Point2> points;
80 CGAL::copy_n_unique(Point_generator(half_width), num_vertices,
81 std::back_inserter(points));
82 Polygon_2 poly_cgal;
83 CGAL::random_polygon_2(num_vertices, std::back_inserter(poly_cgal),
84 points.begin());
85 auto bbox = poly_cgal.bbox();
86 CGAL_Point2 lower_left(bbox.xmin(), bbox.ymin());
87 CGAL_Point2 upper_right(bbox.xmax() - bbox.xmin(),
88 bbox.ymax() - bbox.ymin());
89 CGAL_Point2 polygon_base = CGAL_Point2(
90 rand.get_double(kLargeEps, world_size - kLargeEps -
91 CGAL::to_double(upper_right.x())),
92 rand.get_double(kLargeEps, world_size - kLargeEps -
93 CGAL::to_double(upper_right.y())));
94 for (auto const &pt : poly_cgal) {
95 Point2 new_pt = CGALtoCC(pt);
96 new_pt += CGALtoCC(polygon_base);
97 new_pt -= CGALtoCC(lower_left);
98 poly.push_back(new_pt);
99 }
100 polygons.push_back(poly);
101 }
102}
103
104} /* namespace CoverageControl */
Contains the configuration for the CGAL library.
Constants for the CoverageControl library.
double const kLargeEps
Definition constants.h:51
std::vector< Point2 > PointVector
Definition typedefs.h:51
Eigen::Vector2d Point2
Definition typedefs.h:44
Namespace for the CoverageControl library.
void GenerateRandomPolygons(int const num_polygons, int const max_vertices, double const half_width, double const world_size, std::vector< PointVector > &polygons)
Generate random polygons.
void PolygonYMonotonePartition(PointVector const &polygon, std::vector< PointVector > &y_monotone_polygons)
Partition a polygon into y-monotone polygons.
Provides utilities for polygon manipulation using CGAL.
Contains utility functions for CGAL.