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

270 lines
10 KiB
C++

// This file is part of libigl, a simple C++ geometry processing library.
//
// Copyright (C) 2020 Xiangyu Kong <xiangyu.kong@mail.utoronto.ca>
//
// 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 "direct_delta_mush.h"
#include "cotmatrix.h"
#include "PlainMatrix.h"
template <
typename DerivedV,
typename DerivedOmega,
typename DerivedU>
IGL_INLINE void igl::direct_delta_mush(
const Eigen::MatrixBase<DerivedV> & V,
const std::vector<Eigen::Affine3d, Eigen::aligned_allocator<Eigen::Affine3d> > & T,
const Eigen::MatrixBase<DerivedOmega> & Omega,
Eigen::PlainObjectBase<DerivedU> & U)
{
using namespace Eigen;
// Shape checks
assert(V.cols() == 3 && "V should contain 3D positions.");
assert(Omega.rows() == V.rows() && "Omega contain the same number of rows as V.");
assert(Omega.cols() == T.size() * 10 && "Omega should have #T*10 columns.");
typedef typename DerivedV::Scalar Scalar;
int n = V.rows();
int m = T.size();
// V_homogeneous: #V by 4, homogeneous version of V
// Note:
// In the paper, the rest pose vertices are represented in U \in R^{4 x #V}
// Thus the formulae involving U would differ from the paper by a transpose.
Matrix<Scalar, Dynamic, 4> V_homogeneous(n, 4);
V_homogeneous << V, Matrix<Scalar, Dynamic, 1>::Ones(n, 1);
U.resize(n, 3);
for (int i = 0; i < n; ++i)
{
// Construct Q matrix using Omega and Transformations
Matrix<Scalar, 4, 4> Q_mat(4, 4);
Q_mat = Matrix<Scalar, 4, 4>::Zero(4, 4);
for (int j = 0; j < m; ++j)
{
Matrix<typename DerivedOmega::Scalar, 4, 4> Omega_curr(4, 4);
Matrix<typename DerivedOmega::Scalar, 10, 1> curr = Omega.block(i, j * 10, 1, 10).transpose();
Omega_curr << curr(0), curr(1), curr(2), curr(3),
curr(1), curr(4), curr(5), curr(6),
curr(2), curr(5), curr(7), curr(8),
curr(3), curr(6), curr(8), curr(9);
Affine3d M_curr = T[j];
Q_mat += M_curr.matrix() * Omega_curr;
}
// Normalize so that the last element is 1
Q_mat /= Q_mat(Q_mat.rows() - 1, Q_mat.cols() - 1);
Matrix<Scalar, 3, 3> Q_i = Q_mat.block(0, 0, 3, 3);
Matrix<Scalar, 3, 1> q_i = Q_mat.block(0, 3, 3, 1);
Matrix<Scalar, 3, 1> p_i = Q_mat.block(3, 0, 1, 3).transpose();
// Get rotation and translation matrices using SVD
Matrix<Scalar, 3, 3> SVD_i = Q_i - q_i * p_i.transpose();
JacobiSVD<Matrix<Scalar, 3, 3>> svd;
svd.compute(SVD_i, ComputeFullU | ComputeFullV);
Matrix<Scalar, 3, 3> R_i = svd.matrixU() * svd.matrixV().transpose();
Matrix<Scalar, 3, 1> t_i = q_i - R_i * p_i;
// Gamma final transformation matrix
Matrix<Scalar, 3, 4> Gamma_i(3, 4);
Gamma_i.block(0, 0, 3, 3) = R_i;
Gamma_i.block(0, 3, 3, 1) = t_i;
// Final deformed position
Matrix<Scalar, 4, 1> v_i = V_homogeneous.row(i);
U.row(i) = Gamma_i * v_i;
}
}
template <
typename DerivedV,
typename DerivedF,
typename DerivedW,
typename DerivedOmega>
IGL_INLINE void igl::direct_delta_mush_precomputation(
const Eigen::MatrixBase<DerivedV> & V,
const Eigen::MatrixBase<DerivedF> & F,
const Eigen::MatrixBase<DerivedW> & W,
const int p,
const typename DerivedV::Scalar lambda,
const typename DerivedV::Scalar kappa,
const typename DerivedV::Scalar alpha,
Eigen::PlainObjectBase<DerivedOmega> & Omega)
{
using namespace Eigen;
// Shape checks
assert(V.cols() == 3 && "V should contain 3D positions.");
assert(F.cols() == 3 && "F should contain triangles.");
assert(W.rows() == V.rows() && "W.rows() should be equal to V.rows().");
// Parameter checks
assert(p > 0 && "Laplacian iteration p should be positive.");
assert(lambda > 0 && "lambda should be positive.");
assert(kappa > 0 && kappa < lambda && "kappa should be positive and less than lambda.");
assert(alpha >= 0 && alpha < 1 && "alpha should be non-negative and less than 1.");
typedef typename DerivedV::Scalar Scalar;
// lambda helper
// Given a square matrix, extract the upper triangle (including diagonal) to an array.
// E.g. 1 2 3 4
// 5 6 7 8 -> [1, 2, 3, 4, 6, 7, 8, 11, 12, 16]
// 9 10 11 12 0 1 2 3 4 5 6 7 8 9
// 13 14 15 16
auto extract_upper_triangle = [](
const Matrix<Scalar, Dynamic, Dynamic> & full) -> Matrix<Scalar, Dynamic, 1>
{
int dims = full.rows();
Matrix<Scalar, Dynamic, 1> upper_triangle((dims * (dims + 1)) / 2);
int vector_idx = 0;
for (int i = 0; i < dims; ++i)
{
for (int j = i; j < dims; ++j)
{
upper_triangle(vector_idx) = full(i, j);
vector_idx++;
}
}
return upper_triangle;
};
const int n = V.rows();
const int m = W.cols();
// V_homogeneous: #V by 4, homogeneous version of V
// Note:
// in the paper, the rest pose vertices are represented in U \in R^{4 \times #V}
// Thus the formulae involving U would differ from the paper by a transpose.
Matrix<Scalar, Dynamic, 4> V_homogeneous(n, 4);
V_homogeneous << V, Matrix<Scalar, Dynamic, 1>::Ones(n);
// Identity matrix of #V by #V
SparseMatrix<Scalar> I(n, n);
I.setIdentity();
// Laplacian matrix of #V by #V
// L_bar = L \times D_L^{-1}
SparseMatrix<Scalar> L;
igl::cotmatrix(V, F, L);
L = -L;
// Inverse of diagonal matrix = reciprocal elements in diagonal
Matrix<Scalar, Dynamic, 1> D_L = L.diagonal();
// D_L = D_L.array().pow(-1); // Not using this since not sure if diagonal contains 0
for (int i = 0; i < D_L.size(); ++i)
{
if (D_L(i) != 0)
{
D_L(i) = 1 / D_L(i);
}
}
SparseMatrix<Scalar> D_L_inv = D_L.asDiagonal().toDenseMatrix().sparseView();
SparseMatrix<Scalar> L_bar = L * D_L_inv;
// Implicitly and iteratively solve for W'
// w'_{ij} = \sum_{k=1}^{n}{C_{ki} w_{kj}} where C = (I + kappa L_bar)^{-p}:
// W' = C^T \times W => c^T W_k = W_{k-1} where c = (I + kappa L_bar)
// C positive semi-definite => ldlt solver
SimplicialLDLT<SparseMatrix<Scalar>> ldlt_W_prime;
SparseMatrix<Scalar> c(I + kappa * L_bar);
// working copy
PlainMatrix<DerivedW,Dynamic,Dynamic> W_prime(W);
ldlt_W_prime.compute(c.transpose());
for (int iter = 0; iter < p; ++iter)
{
W_prime = ldlt_W_prime.solve(W_prime);
}
// U_precomputed: #V by 10
// Cache u_i^T \dot u_i \in R^{4 x 4} to reduce computation time.
Matrix<Scalar, Dynamic, 10> U_precomputed(n, 10);
for (int k = 0; k < n; ++k)
{
Matrix<Scalar, 4, 4> u_full = V_homogeneous.row(k).transpose() * V_homogeneous.row(k);
U_precomputed.row(k) = extract_upper_triangle(u_full);
}
// U_prime: #V by #T*10 of u_{jx}
// Each column of U_prime (u_{jx}) is the element-wise product of
// W_j and U_precomputed_x where j \in {1...m}, x \in {1...10}
Matrix<Scalar, Dynamic, Dynamic> U_prime(n, m * 10);
for (int j = 0; j < m; ++j)
{
Matrix<Scalar, Dynamic, 1> w_j = W.col(j);
for (int x = 0; x < 10; ++x)
{
Matrix<Scalar, Dynamic, 1> u_x = U_precomputed.col(x);
U_prime.col(10 * j + x) = w_j.array() * u_x.array();
}
}
// Implicitly and iteratively solve for Psi: #V by #T*10 of \Psi_{ij}s.
// Note: Using dense matrices to solve for Psi will cause the program to hang.
// The following won't work
// Matrix<Scalar, Dynamic, Dynamic> Psi(U_prime);
// Matrix<Scalar, Dynamic, Dynamic> b((I + lambda * L_bar).transpose());
// for (int iter = 0; iter < p; ++iter)
// {
// Psi = b.ldlt().solve(Psi); // hangs here
// }
// Convert to sparse matrices and compute
Matrix<Scalar, Dynamic, Dynamic> Psi = U_prime.sparseView();
SparseMatrix<Scalar> b = (I + lambda * L_bar).transpose();
SimplicialLDLT<SparseMatrix<Scalar>> ldlt_Psi;
ldlt_Psi.compute(b);
for (int iter = 0; iter < p; ++iter)
{
Psi = ldlt_Psi.solve(Psi);
}
// P: #V by 10 precomputed upper triangle of
// p_i p_i^T , p_i
// p_i^T , 1
// where p_i = (\sum_{j=1}^{n} Psi_{ij})'s top right 3 by 1 column
Matrix<Scalar, Dynamic, 10> P(n, 10);
for (int i = 0; i < n; ++i)
{
Matrix<Scalar, 3, 1> p_i = Matrix<Scalar, 3, 1>::Zero(3);
Scalar last = 0;
for (int j = 0; j < m; ++j)
{
Matrix<Scalar, 3, 1> p_i_curr(3);
p_i_curr << Psi(i, j * 10 + 3), Psi(i, j * 10 + 6), Psi(i, j * 10 + 8);
p_i += p_i_curr;
last += Psi(i, j * 10 + 9);
}
p_i /= last; // normalize
Matrix<Scalar, 4, 4> p_matrix(4, 4);
p_matrix.block(0, 0, 3, 3) = p_i * p_i.transpose();
p_matrix.block(0, 3, 3, 1) = p_i;
p_matrix.block(3, 0, 1, 3) = p_i.transpose();
p_matrix(3, 3) = 1;
P.row(i) = extract_upper_triangle(p_matrix);
}
// Omega
Omega.resize(n, m * 10);
for (int i = 0; i < n; ++i)
{
Matrix<Scalar, 10, 1> p_vector = P.row(i);
for (int j = 0; j < m; ++j)
{
Matrix<Scalar, 10, 1> Omega_curr(10);
Matrix<Scalar, 10, 1> Psi_curr = Psi.block(i, j * 10, 1, 10).transpose();
Omega_curr = (1. - alpha) * Psi_curr + alpha * W_prime(i, j) * p_vector;
Omega.block(i, j * 10, 1, 10) = Omega_curr.transpose();
}
}
}
#ifdef IGL_STATIC_LIBRARY
// Explicit template instantiation
template void igl::direct_delta_mush<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, std::vector<Eigen::Transform<double, 3, 2, 0>, Eigen::aligned_allocator<Eigen::Transform<double, 3, 2, 0> > > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&); template void igl::direct_delta_mush_precomputation<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<double, -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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
#endif