From dc854d808836815a3fc5d314f114581a50e8a7ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mael=20Rouxel-Labb=C3=A9?= Date: Wed, 31 Mar 2021 11:54:38 +0200 Subject: [PATCH] Use std::remove_if to speed-up invalid polygon removal --- .../repair_polygon_soup.h | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/repair_polygon_soup.h b/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/repair_polygon_soup.h index 782ba395cb9..9edd6c93205 100644 --- a/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/repair_polygon_soup.h +++ b/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/repair_polygon_soup.h @@ -316,27 +316,22 @@ template std::size_t remove_invalid_polygons_in_polygon_soup(PointRange& /*points*/, PolygonRange& polygons) { - std::vector to_remove; - const std::size_t ini_polygons_size = polygons.size(); - for(std::size_t polygon_index=0; polygon_index!=ini_polygons_size; ++polygon_index) - { - if(polygons[polygon_index].size() <= 2) - { + const auto rit = std::remove_if(polygons.begin(), polygons.end(), + [](auto& polygon) -> bool + { #ifdef CGAL_PMP_REPAIR_POLYGON_SOUP_VERBOSE_PP - std::cout << "Invalid polygon:"; - print_polygon(std::cout, polygons[polygon_index]); + if(polygon.size() <= 2) + { + std::cout << "Invalid polygon:"; + print_polygon(std::cout, polygon); + } #endif - to_remove.push_back(polygon_index); - } - } + return (polygon.size() <= 2); + }); - while(!to_remove.empty()) - { - polygons.erase(polygons.begin() + to_remove.back()); - to_remove.pop_back(); - } + const std::size_t removed_polygons_n = static_cast(std::distance(rit, polygons.end())); - const std::size_t removed_polygons_n = ini_polygons_size - polygons.size(); + polygons.erase(rit, polygons.end()); #ifdef CGAL_PMP_REPAIR_POLYGON_SOUP_VERBOSE if(removed_polygons_n > 0)