From 7b0cb715191af0d73acd780e934a4c4ddfc9123f Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Thu, 17 Oct 2024 14:02:47 +0100 Subject: [PATCH 1/6] Add Weighted Sample Elimination --- .../Polygon_mesh_processing/CMakeLists.txt | 10 +++ .../poisson_eliminate.cpp | 80 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 Polygon_mesh_processing/examples/Polygon_mesh_processing/poisson_eliminate.cpp diff --git a/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt b/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt index b0958a8ff4f5..9c39bd21d7fa 100644 --- a/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt +++ b/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt @@ -152,3 +152,13 @@ if(TARGET CGAL::Ceres_support) else() message(STATUS "NOTICE: The example 'mesh_smoothing_example' uses the Ceres library, and will not be compiled.") endif() + + +find_path(CY_INCLUDE "cySampleElim.h" ) + +if(CY_INCLUDE-NOTFOUND) + message( STATUS "poisson_eliminate needs the CY library") + else() + create_single_source_cgal_program("poisson_eliminate.cpp") + target_include_directories( poisson_eliminate AFTER PRIVATE ${CY_INCLUDE} ) +endif() \ No newline at end of file diff --git a/Polygon_mesh_processing/examples/Polygon_mesh_processing/poisson_eliminate.cpp b/Polygon_mesh_processing/examples/Polygon_mesh_processing/poisson_eliminate.cpp new file mode 100644 index 000000000000..5dc3884ae211 --- /dev/null +++ b/Polygon_mesh_processing/examples/Polygon_mesh_processing/poisson_eliminate.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +typedef CGAL::Simple_cartesian K; +typedef K::Point_3 Point_3; +typedef CGAL::Surface_mesh Mesh; + + +std::vector< cy::Vec3d > inputPoints, outputPoints; + +int main(int argc, char* argv[]) +{ + std::string filename = std::filesystem::path(argv[1]).stem().string(); + Mesh sm; + CGAL::IO::read_polygon_mesh(argv[1], sm); + + CGAL::Bbox_3 bb = CGAL::bbox_3(sm.points().begin(), sm.points().end()); + cy::Vec3d bl(bb.xmin(), bb.ymin(), bb.zmin()); + cy::Vec3d tr(bb.xmax(), bb.ymax(), bb.zmax()); + std::vector points; + + CGAL::Polygon_mesh_processing::sample_triangle_mesh(sm, std::back_inserter(points), + CGAL::parameters::number_of_points_on_faces(2* num_vertices(sm)).do_sample_vertices(false).do_sample_edges(false)); + double x, y, z; + std::cout << "# samples = " << points.size() << std::endl; + double area = CGAL::Polygon_mesh_processing::area(sm); + + std::cout << "area = " << area << std::endl; + { + std::string random_points = filename+"-sampled.xyz"; + std::ofstream out(random_points.c_str()); + out.precision(17); + for (auto p : points) { + out << p << std::endl; + } + } + + for(int i = 0; i < points.size(); ++i){ + inputPoints.push_back(cy::Vec3d(points[i].x(), points[i].y(), points[i].z())); + } + + outputPoints.resize(num_vertices(sm)/2); + + cy::WeightedSampleElimination< cy::Vec3d, double, 3, int > wse; + wse.SetBoundsMin(bl); + wse.SetBoundsMax(tr); + bool isProgressive = true; + + // independent from CGAL + std::cout << wse.GetMaxPoissonDiskRadius(2, outputPoints.size(), area) << std::endl; + + double d_max = 2 * wse.GetMaxPoissonDiskRadius( 2, outputPoints.size(), area ); + + wse.Eliminate( inputPoints.data(), inputPoints.size(), + outputPoints.data(), outputPoints.size(), + isProgressive, + d_max, 2 ); + + + + std::string poisson_points = filename+"-poisson.xyz"; + std::ofstream out(poisson_points); + out.precision(17); + for(int i = 0; i < outputPoints.size(); ++i){ + out << outputPoints[i].x << " " << outputPoints[i].y << outputPoints[i].z << std::endl; + } + return 0; +} From 05f63a0962df852f296243a7d7d0f2b67c96d8be Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Thu, 17 Oct 2024 14:53:56 +0100 Subject: [PATCH 2/6] untabify --- .../Polygon_mesh_processing/poisson_eliminate.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Polygon_mesh_processing/examples/Polygon_mesh_processing/poisson_eliminate.cpp b/Polygon_mesh_processing/examples/Polygon_mesh_processing/poisson_eliminate.cpp index 5dc3884ae211..0181bbc821c0 100644 --- a/Polygon_mesh_processing/examples/Polygon_mesh_processing/poisson_eliminate.cpp +++ b/Polygon_mesh_processing/examples/Polygon_mesh_processing/poisson_eliminate.cpp @@ -32,7 +32,7 @@ int main(int argc, char* argv[]) std::vector points; CGAL::Polygon_mesh_processing::sample_triangle_mesh(sm, std::back_inserter(points), - CGAL::parameters::number_of_points_on_faces(2* num_vertices(sm)).do_sample_vertices(false).do_sample_edges(false)); + CGAL::parameters::number_of_points_on_faces(2* num_vertices(sm)).do_sample_vertices(false).do_sample_edges(false)); double x, y, z; std::cout << "# samples = " << points.size() << std::endl; double area = CGAL::Polygon_mesh_processing::area(sm); @@ -40,11 +40,11 @@ int main(int argc, char* argv[]) std::cout << "area = " << area << std::endl; { std::string random_points = filename+"-sampled.xyz"; - std::ofstream out(random_points.c_str()); - out.precision(17); - for (auto p : points) { - out << p << std::endl; - } + std::ofstream out(random_points.c_str()); + out.precision(17); + for (auto p : points) { + out << p << std::endl; + } } for(int i = 0; i < points.size(); ++i){ From b871856448aa02c1dae2a01110e1f6fe691ca842 Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Thu, 17 Oct 2024 15:10:02 +0100 Subject: [PATCH 3/6] Fix usage of find_path --- .../examples/Polygon_mesh_processing/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt b/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt index 9c39bd21d7fa..e5e4d79aae11 100644 --- a/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt +++ b/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt @@ -156,7 +156,7 @@ endif() find_path(CY_INCLUDE "cySampleElim.h" ) -if(CY_INCLUDE-NOTFOUND) +if(NOT CY_INCLUDE) message( STATUS "poisson_eliminate needs the CY library") else() create_single_source_cgal_program("poisson_eliminate.cpp") From f388f71ce697fea3e9a843577827bb2bdf7f3f7e Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Thu, 17 Oct 2024 17:27:41 +0100 Subject: [PATCH 4/6] put code in header file --- .../poisson_eliminate.cpp | 53 +------------ .../poisson_eliminate.h | 76 +++++++++++++++++++ 2 files changed, 80 insertions(+), 49 deletions(-) create mode 100644 Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/poisson_eliminate.h diff --git a/Polygon_mesh_processing/examples/Polygon_mesh_processing/poisson_eliminate.cpp b/Polygon_mesh_processing/examples/Polygon_mesh_processing/poisson_eliminate.cpp index 0181bbc821c0..89a8b51359f9 100644 --- a/Polygon_mesh_processing/examples/Polygon_mesh_processing/poisson_eliminate.cpp +++ b/Polygon_mesh_processing/examples/Polygon_mesh_processing/poisson_eliminate.cpp @@ -4,77 +4,32 @@ #include #include -#include -#include - #include #include #include #include -#include +#include typedef CGAL::Simple_cartesian K; typedef K::Point_3 Point_3; typedef CGAL::Surface_mesh Mesh; -std::vector< cy::Vec3d > inputPoints, outputPoints; - int main(int argc, char* argv[]) { std::string filename = std::filesystem::path(argv[1]).stem().string(); Mesh sm; CGAL::IO::read_polygon_mesh(argv[1], sm); - CGAL::Bbox_3 bb = CGAL::bbox_3(sm.points().begin(), sm.points().end()); - cy::Vec3d bl(bb.xmin(), bb.ymin(), bb.zmin()); - cy::Vec3d tr(bb.xmax(), bb.ymax(), bb.zmax()); std::vector points; - CGAL::Polygon_mesh_processing::sample_triangle_mesh(sm, std::back_inserter(points), - CGAL::parameters::number_of_points_on_faces(2* num_vertices(sm)).do_sample_vertices(false).do_sample_edges(false)); - double x, y, z; - std::cout << "# samples = " << points.size() << std::endl; - double area = CGAL::Polygon_mesh_processing::area(sm); - - std::cout << "area = " << area << std::endl; - { - std::string random_points = filename+"-sampled.xyz"; - std::ofstream out(random_points.c_str()); - out.precision(17); - for (auto p : points) { - out << p << std::endl; - } - } - - for(int i = 0; i < points.size(); ++i){ - inputPoints.push_back(cy::Vec3d(points[i].x(), points[i].y(), points[i].z())); - } - - outputPoints.resize(num_vertices(sm)/2); - - cy::WeightedSampleElimination< cy::Vec3d, double, 3, int > wse; - wse.SetBoundsMin(bl); - wse.SetBoundsMax(tr); - bool isProgressive = true; - - // independent from CGAL - std::cout << wse.GetMaxPoissonDiskRadius(2, outputPoints.size(), area) << std::endl; - - double d_max = 2 * wse.GetMaxPoissonDiskRadius( 2, outputPoints.size(), area ); - - wse.Eliminate( inputPoints.data(), inputPoints.size(), - outputPoints.data(), outputPoints.size(), - isProgressive, - d_max, 2 ); - - + CGAL::Polygon_mesh_processing::poisson_eliminate(sm, std::back_inserter(points)); std::string poisson_points = filename+"-poisson.xyz"; std::ofstream out(poisson_points); out.precision(17); - for(int i = 0; i < outputPoints.size(); ++i){ - out << outputPoints[i].x << " " << outputPoints[i].y << outputPoints[i].z << std::endl; + for(const Point_3& p : points){ + out << p << std::endl; } return 0; } diff --git a/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/poisson_eliminate.h b/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/poisson_eliminate.h new file mode 100644 index 000000000000..6006006466e3 --- /dev/null +++ b/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/poisson_eliminate.h @@ -0,0 +1,76 @@ +// Copyright (c) 2024 GeometryFactory (France). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org). +// +// $URL$ +// $Id$ +// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial +// +// +// Author(s) : Andreas Fabri + +#ifndef CGAL_POLYGON_MESH_PROCESSING_POISSON_ELIMINATE_H +#define CGAL_POLYGON_MESH_PROCESSING_POISSON_ELIMINATE_H + +#include + + +#include +#include + +#include +#include + + +namespace CGAL { +namespace Polygon_mesh_processing { + +template +poisson_eliminate(const TriangleMesh& sm, OutputIterator out, const NamedParameters& np = parameters::default_values()) +{ + typedef typename GetGeomTraits::type GeomTraits; + typedef typename GeomTraits::Point_3 Point_3; + + Bbox_3 bb = bbox_3(sm.points().begin(), sm.points().end()); + cy::Vec3d bl(bb.xmin(), bb.ymin(), bb.zmin()); + cy::Vec3d tr(bb.xmax(), bb.ymax(), bb.zmax()); + + std::vector inputPoints, outputPoints; + std::vector points; + + // @todo write with a transform_iterator directly into inputPoints + sample_triangle_mesh(sm, + std::back_inserter(points), + CGAL::parameters::number_of_points_on_faces(2* num_vertices(sm)) + .do_sample_vertices(false) + .do_sample_edges(false)); + double area = CGAL::Polygon_mesh_processing::area(sm); + + for(int i = 0; i < points.size(); ++i){ + inputPoints.push_back(cy::Vec3d(to_double(points[i].x()), to_double(points[i].y()), to_double(points[i].z()))); + } + + outputPoints.resize(num_vertices(sm)/2); + + cy::WeightedSampleElimination< cy::Vec3d, double, 3, int > wse; + wse.SetBoundsMin(bl); + wse.SetBoundsMax(tr); + bool isProgressive = true; + + double d_max = 2 * wse.GetMaxPoissonDiskRadius( 2, outputPoints.size(), area ); + + wse.Eliminate( inputPoints.data(), inputPoints.size(), + outputPoints.data(), outputPoints.size(), + isProgressive, + d_max, 2 ); + + for (const cy::Vec3d& p : outputPoints){ + *out++ = Point_3(p.x, p.y, p.z); + } +} + +} // namespace Polygon_mesh_processing +} // namespace CGAL + +#endif // CGAL_POLYGON_MESH_PROCESSING_POISSON_ELIMINATE_H \ No newline at end of file From 119bf909f6c370ea84e683e5c77763279d5af390 Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Thu, 17 Oct 2024 18:19:53 +0100 Subject: [PATCH 5/6] Add CGAL_USE_CY --- .../examples/Polygon_mesh_processing/CMakeLists.txt | 3 ++- .../CGAL/Polygon_mesh_processing/poisson_eliminate.h | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt b/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt index e5e4d79aae11..2c0fb41a27c6 100644 --- a/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt +++ b/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt @@ -158,7 +158,8 @@ find_path(CY_INCLUDE "cySampleElim.h" ) if(NOT CY_INCLUDE) message( STATUS "poisson_eliminate needs the CY library") - else() +else() create_single_source_cgal_program("poisson_eliminate.cpp") + target_compile_definitions( poisson_eliminate CGAL_USE_CY ) target_include_directories( poisson_eliminate AFTER PRIVATE ${CY_INCLUDE} ) endif() \ No newline at end of file diff --git a/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/poisson_eliminate.h b/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/poisson_eliminate.h index 6006006466e3..f7e7f9595168 100644 --- a/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/poisson_eliminate.h +++ b/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/poisson_eliminate.h @@ -15,10 +15,12 @@ #include +#ifdef CGAL_USE_CY #include #include + #include #include @@ -73,4 +75,6 @@ poisson_eliminate(const TriangleMesh& sm, OutputIterator out, const NamedParamet } // namespace Polygon_mesh_processing } // namespace CGAL -#endif // CGAL_POLYGON_MESH_PROCESSING_POISSON_ELIMINATE_H \ No newline at end of file +#endif // ifdef CGAL_USE_CY + +#endif // CGAL_POLYGON_MESH_PROCESSING_POISSON_ELIMINATE_H From 6033a48de308feaec3fce16e82621700cb4b98f9 Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Fri, 18 Oct 2024 16:22:42 +0200 Subject: [PATCH 6/6] Update Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt Co-authored-by: Laurent Rineau --- .../examples/Polygon_mesh_processing/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt b/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt index 2c0fb41a27c6..9f95dc2cb98a 100644 --- a/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt +++ b/Polygon_mesh_processing/examples/Polygon_mesh_processing/CMakeLists.txt @@ -154,12 +154,12 @@ else() endif() -find_path(CY_INCLUDE "cySampleElim.h" ) +find_path(CYCODEBASE_INCLUDE "cySampleElim.h" DOC "Path to the cyCodeBase directory (https://github.com/cemyuksel/cyCodeBase)") -if(NOT CY_INCLUDE) - message( STATUS "poisson_eliminate needs the CY library") +if(NOT CYCODEBASE_INCLUDE) + message( STATUS "poisson_eliminate needs the cyCodeBase library (https://github.com/cemyuksel/cyCodeBase)") else() create_single_source_cgal_program("poisson_eliminate.cpp") target_compile_definitions( poisson_eliminate CGAL_USE_CY ) - target_include_directories( poisson_eliminate AFTER PRIVATE ${CY_INCLUDE} ) + target_include_directories( poisson_eliminate AFTER PRIVATE ${CYCODEBASE_INCLUDE} ) endif() \ No newline at end of file