min_quad_dense.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "min_quad_dense.h"
  2. #include <Eigen/Core>
  3. #include <Eigen/LU>
  4. #include "EPS.h"
  5. #include <cstdio>
  6. template <typename T>
  7. IGL_INLINE void igl::min_quad_dense_precompute(
  8. const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& A,
  9. const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& Aeq,
  10. const bool use_lu_decomposition,
  11. Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& S)
  12. {
  13. typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> Mat;
  14. // This threshold seems to matter a lot but I'm not sure how to
  15. // set it
  16. const T treshold = igl::FLOAT_EPS;
  17. //const T treshold = igl::DOUBLE_EPS;
  18. const int n = A.rows();
  19. assert(A.cols() == n);
  20. const int m = Aeq.rows();
  21. assert(Aeq.cols() == n);
  22. // Lagrange multipliers method:
  23. Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> LM(n + m, n + m);
  24. LM.block(0, 0, n, n) = A;
  25. LM.block(0, n, n, m) = Aeq.transpose();
  26. LM.block(n, 0, m, n) = Aeq;
  27. LM.block(n, n, m, m).setZero();
  28. Mat LMpinv;
  29. if(use_lu_decomposition)
  30. {
  31. // if LM is close to singular, use at your own risk :)
  32. LMpinv = LM.inverse();
  33. }else
  34. {
  35. // use SVD
  36. typedef Eigen::Matrix<T, Eigen::Dynamic, 1> Vec;
  37. Vec singValues;
  38. Eigen::JacobiSVD<Mat> svd;
  39. svd.compute(LM, Eigen::ComputeFullU | Eigen::ComputeFullV );
  40. const Mat& u = svd.matrixU();
  41. const Mat& v = svd.matrixV();
  42. const Vec& singVals = svd.singularValues();
  43. Vec pi_singVals(n + m);
  44. int zeroed = 0;
  45. for (int i=0; i<n + m; i++)
  46. {
  47. T sv = singVals(i, 0);
  48. assert(sv >= 0);
  49. // printf("sv: %lg ? %lg\n",(double) sv,(double)treshold);
  50. if (sv > treshold) pi_singVals(i, 0) = T(1) / sv;
  51. else
  52. {
  53. pi_singVals(i, 0) = T(0);
  54. zeroed++;
  55. }
  56. }
  57. printf("min_quad_dense_precompute: %i singular values zeroed (threshold = %e)\n", zeroed, treshold);
  58. Eigen::DiagonalMatrix<T, Eigen::Dynamic> pi_diag(pi_singVals);
  59. LMpinv = v * pi_diag * u.transpose();
  60. }
  61. S = LMpinv.block(0, 0, n, n + m);
  62. //// debug:
  63. //mlinit(&g_pEngine);
  64. //
  65. //mlsetmatrix(&g_pEngine, "A", A);
  66. //mlsetmatrix(&g_pEngine, "Aeq", Aeq);
  67. //mlsetmatrix(&g_pEngine, "LM", LM);
  68. //mlsetmatrix(&g_pEngine, "u", u);
  69. //mlsetmatrix(&g_pEngine, "v", v);
  70. //MatrixXd svMat = singVals;
  71. //mlsetmatrix(&g_pEngine, "singVals", svMat);
  72. //mlsetmatrix(&g_pEngine, "LMpinv", LMpinv);
  73. //mlsetmatrix(&g_pEngine, "S", S);
  74. //int hu = 1;
  75. }
  76. #ifndef IGL_HEADER_ONLY
  77. // Explicit template specialization
  78. template void igl::min_quad_dense_precompute<double>(Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, bool, Eigen::Matrix<double, -1, -1, 0, -1, -1>&);
  79. #endif