I would like to know if anyone knows why the following function is not working as it should.
What I would have to do is for a given Surface_mesh
, first apply a function of isotropic_remeshing
and then a smoothing_shape
. The function is this:
Surface_mesh smoothShape(Surface_mesh& mesh){
std::cout << "start to compute smooth Shape" << std::endl;
// Apply the isotropic remeshinkg to the mesh
mesh = isotropicRemesh(mesh);
// Apply the smooth shape to the mesh after the isotropic_remesh
const unsigned int nb_iterations = 3;
const double time = 1.0;
CGAL::Polygon_mesh_processing::smooth_shape(mesh, time, CGAL::parameters::number_of_iterations(nb_iterations));
CGAL::IO::write_polygon_mesh("tmp/test_smooth.stl",mesh);
return mesh;
}
However, if I export the initial mesh and import it again, the program works perfectly, that is to say, this function executes the instructions correctly:
Surface_mesh smoothShape(Surface_mesh& mesh){
std::cout << "start to compute smooth Shape" << std::endl;
CGAL::IO::write_polygon_mesh("out.stl",mesh);
Surface_mesh sm;
PMP::IO::read_polygon_mesh("out.stl",sm);
// Apply the isotropic remeshinkg to the mesh
sm = isotropicRemesh(sm);
// Apply the smooth shape to the mesh after the isotropic_remesh
const unsigned int nb_iterations = 3;
const double time = 1.0;
CGAL::Polygon_mesh_processing::smooth_shape(sm, time, CGAL::parameters::number_of_iterations(nb_iterations));
CGAL::IO::write_polygon_mesh("tmp/test_smooth.stl",sm);
return sm;
}
The main problem with the first function is that, yes, I am getting a finer mesh according to the parameters set in the function but the smooth function is not generating the expected result (it does nothing). In the other hand, the second function with the same parameters is workingk perfectly and smoothing the mesh.
Any idea of the problem? Thanks in advance.
EDIT 1. Update. Minimal example:
CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
project(testCGAL)
set(Qt5_DIR "/Users/alejandro/Qt/5.15.2/clang_64/lib/cmake/Qt5")
set(PCL_DIR "/Users/alejandro/Documents/Libraries/pcl 1.12.1/share/pcl-1.12")
set(CGAL_DIR "/Users/alejandro/Documents/Libraries/cgal 5.5/lib/cmake/CGAL")
set(VTK_DIR "/Users/alejandro/Documents/Libraries/vtk 9.1.0/lib/cmake/vtk-9.1")
find_package(CGAL REQUIRED)
# Cargar la librería de VTK
find_package(VTK REQUIRED)
# Cargar la librería de PCL
find_package( PCL 1.12 REQUIRED )
include_directories( ${PCL_INCLUDE_DIRS} )
add_definitions( ${PCL_DEFINITIONS} )
link_directories( ${PCL_LIBRARY_DIRS} )
set(CMAKE_CXX_STANDARD 14)
add_executable(testCGAL main.cpp)
vtk_module_autoinit( TARGETS testCGAL MODULES ${VTK_LIBRARIES})
target_link_libraries(testCGAL CGAL::CGAL)
target_link_libraries(testCGAL ${VTK_LIBRARIES})
target_link_libraries( testCGAL ${PCL_LIBRARIES} )
main.cpp
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/smooth_shape.h>
#include <CGAL/Polygon_mesh_processing/IO/polygon_mesh_io.h>
#include <CGAL/Polygon_mesh_processing/remesh.h>
#include <iostream>
#include <set>
#include <string>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Surface_mesh<K::Point_3> Mesh;
namespace PMP = CGAL::Polygon_mesh_processing;
Mesh isotropicRemesh(Mesh &mesh) {
PMP::isotropic_remeshing(mesh.faces(), // Caras a las que se tiene que aplicar el procedimiento
1.0f, // Longitud a la que se tienen que dejar los bordes
mesh, // Malla en la que aplicar los resultados
CGAL::parameters::number_of_iterations(1).
protect_constraints(false).
number_of_relaxation_steps(1).
relax_constraints(false));
return mesh;
}
Mesh smoothShape(Mesh& mesh, bool opt_1){
// Option 1 - Doens't work
if (opt_1)
{
mesh = isotropicRemesh(mesh);
CGAL::IO::write_polygon_mesh("tmp/test_remesh.stl",mesh);
const unsigned int nb_iterations = 1;
const double time = 1.0;
PMP::smooth_shape(mesh, time, CGAL::parameters::number_of_iterations(nb_iterations));
CGAL::IO::write_polygon_mesh("tmp/test_smooth.stl",mesh);
return mesh;
}
else // Optino 2 - This works
{
CGAL::IO::write_polygon_mesh("out.stl",mesh);
Surface_mesh sm;
PMP::IO::read_polygon_mesh("out.stl",sm);
// Apply the isotropic remeshinkg to the mesh
sm = isotropicRemesh(sm);
// Apply the smooth shape to the mesh after the isotropic_remesh
const unsigned int nb_iterations = 3;
const double time = 1.0;
CGAL::Polygon_mesh_processing::smooth_shape(sm, time, CGAL::parameters::number_of_iterations(nb_iterations));
CGAL::IO::write_polygon_mesh("tmp/test_smooth.stl",sm);
return sm;
}
}
int main(int argc, char* argv[])
{
// Load data
const std::string filename = (argc > 1) ? argv[1] : CGAL::data_file_path("meshes/pig.off");
Mesh mesh;
if(!PMP::IO::read_polygon_mesh(filename, mesh))
{
std::cerr << "Invalid input." << std::endl;
return -1;
}
Surface_mesh sm1 = smoothShape(mesh, true);
CGAL::IO::write_polygon_mesh("mesh_smooth_opt1.stl", mesh, CGAL::parameters::stream_precision(17));
Surface_mesh sm2 = smoothShape(mesh, false);
CGAL::IO::write_polygon_mesh("mesh_smooth_opt2.stl", mesh, CGAL::parameters::stream_precision(17));
std::cout << "Done! << std::endl;
}
isotropicRemesh()
.mesh
is refined/smoothed It means that the second call is using the updated mesh and not the input. Additionally, if you do not see any update of the mesh by the smooth function you might have to increase the time parameter.