polar_dec.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. typedef typename DerivedA::Scalar Scalar;
  35. const Scalar th = std::sqrt(Eigen::NumTraits<Scalar>::dummy_precision());
  36. Eigen::SelfAdjointEigenSolver<DerivedA> eig;
  37. feclearexcept(FE_UNDERFLOW);
  38. eig.computeDirect(A.transpose()*A);
  39. if(fetestexcept(FE_UNDERFLOW) || eig.eigenvalues()(0)/eig.eigenvalues()(2)<th)
  40. {
  41. cout<<"resorting to svd 1..."<<endl;
  42. return polar_svd(A,R,T,U,S,V);
  43. }
  44. S = eig.eigenvalues().cwiseSqrt();
  45. T = eig.eigenvectors() * S.asDiagonal() * eig.eigenvectors().transpose();
  46. U = A * eig.eigenvectors();
  47. V = eig.eigenvectors();
  48. R = U * S.asDiagonal().inverse() * V.transpose();
  49. S = S.reverse().eval();
  50. V = V.rowwise().reverse().eval();
  51. U = U.rowwise().reverse().eval() * S.asDiagonal().inverse();
  52. if(std::fabs(R.squaredNorm()-3.) > th)
  53. {
  54. cout<<"resorting to svd 2..."<<endl;
  55. return polar_svd(A,R,T,U,S,V);
  56. }
  57. }
  58. template <
  59. typename DerivedA,
  60. typename DerivedR,
  61. typename DerivedT>
  62. IGL_INLINE void igl::polar_dec(
  63. const Eigen::PlainObjectBase<DerivedA> & A,
  64. Eigen::PlainObjectBase<DerivedR> & R,
  65. Eigen::PlainObjectBase<DerivedT> & T)
  66. {
  67. Eigen::PlainObjectBase<DerivedA> U;
  68. Eigen::PlainObjectBase<DerivedA> V;
  69. Eigen::Matrix<typename DerivedA::Scalar,DerivedA::RowsAtCompileTime,1> S;
  70. return igl::polar_dec(A,R,T,U,S,V);
  71. }
  72. #ifndef IGL_HEADER_ONLY
  73. // Explicit template instanciation
  74. 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> >&);
  75. #endif