Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polygon_repair:: Use move semantics; Add Polygon_2::reserve() #8143

Merged
merged 17 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ MainWindow::MainWindow()
// Setup input handlers. They get events before the scene gets them
// and the input they generate is passed to the triangulation with
// the signal/slot mechanism
pi = new CGAL::Qt::GraphicsViewPolylineInput<K>(this, &scene, 0, true); // inputs polylines which are not closed
pi = new CGAL::Qt::GraphicsViewPolylineInput<K>(this, &scene, 0, true); // inputs polylines which are closed
QObject::connect(pi, SIGNAL(generate(CGAL::Object)),
this, SLOT(processInput(CGAL::Object)));

Expand Down
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
15 changes: 14 additions & 1 deletion Polygon/include/CGAL/General_polygon_with_holes_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class General_polygon_with_holes_2 {

typedef unsigned int Size;

General_polygon_with_holes_2() : m_pgn() {}
General_polygon_with_holes_2() = default;


explicit General_polygon_with_holes_2(const Polygon_2& pgn_boundary) :
Expand Down Expand Up @@ -123,6 +123,19 @@ class General_polygon_with_holes_2 {

bool is_plane() const { return (m_pgn.is_empty() && m_holes.empty()); }

bool is_empty() const
{
if(! outer_boundary().is_empty()) {
return false;
}
for(const auto& h : holes()){
if(! h.is_empty()){
return false;
}
}
return true;
}

protected:
Polygon_2 m_pgn;
Holes_container m_holes;
Expand Down
47 changes: 46 additions & 1 deletion Polygon/include/CGAL/Multipolygon_with_holes_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Multipolygon_with_holes_2 {

/// @}

using value_type = Polygon_with_holes_2;
using Polygon_with_holes_container = std::deque<Polygon_with_holes_2>;

using Polygon_with_holes_iterator = typename Polygon_with_holes_container::iterator;
Expand All @@ -55,7 +56,7 @@ class Multipolygon_with_holes_2 {
using Size = unsigned int;

/*! %Default constructor. */
Multipolygon_with_holes_2() {}
Multipolygon_with_holes_2() = default;

/*! Constructor from polygons. */
template <typename PolygonsInputIterator>
Expand All @@ -72,22 +73,54 @@ class Multipolygon_with_holes_2 {

Polygon_with_holes_iterator polygons_with_holes_end() { return m_polygons.end(); }

Polygon_with_holes_iterator begin() { return m_polygons.begin(); }

Polygon_with_holes_iterator end() { return m_polygons.end(); }

Polygon_with_holes_const_iterator polygons_with_holes_begin() const { return m_polygons.begin(); }

Polygon_with_holes_const_iterator polygons_with_holes_end() const { return m_polygons.end(); }

Polygon_with_holes_const_iterator begin() const { return m_polygons.begin(); }

Polygon_with_holes_const_iterator end() const { return m_polygons.end(); }


void add_polygon(const Polygon_2& pgn) { m_polygons.push_back(Polygon_with_holes_2(pgn)); }
afabri marked this conversation as resolved.
Show resolved Hide resolved

void add_polygon(Polygon_2&& pgn) { m_polygons.emplace_back(std::move(pgn)); }

void add_polygon_with_holes(const Polygon_with_holes_2& pgn) { m_polygons.push_back(pgn); }

void add_polygon_with_holes(Polygon_with_holes_2&& pgn) { m_polygons.emplace_back(std::move(pgn)); }

void push_back(const Polygon_with_holes_2& pgn) { m_polygons.push_back(pgn); }

void erase_polygon_with_holes(Polygon_with_holes_iterator pit) { m_polygons.erase(pit); }

void clear() { m_polygons.clear(); }

Size number_of_polygons_with_holes() const { return static_cast<Size>(m_polygons.size()); }

Bbox_2 bbox() const
{
Bbox_2 bb;
for(const auto& pwh : polygons_with_holes()){
bb += pwh.bbox();
}
return bb;
}

bool is_empty() const
{
for(const auto& pwh : polygons_with_holes()){
if(! pwh.is_empty()){
return false;
}
}
return true;
}

protected:
Polygon_with_holes_container m_polygons;
};
Expand Down Expand Up @@ -180,6 +213,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(transform(t, pwh));
}
return result;

}


} //namespace CGAL

Expand Down
19 changes: 9 additions & 10 deletions Polygon/include/CGAL/Polygon_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <CGAL/enum.h>

#include <CGAL/Aff_transformation_2.h>
#include <CGAL/Container_helper.h>

#include <CGAL/Polygon_2_algorithms.h>
#include <CGAL/Polygon_2/Polygon_2_vertex_circulator.h>
Expand Down Expand Up @@ -160,11 +161,8 @@ class Polygon_2 {
/// Creates an empty polygon.
Polygon_2(const Traits & p_traits) : traits(p_traits) {}

/// Copy constructor.
Polygon_2(const Polygon_2<Traits_P,Container_P>& polygon) = default;

/// Move constructor
Polygon_2(Polygon_2<Traits_P,Container_P>&& polygon) = default;
// Move constructor
// Polygon_2(Polygon_2<Traits_P,Container_P>&& polygon) = default;

/// Creates a polygon with vertices from the sequence
/// defined by the range \c [first,last).
Expand All @@ -175,11 +173,6 @@ class Polygon_2 {
: d_container(first,last), traits(p_traits)
{}

#ifndef DOXYGEN_RUNNING
Polygon_2& operator=(const Polygon_2&) = default;
Polygon_2& operator=(Polygon_2&& p) = default;
#endif

/// @}

/// \name Modifiers
Expand Down Expand Up @@ -543,6 +536,12 @@ class Polygon_2 {
container().resize(s);
}

/// Calls `container().reserve(s)` if this is available for `Container`.
void reserve(std::size_t s)
{
internal::reserve(container(),s);
}

/// @}

bool identical(const Polygon_2<Traits_P,Container_P> &q) const
Expand Down
2 changes: 2 additions & 0 deletions Polygon/include/CGAL/Polygon_2/Polygon_2_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ operator>>(std::istream &is, Polygon_2<Traits_P,Container_P>& p)

if (is) {
p.erase(p.vertices_begin(),p.vertices_end());
p.reserve(n);
for (int i=0; i<n; i++) {
if(is >> point){
p.push_back(point);
Expand Down Expand Up @@ -146,6 +147,7 @@ transform(const Transformation& t, const Polygon_2<Traits_P,Container_P>& p)
{
typedef typename Polygon_2<Traits_P,Container_P>::Vertex_const_iterator VI;
Polygon_2<Traits_P,Container_P> result;
result.reserve(p.size());
for (VI i = p.vertices_begin(); i != p.vertices_end(); ++i)
result.push_back(t(*i));
return result;
Expand Down
23 changes: 17 additions & 6 deletions Polygon/include/CGAL/Polygon_with_holes_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ class Polygon_with_holes_2 :
typedef typename Base::Size Size;

/*! %Default constructor. */
Polygon_with_holes_2 () :
Base()
{}
Polygon_with_holes_2 () = default;

/*! Constructor from the base class. */
Polygon_with_holes_2 (const Base& base) :
Expand All @@ -63,7 +61,7 @@ class Polygon_with_holes_2 :
Base (pgn_boundary)
{}

/*! Move constructor */
/*! Cconstructor moving a polygon */
explicit Polygon_with_holes_2 (Polygon_2&& pgn_boundary) :
Base (std::move(pgn_boundary))
{}
Expand All @@ -76,7 +74,7 @@ class Polygon_with_holes_2 :
Base (pgn_boundary, h_begin, h_end)
{}

/*! Move constructor.
/*! Cconstructor moving a polygon.
* \note In order to move the hole polygons a
* `std::move_iterator` may be used.
*/
Expand All @@ -87,10 +85,23 @@ class Polygon_with_holes_2 :
Base (std::move(pgn_boundary), h_begin, h_end)
{}

/*! Obtain the bounding box of the polygon with holes */
/*! returns 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(transform(t, hole));
}
return result;
}


//-----------------------------------------------------------------------//
// operator<<
//-----------------------------------------------------------------------//
Expand Down
52 changes: 52 additions & 0 deletions Polygon/test/Polygon/Multipolygon_with_holes_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#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;

CGAL::Bbox_2 bb = mp.bbox();
std::cout << bb << std::endl;
assert(! mp.is_empty());
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;
}
4 changes: 4 additions & 0 deletions Polygon_repair/examples/Polygon_repair/data/flat.wkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POLYGON((0 0, 40 0, 40 40, 0 40))
POLYGON((10 10 , 30 10, 30 30, 0 30))
POLYGON((1 1, 2 1, 2 2, 1 2))
POLYGON((11 11, 12 11, 12 12, 11 12))
32 changes: 32 additions & 0 deletions Polygon_repair/examples/Polygon_repair/polygons2multipolygon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <fstream>
#include <deque>

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_repair/repair.h>
#include <CGAL/IO/WKT.h>

using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
using Point_2 = Kernel::Point_2;
using Polygon_2 = CGAL::Polygon_2<Kernel>;
using Polygon_with_holes_2 = CGAL::Polygon_with_holes_2<Kernel>;
using Multipolygon_with_holes_2 = CGAL::Multipolygon_with_holes_2<Kernel>;

int main(int argc, char* argv[])
{
std::ifstream in((argc > 1) ? argv[1] : "data/flat.wkt");

typedef std::vector<Point_2> MultiPoint;
typedef std::vector<Point_2> LineString;
typedef std::deque<LineString> MultiLineString;

MultiPoint points;
MultiLineString polylines;
Multipolygon_with_holes_2 polygons;
CGAL::IO::read_WKT(in, points,polylines,polygons);

Multipolygon_with_holes_2 mp = CGAL::Polygon_repair::repair(polygons);
CGAL::IO::write_multi_polygon_WKT(std::cout, mp);

return 0;
}
Loading