-
Notifications
You must be signed in to change notification settings - Fork 1.6k
STL_Extension: Add bisect_failures debugging utility #9118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sloriot
merged 24 commits into
CGAL:main
from
lrineau:STL_Extension-bisect_failures-lrineau
Feb 12, 2026
+676
−5
Merged
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
669c7c8
STL_Extension: Add bisect_failures debugging utility
lrineau 04cb44b
fix license: LGPL
lrineau c89c212
bisect_failures: fix documentation
lrineau afff966
fix errors in the testsuite
lrineau 5bd39bf
address Andreas' review, except for STL_Extension vs Profiling_tools
lrineau 919bfa2
Update STL_Extension/test/STL_Extension/test_bisect_failures.cpp
lrineau 5318889
fixes after Andreas's second review
lrineau 66d16fc
avoid failures in the testsuite
lrineau b6c40b3
undef NDEBUG and CGAL_NDEBUG
lrineau 14ecf5f
use std::endl to flush cout
lrineau c6a9a1d
fix compilation error
lrineau 0a6eecd
explain why NDEBUG must not be defined
lrineau d322544
commit suggestions
lrineau 088bf68
tweak SKIP message
lrineau 67ee46f
WIP: rewrite the documentation after review
lrineau 15faab0
fix error
lrineau 30699f6
remove the `*Fn`/`*_fn` suffixes
lrineau d05e6ba
rewrite the doc after review
lrineau 0077f64
tie clog to cerr in bisect_failures
lrineau 3e0e6d6
fix the Github action demo.yml
lrineau b02f873
apply suggestions during reviews
lrineau 85075d2
avoid conversion warning
sloriot ada0be2
fix a warning
lrineau d9e4c42
bisect failure: do not compare cgal_exception->message()
lrineau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| OFF | ||
| 8 4 0 | ||
|
|
||
| 0 0 0 | ||
| 1 0 0 | ||
| 1 1 0 | ||
| 2 0 0 | ||
| 0 0 0 | ||
| 1 0 0 | ||
| 1 1 0 | ||
| 2 0 0 | ||
|
|
||
| 3 0 1 2 | ||
| 3 2 1 3 | ||
| 3 5 4 6 | ||
| 3 5 6 7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
STL_Extension/examples/STL_Extension/bisect_failures_example.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #include <CGAL/bisect_failures.h> | ||
| #include <CGAL/boost/graph/Euler_operations.h> | ||
| #include <CGAL/Exact_predicates_inexact_constructions_kernel.h> | ||
| #include <CGAL/Polygon_mesh_processing/IO/polygon_mesh_io.h> | ||
| #include <CGAL/Polygon_mesh_processing/clip.h> | ||
| #include <CGAL/Surface_mesh/Surface_mesh.h> | ||
|
|
||
| #include <iostream> | ||
|
|
||
| using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel; | ||
| using Point = Kernel::Point_3; | ||
| using Mesh = CGAL::Surface_mesh<Point>; | ||
|
|
||
|
|
||
| int main(int argc, char* argv[]) { | ||
| const std::string filename = (argc > 1) ? argv[1] : CGAL::data_file_path("meshes/non_manifold.off"); | ||
|
|
||
| Mesh mesh_a; | ||
| if(!CGAL::IO::read_polygon_mesh(filename, mesh_a)) { | ||
| std::cerr << "Error: cannot read file " << filename << "\n"; | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| std::cout << "Loaded mesh with " << mesh_a.number_of_vertices() << " vertices and " | ||
| << mesh_a.number_of_faces() << " faces\n"; | ||
|
|
||
| const std::string clip_name = (argc > 2) ? argv[2] : CGAL::data_file_path("meshes/cube.off"); | ||
| Mesh mesh_b; | ||
| if(!CGAL::IO::read_polygon_mesh(clip_name, mesh_b)) { | ||
| std::cerr << "Error: cannot read file " << clip_name << "\n"; | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| std::cout << "Loaded clipping mesh mesh with " << mesh_b.number_of_vertices() << " vertices and " | ||
| << mesh_b.number_of_faces() << " faces\n"; | ||
|
|
||
| //! [bisect_failures_snippet] | ||
| // Define the callbacks for bisect_failures | ||
|
|
||
| auto get_size = [](const Mesh& m) -> std::size_t { | ||
| return m.number_of_faces(); | ||
| }; | ||
|
|
||
| auto simplify = [](Mesh& m, int start, int end) -> bool { | ||
| for(auto i = end - 1; i >= start; --i) { | ||
| const auto f = m.faces().begin() + i; | ||
| CGAL::Euler::remove_face(halfedge(*f, m), m); | ||
| } | ||
|
|
||
| return m.is_valid(); | ||
| }; | ||
|
|
||
| auto run = [&mesh_b](Mesh& mesh) -> int { | ||
| return CGAL::Polygon_mesh_processing::clip(mesh, mesh_b) ? EXIT_SUCCESS : EXIT_FAILURE; | ||
| }; | ||
|
|
||
| auto save = [](const Mesh& m, const std::string& prefix) { | ||
| std::string out_filename = prefix + ".off"; | ||
| if(!CGAL::IO::write_polygon_mesh(out_filename, m)) { | ||
| std::cerr << "Warning: Could not save mesh to " << out_filename << "\n"; | ||
| } else { | ||
| std::cout << "Saved mesh with " << m.number_of_faces() | ||
| << " faces to " << out_filename << "\n"; | ||
| } | ||
| }; | ||
|
|
||
| // Run bisection to find minimal failing case | ||
| std::cout << "\n=== Starting bisection to find minimal failing case ===\n\n"; | ||
|
|
||
| int result = CGAL::bisect_failures(mesh_a, get_size, simplify, run, save); | ||
| //! [bisect_failures_snippet] | ||
|
|
||
| if(result == EXIT_SUCCESS) { | ||
| std::cout << "\nNo failure detected during bisection.\n"; | ||
| } else { | ||
| std::cout << "\nFailure detected during bisection. Result code: " << result << "\n"; | ||
| } | ||
|
|
||
| return EXIT_SUCCESS; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| // Copyright (c) 2025 GeometryFactory (France). | ||
| // All rights reserved. | ||
| // | ||
| // This file is part of CGAL (www.cgal.org). | ||
| // | ||
| // $URL$ | ||
| // $Id$ | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial | ||
| // | ||
| // | ||
| // Author(s) : Laurent Rineau | ||
| // | ||
| // The documentation has been partially generated using Github Copilot. | ||
lrineau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #ifndef CGAL_BISECT_FAILURES_H | ||
| #define CGAL_BISECT_FAILURES_H | ||
|
|
||
| #include <CGAL/config.h> | ||
|
|
||
| #include <CGAL/exceptions.h> | ||
| #include <CGAL/utility.h> | ||
|
|
||
| #include <cmath> | ||
| #include <exception> | ||
| #include <iostream> | ||
| #include <optional> | ||
| #include <string> | ||
|
|
||
| namespace CGAL { | ||
|
|
||
| /** | ||
| * \ingroup PkgSTLExtensionUtilities | ||
| * | ||
| * \brief Bisects input data by iteratively simplifying it to identify the minimal failing case. | ||
lrineau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
lrineau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * This debugging utility helps identify minimal test cases when complex input data causes failures. | ||
lrineau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * It works by iteratively simplifying the data and testing whether the failure persists, | ||
| * using a bisection-like approach to narrow down to the smallest failing case. | ||
lrineau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
lrineau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * The algorithm divides the input data into "buckets" and systematically removes each bucket | ||
| * to test if the failure persists. It starts with a coarse granularity (ratio=0.5, removing | ||
| * half the elements) and automatically becomes more fine-grained (dividing ratio by 2) when | ||
| * no fault is found. When a failure is found, it restarts the bisection with the smaller | ||
| * failing data, progressively narrowing down to the minimal case. | ||
lrineau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * \tparam InputData The type of input data to bisect (must be copyable and assignable) | ||
| * \tparam GetSizeFn Function object type: `std::size_t GetSizeFn(const InputData& data)` | ||
| * \tparam SimplifyFn Function object type: `bool SimplifyFn(InputData& data, std::size_t start, std::size_t end)` | ||
| * \tparam RunFn Function object type: `int RunFn(const InputData& data)` | ||
| * \tparam SaveFn Function object type: `void SaveFn(const InputData& data, const std::string& filename_prefix)` | ||
lrineau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * \param data The input data to bisect | ||
| * \param get_size_fn Function that returns the "size" of the data (e.g., number of elements). | ||
| * \param simplify_fn Function that simplifies the data by removing elements from `[start, end)`. | ||
lrineau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * Should return `true` if simplification succeeded, `false` otherwise. | ||
| * \param run_fn Function that tests the data. Should return 0 (`EXIT_SUCCESS`) on success, non-zero on failure. | ||
| * May also throw exceptions to indicate failure. | ||
| * \param save_fn Function that saves the data to a file or output. Its second parameter | ||
lrineau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * (`filename_prefix`) indicates the context (e.g., "bad", "final_bad", "error", "current") | ||
| * and can be used to name the output accordingly. | ||
| * | ||
| * \return Exit code: 0 (EXIT_SUCCESS) if no failures found, non-zero otherwise | ||
lrineau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * The algorithm: | ||
| * 1. Tests the full data first to verify it fails and capture the failure pattern | ||
lrineau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * 2. Starts with a ratio of 0.5 (removing 50% of elements) and divides data into "buckets" | ||
lrineau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * 3. For each bucket, creates a simplified version by removing that portion | ||
| * 4. Tests the simplified version with `run_fn` | ||
| * 5. If it fails with the same pattern as the original, saves it as "bad" and restarts bisection with this smaller dataset | ||
| * 6. If it succeeds or fails differently, tries the next bucket | ||
| * 7. After a complete pass with no matching failures found, reduces the ratio by half (0.5 → 0.25 → 0.125...) | ||
| * 8. Repeats until no further simplification is possible (minimal failing case found) | ||
| * 9. Saves the minimal failing case as "final_bad" and returns its exit code | ||
lrineau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * \snippet STL_Extension/bisect_failures_example.cpp bisect_failures_snippet | ||
| */ | ||
| template<typename InputData, typename GetSizeFn, typename SimplifyFn, typename RunFn, typename SaveFn> | ||
| int bisect_failures(const InputData& data, | ||
| GetSizeFn get_size_fn, | ||
| SimplifyFn simplify_fn, | ||
| RunFn run_fn, | ||
| SaveFn save_fn) | ||
| { | ||
| // Redirect temporarily cout to cerr, and clog to stdout, for debug output | ||
| auto* old_clog_buf = std::clog.rdbuf(); | ||
| auto* old_cout_buf = std::cout.rdbuf(); | ||
| auto _ = make_scope_exit([&]() { | ||
| std::clog.rdbuf(old_clog_buf); | ||
| std::cout.rdbuf(old_cout_buf); | ||
| }); | ||
| std::clog.rdbuf(old_cout_buf); | ||
| std::cout.rdbuf(std::cerr.rdbuf()); | ||
| // The following code uses std::clog to display its messages, and the user's | ||
| // code (run_fn) usages of std::cout will go to std::cerr. | ||
|
|
||
| auto do_run = [&](InputData current_data) { | ||
| std::optional<Failure_exception> cgal_exc; | ||
| std::optional<std::string> std_exc_msg; | ||
| int exit_code = EXIT_SUCCESS; | ||
| try { | ||
| exit_code = run_fn(current_data); | ||
| } catch(Failure_exception& e) { | ||
| cgal_exc = e; | ||
| std::clog << " CAUGHT CGAL EXCEPTION: " << e.what() << '\n'; | ||
| } catch(std::exception& e) { | ||
| std::clog << " CAUGHT EXCEPTION: " << e.what() << '\n'; | ||
| std_exc_msg = e.what(); | ||
| } | ||
| if(exit_code != EXIT_SUCCESS) | ||
| std::clog << " RUN RETURNED EXIT CODE: " << exit_code << '\n'; | ||
| return std::make_tuple(cgal_exc, std_exc_msg, exit_code); | ||
| }; | ||
|
|
||
| std::clog << "First run of the algorithm on full data, to check how it fails\n"; | ||
| auto [initial_cgal_exception, initial_std_exception, initial_exit_code] = do_run(data); | ||
| if(!initial_cgal_exception && !initial_std_exception && initial_exit_code == EXIT_SUCCESS) { | ||
| std::clog << "Initial run succeeded, no failure to bisect\n"; | ||
| return EXIT_SUCCESS; | ||
| } | ||
|
|
||
| double ratio = 0.5; // Start with removing half the elements | ||
|
|
||
| InputData bad_data{data}; | ||
| InputData working_data{data}; | ||
|
|
||
| int exit_code = EXIT_SUCCESS; | ||
|
|
||
| while(true) { | ||
| std::size_t nb_buckets = static_cast<std::size_t>(std::floor(1.0 / ratio)) + 1; | ||
| std::clog << "RATIO: " << ratio << '\n'; | ||
| bool found_fault_this_pass = false; | ||
|
|
||
| std::size_t nb_to_skip = 0; | ||
| for(std::size_t bucket = 0; bucket < nb_buckets;) { | ||
| const auto data_size = get_size_fn(working_data); | ||
| nb_to_skip = static_cast<std::size_t>(std::round(data_size * ratio)); | ||
| if(nb_to_skip < 1) { | ||
| nb_to_skip = 1; | ||
| nb_buckets = data_size; | ||
| } | ||
|
|
||
| const auto start = (std::min)(bucket * nb_to_skip, data_size); | ||
| const auto end = (std::min)(start + nb_to_skip, data_size); | ||
| std::clog << " SKIP from " << start << " to " << end << '\n'; | ||
|
|
||
|
|
||
| // Try to simplify the data | ||
| if(simplify_fn(working_data, start, end)) { | ||
| const auto new_size = get_size_fn(working_data); | ||
| std::clog << " size after simplification: " << new_size << '\n'; | ||
|
|
||
| if(new_size >= data_size) { | ||
| std::clog << " ERROR: could not simplify data\n"; | ||
| working_data = bad_data; | ||
| ++bucket; | ||
| continue; | ||
| } | ||
|
|
||
| // Save current state | ||
| save_fn(working_data, "current"); | ||
|
|
||
| auto [cgal_exception, std_exception, this_run_exit_code] = do_run(working_data); | ||
|
|
||
| bool same_exception = false; | ||
| bool same_exit_code = false; | ||
|
|
||
| if(cgal_exception) { | ||
| if(initial_cgal_exception && | ||
| cgal_exception->message() == initial_cgal_exception->message() && | ||
| cgal_exception->expression() == initial_cgal_exception->expression() && | ||
| cgal_exception->library() == initial_cgal_exception->library() && | ||
| cgal_exception->filename() == initial_cgal_exception->filename() && | ||
| cgal_exception->line_number() == initial_cgal_exception->line_number()) | ||
| { | ||
| same_exception = true; | ||
| } | ||
| } else if(std_exception) { | ||
| // Check if this is the same type of failure we're looking for | ||
| if(initial_std_exception && | ||
| *initial_std_exception == *std_exception) | ||
| { | ||
| same_exception = true; | ||
| } | ||
| } else if(this_run_exit_code != EXIT_SUCCESS) { | ||
| if(this_run_exit_code == initial_exit_code) { | ||
| same_exit_code = true; | ||
| } | ||
| } | ||
| if(this_run_exit_code != EXIT_SUCCESS) { | ||
| exit_code = this_run_exit_code; | ||
| } | ||
| if(same_exception || same_exit_code) { | ||
| std::clog << " -> BAD DATA! (size: " << get_size_fn(working_data) << ")\n"; | ||
| save_fn(working_data, "bad"); | ||
| bad_data = working_data; | ||
| found_fault_this_pass = true; | ||
| bucket = 0; // Reset to bisect further | ||
| continue; | ||
| } | ||
| if (cgal_exception || std_exception || this_run_exit_code != EXIT_SUCCESS) { | ||
| // Different type of error - log it but continue | ||
| if(exit_code == EXIT_SUCCESS) exit_code = EXIT_FAILURE; | ||
| std::clog << " -> ERROR DATA (different error type)\n"; | ||
| save_fn(working_data, "error"); | ||
| std::clog << " go on...\n"; | ||
| } else { | ||
| std::clog << " -> GOOD DATA :-( (size: " << get_size_fn(working_data) << ")\n"; | ||
| } | ||
| } | ||
|
|
||
| // Reset to bad_data for next iteration | ||
| working_data = bad_data; | ||
| ++bucket; | ||
| } | ||
|
|
||
| // After completing a full pass through all buckets | ||
| if(!found_fault_this_pass) { | ||
| if(nb_to_skip <= 1) { | ||
| // Cannot subdivide further - we've found the minimal failing case | ||
| break; | ||
| } | ||
| // No fault found at this ratio - make ratio smaller (more granular) | ||
| ratio = ratio / 2.0; | ||
|
|
||
| nb_buckets = static_cast<std::size_t>(std::floor(1.0 / ratio)) + 1; | ||
| std::clog << " No fault found at this ratio. Reducing ratio to: " << ratio << '\n'; | ||
| } | ||
| } | ||
|
|
||
| if(get_size_fn(bad_data) < get_size_fn(data)) { | ||
| std::clog << "FINAL BAD DATA: " << get_size_fn(bad_data) << " elements\n"; | ||
| save_fn(bad_data, "final_bad"); | ||
| return run_fn(bad_data); | ||
| } | ||
|
|
||
| return exit_code; | ||
| } | ||
|
|
||
| } // namespace CGAL | ||
|
|
||
| #endif // CGAL_BISECT_FAILURES_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.