From 78c8b4480e31e4f47e96ca35d0358133db4642d0 Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Wed, 6 Nov 2024 13:05:46 +0000 Subject: [PATCH] Add free functions join() and intersection() for any combination of P, MP, MPwH --- Polygon/include/CGAL/Polygon_with_holes_2.h | 4 +- .../repair_union_intersect_2.cpp | 12 ++- .../include/CGAL/Polygon_repair/Boolean.h | 75 ++++++++++++++++--- .../include/CGAL/Polygon_repair/repair.h | 12 ++- 4 files changed, 87 insertions(+), 16 deletions(-) diff --git a/Polygon/include/CGAL/Polygon_with_holes_2.h b/Polygon/include/CGAL/Polygon_with_holes_2.h index 032b2c52084d..e7178d650cae 100644 --- a/Polygon/include/CGAL/Polygon_with_holes_2.h +++ b/Polygon/include/CGAL/Polygon_with_holes_2.h @@ -30,7 +30,7 @@ namespace CGAL { The class `Polygon_with_holes_2` models the concept `GeneralPolygonWithHoles_2`. It represents a linear polygon with holes. It is parameterized with two types (`Kernel` and `Container`) that are used to instantiate -the type `Polygon_2`. This poygon type is used to +the type `Polygon_2`. This polygon type is used to represent the outer boundary and the boundary of the holes (if any exist). \cgalModels{GeneralPolygonWithHoles_2} @@ -42,7 +42,7 @@ class Polygon_with_holes_2 : public General_polygon_with_holes_2 > { public: - + typedef Kernel Traits; typedef CGAL::Polygon_2 Polygon_2; typedef General_polygon_with_holes_2 Base; typedef typename Base::Hole_const_iterator Hole_const_iterator; diff --git a/Polygon_repair/examples/Polygon_repair/repair_union_intersect_2.cpp b/Polygon_repair/examples/Polygon_repair/repair_union_intersect_2.cpp index b8e5a9c752b8..f4d13ddaafb7 100644 --- a/Polygon_repair/examples/Polygon_repair/repair_union_intersect_2.cpp +++ b/Polygon_repair/examples/Polygon_repair/repair_union_intersect_2.cpp @@ -14,6 +14,8 @@ using K = CGAL::Exact_predicates_inexact_constructions_kernel; +using Point_2 = K::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; @@ -45,6 +47,14 @@ main(int argc, char* argv[]) CGAL::draw(mpwh); } - + { + Polygon_2 pB; + pB.push_back(Point_2(-1,-1)); + pB.push_back(Point_2(1,-1)); + pB.push_back(Point_2(1,1)); + pB.push_back(Point_2(-1,1)); + mpwh = CGAL::Polygon_repair::join(pA,pB); + CGAL::draw(mpwh); + } return 0; } diff --git a/Polygon_repair/include/CGAL/Polygon_repair/Boolean.h b/Polygon_repair/include/CGAL/Polygon_repair/Boolean.h index 35abad35d3ab..b794ecd9489d 100644 --- a/Polygon_repair/include/CGAL/Polygon_repair/Boolean.h +++ b/Polygon_repair/include/CGAL/Polygon_repair/Boolean.h @@ -135,6 +135,22 @@ default constructor. /*! sets the polygons as input of the %Boolean operation. */ + void + insert(const Polygon_2& pA) + { + cdt.insert_constraint(pA.vertices_begin(), pA.vertices_end(), true); + } + + + void + insert(const Polygon_with_holes_2& pwh) + { + cdt.insert_constraint(pwh.outer_boundary().vertices_begin(), pwh.outer_boundary().vertices_end(), true); + for(auto const& hole : pwh.holes()){ + cdt.insert_constraint(hole.vertices_begin(), hole.vertices_end(), true); + } + } + void insert(const Multipolygon_with_holes_2& pA) { @@ -144,10 +160,7 @@ sets the polygons as input of the %Boolean operation. cdt.insert_constraint(hole.vertices_begin(), hole.vertices_end(), true); } } - - mark_domains(); } - private: void @@ -181,7 +194,7 @@ sets the polygons as input of the %Boolean operation. } } - + public: // this marks how many multipolygon interiors overlap a cell of the arrangement of mutipolygons void mark_domains() @@ -217,7 +230,7 @@ sets the polygons as input of the %Boolean operation. } } - +private: template void label_domain(Face_handle start, int label, const Fct& fct) @@ -378,26 +391,45 @@ template Multipolygon_with_holes_2 join(const Multipolygon_with_holes_2& pA) { - CGAL::Polygon_repair::Boolean bops; - bops.insert(pA); struct Larger_than_zero { bool operator()(int i) const { return i > 0; } }; + + CGAL::Polygon_repair::Boolean bops; + bops.insert(pA); + bops.mark_domains(); Larger_than_zero ltz; return bops(ltz); } +template +decltype(auto) // Multipolygon_with_holes_2 +join(const PA& pA, const PB& pB, const K& k = Default()) +{ + typedef typename Default::Get::type Traits; + struct Larger_than_zero { + bool operator()(int i) const + { + return i > 0; + } + }; + + CGAL::Polygon_repair::Boolean bops; + bops.insert(pA); + bops.insert(pB); + bops.mark_domains(); + Larger_than_zero ltz; + return bops(ltz); +} template Multipolygon_with_holes_2 intersection(const Multipolygon_with_holes_2& pA) { - CGAL::Polygon_repair::Boolean bops; - bops.insert(pA); struct Equal { int val; Equal(int val) @@ -409,10 +441,35 @@ intersection(const Multipolygon_with_holes_2& pA) return i == val; } }; + + CGAL::Polygon_repair::Boolean bops; + bops.insert(pA); + bops.mark_domains(); Equal equal(pA.number_of_polygons_with_holes()); return bops(equal); } +template +decltype(auto) // Multipolygon_with_holes_2 +intersection(const PA& pA, const PB& pB, const K& k = Default()) +{ + typedef typename Default::Get::type Traits; + + struct Equal { + bool operator()(int i) const + { + return i == 2; + } + }; + + CGAL::Polygon_repair::Boolean bops; + bops.insert(pA); + bops.insert(pB); + bops.mark_domains(); + Equal equal; + return bops(equal); +} + } // namespace Polygon_repair } //namespace CGAL diff --git a/Polygon_repair/include/CGAL/Polygon_repair/repair.h b/Polygon_repair/include/CGAL/Polygon_repair/repair.h index 33f88d069380..51c81c3229f3 100644 --- a/Polygon_repair/include/CGAL/Polygon_repair/repair.h +++ b/Polygon_repair/include/CGAL/Polygon_repair/repair.h @@ -109,14 +109,16 @@ Multipolygon_with_holes_2 repair(const Multipolygon_with_hole template Multipolygon_with_holes_2 repair(const Multipolygon_with_holes_2& p, Union_rule) { - CGAL::Polygon_repair::Boolean bops; - bops.insert(p); struct Larger_than_zero { bool operator()(int i) const { return i > 0; } }; + + CGAL::Polygon_repair::Boolean bops; + bops.insert(p); + bops.mark_domains(); Larger_than_zero ltz; return bops(ltz); } @@ -125,8 +127,6 @@ Multipolygon_with_holes_2 repair(const Multipolygon_with_hole template Multipolygon_with_holes_2 repair(const Multipolygon_with_holes_2& p, Intersection_rule) { - CGAL::Polygon_repair::Boolean bops; - bops.insert(p); struct Equal { int val; Equal(int val) @@ -138,6 +138,10 @@ Multipolygon_with_holes_2 repair(const Multipolygon_with_hole return i == val; } }; + + CGAL::Polygon_repair::Boolean bops; + bops.insert(p); + bops.mark_domains(); Equal equal(p.number_of_polygons_with_holes()); return bops(equal); }