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

This commit is contained in:
Andreas Fabri 2024-04-15 11:28:13 +01:00
parent 180dbaeb40
commit ed70775ee9
4 changed files with 19 additions and 6 deletions

View File

@ -78,6 +78,8 @@ public:
void add_polygon(const Polygon_2& pgn) { m_polygons.push_back(Polygon_with_holes_2(pgn)); } void add_polygon(const Polygon_2& pgn) { m_polygons.push_back(Polygon_with_holes_2(pgn)); }
void add_polygon(const Polygon_2&& pgn) { m_polygons.push_back(Polygon_with_holes_2(std::forward(pgn))); }
void add_polygon_with_holes(const Polygon_with_holes_2& pgn) { m_polygons.push_back(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 add_polygon_with_holes(Polygon_with_holes_2&& pgn) { m_polygons.emplace_back(std::move(pgn)); }

View File

@ -34,6 +34,7 @@
#include <CGAL/enum.h> #include <CGAL/enum.h>
#include <CGAL/Aff_transformation_2.h> #include <CGAL/Aff_transformation_2.h>
#include <CGAL/Container_helper.h>
#include <CGAL/Polygon_2_algorithms.h> #include <CGAL/Polygon_2_algorithms.h>
#include <CGAL/Polygon_2/Polygon_2_vertex_circulator.h> #include <CGAL/Polygon_2/Polygon_2_vertex_circulator.h>
@ -543,6 +544,12 @@ class Polygon_2 {
container().resize(s); 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 bool identical(const Polygon_2<Traits_P,Container_P> &q) const

View File

@ -88,6 +88,7 @@ operator>>(std::istream &is, Polygon_2<Traits_P,Container_P>& p)
if (is) { if (is) {
p.erase(p.vertices_begin(),p.vertices_end()); p.erase(p.vertices_begin(),p.vertices_end());
p.reserve(n);
for (int i=0; i<n; i++) { for (int i=0; i<n; i++) {
if(is >> point){ if(is >> point){
p.push_back(point); p.push_back(point);
@ -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; typedef typename Polygon_2<Traits_P,Container_P>::Vertex_const_iterator VI;
Polygon_2<Traits_P,Container_P> result; Polygon_2<Traits_P,Container_P> result;
result.reserve(p.size());
for (VI i = p.vertices_begin(); i != p.vertices_end(); ++i) for (VI i = p.vertices_begin(); i != p.vertices_end(); ++i)
result.push_back(t(*i)); result.push_back(t(*i));
return result; return result;

View File

@ -645,12 +645,14 @@ public:
reconstruct_ring(ring, face, opposite_vertex); reconstruct_ring(ring, face, opposite_vertex);
// Put ring in polygons // Put ring in polygons
Polygon_2<Kernel, Container> polygon(ring.begin(), ring.end()); Polygon_2<Kernel, Container> polygon;
polygon.reserve(ring.size());
polygon.insert(polygon.vertices_end(),ring.begin(), ring.end());
// std::cout << "Reconstructed ring for polygon " << face->label() << " with ccw? " << (polygon.orientation() == CGAL::COUNTERCLOCKWISE) << std::endl; // std::cout << "Reconstructed ring for polygon " << face->label() << " with ccw? " << (polygon.orientation() == CGAL::COUNTERCLOCKWISE) << std::endl;
if (polygon.orientation() == CGAL::COUNTERCLOCKWISE) { if (polygon.orientation() == CGAL::COUNTERCLOCKWISE) {
polygons[face->label()-1] = polygon; polygons[face->label()-1] = std::move(polygon);
} else { } else {
holes[face->label()-1].insert(polygon); holes[face->label()-1].insert(std::move(polygon));
} break; } break;
} }
} }
@ -658,11 +660,11 @@ public:
// Create polygons with holes and put in multipolygon // Create polygons with holes and put in multipolygon
std::set<Polygon_with_holes_2<Kernel, Container>, Polygon_with_holes_less> ordered_polygons; std::set<Polygon_with_holes_2<Kernel, Container>, Polygon_with_holes_less> ordered_polygons;
for (std::size_t i = 0; i < polygons.size(); ++i) { for (std::size_t i = 0; i < polygons.size(); ++i) {
ordered_polygons.insert(Polygon_with_holes_2<Kernel, Container>(polygons[i], holes[i].begin(), holes[i].end())); ordered_polygons.insert(Polygon_with_holes_2<Kernel, Container>(std::move(polygons[i]), holes[i].begin(), holes[i].end()));
} }
for (auto const& polygon: ordered_polygons) { for (auto const& polygon: ordered_polygons) {
// std::cout << "Adding polygon " << polygon << std::endl; // std::cout << "Adding polygon " << polygon << std::endl;
mp.add_polygon_with_holes(polygon); mp.add_polygon_with_holes(std::move(polygon));
} }
} }
@ -685,7 +687,7 @@ public:
return t; return t;
} }
Multipolygon_with_holes_2<Kernel, Container> multipolygon() { const Multipolygon_with_holes_2<Kernel, Container>& multipolygon() {
return mp; return mp;
} }