rigid_alignment.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2019 Alec Jacobson <[email protected]>
  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 "rigid_alignment.h"
  9. #include "PlainMatrix.h"
  10. #include <Eigen/Sparse>
  11. #include <Eigen/QR>
  12. // Not currently used. See below.
  13. //#include <Eigen/Cholesky>
  14. #include <vector>
  15. #include <iostream>
  16. template <
  17. typename DerivedX,
  18. typename DerivedP,
  19. typename DerivedN,
  20. typename DerivedR,
  21. typename Derivedt
  22. >
  23. IGL_INLINE void igl::rigid_alignment(
  24. const Eigen::MatrixBase<DerivedX> & _X,
  25. const Eigen::MatrixBase<DerivedP> & P,
  26. const Eigen::MatrixBase<DerivedN> & N,
  27. Eigen::PlainObjectBase<DerivedR> & R,
  28. Eigen::PlainObjectBase<Derivedt> & t)
  29. {
  30. typedef typename DerivedX::Scalar Scalar;
  31. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> MatrixXS;
  32. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1> VectorXS;
  33. typedef Eigen::Matrix<Scalar,3,3> Matrix3S;
  34. const int k = _X.rows();
  35. VectorXS Z = VectorXS::Zero(k,1);
  36. VectorXS I = VectorXS::Ones(k,1);
  37. PlainMatrix<DerivedX> X = _X;
  38. R = DerivedR::Identity(3,3);
  39. t = Derivedt::Zero(1,3);
  40. // See gptoolbox, each iter could be O(1) instead of O(k)
  41. const int max_iters = 5;
  42. // Weight on point-to-point regularization.
  43. Scalar w = 1e-5;
  44. for(int iters = 0;iters<max_iters;iters++)
  45. {
  46. MatrixXS A(k*3,6);
  47. A <<
  48. Z, X.col(2),-X.col(1),I,Z,Z,
  49. -X.col(2), Z, X.col(0),Z,I,Z,
  50. X.col(1),-X.col(0), Z,Z,Z,I;
  51. VectorXS B(k*3,1);
  52. B<<
  53. P.col(0)-X.col(0),
  54. P.col(1)-X.col(1),
  55. P.col(2)-X.col(2);
  56. std::vector<Eigen::Triplet<Scalar> > NNIJV;
  57. for(int i = 0;i<k;i++)
  58. {
  59. for(int c = 0;c<3;c++)
  60. {
  61. NNIJV.emplace_back(i,i+k*c,N(i,c));
  62. }
  63. }
  64. Eigen::SparseMatrix<Scalar> NN(k,k*3);
  65. NN.setFromTriplets(NNIJV.begin(),NNIJV.end());
  66. MatrixXS NA = (NN * A).eval();
  67. VectorXS NB = (NN * B).eval();
  68. MatrixXS Q = (1.0-w)*(NA.transpose() * NA) + w * A.transpose() * A;
  69. VectorXS f = (1.0-w)*(NA.transpose() * NB) + w * A.transpose() * B;
  70. // This could be a lot faster but isn't rank revealing and may produce wonky
  71. // results when P and N are all the same point and vector.
  72. //VectorXS u = (Q).ldlt().solve(f);
  73. Eigen::CompleteOrthogonalDecomposition<decltype(Q)> qr(Q);
  74. if(qr.rank() < 6)
  75. {
  76. w = 1.0-(1.0-w)/2.0;
  77. }
  78. VectorXS u = qr.solve(f);
  79. Derivedt ti = u.tail(3).transpose();
  80. Matrix3S W;
  81. W<<
  82. 0, u(2),-u(1),
  83. -u(2), 0, u(0),
  84. u(1),-u(0), 0;
  85. // strayed from a perfect rotation. Correct it.
  86. const double x = u.head(3).stableNorm();
  87. DerivedR Ri;
  88. if(x == 0)
  89. {
  90. Ri = DerivedR::Identity(3,3);
  91. }else
  92. {
  93. Ri =
  94. DerivedR::Identity(3,3) +
  95. sin(x)/x*W +
  96. (1.0-cos(x))/(x*x)*W*W;
  97. }
  98. R = (R*Ri).eval();
  99. t = (t*Ri + ti).eval();
  100. X = ((_X*R).rowwise()+t).eval();
  101. }
  102. }
  103. #ifdef IGL_STATIC_LIBRARY
  104. // Explicit template instantiation
  105. template void igl::rigid_alignment<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, 3, 3, 0, 3, 3>, Eigen::Matrix<double, 1, 3, 1, 1, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3, 0, 3, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&);
  106. #endif