Add transform to Polygon_with_holes and Multipolygon_with_holes

This commit is contained in:
Andreas Fabri 2024-04-16 09:05:50 +01:00
parent fcff28f0aa
commit 3effd785c4
5 changed files with 90 additions and 10 deletions

View File

@ -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;
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(ROTATION, sin(pi), cos(pi));
Transformation rational_rotate(ROTATION,Direction(1,1), 1, 100);
Transformation translate(TRANSLATION, Vector(-2, 0));
Transformation scale(SCALING, 3);
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);

View File

@ -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

View File

@ -89,8 +89,21 @@ public:
/*! 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<<
//-----------------------------------------------------------------------//

View File

@ -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;
}

View File

@ -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;
@ -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;
}