Files
OrcaSlicer/deps_src/libigl/igl/copyleft/cgal/wire_mesh.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

255 lines
7.6 KiB
C++

#include "wire_mesh.h"
#include "../../list_to_matrix.h"
#include "../../PI.h"
#include "../../placeholders.h"
#include "convex_hull.h"
#include "coplanar.h"
#include "mesh_boolean.h"
#include <Eigen/Geometry>
#include <vector>
template <
typename DerivedWV,
typename DerivedWE,
typename Derivedth,
typename DerivedV,
typename DerivedF,
typename DerivedJ>
IGL_INLINE void igl::copyleft::cgal::wire_mesh(
const Eigen::MatrixBase<DerivedWV> & WV,
const Eigen::MatrixBase<DerivedWE> & WE,
const Eigen::MatrixBase<Derivedth> & th,
const int poly_size,
const bool solid,
Eigen::PlainObjectBase<DerivedV> & V,
Eigen::PlainObjectBase<DerivedF> & F,
Eigen::PlainObjectBase<DerivedJ> & J)
{
assert((th.size()==1 || th.size()==WE.rows()) &&
"th should be scalar or size of WE");
typedef typename DerivedWV::Scalar Scalar;
// Canonical polygon to place at each endpoint
typedef Eigen::Matrix<Scalar,Eigen::Dynamic,3> MatrixX3S;
MatrixX3S PV(poly_size,3);
for(int p =0;p<PV.rows();p++)
{
const Scalar phi = (Scalar(p)/Scalar(PV.rows()))*2.*igl::PI;
PV(p,0) = 0.5*cos(phi);
PV(p,1) = 0.5*sin(phi);
PV(p,2) = 0;
}
V.resize(WV.rows() + PV.rows() * 2 * WE.rows(),3);
V.topLeftCorner(WV.rows(),3) = WV;
// Signed adjacency list
std::vector<std::vector<std::pair<int,int> > > A(WV.rows());
// Inputs:
// e index of edge
// c index of endpoint [0,1]
// p index of polygon vertex
// Returns index of corresponding vertex in V
const auto index =
[&PV,&WV](const int e, const int c, const int p)->int
{
return WV.rows() + e*2*PV.rows() + PV.rows()*c + p;
};
//const auto unindex =
// [&PV,&WV](int v, int & e, int & c, int & p)
//{
// assert(v>=WV.rows());
// v = v-WV.rows();
// e = v/(2*PV.rows());
// v = v-e*(2*PV.rows());
// c = v/(PV.rows());
// v = v-c*(PV.rows());
// p = v;
//};
// Count each vertex's indicident edges.
std::vector<int> nedges(WV.rows(), 0);
for(int e = 0;e<WE.rows();e++)
{
++nedges[WE(e, 0)];
++nedges[WE(e, 1)];
}
// loop over all edges
for(int e = 0;e<WE.rows();e++)
{
// Fill in adjacency list as we go
A[WE(e,0)].emplace_back(e,0);
A[WE(e,1)].emplace_back(e,1);
typedef Eigen::Matrix<Scalar,1,3> RowVector3S;
const RowVector3S ev = WV.row(WE(e,1))-WV.row(WE(e,0));
const Scalar len = ev.norm();
// Unit edge vector
const RowVector3S uv = ev.normalized();
Eigen::Quaternion<Scalar> q;
q = q.FromTwoVectors(RowVector3S(0,0,1),uv);
// loop over polygon vertices
for(int p = 0;p<PV.rows();p++)
{
RowVector3S qp = q*(PV.row(p)*th(e%th.size()));
// loop over endpoints
for(int c = 0;c<2;c++)
{
// Direction moving along edge vector
const Scalar dir = c==0?1:-1;
// Amount (distance) to move along edge vector
// Start with factor of thickness;
// Max out amount at 1/3 of edge length so that there's always some
// amount of edge
// Zero out if vertex is incident on only one edge
Scalar dist =
std::min(1.*th(e%th.size()),len/3.0)*(nedges[WE(e,c)] > 1);
// Move to endpoint, offset by amount
V.row(index(e,c,p)) =
qp+WV.row(WE(e,c)) + dist*dir*uv;
}
}
}
std::vector<std::vector<typename DerivedF::Index> > vF;
std::vector<int> vJ;
const auto append_hull =
[&V,&vF,&vJ](const Eigen::VectorXi & I, const int j)
{
MatrixX3S Vv = V(I,igl::placeholders::all);
if(coplanar(Vv))
{
return;
}
Eigen::MatrixXi Fv;
convex_hull(Vv,Fv);
for(int f = 0;f<Fv.rows();f++)
{
const Eigen::Array<int,1,3> face(I(Fv(f,0)), I(Fv(f,1)), I(Fv(f,2)));
//const bool on_vertex = (face<WV.rows()).any();
//if(!on_vertex)
//{
// // This correctly prunes fcaes on the "caps" of convex hulls around
// // edges, but for convex hulls around vertices this will only work if
// // the incoming edges are not overlapping.
// //
// // Q: For convex hulls around vertices, is the correct thing to do:
// // check if all corners of face lie *on or _outside_* of plane of "cap"?
// //
// // H: Maybe, but if there's an intersection then the boundary of the
// // incoming convex hulls around edges is still not going to match up
// // with the boundary on the convex hull around the vertices.
// //
// // Might have to bite the bullet and always call self-union.
// bool all_same = true;
// int e0,c0,p0;
// unindex(face(0),e0,c0,p0);
// for(int i = 1;i<3;i++)
// {
// int ei,ci,pi;
// unindex(face(i),ei,ci,pi);
// all_same = all_same && (e0==ei && c0==ci);
// }
// if(all_same)
// {
// // don't add this face
// continue;
// }
//}
vF.push_back( { face(0),face(1),face(2)});
vJ.push_back(j);
}
};
// loop over each vertex
for(int v = 0;v<WV.rows();v++)
{
// Gather together this vertex and the polygon vertices of all incident
// edges
Eigen::VectorXi I(1+A[v].size()*PV.rows());
// This vertex
I(0) = v;
for(int n = 0;n<A[v].size();n++)
{
for(int p = 0;p<PV.rows();p++)
{
const int e = A[v][n].first;
const int c = A[v][n].second;
I(1+n*PV.rows()+p) = index(e,c,p);
}
}
append_hull(I,v);
}
// loop over each edge
for(int e = 0;e<WE.rows();e++)
{
// Gether together polygon vertices of both endpoints
Eigen::VectorXi I(PV.rows()*2);
for(int c = 0;c<2;c++)
{
for(int p = 0;p<PV.rows();p++)
{
I(c*PV.rows()+p) = index(e,c,p);
}
}
append_hull(I,WV.rows()+e);
}
list_to_matrix(vF,F);
if(solid)
{
// Self-union to clean up
igl::copyleft::cgal::mesh_boolean(
Eigen::MatrixXd(V),Eigen::MatrixXi(F),Eigen::MatrixXd(),Eigen::MatrixXi(),
"union",
V,F,J);
for(int j=0;j<J.size();j++) J(j) = vJ[J(j)];
}else
{
list_to_matrix(vJ,J);
}
}
template <
typename DerivedWV,
typename DerivedWE,
typename DerivedV,
typename DerivedF,
typename DerivedJ>
IGL_INLINE void igl::copyleft::cgal::wire_mesh(
const Eigen::MatrixBase<DerivedWV> & WV,
const Eigen::MatrixBase<DerivedWE> & WE,
const double th,
const int poly_size,
const bool solid,
Eigen::PlainObjectBase<DerivedV> & V,
Eigen::PlainObjectBase<DerivedF> & F,
Eigen::PlainObjectBase<DerivedJ> & J)
{
return wire_mesh(
WV,WE,(Eigen::VectorXd(1,1)<<th).finished(),poly_size,solid,V,F,J);
}
template <
typename DerivedWV,
typename DerivedWE,
typename DerivedV,
typename DerivedF,
typename DerivedJ>
IGL_INLINE void igl::copyleft::cgal::wire_mesh(
const Eigen::MatrixBase<DerivedWV> & WV,
const Eigen::MatrixBase<DerivedWE> & WE,
const double th,
const int poly_size,
Eigen::PlainObjectBase<DerivedV> & V,
Eigen::PlainObjectBase<DerivedF> & F,
Eigen::PlainObjectBase<DerivedJ> & J)
{
return wire_mesh(WV,WE,th,poly_size,true,V,F,J);
}
#ifdef IGL_STATIC_LIBRARY
// Explicit template instantiation
template void igl::copyleft::cgal::wire_mesh<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, double, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
#endif