procrustes.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Stefan Brugger <stefanbrugger@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. #ifndef IGL_PROCRUSTES_H
  9. #define IGL_PROCRUSTES_H
  10. #include "igl_inline.h"
  11. #include <Eigen/Dense>
  12. #include <Eigen/Geometry>
  13. namespace igl
  14. {
  15. // Solve Procrustes problem in d dimensions.
  16. // Given two point sets X,Y in R^d find best scale s, orthogonal R and translation t
  17. // s.t. |s*X*R + t - Y|^2 is minimized.
  18. //
  19. // Example:
  20. // MatrixXd X, Y; (containing 3d points as rows)
  21. // double scale;
  22. // MatrixXd R;
  23. // VectorXd t;
  24. // igl::procrustes(X,Y,true,false,scale,R,t);
  25. // R *= scale;
  26. // MatrixXd Xprime = (X * R).rowwise() + t.transpose();
  27. //
  28. // Templates:
  29. // DerivedV point type
  30. // Scalar scalar type
  31. // DerivedR type of R
  32. // DerivedT type of t
  33. // Inputs:
  34. // X #V by DIM first list of points
  35. // Y #V by DIM second list of points
  36. // includeScaling if scaling should be allowed
  37. // includeReflections if R is allowed to be a reflection
  38. // Outputs:
  39. // scale scaling
  40. // R orthogonal matrix
  41. // t translation
  42. template <typename DerivedV, typename Scalar, typename DerivedR, typename DerivedT>
  43. IGL_INLINE void procrustes(
  44. const Eigen::PlainObjectBase<DerivedV>& X,
  45. const Eigen::PlainObjectBase<DerivedV>& Y,
  46. bool includeScaling,
  47. bool includeReflections,
  48. Scalar& scale,
  49. Eigen::PlainObjectBase<DerivedR>& R,
  50. Eigen::PlainObjectBase<DerivedT>& t);
  51. // Same as above but returns Eigen transformation object.
  52. //
  53. // Example:
  54. // MatrixXd X, Y; (containing 3d points as rows)
  55. // AffineCompact3d T;
  56. // igl::procrustes(X,Y,true,false,T);
  57. // MatrixXd Xprime = (X * T.linear()).rowwise() + T.translation().transpose();
  58. // Templates:
  59. // DerivedV point type
  60. // Scalar scalar type
  61. // DIM point dimension
  62. // TType type of transformation (Isometry,Affine,AffineCompact,Projective)
  63. // Inputs:
  64. // X #V by DIM first list of points
  65. // Y #V by DIM second list of points
  66. // includeScaling if scaling should be allowed
  67. // includeReflections if R is allowed to be a reflection
  68. // Outputs:
  69. // T transformation that minimizes error
  70. template <typename DerivedV, typename Scalar, int DIM, int TType>
  71. IGL_INLINE void procrustes(
  72. const Eigen::PlainObjectBase<DerivedV>& X,
  73. const Eigen::PlainObjectBase<DerivedV>& Y,
  74. bool includeScaling,
  75. bool includeReflections,
  76. Eigen::Transform<Scalar,DIM,TType>& T);
  77. // Convenient wrapper that returns S=scale*R instead of scale and R separately
  78. template <typename DerivedV, typename DerivedR, typename DerivedT>
  79. IGL_INLINE void procrustes(
  80. const Eigen::PlainObjectBase<DerivedV>& X,
  81. const Eigen::PlainObjectBase<DerivedV>& Y,
  82. bool includeScaling,
  83. bool includeReflections,
  84. Eigen::PlainObjectBase<DerivedR>& S,
  85. Eigen::PlainObjectBase<DerivedT>& t);
  86. // Convenient wrapper for rigid case (no scaling, no reflections)
  87. template <typename DerivedV, typename DerivedR, typename DerivedT>
  88. IGL_INLINE void procrustes(
  89. const Eigen::PlainObjectBase<DerivedV>& X,
  90. const Eigen::PlainObjectBase<DerivedV>& Y,
  91. Eigen::PlainObjectBase<DerivedR>& R,
  92. Eigen::PlainObjectBase<DerivedT>& t);
  93. // Convenient wrapper for 2D case.
  94. template <typename DerivedV, typename Scalar, typename DerivedT>
  95. IGL_INLINE void procrustes(
  96. const Eigen::PlainObjectBase<DerivedV>& X,
  97. const Eigen::PlainObjectBase<DerivedV>& Y,
  98. Eigen::Rotation2D<Scalar>& R,
  99. Eigen::PlainObjectBase<DerivedT>& t);
  100. }
  101. #ifndef IGL_STATIC_LIBRARY
  102. #include "procrustes.cpp"
  103. #endif
  104. #endif