min_quad_dense.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "min_quad_dense.h"
  9. #include <Eigen/Core>
  10. #include <Eigen/LU>
  11. #include "EPS.h"
  12. #include <cstdio>
  13. template <typename T>
  14. IGL_INLINE void igl::min_quad_dense_precompute(
  15. const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& A,
  16. const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& Aeq,
  17. const bool use_lu_decomposition,
  18. Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& S)
  19. {
  20. typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> Mat;
  21. // This threshold seems to matter a lot but I'm not sure how to
  22. // set it
  23. const T treshold = igl::FLOAT_EPS;
  24. //const T treshold = igl::DOUBLE_EPS;
  25. const int n = A.rows();
  26. assert(A.cols() == n);
  27. const int m = Aeq.rows();
  28. assert(Aeq.cols() == n);
  29. // Lagrange multipliers method:
  30. Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> LM(n + m, n + m);
  31. LM.block(0, 0, n, n) = A;
  32. LM.block(0, n, n, m) = Aeq.transpose();
  33. LM.block(n, 0, m, n) = Aeq;
  34. LM.block(n, n, m, m).setZero();
  35. Mat LMpinv;
  36. if(use_lu_decomposition)
  37. {
  38. // if LM is close to singular, use at your own risk :)
  39. LMpinv = LM.inverse();
  40. }else
  41. {
  42. // use SVD
  43. typedef Eigen::Matrix<T, Eigen::Dynamic, 1> Vec;
  44. Vec singValues;
  45. Eigen::JacobiSVD<Mat> svd;
  46. svd.compute(LM, Eigen::ComputeFullU | Eigen::ComputeFullV );
  47. const Mat& u = svd.matrixU();
  48. const Mat& v = svd.matrixV();
  49. const Vec& singVals = svd.singularValues();
  50. Vec pi_singVals(n + m);
  51. int zeroed = 0;
  52. for (int i=0; i<n + m; i++)
  53. {
  54. T sv = singVals(i, 0);
  55. assert(sv >= 0);
  56. // printf("sv: %lg ? %lg\n",(double) sv,(double)treshold);
  57. if (sv > treshold) pi_singVals(i, 0) = T(1) / sv;
  58. else
  59. {
  60. pi_singVals(i, 0) = T(0);
  61. zeroed++;
  62. }
  63. }
  64. printf("min_quad_dense_precompute: %i singular values zeroed (threshold = %e)\n", zeroed, treshold);
  65. Eigen::DiagonalMatrix<T, Eigen::Dynamic> pi_diag(pi_singVals);
  66. LMpinv = v * pi_diag * u.transpose();
  67. }
  68. S = LMpinv.block(0, 0, n, n + m);
  69. //// debug:
  70. //mlinit(&g_pEngine);
  71. //
  72. //mlsetmatrix(&g_pEngine, "A", A);
  73. //mlsetmatrix(&g_pEngine, "Aeq", Aeq);
  74. //mlsetmatrix(&g_pEngine, "LM", LM);
  75. //mlsetmatrix(&g_pEngine, "u", u);
  76. //mlsetmatrix(&g_pEngine, "v", v);
  77. //MatrixXd svMat = singVals;
  78. //mlsetmatrix(&g_pEngine, "singVals", svMat);
  79. //mlsetmatrix(&g_pEngine, "LMpinv", LMpinv);
  80. //mlsetmatrix(&g_pEngine, "S", S);
  81. //int hu = 1;
  82. }
  83. #ifdef IGL_STATIC_LIBRARY
  84. // Explicit template specialization
  85. 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>&);
  86. #endif