From 91dd7274f486b84c18a937a8437ad72f845e0a61 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Mon, 9 Dec 2024 12:44:54 +0800 Subject: [PATCH] better checks --- src/csg_tree.cpp | 25 ++++++++++++++----------- src/properties.cpp | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/csg_tree.cpp b/src/csg_tree.cpp index 4c5082f76..4c9bf343e 100644 --- a/src/csg_tree.cpp +++ b/src/csg_tree.cpp @@ -115,17 +115,7 @@ std::shared_ptr ImplToLeaf(Manifold::Impl &&impl) { std::shared_ptr SimpleBoolean(const Manifold::Impl &a, const Manifold::Impl &b, OpType op) { #ifdef MANIFOLD_DEBUG - try { - Boolean3 boolean(a, b, op); - auto impl = boolean.Result(op); - if (ManifoldParams().intermediateChecks && impl.IsSelfIntersecting()) { - dump_lock.lock(); - std::cout << "self-intersection detected" << std::endl; - dump_lock.unlock(); - throw logicErr("self intersection detected"); - } - return ImplToLeaf(std::move(impl)); - } catch (logicErr &err) { + auto dump = [&]() { if (ManifoldParams().verbose) { dump_lock.lock(); if (op == OpType::Add) @@ -139,6 +129,19 @@ std::shared_ptr SimpleBoolean(const Manifold::Impl &a, std::cout << b; dump_lock.unlock(); } + }; + try { + Boolean3 boolean(a, b, op); + auto impl = boolean.Result(op); + if (ManifoldParams().intermediateChecks && impl.IsSelfIntersecting()) { + throw logicErr("self intersection detected"); + } + return ImplToLeaf(std::move(impl)); + } catch (logicErr &err) { + dump(); + throw err; + } catch (geometryErr &err) { + dump(); throw err; } #else diff --git a/src/properties.cpp b/src/properties.cpp index c40b9d578..a54666602 100644 --- a/src/properties.cpp +++ b/src/properties.cpp @@ -234,6 +234,25 @@ bool Manifold::Impl::IsSelfIntersecting() const { if (distance2(tri_x[i], tri_y[j]) <= epsilonSq) return true; if (DistanceTriangleTriangleSquared(tri_x, tri_y) == 0.0) { + // try to move the triangles around the normal of the other face + std::array tmp_x, tmp_y; + for (int i : {0, 1, 2}) + tmp_x[i] = tri_x[i] + epsilon_ * faceNormal_[y]; + if (DistanceTriangleTriangleSquared(tmp_x, tri_y) > 0.0) + return true; + for (int i : {0, 1, 2}) + tmp_x[i] = tri_x[i] - epsilon_ * faceNormal_[y]; + if (DistanceTriangleTriangleSquared(tmp_x, tri_y) > 0.0) + return true; + for (int i : {0, 1, 2}) + tmp_y[i] = tri_y[i] + epsilon_ * faceNormal_[x]; + if (DistanceTriangleTriangleSquared(tri_x, tmp_y) > 0.0) + return true; + for (int i : {0, 1, 2}) + tmp_y[i] = tri_y[i] - epsilon_ * faceNormal_[x]; + if (DistanceTriangleTriangleSquared(tri_x, tmp_y) > 0.0) + return true; + #ifdef MANIFOLD_DEBUG if (verbose) { dump_lock.lock();