From ed70775ee93708f979f8fac970b5255548e5542a Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Mon, 15 Apr 2024 11:28:13 +0100 Subject: [PATCH] Polygon_repair:: Use move semantics; Add Polygon_2::reserve() --- Polygon/include/CGAL/Multipolygon_with_holes_2.h | 2 ++ Polygon/include/CGAL/Polygon_2.h | 7 +++++++ Polygon/include/CGAL/Polygon_2/Polygon_2_impl.h | 2 ++ .../include/CGAL/Polygon_repair/repair.h | 14 ++++++++------ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Polygon/include/CGAL/Multipolygon_with_holes_2.h b/Polygon/include/CGAL/Multipolygon_with_holes_2.h index 50edf6d592d..9c81bd95591 100644 --- a/Polygon/include/CGAL/Multipolygon_with_holes_2.h +++ b/Polygon/include/CGAL/Multipolygon_with_holes_2.h @@ -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(std::forward(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)); } diff --git a/Polygon/include/CGAL/Polygon_2.h b/Polygon/include/CGAL/Polygon_2.h index b2b7ab7340a..ff97d3c8993 100644 --- a/Polygon/include/CGAL/Polygon_2.h +++ b/Polygon/include/CGAL/Polygon_2.h @@ -34,6 +34,7 @@ #include #include +#include #include #include @@ -543,6 +544,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 &q) const diff --git a/Polygon/include/CGAL/Polygon_2/Polygon_2_impl.h b/Polygon/include/CGAL/Polygon_2/Polygon_2_impl.h index 8ba11273428..d2b812f4b34 100644 --- a/Polygon/include/CGAL/Polygon_2/Polygon_2_impl.h +++ b/Polygon/include/CGAL/Polygon_2/Polygon_2_impl.h @@ -88,6 +88,7 @@ operator>>(std::istream &is, Polygon_2& p) if (is) { p.erase(p.vertices_begin(),p.vertices_end()); + p.reserve(n); for (int i=0; i> point){ p.push_back(point); @@ -146,6 +147,7 @@ transform(const Transformation& t, const Polygon_2& p) { typedef typename Polygon_2::Vertex_const_iterator VI; Polygon_2 result; + result.reserve(p.size()); for (VI i = p.vertices_begin(); i != p.vertices_end(); ++i) result.push_back(t(*i)); return result; diff --git a/Polygon_repair/include/CGAL/Polygon_repair/repair.h b/Polygon_repair/include/CGAL/Polygon_repair/repair.h index 0188f1630fb..8aee2108d2a 100644 --- a/Polygon_repair/include/CGAL/Polygon_repair/repair.h +++ b/Polygon_repair/include/CGAL/Polygon_repair/repair.h @@ -645,12 +645,14 @@ public: reconstruct_ring(ring, face, opposite_vertex); // Put ring in polygons - Polygon_2 polygon(ring.begin(), ring.end()); + Polygon_2 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; if (polygon.orientation() == CGAL::COUNTERCLOCKWISE) { - polygons[face->label()-1] = polygon; + polygons[face->label()-1] = std::move(polygon); } else { - holes[face->label()-1].insert(polygon); + holes[face->label()-1].insert(std::move(polygon)); } break; } } @@ -658,11 +660,11 @@ public: // Create polygons with holes and put in multipolygon std::set, Polygon_with_holes_less> ordered_polygons; for (std::size_t i = 0; i < polygons.size(); ++i) { - ordered_polygons.insert(Polygon_with_holes_2(polygons[i], holes[i].begin(), holes[i].end())); + ordered_polygons.insert(Polygon_with_holes_2(std::move(polygons[i]), holes[i].begin(), holes[i].end())); } for (auto const& polygon: ordered_polygons) { // 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; } - Multipolygon_with_holes_2 multipolygon() { + const Multipolygon_with_holes_2& multipolygon() { return mp; }