polar_svd.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_svd.h"
  9. #include <Eigen/SVD>
  10. // Adapted from Olga's CGAL mentee's ARAP code
  11. template <
  12. typename DerivedA,
  13. typename DerivedR,
  14. typename DerivedT>
  15. IGL_INLINE void igl::polar_svd(
  16. const Eigen::PlainObjectBase<DerivedA> & A,
  17. Eigen::PlainObjectBase<DerivedR> & R,
  18. Eigen::PlainObjectBase<DerivedT> & T)
  19. {
  20. Eigen::PlainObjectBase<DerivedA> U;
  21. Eigen::PlainObjectBase<DerivedA> V;
  22. Eigen::Matrix<typename DerivedA::Scalar,DerivedA::RowsAtCompileTime,1> S;
  23. return igl::polar_svd(A,R,T,U,S,V);
  24. }
  25. template <
  26. typename DerivedA,
  27. typename DerivedR,
  28. typename DerivedT,
  29. typename DerivedU,
  30. typename DerivedS,
  31. typename DerivedV>
  32. IGL_INLINE void igl::polar_svd(
  33. const Eigen::PlainObjectBase<DerivedA> & A,
  34. Eigen::PlainObjectBase<DerivedR> & R,
  35. Eigen::PlainObjectBase<DerivedT> & T,
  36. Eigen::PlainObjectBase<DerivedU> & U,
  37. Eigen::PlainObjectBase<DerivedS> & S,
  38. Eigen::PlainObjectBase<DerivedV> & V)
  39. {
  40. Eigen::JacobiSVD<DerivedA> svd;
  41. svd.compute(A, Eigen::ComputeFullU | Eigen::ComputeFullV );
  42. U = svd.matrixU();
  43. V = svd.matrixV();
  44. S = svd.singularValues();
  45. R = U*V.transpose();
  46. T = V*S.asDiagonal()*V.adjoint();
  47. }
  48. #ifndef IGL_HEADER_ONLY
  49. // Explicit template instanciation
  50. template void igl::polar_svd<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> >&);
  51. template void igl::polar_svd<Eigen::Matrix<double, 2, 2, 0, 2, 2>, Eigen::Matrix<double, 2, 2, 0, 2, 2>, Eigen::Matrix<double, 2, 2, 0, 2, 2> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 2, 2, 0, 2, 2> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 2, 2, 0, 2, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 2, 2, 0, 2, 2> >&);
  52. template void igl::polar_svd<Eigen::Matrix<double, 3, 3, 0, 3, 3>, Eigen::Matrix<double, 3, 3, 0, 3, 3>, Eigen::Matrix<double, 3, 3, 0, 3, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3, 0, 3, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3, 0, 3, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3, 0, 3, 3> >&);
  53. template void igl::polar_svd<Eigen::Matrix<float, 2, 2, 0, 2, 2>, Eigen::Matrix<float, 2, 2, 0, 2, 2>, Eigen::Matrix<float, 2, 2, 0, 2, 2> >(Eigen::PlainObjectBase<Eigen::Matrix<float, 2, 2, 0, 2, 2> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, 2, 2, 0, 2, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<float, 2, 2, 0, 2, 2> >&);
  54. #endif