min_quad_with_fixed.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef IGL_MIN_QUAD_WITH_FIXED_H
  2. #define IGL_MIN_QUAD_WITH_FIXED_H
  3. #include "igl_inline.h"
  4. #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
  5. #include <Eigen/Core>
  6. #include <Eigen/Dense>
  7. #include <Eigen/Sparse>
  8. //#include <Eigen/SparseExtra>
  9. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  10. #include <iostream>
  11. #include <unsupported/Eigen/SparseExtra>
  12. namespace igl
  13. {
  14. template <typename T>
  15. struct min_quad_with_fixed_data;
  16. // MIN_QUAD_WITH_FIXED Minimize quadratic energy Z'*A*Z + Z'*B + C with
  17. // constraints that Z(known) = Y, optionally also subject to the constraints
  18. // Aeq*Z = Beq
  19. //
  20. // Templates:
  21. // T should be a eigen matrix primitive type like int or double
  22. // Inputs:
  23. // A n by n matrix of quadratic coefficients
  24. // B n by 1 column of linear coefficients
  25. // known list of indices to known rows in Z
  26. // Y list of fixed values corresponding to known rows in Z
  27. // Optional:
  28. // Aeq m by n list of linear equality constraint coefficients
  29. // Beq m by 1 list of linear equality constraint constant values
  30. // pd flag specifying whether A(unknown,unknown) is positive definite
  31. // Outputs:
  32. // data factorization struct with all necessary information to solve
  33. // using min_quad_with_fixed_solve
  34. // Returns true on success, false on error
  35. template <typename T>
  36. IGL_INLINE bool min_quad_with_fixed_precompute(
  37. const Eigen::SparseMatrix<T>& A,
  38. const Eigen::Matrix<int,Eigen::Dynamic,1> & known,
  39. const Eigen::SparseMatrix<T>& Aeq,
  40. const bool pd,
  41. min_quad_with_fixed_data<T> & data
  42. );
  43. // Solves a system previously factored using min_quad_with_fixed_precompute
  44. //
  45. // Template:
  46. // T type of sparse matrix (e.g. double)
  47. // DerivedY type of Y (e.g. derived from VectorXd or MatrixXd)
  48. // DerivedZ type of Z (e.g. derived from VectorXd or MatrixXd)
  49. // Inputs:
  50. // data factorization struct with all necessary precomputation to solve
  51. // Outputs:
  52. // Z n by cols solution
  53. // Returns true on success, false on error
  54. template <typename T,typename DerivedY,typename DerivedZ>
  55. IGL_INLINE bool min_quad_with_fixed_solve(
  56. const min_quad_with_fixed_data<T> & data,
  57. const Eigen::Matrix<T,Eigen::Dynamic,1> & B,
  58. const Eigen::PlainObjectBase<DerivedY> & Y,
  59. const Eigen::Matrix<T,Eigen::Dynamic,1> & Beq,
  60. Eigen::PlainObjectBase<DerivedZ> & Z);
  61. }
  62. template <typename T>
  63. struct igl::min_quad_with_fixed_data
  64. {
  65. // Size of original system: number of unknowns + number of knowns
  66. int n;
  67. // Whether A(unknown,unknown) is positive definite
  68. bool Auu_pd;
  69. // Whether A(unknown,unknown) is symmetric
  70. bool Auu_sym;
  71. // Indices of known variables
  72. Eigen::Matrix<int,Eigen::Dynamic,1> known;
  73. // Indices of unknown variables
  74. Eigen::Matrix<int,Eigen::Dynamic,1> unknown;
  75. // Indices of lagrange variables
  76. Eigen::Matrix<int,Eigen::Dynamic,1> lagrange;
  77. // Indices of unknown variable followed by Indices of lagrange variables
  78. Eigen::Matrix<int,Eigen::Dynamic,1> unknown_lagrange;
  79. // Matrix multiplied against Y when constructing right hand side
  80. Eigen::SparseMatrix<T> preY;
  81. // Tells whether system is sparse
  82. bool sparse;
  83. // Lower triangle of LU decomposition of final system matrix
  84. Eigen::SparseMatrix<T> L;
  85. // Upper triangle of LU decomposition of final system matrix
  86. Eigen::SparseMatrix<T> U;
  87. // Dense LU factorization
  88. Eigen::FullPivLU<Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> > lu;
  89. };
  90. #ifdef IGL_HEADER_ONLY
  91. # include "min_quad_with_fixed.cpp"
  92. #endif
  93. #endif