polar_dec.cpp 3.5 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 "polar_dec.h"
  9. #include "polar_svd.h"
  10. #ifdef _WIN32
  11. #else
  12. # include <fenv.h>
  13. #endif
  14. #include <cmath>
  15. #include <Eigen/Eigenvalues>
  16. #include <iostream>
  17. #include <cfenv>
  18. // From Olga's CGAL mentee's ARAP code
  19. template <
  20. typename DerivedA,
  21. typename DerivedR,
  22. typename DerivedT,
  23. typename DerivedU,
  24. typename DerivedS,
  25. typename DerivedV>
  26. IGL_INLINE void igl::polar_dec(
  27. const Eigen::PlainObjectBase<DerivedA> & A,
  28. Eigen::PlainObjectBase<DerivedR> & R,
  29. Eigen::PlainObjectBase<DerivedT> & T,
  30. Eigen::PlainObjectBase<DerivedU> & U,
  31. Eigen::PlainObjectBase<DerivedS> & S,
  32. Eigen::PlainObjectBase<DerivedV> & V)
  33. {
  34. using namespace std;
  35. using namespace Eigen;
  36. typedef typename DerivedA::Scalar Scalar;
  37. const Scalar th = std::sqrt(Eigen::NumTraits<Scalar>::dummy_precision());
  38. Eigen::SelfAdjointEigenSolver<DerivedA> eig;
  39. feclearexcept(FE_UNDERFLOW);
  40. eig.computeDirect(A.transpose()*A);
  41. if(fetestexcept(FE_UNDERFLOW) || eig.eigenvalues()(0)/eig.eigenvalues()(2)<th)
  42. {
  43. cout<<"resorting to svd 1..."<<endl;
  44. return polar_svd(A,R,T,U,S,V);
  45. }
  46. S = eig.eigenvalues().cwiseSqrt();
  47. V = eig.eigenvectors();
  48. U = A * V;
  49. R = U * S.asDiagonal().inverse() * V.transpose();
  50. T = V * S.asDiagonal() * V.transpose();
  51. S = S.reverse().eval();
  52. V = V.rowwise().reverse().eval();
  53. U = U.rowwise().reverse().eval() * S.asDiagonal().inverse();
  54. if(R.determinant() < 0)
  55. {
  56. // Annoyingly the .eval() is necessary
  57. auto W = V.eval();
  58. const auto & SVT = S.asDiagonal() * V.adjoint();
  59. W.col(V.cols()-1) *= -1.;
  60. R = U*W.transpose();
  61. T = W*SVT;
  62. }
  63. if(std::fabs(R.squaredNorm()-3.) > th)
  64. {
  65. cout<<"resorting to svd 2..."<<endl;
  66. return polar_svd(A,R,T,U,S,V);
  67. }
  68. }
  69. template <
  70. typename DerivedA,
  71. typename DerivedR,
  72. typename DerivedT>
  73. IGL_INLINE void igl::polar_dec(
  74. const Eigen::PlainObjectBase<DerivedA> & A,
  75. Eigen::PlainObjectBase<DerivedR> & R,
  76. Eigen::PlainObjectBase<DerivedT> & T)
  77. {
  78. DerivedA U;
  79. DerivedA V;
  80. Eigen::Matrix<typename DerivedA::Scalar,DerivedA::RowsAtCompileTime,1> S;
  81. return igl::polar_dec(A,R,T,U,S,V);
  82. }
  83. #ifdef IGL_STATIC_LIBRARY
  84. // Explicit template specialization
  85. template void igl::polar_dec<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  86. template void igl::polar_dec<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 2, 2, 0, 2, 2>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 2, 2, 0, 2, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  87. #endif