Skip to content

Commit

Permalink
Add free functions join() and intersection() for any combination of P…
Browse files Browse the repository at this point in the history
…, MP, MPwH
  • Loading branch information
afabri committed Nov 6, 2024
1 parent b24f19c commit 78c8b44
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Polygon/include/CGAL/Polygon_with_holes_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Kernel,Container>`. This poygon type is used to
the type `Polygon_2<Kernel,Container>`. This polygon type is used to
represent the outer boundary and the boundary of the holes (if any exist).
\cgalModels{GeneralPolygonWithHoles_2}
Expand All @@ -42,7 +42,7 @@ class Polygon_with_holes_2 :
public General_polygon_with_holes_2<CGAL::Polygon_2<Kernel, Containter> >
{
public:

typedef Kernel Traits;
typedef CGAL::Polygon_2<Kernel, Containter> Polygon_2;
typedef General_polygon_with_holes_2<Polygon_2> Base;
typedef typename Base::Hole_const_iterator Hole_const_iterator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

using K = CGAL::Exact_predicates_inexact_constructions_kernel;

using Point_2 = K::Point_2;
using Polygon_2 = CGAL::Polygon_2<K>;
using Polygon_with_holes_2 = CGAL::Polygon_with_holes_2<K>;
using Multipolygon_with_holes_2 = CGAL::Multipolygon_with_holes_2<K>;

Expand Down Expand Up @@ -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;
}
75 changes: 66 additions & 9 deletions Polygon_repair/include/CGAL/Polygon_repair/Boolean.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -217,7 +230,7 @@ sets the polygons as input of the %Boolean operation.
}
}


private:
template <typename Fct>
void
label_domain(Face_handle start, int label, const Fct& fct)
Expand Down Expand Up @@ -378,26 +391,45 @@ template <typename K>
Multipolygon_with_holes_2<K>
join(const Multipolygon_with_holes_2<K>& pA)
{
CGAL::Polygon_repair::Boolean<K> bops;
bops.insert(pA);
struct Larger_than_zero {
bool operator()(int i) const
{
return i > 0;
}
};

CGAL::Polygon_repair::Boolean<K> bops;
bops.insert(pA);
bops.mark_domains();
Larger_than_zero ltz;
return bops(ltz);
}

template <typename PA, typename PB, typename K = Default>
decltype(auto) // Multipolygon_with_holes_2<K>
join(const PA& pA, const PB& pB, const K& k = Default())
{
typedef typename Default::Get<K, typename PA::Traits>::type Traits;

struct Larger_than_zero {
bool operator()(int i) const
{
return i > 0;
}
};

CGAL::Polygon_repair::Boolean<Traits> bops;
bops.insert(pA);
bops.insert(pB);
bops.mark_domains();
Larger_than_zero ltz;
return bops(ltz);
}

template <typename K>
Multipolygon_with_holes_2<K>
intersection(const Multipolygon_with_holes_2<K>& pA)
{
CGAL::Polygon_repair::Boolean<K> bops;
bops.insert(pA);
struct Equal {
int val;
Equal(int val)
Expand All @@ -409,10 +441,35 @@ intersection(const Multipolygon_with_holes_2<K>& pA)
return i == val;
}
};

CGAL::Polygon_repair::Boolean<K> bops;
bops.insert(pA);
bops.mark_domains();
Equal equal(pA.number_of_polygons_with_holes());
return bops(equal);
}

template <typename PA, typename PB, typename K = Default>
decltype(auto) // Multipolygon_with_holes_2<K>
intersection(const PA& pA, const PB& pB, const K& k = Default())
{
typedef typename Default::Get<K, typename PA::Traits>::type Traits;

struct Equal {
bool operator()(int i) const
{
return i == 2;
}
};

CGAL::Polygon_repair::Boolean<Traits> bops;
bops.insert(pA);
bops.insert(pB);
bops.mark_domains();
Equal equal;
return bops(equal);
}

} // namespace Polygon_repair
} //namespace CGAL

Expand Down
12 changes: 8 additions & 4 deletions Polygon_repair/include/CGAL/Polygon_repair/repair.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,16 @@ Multipolygon_with_holes_2<Kernel, Container> repair(const Multipolygon_with_hole
template <class Kernel, class Container>
Multipolygon_with_holes_2<Kernel, Container> repair(const Multipolygon_with_holes_2<Kernel, Container>& p, Union_rule)
{
CGAL::Polygon_repair::Boolean<Kernel> bops;
bops.insert(p);
struct Larger_than_zero {
bool operator()(int i) const
{
return i > 0;
}
};

CGAL::Polygon_repair::Boolean<Kernel> bops;
bops.insert(p);
bops.mark_domains();
Larger_than_zero ltz;
return bops(ltz);
}
Expand All @@ -125,8 +127,6 @@ Multipolygon_with_holes_2<Kernel, Container> repair(const Multipolygon_with_hole
template <class Kernel, class Container>
Multipolygon_with_holes_2<Kernel, Container> repair(const Multipolygon_with_holes_2<Kernel, Container>& p, Intersection_rule)
{
CGAL::Polygon_repair::Boolean<Kernel> bops;
bops.insert(p);
struct Equal {
int val;
Equal(int val)
Expand All @@ -138,6 +138,10 @@ Multipolygon_with_holes_2<Kernel, Container> repair(const Multipolygon_with_hole
return i == val;
}
};

CGAL::Polygon_repair::Boolean<Kernel> bops;
bops.insert(p);
bops.mark_domains();
Equal equal(p.number_of_polygons_with_holes());
return bops(equal);
}
Expand Down

0 comments on commit 78c8b44

Please sign in to comment.