fit_rotations.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "fit_rotations.h"
  9. #include "polar_svd3x3.h"
  10. #include "repmat.h"
  11. #include "verbose.h"
  12. #include "polar_dec.h"
  13. #include "polar_svd.h"
  14. #include "C_STR.h"
  15. #include <iostream>
  16. template <typename DerivedS, typename DerivedD>
  17. IGL_INLINE void igl::fit_rotations(
  18. const Eigen::PlainObjectBase<DerivedS> & S,
  19. const bool single_precision,
  20. Eigen::PlainObjectBase<DerivedD> & R)
  21. {
  22. using namespace std;
  23. const int dim = S.cols();
  24. const int nr = S.rows()/dim;
  25. assert(nr * dim == S.rows());
  26. assert(dim == 3);
  27. // resize output
  28. R.resize(dim,dim*nr); // hopefully no op (should be already allocated)
  29. //std::cout<<"S=["<<std::endl<<S<<std::endl<<"];"<<std::endl;
  30. //MatrixXd si(dim,dim);
  31. Eigen::Matrix<typename DerivedS::Scalar,3,3> si;// = Eigen::Matrix3d::Identity();
  32. // loop over number of rotations we're computing
  33. for(int r = 0;r<nr;r++)
  34. {
  35. // build this covariance matrix
  36. for(int i = 0;i<dim;i++)
  37. {
  38. for(int j = 0;j<dim;j++)
  39. {
  40. si(i,j) = S(i*nr+r,j);
  41. }
  42. }
  43. typedef Eigen::Matrix<typename DerivedD::Scalar,3,3> Mat3;
  44. typedef Eigen::Matrix<typename DerivedD::Scalar,3,1> Vec3;
  45. Mat3 ri;
  46. if(single_precision)
  47. {
  48. polar_svd3x3(si, ri);
  49. }else
  50. {
  51. Mat3 ti,ui,vi;
  52. Vec3 _;
  53. igl::polar_svd(si,ri,ti,ui,_,vi);
  54. }
  55. assert(ri.determinant() >= 0);
  56. R.block(0,r*dim,dim,dim) = ri.block(0,0,dim,dim).transpose();
  57. //cout<<matlab_format(si,C_STR("si_"<<r))<<endl;
  58. //cout<<matlab_format(ri.transpose().eval(),C_STR("ri_"<<r))<<endl;
  59. }
  60. }
  61. template <typename DerivedS, typename DerivedD>
  62. IGL_INLINE void igl::fit_rotations_planar(
  63. const Eigen::PlainObjectBase<DerivedS> & S,
  64. Eigen::PlainObjectBase<DerivedD> & R)
  65. {
  66. using namespace std;
  67. const int dim = S.cols();
  68. const int nr = S.rows()/dim;
  69. //assert(dim == 2 && "_planar input should be 2D");
  70. assert(nr * dim == S.rows());
  71. // resize output
  72. R.resize(dim,dim*nr); // hopefully no op (should be already allocated)
  73. Eigen::Matrix<typename DerivedS::Scalar,2,2> si;
  74. // loop over number of rotations we're computing
  75. for(int r = 0;r<nr;r++)
  76. {
  77. // build this covariance matrix
  78. for(int i = 0;i<2;i++)
  79. {
  80. for(int j = 0;j<2;j++)
  81. {
  82. si(i,j) = S(i*nr+r,j);
  83. }
  84. }
  85. typedef Eigen::Matrix<typename DerivedD::Scalar,2,2> Mat2;
  86. typedef Eigen::Matrix<typename DerivedD::Scalar,2,1> Vec2;
  87. Mat2 ri,ti,ui,vi;
  88. Vec2 _;
  89. igl::polar_svd(si,ri,ti,ui,_,vi);
  90. #ifndef FIT_ROTATIONS_ALLOW_FLIPS
  91. // Check for reflection
  92. if(ri.determinant() < 0)
  93. {
  94. vi.col(1) *= -1.;
  95. ri = ui * vi.transpose();
  96. }
  97. assert(ri.determinant() >= 0);
  98. #endif
  99. // Not sure why polar_dec computes transpose...
  100. R.block(0,r*dim,dim,dim).setIdentity();
  101. R.block(0,r*dim,2,2) = ri.transpose();
  102. }
  103. }
  104. #ifdef __SSE__
  105. IGL_INLINE void igl::fit_rotations_SSE(
  106. const Eigen::MatrixXf & S,
  107. Eigen::MatrixXf & R)
  108. {
  109. const int cStep = 4;
  110. assert(S.cols() == 3);
  111. const int dim = 3; //S.cols();
  112. const int nr = S.rows()/dim;
  113. assert(nr * dim == S.rows());
  114. // resize output
  115. R.resize(dim,dim*nr); // hopefully no op (should be already allocated)
  116. Eigen::Matrix<float, 3*cStep, 3> siBig;
  117. // using SSE decompose cStep matrices at a time:
  118. int r = 0;
  119. for( ; r<nr; r+=cStep)
  120. {
  121. int numMats = cStep;
  122. if (r + cStep >= nr) numMats = nr - r;
  123. // build siBig:
  124. for (int k=0; k<numMats; k++)
  125. {
  126. for(int i = 0;i<dim;i++)
  127. {
  128. for(int j = 0;j<dim;j++)
  129. {
  130. siBig(i + 3*k, j) = S(i*nr + r + k, j);
  131. }
  132. }
  133. }
  134. Eigen::Matrix<float, 3*cStep, 3> ri;
  135. polar_svd3x3_sse(siBig, ri);
  136. for (int k=0; k<cStep; k++)
  137. assert(ri.block(3*k, 0, 3, 3).determinant() >= 0);
  138. // Not sure why polar_dec computes transpose...
  139. for (int k=0; k<numMats; k++)
  140. {
  141. R.block(0, (r + k)*dim, dim, dim) = ri.block(3*k, 0, dim, dim).transpose();
  142. }
  143. }
  144. }
  145. IGL_INLINE void igl::fit_rotations_SSE(
  146. const Eigen::MatrixXd & S,
  147. Eigen::MatrixXd & R)
  148. {
  149. const Eigen::MatrixXf Sf = S.cast<float>();
  150. Eigen::MatrixXf Rf;
  151. fit_rotations_SSE(Sf,Rf);
  152. R = Rf.cast<double>();
  153. }
  154. #endif
  155. #ifdef __AVX__
  156. IGL_INLINE void igl::fit_rotations_AVX(
  157. const Eigen::MatrixXf & S,
  158. Eigen::MatrixXf & R)
  159. {
  160. const int cStep = 8;
  161. assert(S.cols() == 3);
  162. const int dim = 3; //S.cols();
  163. const int nr = S.rows()/dim;
  164. assert(nr * dim == S.rows());
  165. // resize output
  166. R.resize(dim,dim*nr); // hopefully no op (should be already allocated)
  167. Eigen::Matrix<float, 3*cStep, 3> siBig;
  168. // using SSE decompose cStep matrices at a time:
  169. int r = 0;
  170. for( ; r<nr; r+=cStep)
  171. {
  172. int numMats = cStep;
  173. if (r + cStep >= nr) numMats = nr - r;
  174. // build siBig:
  175. for (int k=0; k<numMats; k++)
  176. {
  177. for(int i = 0;i<dim;i++)
  178. {
  179. for(int j = 0;j<dim;j++)
  180. {
  181. siBig(i + 3*k, j) = S(i*nr + r + k, j);
  182. }
  183. }
  184. }
  185. Eigen::Matrix<float, 3*cStep, 3> ri;
  186. polar_svd3x3_avx(siBig, ri);
  187. for (int k=0; k<cStep; k++)
  188. assert(ri.block(3*k, 0, 3, 3).determinant() >= 0);
  189. // Not sure why polar_dec computes transpose...
  190. for (int k=0; k<numMats; k++)
  191. {
  192. R.block(0, (r + k)*dim, dim, dim) = ri.block(3*k, 0, dim, dim).transpose();
  193. }
  194. }
  195. }
  196. #endif
  197. #ifdef IGL_STATIC_LIBRARY
  198. // Explicit template instantiation
  199. template void igl::fit_rotations<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&, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  200. template void igl::fit_rotations_planar<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> >&);
  201. template void igl::fit_rotations_planar<Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&);
  202. template void igl::fit_rotations<Eigen::Matrix<float,-1,-1,0,-1,-1>,Eigen::Matrix<float,-1,-1,0,-1,-1> >(Eigen::PlainObjectBase<Eigen::Matrix<float,-1,-1,0,-1,-1> > const &,bool,Eigen::PlainObjectBase<Eigen::Matrix<float,-1,-1,0,-1,-1> > &);
  203. #endif