Files
OrcaSlicer/deps_src/libigl/igl/predicates/triangle_triangle_intersect.cpp
Donovan Baarda dc5897d7b5 Update eigen to v5.0.1 and libigl to v2.6.0. (#11311)
* Update eigen from v3.3.7 to v5.0.1.

This updates eigen from v3.3.7 released on  December 11, 2018-12-11 to v5.0.1
released on 2025-11-11. There have be a large number of bug-fixes,
optimizations, and improvements between these releases. See the details at;

https://gitlab.com/libeigen/eigen/-/releases

It retains the previous custom minimal `CMakeLists.txt`, and adds a
README-OrcaSlicer.md that explains what version and parts of the upstream
eigen release have been included, and where the full release can be found.

* Update libigl from v2.0.0 (or older) to v2.6.0.

This updates libigl from what was probably v2.0.0 released on 2018-10-16 to
v2.6.0 released on 2025-05-15. It's possible the old version was even older
than that but there is no version indicators in the code and I ran out of
patience identifying missing changes and only went back as far as v2.0.0.

There have been a large number of bug-fixes, optimizations, and improvements
between these versions. See the following for details;

https://github.com/libigl/libigl/releases

I retained the minimal custom `CMakeLists.txt`, added `README.md` from the
libigl distribution which identifies the version, and added a
README-OrcaSlicer.md that details the version and parts that have been
included.

* Update libslic3r for libigl v2.6.0 changes.

This updates libslic3r for all changes moving to eigen v5.0.1 and libigl
v2.6.0. Despite the large number of updates to both dependencies, no changes
were required for the eigen update, and only one change was required for the
libigl update.

For libigl, `igl::Hit` was changed to a template taking the Scalar type to
use. Previously it was hard-coded to `float`, so to minimize possible impact
I've updated all places it is used from `igl::Hit` to `igl::Hit<float>`.

* Add compiler option `-DNOMINMAX` for libigl with MSVC.

MSVC by default defines `min(()` and `max()` macros that break
`std::numeric_limits<>::max()`. The upstream cmake that we don't include
adds `-DNOMINMAX` for the libigl module when compiling with MSVC, so we need
to add the same thing here.

* Fix src/libslic3r/TriangleMeshDeal.cpp for the unmodified upstream libigl.

This fixes `TriangleMeshDeal.cpp` to work with the unmodified upstream
libigl v2.6.0. loop.{h,cpp} implementation.

This file and feature was added in PR "BBS Port: Mesh Subdivision" (#12150)
which included changes to `loop.{h,cpp}` in the old version of libigl. This PR
avoids modifying the included dependencies, and uses the updated upstream
versions of those files without any modifications, which requires fixing
TriangleMeshDeal.cpp to work with them.

In particular, the modifications made to `loop.{h,cpp}` included changing the
return type from void to bool, adding additional validation checking of the
input meshes, and returning false if they failed validation. These added
checks looked unnecessary and would only have caught problems if the input
mesh was very corrupt.

To make `TriangleMeshDeal.cpp` work without this built-in checking
functionality, I removed checking/handling of any `false` return value.

There was also a hell of a lot of redundant copying and casting back and forth
between float and double, so I cleaned that up. The input and output meshs use
floats for the vertexes, and there would be no accuracy benefits from casting
to and from doubles for the simple weighted average operations done by
igl::loop(). So this just uses `Eigen:Map` to use the original input mesh
vertex data directly without requiring any copy or casting.

* Move eigen from included `deps_src` to externaly fetched `deps`.

This copys what PrusaSlicer did and moved it from an included dependency under
`deps_src` to an externaly fetched dependency under `deps`. This requires
updating some `CMakeList.txt` configs and removing the old and obsolete
`cmake/modules/FindEigen3.cmake`. The details of when this was done in
PrusaSlicer and the followup fixes are at;

* 21116995d7
* https://github.com/prusa3d/PrusaSlicer/issues/13608
* https://github.com/prusa3d/PrusaSlicer/pull/13609
* e3c277b9ee

For some reason I don't fully understand this also required fixing
`src/slic3r/GUI/GUI_App.cpp` by adding `#include <boost/nowide/cstdio.hpp>` to
fix an `error: ‘remove’ is not a member of ‘boost::nowide'`. The main thing I
don't understand is how it worked before. Note that this include is in the
PrusaSlicer version of this file, but it also significantly deviates from what
is currently in OrcaSlicer in many other ways.

* Whups... I missed adding the deps/Eigen/Eigen.cmake file...

* Tidy some whitespace indenting in CMakeLists.txt.

* Ugh... tabs indenting needing fixes.

* Change the include order of deps/Eigen.

It turns out that although Boost includes some references to Eigen, Eigen also
includes some references to Boost for supporting some of it's additional
numeric types.

I don't think it matters much since we are not using these features, but I
think technically its more correct to say Eigen depends on Boost than the
other way around, so I've re-ordered them.

* Add source for Eigen 5.0.1 download to flatpak yml config.

* Add explicit `DEPENDS dep_Boost to deps/Eigen.

I missed this before. This ensures we don't rely on include orders to make
sure Boost is installed before we configure Eigen.

* Add `DEPENDS dep_Boost dep_GMP dep_MPFR` to deps/Eigen.

It turns out Eigen can also use GMP and MPFR for multi-precision and
multi-precision-rounded numeric types if they are available.

Again, I don't think we are using these so it doesn't really matter, but it is
technically correct and ensures they are there if we ever do need them.

* Fix deps DEPENDENCY ordering for GMP, MPFR, Eigen, and CGAL.

I think this is finally correct. Apparently CGAL also optionally depends on
Eigen, so the correct dependency order from lowest to highest is GMP, MPFR, Eigen, and CGAL.

---------

Co-authored-by: Donovan Baarda <dbaarda@google.com>
Co-authored-by: Noisyfox <timemanager.rick@gmail.com>
2026-05-12 15:09:13 +08:00

319 lines
11 KiB
C++

#include "triangle_triangle_intersect.h"
#include "predicates.h"
#include <Eigen/Core>
#include <Eigen/Geometry>
template <typename Vector3D>
IGL_INLINE bool igl::predicates::triangle_triangle_intersect(
const Vector3D & p1,
const Vector3D & q1,
const Vector3D & r1,
const Vector3D & p2,
const Vector3D & q2,
const Vector3D & r2,
bool & coplanar)
{
coplanar = false;
// yet another translation of tri_tri_intersect.c [Guigue & Devillers]
exactinit();
using Vector2D = Eigen::Matrix<typename Vector3D::Scalar,2,1>;
constexpr Orientation COPLANAR = igl::predicates::Orientation::COPLANAR;
constexpr Orientation NEGATIVE = igl::predicates::Orientation::NEGATIVE;
constexpr Orientation POSITIVE = igl::predicates::Orientation::POSITIVE;
// Determine for each vertex of one triangle if it's above, below or on the
// plane of the other triangle.
// SUB(v1,p2,r2)
// SUB(v2,q2,r2)
// CROSS(N2,v1,v2)
// SUB(v1,p1,r2)
// dp1 = DOT(v1,N2);
// dp1 = (p1-r2).dot( (p2-r2).cross(q2-r2) );
const Orientation dp1 = orient3d(p2,q2,r2,p1);
const Orientation dq1 = orient3d(p2,q2,r2,q1);
const Orientation dr1 = orient3d(p2,q2,r2,r1);
const auto same_non_coplanar = [&NEGATIVE,&COPLANAR,&POSITIVE](
const Orientation a,
const Orientation b,
const Orientation c)
{
return (a == POSITIVE && b == POSITIVE && c == POSITIVE) ||
(a == NEGATIVE && b == NEGATIVE && c == NEGATIVE);
};
if(same_non_coplanar(dp1,dq1,dr1)) { return false; }
const Orientation dp2 = orient3d(p1,q1,r1,p2);
const Orientation dq2 = orient3d(p1,q1,r1,q2);
const Orientation dr2 = orient3d(p1,q1,r1,r2);
// Theoreticaly, this should have already been fired above
if(same_non_coplanar(dp2,dq2,dr2)) { return false; }
const auto tri_tri_overlap_test_2d = [&NEGATIVE,&COPLANAR,&POSITIVE](
const Vector2D & p1,
const Vector2D & q1,
const Vector2D & r1,
const Vector2D & p2,
const Vector2D & q2,
const Vector2D & r2)->bool
{
const auto ccw_tri_tri_intersection_2d = [&NEGATIVE,&COPLANAR,&POSITIVE](
const Vector2D & p1,
const Vector2D & q1,
const Vector2D & r1,
const Vector2D & p2,
const Vector2D & q2,
const Vector2D & r2)->bool
{
const auto INTERSECTION_TEST_VERTEX = [&NEGATIVE,&COPLANAR,&POSITIVE](
const Vector2D & P1,
const Vector2D & Q1,
const Vector2D & R1,
const Vector2D & P2,
const Vector2D & Q2,
const Vector2D & R2)->bool
{
if (orient2d(R2,P2,Q1) != NEGATIVE)
if (orient2d(R2,Q2,Q1) != POSITIVE)
if (orient2d(P1,P2,Q1) == POSITIVE) {
if (orient2d(P1,Q2,Q1) != POSITIVE) return 1;
else return 0;} else {
if (orient2d(P1,P2,R1) != NEGATIVE)
if (orient2d(Q1,R1,P2) != NEGATIVE) return 1;
else return 0;
else return 0;}
else
if (orient2d(P1,Q2,Q1) != POSITIVE)
if (orient2d(R2,Q2,R1) != POSITIVE)
if (orient2d(Q1,R1,Q2) != NEGATIVE) return 1;
else return 0;
else return 0;
else return 0;
else
if (orient2d(R2,P2,R1) != NEGATIVE)
if (orient2d(Q1,R1,R2) != NEGATIVE)
if (orient2d(P1,P2,R1) != NEGATIVE) return 1;
else return 0;
else
if (orient2d(Q1,R1,Q2) != NEGATIVE) {
if (orient2d(R2,R1,Q2) != NEGATIVE) return 1;
else return 0; }
else return 0;
else return 0;
};
const auto INTERSECTION_TEST_EDGE = [&NEGATIVE,&COPLANAR,&POSITIVE](
const Vector2D & P1,
const Vector2D & Q1,
const Vector2D & R1,
const Vector2D & P2,
const Vector2D & Q2,
const Vector2D & R2)->bool
{
if (orient2d(R2,P2,Q1) != NEGATIVE) {
if (orient2d(P1,P2,Q1) != NEGATIVE) {
if (orient2d(P1,Q1,R2) != NEGATIVE) return 1;
else return 0;} else {
if (orient2d(Q1,R1,P2) != NEGATIVE){
if (orient2d(R1,P1,P2) != NEGATIVE) return 1; else return 0;}
else return 0; }
} else {
if (orient2d(R2,P2,R1) != NEGATIVE) {
if (orient2d(P1,P2,R1) != NEGATIVE) {
if (orient2d(P1,R1,R2) != NEGATIVE) return 1;
else {
if (orient2d(Q1,R1,R2) != NEGATIVE) return 1; else return 0;}}
else return 0; }
else return 0; }
};
if ( orient2d(p2,q2,p1) != NEGATIVE ) {
if ( orient2d(q2,r2,p1) != NEGATIVE ) {
if ( orient2d(r2,p2,p1) != NEGATIVE ) return 1;
else return INTERSECTION_TEST_EDGE(p1,q1,r1,p2,q2,r2);
} else {
if ( orient2d(r2,p2,p1) != NEGATIVE )
return INTERSECTION_TEST_EDGE(p1,q1,r1,r2,p2,q2);
else return INTERSECTION_TEST_VERTEX(p1,q1,r1,p2,q2,r2);}}
else {
if ( orient2d(q2,r2,p1) != NEGATIVE ) {
if ( orient2d(r2,p2,p1) != NEGATIVE )
return INTERSECTION_TEST_EDGE(p1,q1,r1,q2,r2,p2);
else return INTERSECTION_TEST_VERTEX(p1,q1,r1,q2,r2,p2);}
else return INTERSECTION_TEST_VERTEX(p1,q1,r1,r2,p2,q2);}
return false;
};
if ( orient2d(p1,q1,r1) == NEGATIVE )
if ( orient2d(p2,q2,r2) == NEGATIVE )
return ccw_tri_tri_intersection_2d(p1,r1,q1,p2,r2,q2);
else
return ccw_tri_tri_intersection_2d(p1,r1,q1,p2,q2,r2);
else
if ( orient2d(p2,q2,r2) == NEGATIVE )
return ccw_tri_tri_intersection_2d(p1,q1,r1,p2,r2,q2);
else
return ccw_tri_tri_intersection_2d(p1,q1,r1,p2,q2,r2);
};
const auto coplanar_tri_tri3d = [&tri_tri_overlap_test_2d,&NEGATIVE,&COPLANAR,&POSITIVE](
const Vector3D & p1,
const Vector3D & q1,
const Vector3D & r1,
const Vector3D & p2,
const Vector3D & q2,
const Vector3D & r2)->bool
{
Vector3D normal_1 = (q1-p1).cross(r1-p1);
const auto n_x = ((normal_1[0]<0)?-normal_1[0]:normal_1[0]);
const auto n_y = ((normal_1[1]<0)?-normal_1[1]:normal_1[1]);
const auto n_z = ((normal_1[2]<0)?-normal_1[2]:normal_1[2]);
Vector2D P1,Q1,R1,P2,Q2,R2;
if (( n_x > n_z ) && ( n_x >= n_y )) {
// Project onto plane YZ
P1[0] = q1[2]; P1[1] = q1[1];
Q1[0] = p1[2]; Q1[1] = p1[1];
R1[0] = r1[2]; R1[1] = r1[1];
P2[0] = q2[2]; P2[1] = q2[1];
Q2[0] = p2[2]; Q2[1] = p2[1];
R2[0] = r2[2]; R2[1] = r2[1];
} else if (( n_y > n_z ) && ( n_y >= n_x )) {
// Project onto plane XZ
P1[0] = q1[0]; P1[1] = q1[2];
Q1[0] = p1[0]; Q1[1] = p1[2];
R1[0] = r1[0]; R1[1] = r1[2];
P2[0] = q2[0]; P2[1] = q2[2];
Q2[0] = p2[0]; Q2[1] = p2[2];
R2[0] = r2[0]; R2[1] = r2[2];
} else {
// Project onto plane XY
P1[0] = p1[0]; P1[1] = p1[1];
Q1[0] = q1[0]; Q1[1] = q1[1];
R1[0] = r1[0]; R1[1] = r1[1];
P2[0] = p2[0]; P2[1] = p2[1];
Q2[0] = q2[0]; Q2[1] = q2[1];
R2[0] = r2[0]; R2[1] = r2[1];
}
return tri_tri_overlap_test_2d(P1,Q1,R1,P2,Q2,R2);
exit(1);
return false;
};
const auto TRI_TRI_3D = [&coplanar_tri_tri3d,&coplanar,&NEGATIVE,&COPLANAR,&POSITIVE](
const Vector3D & p1,
const Vector3D & q1,
const Vector3D & r1,
const Vector3D & p2,
const Vector3D & q2,
const Vector3D & r2,
const Orientation dp2,
const Orientation dq2,
const Orientation dr2)->bool
{
const auto CHECK_MIN_MAX = [&NEGATIVE,&COPLANAR,&POSITIVE](
const Vector3D & p1,
const Vector3D & q1,
const Vector3D & r1,
const Vector3D & p2,
const Vector3D & q2,
const Vector3D & r2)->bool
{
if (orient3d(p2,p1,q1,q2) == POSITIVE) { return false; }
if (orient3d(p2,r1,p1,r2) == POSITIVE) { return false; }
return true;
};
if (dp2 == POSITIVE) {
if (dq2 == POSITIVE) return CHECK_MIN_MAX(p1,r1,q1,r2,p2,q2) ;
else if (dr2 == POSITIVE) return CHECK_MIN_MAX(p1,r1,q1,q2,r2,p2);
else return CHECK_MIN_MAX(p1,q1,r1,p2,q2,r2);
} else if (dp2 == NEGATIVE) {
if (dq2 == NEGATIVE) return CHECK_MIN_MAX(p1,q1,r1,r2,p2,q2);
else if (dr2 == NEGATIVE) return CHECK_MIN_MAX(p1,q1,r1,q2,r2,p2);
else return CHECK_MIN_MAX(p1,r1,q1,p2,q2,r2);
} else {
if (dq2 == NEGATIVE) {
if (dr2 == POSITIVE || dr2 == COPLANAR) return CHECK_MIN_MAX(p1,r1,q1,q2,r2,p2);
else return CHECK_MIN_MAX(p1,q1,r1,p2,q2,r2);
}
else if (dq2 == POSITIVE) {
if (dr2 == POSITIVE) return CHECK_MIN_MAX(p1,r1,q1,p2,q2,r2);
else return CHECK_MIN_MAX(p1,q1,r1,q2,r2,p2);
}
else {
if (dr2 == POSITIVE) return CHECK_MIN_MAX(p1,q1,r1,r2,p2,q2);
else if (dr2 == NEGATIVE) return CHECK_MIN_MAX(p1,r1,q1,r2,p2,q2);
else
{
coplanar = coplanar_tri_tri3d(p1,q1,r1,p2,q2,r2);
return coplanar;
}
}
}
};
if (dp1 == POSITIVE) {
if (dq1 == POSITIVE) return TRI_TRI_3D(r1,p1,q1,p2,r2,q2,dp2,dr2,dq2);
else if (dr1 == POSITIVE) return TRI_TRI_3D(q1,r1,p1,p2,r2,q2,dp2,dr2,dq2) ;
else return TRI_TRI_3D(p1,q1,r1,p2,q2,r2,dp2,dq2,dr2);
} else if (dp1 == NEGATIVE) {
if (dq1 == NEGATIVE) return TRI_TRI_3D(r1,p1,q1,p2,q2,r2,dp2,dq2,dr2);
else if (dr1 == NEGATIVE) return TRI_TRI_3D(q1,r1,p1,p2,q2,r2,dp2,dq2,dr2);
else return TRI_TRI_3D(p1,q1,r1,p2,r2,q2,dp2,dr2,dq2);
} else {
if (dq1 == NEGATIVE) {
// why COPLANAR here? It seems so haphazard.
if (dr1 == POSITIVE || dr1 == COPLANAR) return TRI_TRI_3D(q1,r1,p1,p2,r2,q2,dp2,dr2,dq2);
else return TRI_TRI_3D(p1,q1,r1,p2,q2,r2,dp2,dq2,dr2);
}
else if (dq1 == POSITIVE) {
if (dr1 == POSITIVE) return TRI_TRI_3D(p1,q1,r1,p2,r2,q2,dp2,dr2,dq2);
else return TRI_TRI_3D(q1,r1,p1,p2,q2,r2,dp2,dq2,dr2);
}
else {
if (dr1 == POSITIVE) return TRI_TRI_3D(r1,p1,q1,p2,q2,r2,dp2,dq2,dr2);
else if (dr1 == NEGATIVE) return TRI_TRI_3D(r1,p1,q1,p2,r2,q2,dp2,dr2,dq2);
else
{
coplanar = coplanar_tri_tri3d(p1,q1,r1,p2,q2,r2);
return coplanar;
}
}
}
}
#ifdef IGL_STATIC_LIBRARY
// Explicit template specialization
// template using Eigen::Vector3d.
template bool igl::predicates::triangle_triangle_intersect<Eigen::Vector3d>(
const Eigen::Vector3d & p1,
const Eigen::Vector3d & q1,
const Eigen::Vector3d & r1,
const Eigen::Vector3d & p2,
const Eigen::Vector3d & q2,
const Eigen::Vector3d & r2,
bool & coplanar);
template bool igl::predicates::triangle_triangle_intersect<Eigen::RowVector3d>(
const Eigen::RowVector3d & p1,
const Eigen::RowVector3d & q1,
const Eigen::RowVector3d & r1,
const Eigen::RowVector3d & p2,
const Eigen::RowVector3d & q2,
const Eigen::RowVector3d & r2,
bool & coplanar);
#endif