Skip to content

Commit

Permalink
Add transform to Polygon_with_holes and Multipolygon_with_holes
Browse files Browse the repository at this point in the history
  • Loading branch information
afabri committed Apr 16, 2024
1 parent fcff28f commit 3effd78
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 11 deletions.
20 changes: 10 additions & 10 deletions Kernel_23/doc/Kernel_23/CGAL/Aff_transformation_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ therefore do not appear in the constructors.
\cgalHeading{Example}
\code
typedef Cartesian<double> K;
typedef Aff_transformation_2<K> Transformation;
typedef Point_2<K> Point;
typedef Vector_2<K> Vector;
typedef Direction_2<K> Direction;
Transformation rotate(ROTATION, sin(pi), cos(pi));
Transformation rational_rotate(ROTATION,Direction(1,1), 1, 100);
Transformation translate(TRANSLATION, Vector(-2, 0));
Transformation scale(SCALING, 3);
typedef CGAL::Simple_cartesian<double> K;
typedef CGAL::Aff_transformation_2<K> Transformation;
typedef CGAL::Point_2<K> Point;
typedef CGAL::Vector_2<K> Vector;
typedef CGAL::Direction_2<K> Direction;
Transformation rotate(CGAL::ROTATION, sin(pi), cos(pi));
Transformation rational_rotate(CGAL::ROTATION,Direction(1,1), 1, 100);
Transformation translate(CGAL::TRANSLATION, Vector(-2, 0));
Transformation scale(CGAL::SCALING, 3);
Point q(0, 1);
q = rational_rotate(q);
Expand Down
12 changes: 12 additions & 0 deletions Polygon/include/CGAL/Multipolygon_with_holes_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ std::ostream& operator<<(std::ostream& os,
}
}

template <class Transformation, class Kernel, class Container>
Multipolygon_with_holes_2<Kernel, Container> transform(const Transformation& t,
const Multipolygon_with_holes_2<Kernel, Container>& mp)
{
Multipolygon_with_holes_2<Kernel, Container> result;
for(const auto& pwh : mp.polygons_with_holes()){
result.add_polygon_with_holes(std::move(transform(t, pwh)));
}
return result;

}


} //namespace CGAL

Expand Down
13 changes: 13 additions & 0 deletions Polygon/include/CGAL/Polygon_with_holes_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,21 @@ class Polygon_with_holes_2 :

/*! Obtain the bounding box of the polygon with holes */
Bbox_2 bbox() const { return this->outer_boundary().bbox(); }

};

template <class Transformation, class Kernel, class Container>
Polygon_with_holes_2<Kernel,Container> transform(const Transformation& t,
const Polygon_with_holes_2<Kernel,Container>& pwh)
{
Polygon_with_holes_2<Kernel,Container> result(transform(t, pwh.outer_boundary()));
for(const auto& hole : pwh.holes()){
result.add_hole(std::move(transform(t, hole)));
}
return result;
}


//-----------------------------------------------------------------------//
// operator<<
//-----------------------------------------------------------------------//
Expand Down
49 changes: 49 additions & 0 deletions Polygon/test/Polygon/Multipolygon_with_holes_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <CGAL/Multipolygon_with_holes_2.h>

#include <vector>
#include <iostream>
#include <cassert>
#include <iterator>


typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_2 Point;
typedef K::Vector_2 Vector_2;
typedef K::Aff_transformation_2 Transformation;

typedef CGAL::Polygon_2<K> Polygon_2;
typedef CGAL::Polygon_with_holes_2<K> Polygon_with_holes_2;
typedef CGAL::Multipolygon_with_holes_2<K> Multipolygon_with_holes_2;

int main()
{
std::array<Point,4> outer = { Point(0, 0), Point(10, 0), Point(10, 10), Point(0, 10) };
std::array<Point, 4> hole1 = { Point(1, 1), Point(1, 2), Point(2, 2), Point(2, 1) };
std::array<Point, 4> hole2 = { Point(3, 3), Point(3, 4), Point(4, 4), Point(4, 3) };

std::vector<Polygon_2> holes;
holes.reserve(2);
holes.emplace_back(hole1.begin(), hole1.end());
holes.emplace_back(hole2.begin(), hole2.end());

Polygon_2 pouter(outer.begin(), outer.end());

Polygon_with_holes_2 pwh(std::move(pouter), std::move_iterator<std::vector<Polygon_2>::iterator>(holes.begin()), std::move_iterator<std::vector<Polygon_2>::iterator>(holes.end()));


Transformation translate(CGAL::TRANSLATION, Vector_2(20, 20));
Polygon_with_holes_2 pwhc = CGAL::transform(translate, pwh);

Multipolygon_with_holes_2 mp;
mp.add_polygon_with_holes(pwh);
mp.add_polygon_with_holes(pwhc);

mp = CGAL::transform(Transformation(CGAL::SCALING, 2.0), mp);

std::cout << mp << std::endl;

return 0;
}
8 changes: 7 additions & 1 deletion Polygon/test/Polygon/Polygon_with_holes_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_2 Point;

typedef K::Vector_2 Vector_2;
typedef K::Aff_transformation_2 Transformation;

typedef CGAL::Polygon_2<K> Polygon_2;
typedef CGAL::Polygon_with_holes_2<K> Polygon_with_holes_2;
Expand Down Expand Up @@ -41,5 +42,10 @@ int main()
assert(pwh.outer_boundary().is_empty());
Polygon_with_holes_2 pwh_move_assigned;
pwh_move_assigned = std::move(pwh_copy);

std::cout << pwh_move_assigned << std::endl << "translated by Vector_2(2.0, 2.0)" << std::endl;
Transformation translate(CGAL::TRANSLATION, Vector_2(2, 2));
pwh_move_assigned = CGAL::transform(translate, pwh_move_assigned);
std::cout << pwh_move_assigned << std::endl;
return 0;
}

0 comments on commit 3effd78

Please sign in to comment.