polar_dec.cpp 3.5 KB

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