Files
OrcaSlicer/deps_src/libigl/igl/split_nonmanifold.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

478 lines
14 KiB
C++

// This file is part of libigl, a simple c++ geometry processing library.
//
// Copyright (C) 2022 Alec Jacobson <alecjacobson@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "split_nonmanifold.h"
#include "unique_edge_map.h"
#include "connected_components.h"
#include "unique.h"
#include "sort.h"
#include "triangle_triangle_adjacency.h"
#include "placeholders.h"
#include "is_edge_manifold.h"
#include <unordered_map>
#include <cassert>
#include <type_traits>
#include "is_vertex_manifold.h"
#include "matlab_format.h"
#include <iostream>
#include <unordered_set>
#include <utility>
template <
typename DerivedF,
typename DerivedSF,
typename DerivedSVI
>
IGL_INLINE void igl::split_nonmanifold(
const Eigen::MatrixBase<DerivedF> & F,
Eigen::PlainObjectBase <DerivedSF> & SF,
Eigen::PlainObjectBase <DerivedSVI> & SVI)
{
using Scalar = typename DerivedSF::Scalar;
// Scalar must allow negative values
static_assert(std::is_signed<Scalar>::value,"Scalar must be signed");
using MatrixX2I = Eigen::Matrix<Scalar,Eigen::Dynamic,2>;
using MatrixX3I = Eigen::Matrix<Scalar,Eigen::Dynamic,3>;
using VectorXI = Eigen::Matrix< Scalar,Eigen::Dynamic,1>;
MatrixX2I E,uE;
VectorXI EMAP,uEC,uEE;
igl::unique_edge_map(F,E,uE,EMAP,uEC,uEE);
// Let's assume the most convenient connectivity data structure and worry
// about performance later
// There are always 3#F "corners".
//
// V[c] = v means that corner c is mapped to new-vertex v
// Start with all corners mapped to singleton new-vertices
Eigen::VectorXi V = Eigen::VectorXi::LinSpaced(F.size(),0,F.size()-1);
// Convenience map so that CF(f,i) = V[c] = v where c is the ith corner of
// face f.
Eigen::Map<Eigen::MatrixXi> CF = Eigen::Map<Eigen::MatrixXi>(V.data(),F.rows(),F.cols());
// C[v][j] = c means that c is the jth corner in the group of corners at
// new-vertex v. As we merge these, we will clear "dead" new-vertices.
std::vector<std::vector<int> > C(F.size());
for(int i = 0;i<F.size();i++) { C[i] = {i}; }
const int m = F.rows();
// O(S) where S = |star(v)|
// @param[in] v new-vertex index
// @return list of face indices incident on new-vertex v
const auto star = [&](const int v)->std::vector<int>
{
std::vector<int> faces(C[v].size());
for(int i = 0;i<C[v].size();i++)
{
faces[i] = C[v][i]%m;
}
return faces;
};
// O(S) where S = |star(v)|
// @param[in] v new-vertex index
// @return list of half-edge indices incident on new-vertex v
const auto nonmanifold_edge_star = [&](const int v)->std::vector<int>
{
std::vector<int> edges;
// loop over edges opposite corners of v
for(int e : C[v])
{
const int f = e%m;
for(int j = 1;j<3;j++)
{
// next edge
const int e1 = (e+j*m)%(3*m);
const int u1 = EMAP(e1);
if(uEC(u1+1)-uEC(u1) > 2)
{
edges.push_back(e1);
}
}
}
return edges;
};
// O(S) where S = |star(v)|
const std::function<void(
Eigen::VectorXi &,
std::vector<std::vector<int> > &,
const int, const int)> merge_vertex =
[&merge_vertex](Eigen::VectorXi & V,
std::vector<std::vector<int> > & C,
const int u, const int v)
{
if(u == v) { return; }
if(u > v) { merge_vertex(V,C,v,u); return; }
assert(u < v);
// Consider each corner in v
for(const int c : C[v])
{
V[c] = u;
}
// Merge C[v] into C[u]
C[u].insert(C[u].end(),C[v].begin(),C[v].end());
C[v].clear();
};
// O(S) where S is the size of the star of e's first vertex.
// This could probably be O(1) with careful bookkeeping
const auto is_boundary = [&](const int e)->bool
{
// e----d
// \ |
// \f₁↑
// \ |
// s
const int s = (e+1*m)%(3*m);
const int d = (e+2*m)%(3*m);
const int f = e%m;
const int vs = V[s];
const int vd = V[d];
// Consider every face in the star of s
for(const int g : star(vs))
{
if(g == f) { continue; }
// Consider each edge in g
for(int i = 0;i<3;i++)
{
const int a = (g+(i+1)*m)%(3*m);
const int b = (g+(i+2)*m)%(3*m);
// Is that edge the same as e?
if(V[a] == vd && V[b] == vs) { return false; }
if(V[a] == vs && V[b] == vd) { return false; }
}
}
return true;
};
// Ω(m) and probably O(m log m) or worse.
// This should take in the candidate merge edge pair, extract the submesh and
// just check if that's manifold. Then it would be O(S) where S is the size of
// biggest star of the edges' vertices.
//
// My guess is that is_edge_manifold is O(m) but is_vertex_manifold is
// O(max(F))
const auto is_manifold = [](Eigen::MatrixXi F)->bool
{
Eigen::Array<bool,Eigen::Dynamic,1> referenced =
Eigen::Array<bool,Eigen::Dynamic,1>::Zero(F.maxCoeff()+1,1);
for(int i = 0;i<F.size();i++)
{
referenced(F(i)) = true;
}
Eigen::Array<bool,Eigen::Dynamic,1> VM;
igl::is_vertex_manifold(F,VM);
for(int i = 0;i<VM.size();i++)
{
if(referenced(i) && !VM(i))
{
return false;
}
}
return igl::is_edge_manifold(F);
};
// Ω(S) where S is the largest star of (vs1,vd2) or (vd1,vs2)
// I think that is_vertex/edge_manifold(L) is O(|L| log |L|) so I think that
// should make this O(|S| log |S|) with some gross constants because of all
// the copying and sorting things into different data structures.
//
// merging edges (vs1,vd2) and (vd1,vs2) requires merging vertices (vs1→vd1) and
// (vd2→vd2).
//
// Merging vertices (a→b) will change and only change the stars of a and b.
// That is, some vertex c ≠ a,b will have the sam star before and after.
//
// Whether a vertex is singular depends entirely on its star.
//
// Therefore, the only vertices we need to check for non-manifoldness are
// vs=(vs1,vd2) and vd=(vd1,vs2).
const auto simulated_merge_is_manifold =
[&](
const int vs1, const int vd2,
const int vd1, const int vs2)->bool
{
// all_faces[i] = f means that f is the ith face in the list of stars.
std::vector<int> all_faces;
for(int v : {vs1,vd2,vd1,vs2})
{
std::vector<int> star_v = star(v);
all_faces.insert(all_faces.end(),star_v.begin(),star_v.end());
}
// unique_faces[l] = f means that f is the lth unique face in the list of
// stars.
std::vector<int> unique_faces;
std::vector<size_t> _, local;
igl::unique(all_faces,unique_faces,_,local);
Eigen::MatrixXi L(unique_faces.size(),3);
// collect local faces
for(int l = 0;l<unique_faces.size();l++)
{
L.row(l) = CF.row(unique_faces[l]);
}
{
int f = 0;
const auto merge_local = [&](const int v1, const int v2)
{
const int u = std::min(v1,v2);
for(const int v : {v1,v2})
{
for(const int c : C[v])
{
const int i = c/m;
L(local[f++],i) = u;
}
}
};
// must match order {vs1,vd2,vd1,vs2} above
merge_local(vs1,vd2);
merge_local(vd1,vs2);
}
// remove unreferenced vertices by mapping each index in L to a unique
// index between 0 and size(unique(L))
std::unordered_map<int,int> M;
for(int & i : L.reshaped())
{
if(M.find(i) == M.end())
{
M[i] = M.size();
}
i = M[i];
}
// Only need to check if the two vertices being merged are manifold
Eigen::Array<bool,Eigen::Dynamic,1> VM;
const int vs = std::min(vs1,vd2);
const int vd = std::min(vd1,vs2);
igl::is_vertex_manifold(L,VM);
if(!VM(M[vs])) {
return false;
}
if(!VM(M[vd])) {
return false;
}
// Probably only need to check incident edges in star, but this also
// checks link
return igl::is_edge_manifold(L);
};
const auto merge_edge = [&](const int e1, const int e2)
{
// Ideally we would track whether an edge is a boundary so we can just
// assert these. But because of "implied stitches" it's not necessarily just
// e1 and e2 which become non-boundary when e1 and e2 are merged.
//assert(is_boundary(e1));
//assert(is_boundary(e2));
if(!is_boundary(e1) || !is_boundary(e2)) { return false; }
assert(e1 != e2);
if(EMAP(e1) != EMAP(e2)) { return false; }
assert(EMAP(e1) == EMAP(e2));
const int u = EMAP(e1);
const bool consistent = E(e1,0) == E(e2,1);
// skip if inconsistently oriented
if(!consistent) { return false; }
// The code below is assuming merging consistently oriented edges
if(E(e1,1) != E(e2,0))
{
}
assert(E(e1,1) == E(e2,0));
//
// e1--d1 s2--e2
// \ | | /
// \f₁↑ ↓f₂/
// \ | | /
// s1 d2
//
//
// "Cutting and Stitching: Converting Sets of Polygons to Manifold
// Surfaces" [Guéziec et al. 2001]
const int s1 = (e1+1*m)%(3*m);
const int d1 = (e1+2*m)%(3*m);
#ifndef NDEBUG
{
const int f1 = e1 % m;
const int i1 = e1 / m;
const int s1_test = f1 + ((i1+1)%3)*m;
const int d1_test = f1 + ((i1+2)%3)*m;
assert(s1 == s1_test);
assert(d1 == d1_test);
}
#endif
int s2 = (e2+1*m)%(3*m);
int d2 = (e2+2*m)%(3*m);
const int vs1 = V[s1];
const int vd2 = V[d2];
const int vd1 = V[d1];
const int vs2 = V[s2];
#ifdef IGL_SPLIT_NONMANIFOLD_DEBUG
const auto simulated_merge_is_manifold_old = [&]()->bool
{
Eigen::VectorXi V_copy = V;
std::vector<std::vector<int> > C_copy = C;
merge_vertex(V_copy,C_copy,vs1,vd2);
merge_vertex(V_copy,C_copy,vd1,vs2);
Eigen::Map<Eigen::MatrixXi> CF_copy =
Eigen::Map<Eigen::MatrixXi>(V_copy.data(),CF.rows(),CF.cols());
if(!is_manifold(CF_copy)) { return false; }
return true;
};
const bool ret_old = simulated_merge_is_manifold_old();
const bool ret = simulated_merge_is_manifold(vs1,vd2,vd1,vs2);
if(ret != ret_old)
{
assert(false);
}
#endif
// I claim this is completely unnecessary if the unique edge was originally
// manifold.
//
// I also hypothesize that this is unnecessary when conducting depth-first
// traversals starting at a successful merge.
//
// That is, we never need to call this in the current algorithm.
const int edge_valence = uEC(u+1)-uEC(u);
assert(edge_valence >= 2);
if(edge_valence>2 && !simulated_merge_is_manifold(vs1,vd2,vd1,vs2))
{
return false;
}
// Now we can merge
merge_vertex(V,C,vs1,vd2);
merge_vertex(V,C,vd1,vs2);
return true;
};
// Consider each unique edge in the original mesh
// number of faces incident on each unique edge
VectorXI D = uEC.tail(uEC.rows()-1)-uEC.head(uEC.rows()-1);
VectorXI uI;
{
VectorXI sD;
igl::sort(D,1,true,sD,uI);
}
const std::function<void(const int)> dfs = [&](const int e)
{
// we just successfully merged e, find all other non-manifold edges sharing
// a current vertex with e and try to merge it too.
const int s = (e+1*m)%(3*m);
const int d = (e+2*m)%(3*m);
for(const int c : {s,d})
{
const int v = V[c];
std::vector<int> nme = nonmanifold_edge_star(v);
// My thinking is that this must be size 0 or 2.
//
// But this seems very not true...
for(int i = 0;i<nme.size();i++)
{
const int e1 = nme[i];
for(int j = i+1;j<nme.size();j++)
{
const int e2 = nme[j];
if(merge_edge(e1,e2))
{
dfs(e2);
}
}
}
}
};
// Every edge starts as a boundary
for(auto u : uI)
{
// if boundary skip
if(uEC(u+1)-uEC(u) == 1) { continue; }
for(int j = uEC(u);j<uEC(u+1);j++)
{
const int e1 = uEE(j);
for(int k = j+1;k<uEC(u+1);k++)
{
const int e2 = uEE(k);
if(merge_edge(e1,e2))
{
// for non-manifold edges, launch search from e1 and e2
if(uEC(u+1)-uEC(u) > 2)
{
dfs(e1);
}
break;
}
}
}
}
// Ideally we'd do this so that all duplicated vertices end up at the end
// rather than scrambling the whole mesh.
{
SVI.resize(F.size());
std::vector<bool> marked(F.size());
VectorXI J = VectorXI::Constant(F.size(),-1);
SF.resize(F.rows(),F.cols());
{
int nv = 0;
for(int f = 0;f<m;f++)
{
for(int i = 0;i<3;i++)
{
const int c = CF(f,i);
if(J(c) == -1)
{
J(c) = nv;
SVI(nv) = F(f,i);
nv++;
}
SF(f,i) = J(c);
}
}
SVI.conservativeResize(nv);
}
}
}
template <
typename DerivedV,
typename DerivedF,
typename DerivedSV,
typename DerivedSF,
typename DerivedSVI
>
IGL_INLINE void igl::split_nonmanifold(
const Eigen::MatrixBase<DerivedV> & V,
const Eigen::MatrixBase<DerivedF> & F,
Eigen::PlainObjectBase <DerivedSV> & SV,
Eigen::PlainObjectBase <DerivedSF> & SF,
Eigen::PlainObjectBase <DerivedSVI> & SVI)
{
igl::split_nonmanifold(F,SF,SVI);
SV = V(SVI.derived(),igl::placeholders::all);
}
#ifdef IGL_STATIC_LIBRARY
template void igl::split_nonmanifold<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&, 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