Fix Mesh Boolean negative (cut) text object (#16275)

* Fix Mesh Boolean negative (cut) text object

* Update test_meshboolean.cpp

* missing headers

* Update test_meshboolean.cpp

* Refactor mcut difference test for source splits

* Apply suggestion from @raistlin7447
This commit is contained in:
Rodrigo Faselli
2026-10-08 16:00:28 -03:00
committed by GitHub
parent 9d32c1c545
commit ee2c40ea85
2 changed files with 61 additions and 2 deletions
+22 -1
View File
@@ -1045,7 +1045,28 @@ void do_boolean(McutMesh& srcMesh, const McutMesh& cutMesh, const std::string& b
// But we can force it to work by spliting the src mesh into disconnected components,
// and do booleans seperately, then merge all the results.
indexed_triangle_set all_its;
if (boolean_opts == "UNION" || boolean_opts == "A_NOT_B") {
if (boolean_opts == "A_NOT_B") {
// Each cut can leave the source with several disconnected components, which mcut rejects
// in the next dispatch, so re-split after every cut part (e.g. each letter of a text).
std::vector<indexed_triangle_set> parts = std::move(src_parts);
for (size_t j = 0; j < cut_parts.size(); j++) {
auto cut_part = triangle_mesh_to_mcut(cut_parts[j]);
std::vector<indexed_triangle_set> next_parts;
for (indexed_triangle_set &part : parts) {
auto src_part = triangle_mesh_to_mcut(part);
if (do_boolean_single(*src_part, *cut_part, boolean_opts)) {
TriangleMesh tri_part = mcut_to_triangle_mesh(*src_part);
std::vector<indexed_triangle_set> pieces = its_split(tri_part.its);
std::move(pieces.begin(), pieces.end(), std::back_inserter(next_parts));
} else
next_parts.emplace_back(std::move(part));
}
parts = std::move(next_parts);
}
for (const indexed_triangle_set &part : parts)
its_merge(all_its, part);
}
else if (boolean_opts == "UNION") {
for (size_t i = 0; i < src_parts.size(); i++) {
auto src_part = triangle_mesh_to_mcut(src_parts[i]);
for (size_t j = 0; j < cut_parts.size(); j++) {
+39 -1
View File
@@ -1,9 +1,11 @@
#include <catch2/catch_all.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_approx.hpp>
#include <catch2/matchers/catch_matchers.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
#include <libslic3r/TriangleMesh.hpp>
#include <libslic3r/MeshBoolean.hpp>
#include <vector>
using namespace Slic3r;
@@ -24,3 +26,39 @@ TEST_CASE("CGAL and TriangleMesh conversions", "[MeshBoolean]") {
REQUIRE(! MeshBoolean::cgal::does_self_intersect(M));
}
TEST_CASE("mcut difference handles source splits between cuts", "[MeshBoolean]") {
TriangleMesh body = make_cube(30., 10., 10.);
TriangleMesh tool;
// First cut splits the source into two disconnected components.
TriangleMesh slab = make_cube(2., 12., 20.);
slab.translate(Vec3f(14.f, -1.f, -5.f));
its_merge(tool.its, slab.its);
// These cuts must still be applied after the source has been split.
TriangleMesh left_hole = make_cube(4., 4., 20.);
left_hole.translate(Vec3f(3.f, 3.f, -5.f));
its_merge(tool.its, left_hole.its);
TriangleMesh right_hole = make_cube(4., 4., 20.);
right_hole.translate(Vec3f(21.f, 3.f, -5.f));
its_merge(tool.its, right_hole.its);
std::vector<TriangleMesh> result;
MeshBoolean::mcut::make_boolean(body, tool, result, "A_NOT_B");
REQUIRE(result.size() == 1);
const std::vector<indexed_triangle_set> components =
its_split(result.front().its);
REQUIRE(components.size() == 2);
// 3000 - 200 - 160 - 160 = 2480.
REQUIRE_THAT(
result.front().volume(),
Catch::Matchers::WithinRel(2480., 1e-3)
);
}