From 2af58ecea3ade9f400fad4be7b3934557fa22672 Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Mon, 15 Apr 2024 18:45:05 +0100 Subject: [PATCH] Make read_WKT() work with Multipolygon_with_holes --- .../include/CGAL/Multipolygon_with_holes_2.h | 13 +++++++- .../Polygon_repair/polygons2multipolygon.cpp | 32 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 Polygon_repair/examples/Polygon_repair/polygons2multipolygon.cpp diff --git a/Polygon/include/CGAL/Multipolygon_with_holes_2.h b/Polygon/include/CGAL/Multipolygon_with_holes_2.h index 7ed5313e3efc..2449b9dd8eef 100644 --- a/Polygon/include/CGAL/Multipolygon_with_holes_2.h +++ b/Polygon/include/CGAL/Multipolygon_with_holes_2.h @@ -43,7 +43,7 @@ class Multipolygon_with_holes_2 { /// polygon with holes type using Polygon_with_holes_2 = CGAL::Polygon_with_holes_2; - + using value_type = Polygon_with_holes_2; /// @} using Polygon_with_holes_container = std::deque; @@ -72,10 +72,19 @@ class Multipolygon_with_holes_2 { Polygon_with_holes_iterator polygons_with_holes_end() { return m_polygons.end(); } + Polygon_with_holes_iterator begin() { return m_polygons.begin(); } + + Polygon_with_holes_iterator end() { return m_polygons.end(); } + Polygon_with_holes_const_iterator polygons_with_holes_begin() const { return m_polygons.begin(); } Polygon_with_holes_const_iterator polygons_with_holes_end() const { return m_polygons.end(); } + Polygon_with_holes_const_iterator begin() const { return m_polygons.begin(); } + + Polygon_with_holes_const_iterator end() const { return m_polygons.end(); } + + void add_polygon(const Polygon_2& pgn) { m_polygons.push_back(Polygon_with_holes_2(pgn)); } void add_polygon(Polygon_2&& pgn) { m_polygons.push_back(Polygon_with_holes_2(std::forward(pgn))); } @@ -84,6 +93,8 @@ class Multipolygon_with_holes_2 { void add_polygon_with_holes(Polygon_with_holes_2&& pgn) { m_polygons.emplace_back(std::move(pgn)); } + void push_back(const Polygon_with_holes_2& pgn) { m_polygons.push_back(pgn); } + void erase_polygon_with_holes(Polygon_with_holes_iterator pit) { m_polygons.erase(pit); } void clear() { m_polygons.clear(); } diff --git a/Polygon_repair/examples/Polygon_repair/polygons2multipolygon.cpp b/Polygon_repair/examples/Polygon_repair/polygons2multipolygon.cpp new file mode 100644 index 000000000000..6e2289728218 --- /dev/null +++ b/Polygon_repair/examples/Polygon_repair/polygons2multipolygon.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +#include +#include +#include + +using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel; +using Point_2 = Kernel::Point_2; +using Polygon_2 = CGAL::Polygon_2; +using Polygon_with_holes_2 = CGAL::Polygon_with_holes_2; +using Multipolygon_with_holes_2 = CGAL::Multipolygon_with_holes_2; + +int main(int argc, char* argv[]) +{ + std::ifstream in((argc > 1) ? argv[1] : CGAL::data_file_path("wkt/norway-polygons.wkt")); + + typedef std::vector MultiPoint; + typedef std::vector LineString; + typedef std::deque MultiLineString; + + MultiPoint points; + MultiLineString polylines; + Multipolygon_with_holes_2 polygons; + CGAL::IO::read_WKT(in, points,polylines,polygons); + + Multipolygon_with_holes_2 mp = CGAL::Polygon_repair::repair(polygons); + CGAL::IO::write_multi_polygon_WKT(std::cout, mp); + + return 0; +}