Files
OrcaSlicer/deps_src/libigl/igl/predicates/polygons_to_triangles.cpp
Donovan Baarda 807520ca1d 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 20:35:21 +08:00

249 lines
8.4 KiB
C++

#include "polygons_to_triangles.h"
#include "ear_clipping.h"
#include "../sort.h"
#include "../placeholders.h"
#include "../PlainMatrix.h"
#include <Eigen/Eigenvalues>
template <
typename DerivedV,
typename DerivedI,
typename DerivedC,
typename DerivedF,
typename DerivedJ>
IGL_INLINE void igl::predicates::polygons_to_triangles(
const Eigen::MatrixBase<DerivedV> & V,
const Eigen::MatrixBase<DerivedI> & I,
const Eigen::MatrixBase<DerivedC> & C,
Eigen::PlainObjectBase<DerivedF> & F,
Eigen::PlainObjectBase<DerivedJ> & J)
{
typedef Eigen::Index Index;
// Each polygon results in #sides-2 triangles. So ∑#sides-2
F.resize(C(C.size()-1) - (C.size()-1)*2,3);
J.resize(F.rows());
{
Index f = 0;
for(Index p = 0;p<C.size()-1;p++)
{
const Index np = C(p+1)-C(p);
Eigen::MatrixXi pF;
if(np == 3)
{
pF = (Eigen::MatrixXi(1,3)<<0,1,2).finished();
}else
{
// Make little copy of this polygon with an initial fan
PlainMatrix<DerivedV,Eigen::Dynamic> pV(np,V.cols());
for(Index c = 0;c<np;c++)
{
pV.row(c) = V.row(I(C(p)+c));
}
// Use PCA to project to 2D
Eigen::MatrixXd S;
switch(V.cols())
{
case 2:
S = V.template cast<double>();
break;
case 3:
{
Eigen::MatrixXd P = (pV.rowwise() - pV.colwise().mean()).template cast<double>();
Eigen::Matrix3d O = P.transpose() * P;
Eigen::EigenSolver<Eigen::Matrix3d> es(O);
Eigen::Matrix3d C = es.eigenvectors().real();
{
Eigen::Vector3d _1;
Eigen::Vector3i I;
igl::sort(es.eigenvalues().real().eval(),1,false,_1,I);
C = C(igl::placeholders::all,I).eval();
}
S = P*C.leftCols(2);
break;
}
default: assert(false && "dim>3 not supported");
}
Eigen::VectorXi RT = Eigen::VectorXi::Zero(S.rows(),1);
Eigen::VectorXi _I;
Eigen::MatrixXd _nS;
// compute signed area
{
double area = 0;
for(Index c = 0;c<np;c++)
{
area += S((c+0)%np,0)*S((c+1)%np,1) - S((c+1)%np,0)*S((c+0)%np,1);
}
//prIndexf("area: %g\n",area);
if(area<0)
{
S.col(0) *= -1;
}
}
// This is a really low quality triangulator and will contain nearly
// degenerate elements which become degenerate or worse when unprojected
// back to 3D.
// igl::predicates::ear_clipping does not gracefully fail when the input
// is not simple. Instead it (tends?) to output too few triangles.
if(! igl::predicates::ear_clipping(S,pF) )
{
// Fallback, use a fan
//std::cout<<igl::matlab_format(S,"S")<<std::endl;
//std::cout<<igl::matlab_format(RT,"RT")<<std::endl;
//std::cout<<igl::matlab_format(_I,"I")<<std::endl;
//std::cout<<igl::matlab_format(pF,"pF")<<std::endl;
//std::cout<<igl::matlab_format(_nS,"nS")<<std::endl;
//std::cout<<std::endl;
pF.resize(np-2,3);
for(Index c = 0;c<np;c++)
{
if(c>0 && c<np-1)
{
pF(c-1,0) = 0;
pF(c-1,1) = c;
pF(c-1,2) = c+1;
}
}
}
assert(pF.rows() == np-2);
// Could at least flip edges of degenerate edges
//if(pF.rows()>1)
//{
// // Delaunay-ize
// Eigen::MatrixXd pl;
// igl::edge_lengths(pV,pF,pl);
// typedef Eigen::Matrix<Index,Eigen::Dynamic,2> MatrixX2I;
// typedef Eigen::Matrix<Index,Eigen::Dynamic,1> VectorXI;
// MatrixX2I E,uE;
// VectorXI EMAP;
// std::vector<std::vector<Index> > uE2E;
// igl::unique_edge_map(pF, E, uE, EMAP, uE2E);
// typedef Index Index;
// typedef double Scalar;
// const Index num_faces = pF.rows();
// std::vector<Index> Q;
// Q.reserve(uE2E.size());
// for (size_t uei=0; uei<uE2E.size(); uei++)
// {
// Q.push_back(uei);
// }
// while(!Q.empty())
// {
// const Index uei = Q.back();
// Q.pop_back();
// if (uE2E[uei].size() == 2)
// {
// double w;
// igl::is_Indexrinsic_delaunay(pl,uE2E,num_faces,uei,w);
// prIndexf("%d : %0.17f\n",uei,w);
// if(w<-1e-7)
// {
// prIndexf(" flippin'\n");
// //
// // v1 v1
// // /|\ / \
// // c/ | \b c/f1 \b
// // v3 /f2|f1\ v4 => v3 /__f__\ v4
// // \ e / \ f2 /
// // d\ | /a d\ /a
// // \|/ \ /
// // v2 v2
// //
// // hmm... is the flip actually in the other direction?
// const Index f1 = uE2E[uei][0]%num_faces;
// const Index f2 = uE2E[uei][1]%num_faces;
// const Index c1 = uE2E[uei][0]/num_faces;
// const Index c2 = uE2E[uei][1]/num_faces;
// const size_t e_24 = f1 + ((c1 + 1) % 3) * num_faces;
// const size_t e_41 = f1 + ((c1 + 2) % 3) * num_faces;
// const size_t e_13 = f2 + ((c2 + 1) % 3) * num_faces;
// const size_t e_32 = f2 + ((c2 + 2) % 3) * num_faces;
// const size_t ue_24 = EMAP(e_24);
// const size_t ue_41 = EMAP(e_41);
// const size_t ue_13 = EMAP(e_13);
// const size_t ue_32 = EMAP(e_32);
// // new edge lengths
// const Index v1 = pF(f1, (c1+1)%3);
// const Index v2 = pF(f1, (c1+2)%3);
// const Index v4 = pF(f1, c1);
// const Index v3 = pF(f2, c2);
// {
// const Scalar e = pl(f1,c1);
// const Scalar a = pl(f1,(c1+1)%3);
// const Scalar b = pl(f1,(c1+2)%3);
// const Scalar c = pl(f2,(c2+1)%3);
// const Scalar d = pl(f2,(c2+2)%3);
// const double f = (pV.row(v3)-pV.row(v4)).norm();
// // New order
// pl(f1,0) = f;
// pl(f1,1) = b;
// pl(f1,2) = c;
// pl(f2,0) = f;
// pl(f2,1) = d;
// pl(f2,2) = a;
// }
// prIndexf("%d,%d %d,%d -> %d,%d\n",uE(uei,0),uE(uei,1),v1,v2,v3,v4);
// igl::flip_edge(pF, E, uE, EMAP, uE2E, uei);
// std::cout<<" "<<pl.row(f1)<<std::endl;
// std::cout<<" "<<pl.row(f2)<<std::endl;
// //// new edge lengths, slow!
// //igl::edge_lengths(pV,pF,pl);
// // recompute edge lengths of two faces. (extra work on untouched
// // edges)
// for(Index f : {f1,f2})
// {
// for(Index c=0;c<3;c++)
// {
// pl(f,c) =
// (pV.row(pF(f,(c+1)%3))-pV.row(pF(f,(c+2)%3))).norm();
// }
// }
// std::cout<<" "<<pl.row(f1)<<std::endl;
// std::cout<<" "<<pl.row(f2)<<std::endl;
// std::cout<<std::endl;
// Q.push_back(ue_24);
// Q.push_back(ue_41);
// Q.push_back(ue_13);
// Q.push_back(ue_32);
// }
// }
// }
// // check for self-loops (I claim these cannot happen)
// for(Index f = 0;f<pF.rows();f++)
// {
// for(Index c =0;c<3;c++)
// {
// assert(pF(f,c) != pF(f,(c+1)%3) && "self loops should not exist");
// }
// }
//}
}
// Copy Indexo global list
for(Index i = 0;i<pF.rows();i++)
{
for(Index c =0;c<3;c++)
{
F(f,c) = I(C(p)+pF(i,c));
}
J(f) = p;
f++;
}
}
assert(f == F.rows());
}
}
#ifdef IGL_STATIC_LIBRARY
// Explicit template instantiation
#endif