From ecd2ed13f883efffdbde4b49aab8e71ff389fbdd Mon Sep 17 00:00:00 2001 From: Jane Tournois Date: Thu, 19 Jun 2025 15:01:45 +0200 Subject: [PATCH] add a test for determinism of Tetrahedral_remeshing --- .../test/Tetrahedral_remeshing/CMakeLists.txt | 1 + ...test_tetrahedral_remeshing_determinism.cpp | 102 ++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 Tetrahedral_remeshing/test/Tetrahedral_remeshing/test_tetrahedral_remeshing_determinism.cpp diff --git a/Tetrahedral_remeshing/test/Tetrahedral_remeshing/CMakeLists.txt b/Tetrahedral_remeshing/test/Tetrahedral_remeshing/CMakeLists.txt index 63bc0c06023..3747ae19125 100644 --- a/Tetrahedral_remeshing/test/Tetrahedral_remeshing/CMakeLists.txt +++ b/Tetrahedral_remeshing/test/Tetrahedral_remeshing/CMakeLists.txt @@ -16,6 +16,7 @@ create_single_source_cgal_program("test_tetrahedral_remeshing_with_features.cpp" create_single_source_cgal_program("test_tetrahedral_remeshing_of_one_subdomain.cpp") create_single_source_cgal_program("test_tetrahedral_remeshing_io.cpp") create_single_source_cgal_program("test_tetrahedral_remeshing_from_mesh_file.cpp") +create_single_source_cgal_program("test_tetrahedral_remeshing_determinism.cpp") # Test MLS projection add_executable(test_tetrahedral_remeshing_mls diff --git a/Tetrahedral_remeshing/test/Tetrahedral_remeshing/test_tetrahedral_remeshing_determinism.cpp b/Tetrahedral_remeshing/test/Tetrahedral_remeshing/test_tetrahedral_remeshing_determinism.cpp new file mode 100644 index 00000000000..024b1f325d5 --- /dev/null +++ b/Tetrahedral_remeshing/test/Tetrahedral_remeshing/test_tetrahedral_remeshing_determinism.cpp @@ -0,0 +1,102 @@ +//#define CGAL_TETRAHEDRAL_REMESHING_VERBOSE +//#define CGAL_TETRAHEDRAL_REMESHING_DEBUG +//#define CGAL_TETRAHEDRAL_REMESHING_GENERATE_INPUT_FILES + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +typedef CGAL::Exact_predicates_inexact_constructions_kernel K; + +typedef CGAL::Tetrahedral_remeshing::Remeshing_triangulation_3 Remeshing_triangulation; + +template +void generate_input_one_subdomain(const std::size_t nbv, T3& tr) +{ + CGAL::Random rng; + + typedef typename T3::Point Point; + std::vector pts; + while (pts.size() < nbv) + { + const float x = rng.uniform_real(-10.f, 10.f); + const float y = rng.uniform_real(-10.f, 10.f); + const float z = rng.uniform_real(-10.f, 10.f); + + pts.push_back(Point(x, y, z)); + } + tr.insert(pts.begin(), pts.end()); + + for (typename T3::Cell_handle c : tr.finite_cell_handles()) + c->set_subdomain_index(1); + + assert(tr.is_valid(true)); + +#ifdef CGAL_TETRAHEDRAL_REMESHING_GENERATE_INPUT_FILES + std::ofstream out("data/triangulation_one_subdomain.binary.cgal", + std::ios_base::out | std::ios_base::binary); + CGAL::save_binary_triangulation(out, tr); + out.close(); +#endif +} + +int main(int argc, char* argv[]) +{ + std::cout << "CGAL Random seed = " << CGAL::get_default_random().get_seed() << std::endl; + + // Collect options + std::size_t nb_runs = 5; + + Remeshing_triangulation tr; + generate_input_one_subdomain(1000, tr); + + const int target_edge_length = (argc > 1) ? atoi(argv[1]) : 1; + + std::ofstream ofs0("in.mesh"); + CGAL::IO::write_MEDIT(ofs0, tr); + ofs0.close(); + + Remeshing_triangulation tr_ref = tr; // Keep a reference for comparison + + std::vector output_tr; + output_tr.reserve(nb_runs); + + for(std::size_t i = 0; i < nb_runs; ++i) + { + std::cout << "Run " << i << " of " << nb_runs << std::endl; + + tr = tr_ref; // Reset triangulation to reference + CGAL::tetrahedral_isotropic_remeshing(tr, target_edge_length); + + std::ostringstream oss; + CGAL::IO::write_MEDIT(oss, tr); + output_tr.push_back(oss.str()); + oss.clear(); + + if(i == 0) + continue; // skip first run, it is the reference + else + { + if(0 != output_tr[i-1].compare(output_tr[i])) + { + std::cerr << "Run " << i << " differs from run " << i-1 << std::endl; + assert(false); // This should not happen + } + else + { + std::cout << "Run " << i << " is identical to run " << i-1 << std::endl; + } + } + } + + return EXIT_SUCCESS; +}