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

223 lines
8.3 KiB
C++

// This file is part of libigl, a simple c++ geometry processing library.
//
// Copyright (C) 2014 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/.
#ifndef IGL_MIN_QUAD_WITH_FIXED_H
#define IGL_MIN_QUAD_WITH_FIXED_H
#include "igl_inline.h"
#define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Sparse>
// Bug in unsupported/Eigen/SparseExtra needs iostream first
#include <iostream>
#include <unsupported/Eigen/SparseExtra>
namespace igl
{
template <typename T>
struct min_quad_with_fixed_data;
/// Minimize a convex quadratic energy subject to fixed value and linear
/// equality constraints. Problems of the form
///
/// trace( 0.5*Z'*A*Z + Z'*B + constant )
///
/// subject to
///
/// Z(known,:) = Y, and
/// Aeq*Z = Beq
///
/// @tparam T should be a eigen matrix primitive type like int or double
/// @param[in] A n by n matrix of quadratic coefficients
/// @param[in] known list of indices to known rows in Z
/// @param[in] Y list of fixed values corresponding to known rows in Z
/// @param[in] Aeq m by n list of linear equality constraint coefficients
/// @param[in] pd flag specifying whether A(unknown,unknown) is positive definite
/// @param[in,out] data factorization struct with all necessary information to solve
/// using min_quad_with_fixed_solve
/// @return true on success, false on error
///
/// \pre rows of Aeq **should probably** be linearly independent.
/// During precomputation, the rows of a Aeq are checked via QR. But in case
/// they're not then resulting probably will no longer be sparse: it will be
/// slow.
///
template <typename T, typename Derivedknown>
IGL_INLINE bool min_quad_with_fixed_precompute(
const Eigen::SparseMatrix<T>& A,
const Eigen::MatrixBase<Derivedknown> & known,
const Eigen::SparseMatrix<T>& Aeq,
const bool pd,
min_quad_with_fixed_data<T> & data
);
/// Solves a system previously factored using min_quad_with_fixed_precompute
///
/// @tparam T type of sparse matrix (e.g. double)
/// @tparam DerivedY type of Y (e.g. derived from VectorXd or MatrixXd)
/// @tparam DerivedZ type of Z (e.g. derived from VectorXd or MatrixXd)
/// @param[in] data factorization struct with all necessary precomputation to solve
/// @param[in] B n by k column of linear coefficients
/// @param[in] Y b by k list of constant fixed values
/// @param[in] Beq m by k list of linear equality constraint constant values
/// @param[out] Z n by k solution
/// @param[out] sol #unknowns+#lagrange by k solution to linear system
/// Returns true on success, false on error
template <
typename T,
typename DerivedB,
typename DerivedY,
typename DerivedBeq,
typename DerivedZ,
typename Derivedsol>
IGL_INLINE bool min_quad_with_fixed_solve(
const min_quad_with_fixed_data<T> & data,
const Eigen::MatrixBase<DerivedB> & B,
const Eigen::MatrixBase<DerivedY> & Y,
const Eigen::MatrixBase<DerivedBeq> & Beq,
Eigen::PlainObjectBase<DerivedZ> & Z,
Eigen::PlainObjectBase<Derivedsol> & sol);
/// \overload
template <
typename T,
typename DerivedB,
typename DerivedY,
typename DerivedBeq,
typename DerivedZ>
IGL_INLINE bool min_quad_with_fixed_solve(
const min_quad_with_fixed_data<T> & data,
const Eigen::MatrixBase<DerivedB> & B,
const Eigen::MatrixBase<DerivedY> & Y,
const Eigen::MatrixBase<DerivedBeq> & Beq,
Eigen::PlainObjectBase<DerivedZ> & Z);
/// \overload
/// \brief Minimize convex quadratic energy subject to fixed value and linear
/// equality constraints. Without prefactorization.
template <
typename T,
typename Derivedknown,
typename DerivedB,
typename DerivedY,
typename DerivedBeq,
typename DerivedZ>
IGL_INLINE bool min_quad_with_fixed(
const Eigen::SparseMatrix<T>& A,
const Eigen::MatrixBase<DerivedB> & B,
const Eigen::MatrixBase<Derivedknown> & known,
const Eigen::MatrixBase<DerivedY> & Y,
const Eigen::SparseMatrix<T>& Aeq,
const Eigen::MatrixBase<DerivedBeq> & Beq,
const bool pd,
Eigen::PlainObjectBase<DerivedZ> & Z);
/// Dense version optimized for very small, known at compile time sizes. Still
/// works for Eigen::Dynamic (and then everything needs to be Dynamic).
///
/// min_x ½ xᵀ H x + xᵀ f
/// subject to
/// A x = b
/// x(i) = bc(i) iff k(i)==true
///
/// @tparam Scalar (e.g., double)
/// @tparam n #H or Eigen::Dynamic if not known at compile time
/// @tparam m #A or Eigen::Dynamic if not known at compile time
/// @tparam Hpd whether H is positive definite (LLT used) or not (QR used)
/// @param[in] H #H by #H quadratic coefficients (only lower triangle used)
/// @param[in] f #H linear coefficients
/// @param[in] k #H list of flags whether to fix value
/// @param[in] bc #H value to fix to (if !k(i) then bc(i) is ignored)
/// @param[in] A #A by #H list of linear equality constraint coefficients, must be
/// linearly independent (with self and fixed value constraints)
/// @param[in] b #A list of linear equality right-hand sides
/// @return #H-long solution x
template <typename Scalar, int n, int m, bool Hpd=true>
IGL_INLINE Eigen::Matrix<Scalar,n,1> min_quad_with_fixed(
const Eigen::Matrix<Scalar,n,n> & H,
const Eigen::Matrix<Scalar,n,1> & f,
const Eigen::Array<bool,n,1> & k,
const Eigen::Matrix<Scalar,n,1> & bc,
const Eigen::Matrix<Scalar,m,n> & A,
const Eigen::Matrix<Scalar,m,1> & b);
/// \overload
template <typename Scalar, int n, bool Hpd=true>
IGL_INLINE Eigen::Matrix<Scalar,n,1> min_quad_with_fixed(
const Eigen::Matrix<Scalar,n,n> & H,
const Eigen::Matrix<Scalar,n,1> & f,
const Eigen::Array<bool,n,1> & k,
const Eigen::Matrix<Scalar,n,1> & bc);
/// \overload
///
/// \brief Special wrapper where the number of constrained values (i.e., true values
/// in k) is exposed as a template parameter. Not intended to be called
/// directly. The overhead of calling the overloads above is already minimal.
template <typename Scalar, int n, int kcount, bool Hpd/*no default*/>
IGL_INLINE Eigen::Matrix<Scalar,n,1> min_quad_with_fixed(
const Eigen::Matrix<Scalar,n,n> & H,
const Eigen::Matrix<Scalar,n,1> & f,
const Eigen::Array<bool,n,1> & k,
const Eigen::Matrix<Scalar,n,1> & bc);
}
/// Parameters and precomputed values for min_quad_with_fixed
template <typename T>
struct igl::min_quad_with_fixed_data
{
/// Size of original system: number of unknowns + number of knowns
int n;
/// Whether A(unknown,unknown) is positive definite
bool Auu_pd;
/// Whether A(unknown,unknown) is symmetric
bool Auu_sym;
/// Indices of known variables
Eigen::VectorXi known;
/// Indices of unknown variables
Eigen::VectorXi unknown;
/// Indices of lagrange variables
Eigen::VectorXi lagrange;
/// Indices of unknown variable followed by Indices of lagrange variables
Eigen::VectorXi unknown_lagrange;
/// Matrix multiplied against Y when constructing right hand side
Eigen::SparseMatrix<T> preY;
/// Type of solver used
enum SolverType
{
LLT = 0,
LDLT = 1,
LU = 2,
QR_LLT = 3,
NUM_SOLVER_TYPES = 4
} solver_type;
/// Solver data (factorization)
Eigen::SimplicialLLT <Eigen::SparseMatrix<T > > llt;
Eigen::SimplicialLDLT<Eigen::SparseMatrix<T > > ldlt;
Eigen::SparseLU<Eigen::SparseMatrix<T, Eigen::ColMajor>, Eigen::COLAMDOrdering<int> > lu;
/// QR factorization
/// Are rows of Aeq linearly independent?
bool Aeq_li;
/// Columns of Aeq corresponding to unknowns
int neq;
Eigen::SparseQR<Eigen::SparseMatrix<T>, Eigen::COLAMDOrdering<int> > AeqTQR;
Eigen::SparseMatrix<T> Aeqk;
Eigen::SparseMatrix<T> Aequ;
Eigen::SparseMatrix<T> Auu;
Eigen::SparseMatrix<T> AeqTQ1;
Eigen::SparseMatrix<T> AeqTQ1T;
Eigen::SparseMatrix<T> AeqTQ2;
Eigen::SparseMatrix<T> AeqTQ2T;
Eigen::SparseMatrix<T> AeqTR1;
Eigen::SparseMatrix<T> AeqTR1T;
Eigen::SparseMatrix<T> AeqTE;
Eigen::SparseMatrix<T> AeqTET;
/// @private Debug
Eigen::SparseMatrix<T> NA;
Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> NB;
};
#ifndef IGL_STATIC_LIBRARY
# include "min_quad_with_fixed.impl.h"
#endif
#endif