Skip to content

Instantly share code, notes, and snippets.

@afabri
Created December 8, 2022 16:25
Show Gist options
  • Save afabri/d4892ac8e8f6adf87d906a9badf6a155 to your computer and use it in GitHub Desktop.
Save afabri/d4892ac8e8f6adf87d906a9badf6a155 to your computer and use it in GitHub Desktop.
Joining several polygons
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <CGAL/Boolean_set_operations_2.h>
#include <CGAL/IO/WKT.h>
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef Kernel::Point_2 Point_2;
typedef CGAL::Polygon_2<Kernel> Polygon_2;
typedef CGAL::Polygon_with_holes_2<Kernel> Polygon_with_holes_2;
int main()
{
// Read the polygons from WTK file
std::ifstream is("polys.txt");
std::list<Polygon_2> polys;
int ccw = 0, cw = 0;
;
do {
Polygon_2 p;
CGAL::IO::read_polygon_WKT(is, p);
std::cout << p << std::endl << std::endl;
if(!p.is_empty()){
assert(p.is_simple());
if(p.is_counterclockwise_oriented()){
ccw++;
}
if(p.is_clockwise_oriented()){
cw++;
p.reverse_orientation();
}
polys.push_back(p);
}
} while(is.good() && !is.eof());
std::cout << ccw << " " << cw << std::endl;
// Join the polygons
std::deque<Polygon_with_holes_2> result;
CGAL::join(polys.begin(), polys.end(), std::back_inserter(result));
for(const Polygon_with_holes_2& p : result)
{
if(p.outer_boundary().is_empty()){
std::cout << "outer boundary is empty" << std::endl;
continue;
}
assert(p.outer_boundary().is_counterclockwise_oriented());
for(Polygon_with_holes_2::Hole_const_iterator h_it =
p.holes_begin(); h_it != p.holes_end(); ++h_it)
{
std::cout << "a hole" << std::endl;
assert(h_it->is_clockwise_oriented());
}
}
std::ofstream out("out.wkt");
out.precision(17);
CGAL::IO::write_multi_polygon_WKT(out, result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment