From 3effd785c4019297cd431621a6d31f1fda0d6ab3 Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Tue, 16 Apr 2024 09:05:50 +0100 Subject: [PATCH] Add transform to Polygon_with_holes and Multipolygon_with_holes --- .../doc/Kernel_23/CGAL/Aff_transformation_2.h | 20 ++++---- .../include/CGAL/Multipolygon_with_holes_2.h | 12 +++++ Polygon/include/CGAL/Polygon_with_holes_2.h | 13 +++++ .../Polygon/Multipolygon_with_holes_test.cpp | 49 +++++++++++++++++++ .../test/Polygon/Polygon_with_holes_test.cpp | 8 ++- 5 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 Polygon/test/Polygon/Multipolygon_with_holes_test.cpp diff --git a/Kernel_23/doc/Kernel_23/CGAL/Aff_transformation_2.h b/Kernel_23/doc/Kernel_23/CGAL/Aff_transformation_2.h index bd9fd496af3a..a47111181d44 100644 --- a/Kernel_23/doc/Kernel_23/CGAL/Aff_transformation_2.h +++ b/Kernel_23/doc/Kernel_23/CGAL/Aff_transformation_2.h @@ -45,16 +45,16 @@ therefore do not appear in the constructors. \cgalHeading{Example} \code -typedef Cartesian K; -typedef Aff_transformation_2 Transformation; -typedef Point_2 Point; -typedef Vector_2 Vector; -typedef Direction_2 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 K; +typedef CGAL::Aff_transformation_2 Transformation; +typedef CGAL::Point_2 Point; +typedef CGAL::Vector_2 Vector; +typedef CGAL::Direction_2 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); diff --git a/Polygon/include/CGAL/Multipolygon_with_holes_2.h b/Polygon/include/CGAL/Multipolygon_with_holes_2.h index 36cad53ecb3d..fcdaa3d6b695 100644 --- a/Polygon/include/CGAL/Multipolygon_with_holes_2.h +++ b/Polygon/include/CGAL/Multipolygon_with_holes_2.h @@ -193,6 +193,18 @@ std::ostream& operator<<(std::ostream& os, } } +template +Multipolygon_with_holes_2 transform(const Transformation& t, + const Multipolygon_with_holes_2& mp) + { + Multipolygon_with_holes_2 result; + for(const auto& pwh : mp.polygons_with_holes()){ + result.add_polygon_with_holes(std::move(transform(t, pwh))); + } + return result; + + } + } //namespace CGAL diff --git a/Polygon/include/CGAL/Polygon_with_holes_2.h b/Polygon/include/CGAL/Polygon_with_holes_2.h index 8f85085ed905..fa7bb9e290c9 100644 --- a/Polygon/include/CGAL/Polygon_with_holes_2.h +++ b/Polygon/include/CGAL/Polygon_with_holes_2.h @@ -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 + Polygon_with_holes_2 transform(const Transformation& t, + const Polygon_with_holes_2& pwh) + { + Polygon_with_holes_2 result(transform(t, pwh.outer_boundary())); + for(const auto& hole : pwh.holes()){ + result.add_hole(std::move(transform(t, hole))); + } + return result; + } + + //-----------------------------------------------------------------------// // operator<< //-----------------------------------------------------------------------// diff --git a/Polygon/test/Polygon/Multipolygon_with_holes_test.cpp b/Polygon/test/Polygon/Multipolygon_with_holes_test.cpp new file mode 100644 index 000000000000..1257db73f903 --- /dev/null +++ b/Polygon/test/Polygon/Multipolygon_with_holes_test.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + + +typedef CGAL::Simple_cartesian K; +typedef K::Point_2 Point; +typedef K::Vector_2 Vector_2; +typedef K::Aff_transformation_2 Transformation; + +typedef CGAL::Polygon_2 Polygon_2; +typedef CGAL::Polygon_with_holes_2 Polygon_with_holes_2; +typedef CGAL::Multipolygon_with_holes_2 Multipolygon_with_holes_2; + +int main() +{ + std::array outer = { Point(0, 0), Point(10, 0), Point(10, 10), Point(0, 10) }; + std::array hole1 = { Point(1, 1), Point(1, 2), Point(2, 2), Point(2, 1) }; + std::array hole2 = { Point(3, 3), Point(3, 4), Point(4, 4), Point(4, 3) }; + + std::vector 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::iterator>(holes.begin()), std::move_iterator::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; +} diff --git a/Polygon/test/Polygon/Polygon_with_holes_test.cpp b/Polygon/test/Polygon/Polygon_with_holes_test.cpp index 97a3fc80fd18..482ab1af20d6 100644 --- a/Polygon/test/Polygon/Polygon_with_holes_test.cpp +++ b/Polygon/test/Polygon/Polygon_with_holes_test.cpp @@ -10,7 +10,8 @@ typedef CGAL::Simple_cartesian K; typedef K::Point_2 Point; - +typedef K::Vector_2 Vector_2; +typedef K::Aff_transformation_2 Transformation; typedef CGAL::Polygon_2 Polygon_2; typedef CGAL::Polygon_with_holes_2 Polygon_with_holes_2; @@ -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; }