min_quad_with_fixed.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // Inputs:
  45. // data factorization struct with all necessary precomputation to solve
  46. // Outputs:
  47. // Z n by cols solution
  48. // Returns true on success, false on error
  49. template <typename T>
  50. IGL_INLINE bool min_quad_with_fixed_solve(
  51. const min_quad_with_fixed_data<T> & data,
  52. const Eigen::Matrix<T,Eigen::Dynamic,1> & B,
  53. const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & Y,
  54. const Eigen::Matrix<T,Eigen::Dynamic,1> & Beq,
  55. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & Z);
  56. }
  57. template <typename T>
  58. struct igl::min_quad_with_fixed_data
  59. {
  60. // Size of original system: number of unknowns + number of knowns
  61. int n;
  62. // Whether A(unknown,unknown) is positive definite
  63. bool Auu_pd;
  64. // Whether A(unknown,unknown) is symmetric
  65. bool Auu_sym;
  66. // Indices of known variables
  67. Eigen::Matrix<int,Eigen::Dynamic,1> known;
  68. // Indices of unknown variables
  69. Eigen::Matrix<int,Eigen::Dynamic,1> unknown;
  70. // Indices of lagrange variables
  71. Eigen::Matrix<int,Eigen::Dynamic,1> lagrange;
  72. // Indices of unknown variable followed by Indices of lagrange variables
  73. Eigen::Matrix<int,Eigen::Dynamic,1> unknown_lagrange;
  74. // Matrix multiplied against Y when constructing right hand side
  75. Eigen::SparseMatrix<T> preY;
  76. // Tells whether system is sparse
  77. bool sparse;
  78. // Lower triangle of LU decomposition of final system matrix
  79. Eigen::SparseMatrix<T> L;
  80. // Upper triangle of LU decomposition of final system matrix
  81. Eigen::SparseMatrix<T> U;
  82. // Dense LU factorization
  83. Eigen::FullPivLU<Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> > lu;
  84. };
  85. #ifdef IGL_HEADER_ONLY
  86. # include "min_quad_with_fixed.cpp"
  87. #endif
  88. #endif