arap.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 "arap.h"
  9. #include <igl/colon.h>
  10. #include <igl/cotmatrix.h>
  11. #include <igl/massmatrix.h>
  12. #include <igl/group_sum_matrix.h>
  13. #include <igl/covariance_scatter_matrix.h>
  14. #include <igl/speye.h>
  15. #include <igl/mode.h>
  16. #include <igl/project_isometrically_to_plane.h>
  17. #include <igl/slice.h>
  18. #include <igl/arap_rhs.h>
  19. #include <igl/repdiag.h>
  20. #include <igl/columnize.h>
  21. #include <igl/matlab/MatlabWorkspace.h>
  22. #include "fit_rotations.h"
  23. #include <cassert>
  24. #include <iostream>
  25. template <
  26. typename DerivedV,
  27. typename DerivedF,
  28. typename Derivedb>
  29. IGL_INLINE bool igl::arap_precomputation(
  30. const Eigen::PlainObjectBase<DerivedV> & V,
  31. const Eigen::PlainObjectBase<DerivedF> & F,
  32. const int dim,
  33. const Eigen::PlainObjectBase<Derivedb> & b,
  34. ARAPData & data)
  35. {
  36. using namespace std;
  37. using namespace Eigen;
  38. typedef typename DerivedV::Scalar Scalar;
  39. // number of vertices
  40. const int n = V.rows();
  41. data.n = n;
  42. assert((b.size() == 0 || b.maxCoeff() < n) && "b out of bounds");
  43. assert((b.size() == 0 || b.minCoeff() >=0) && "b out of bounds");
  44. // remember b
  45. data.b = b;
  46. //assert(F.cols() == 3 && "For now only triangles");
  47. // dimension
  48. //const int dim = V.cols();
  49. assert((dim == 3 || dim ==2) && "dim should be 2 or 3");
  50. data.dim = dim;
  51. //assert(dim == 3 && "Only 3d supported");
  52. // Defaults
  53. data.f_ext = MatrixXd::Zero(n,data.dim);
  54. assert(data.dim <= V.cols() && "solve dim should be <= embedding");
  55. bool flat = (V.cols() - data.dim)==1;
  56. PlainObjectBase<DerivedV> plane_V;
  57. PlainObjectBase<DerivedF> plane_F;
  58. typedef SparseMatrix<Scalar> SparseMatrixS;
  59. SparseMatrixS ref_map,ref_map_dim;
  60. if(flat)
  61. {
  62. project_isometrically_to_plane(V,F,plane_V,plane_F,ref_map);
  63. repdiag(ref_map,dim,ref_map_dim);
  64. }
  65. const PlainObjectBase<DerivedV>& ref_V = (flat?plane_V:V);
  66. const PlainObjectBase<DerivedF>& ref_F = (flat?plane_F:F);
  67. SparseMatrixS L;
  68. cotmatrix(V,F,L);
  69. ARAPEnergyType eff_energy = data.energy;
  70. if(eff_energy == ARAP_ENERGY_TYPE_DEFAULT)
  71. {
  72. switch(F.cols())
  73. {
  74. case 3:
  75. if(data.dim == 3)
  76. {
  77. eff_energy = ARAP_ENERGY_TYPE_SPOKES_AND_RIMS;
  78. }else
  79. {
  80. eff_energy = ARAP_ENERGY_TYPE_ELEMENTS;
  81. }
  82. break;
  83. case 4:
  84. eff_energy = ARAP_ENERGY_TYPE_ELEMENTS;
  85. break;
  86. default:
  87. assert(false);
  88. }
  89. }
  90. // Get covariance scatter matrix, when applied collects the covariance
  91. // matrices used to fit rotations to during optimization
  92. covariance_scatter_matrix(ref_V,ref_F,eff_energy,data.CSM);
  93. if(flat)
  94. {
  95. data.CSM = (data.CSM * ref_map_dim.transpose()).eval();
  96. }
  97. assert(data.CSM.cols() == V.rows()*data.dim);
  98. // Get group sum scatter matrix, when applied sums all entries of the same
  99. // group according to G
  100. SparseMatrix<double> G_sum;
  101. if(data.G.size() == 0)
  102. {
  103. if(eff_energy == ARAP_ENERGY_TYPE_ELEMENTS)
  104. {
  105. speye(F.rows(),G_sum);
  106. }else
  107. {
  108. speye(n,G_sum);
  109. }
  110. }else
  111. {
  112. // groups are defined per vertex, convert to per face using mode
  113. if(eff_energy == ARAP_ENERGY_TYPE_ELEMENTS)
  114. {
  115. Eigen::Matrix<int,Eigen::Dynamic,1> GG;
  116. MatrixXi GF(F.rows(),F.cols());
  117. for(int j = 0;j<F.cols();j++)
  118. {
  119. Matrix<int,Eigen::Dynamic,1> GFj;
  120. slice(data.G,F.col(j),GFj);
  121. GF.col(j) = GFj;
  122. }
  123. mode<int>(GF,2,GG);
  124. data.G=GG;
  125. }
  126. //printf("group_sum_matrix()\n");
  127. group_sum_matrix(data.G,G_sum);
  128. }
  129. SparseMatrix<double> G_sum_dim;
  130. repdiag(G_sum,data.dim,G_sum_dim);
  131. assert(G_sum_dim.cols() == data.CSM.rows());
  132. data.CSM = (G_sum_dim * data.CSM).eval();
  133. arap_rhs(ref_V,ref_F,data.dim,eff_energy,data.K);
  134. if(flat)
  135. {
  136. data.K = (ref_map_dim * data.K).eval();
  137. }
  138. assert(data.K.rows() == data.n*data.dim);
  139. SparseMatrix<double> Q = (-L).eval();
  140. if(data.with_dynamics)
  141. {
  142. const double h = data.h;
  143. assert(h != 0);
  144. SparseMatrix<double> M;
  145. massmatrix(V,F,MASSMATRIX_TYPE_DEFAULT,data.M);
  146. SparseMatrix<double> DQ = 1./(h*h)*data.M;
  147. Q += DQ;
  148. // Dummy external forces
  149. data.f_ext = MatrixXd::Zero(n,data.dim);
  150. data.vel = MatrixXd::Zero(n,data.dim);
  151. }
  152. return min_quad_with_fixed_precompute(
  153. Q,b,SparseMatrix<double>(),true,data.solver_data);
  154. }
  155. template <
  156. typename Derivedbc,
  157. typename DerivedU>
  158. IGL_INLINE bool igl::arap_solve(
  159. const Eigen::PlainObjectBase<Derivedbc> & bc,
  160. ARAPData & data,
  161. Eigen::PlainObjectBase<DerivedU> & U)
  162. {
  163. using namespace igl;
  164. using namespace Eigen;
  165. using namespace std;
  166. assert(data.b.size() == bc.rows());
  167. if(bc.size() > 0)
  168. {
  169. assert(bc.cols() == data.dim && "bc.cols() match data.dim");
  170. }
  171. const int n = data.n;
  172. int iter = 0;
  173. if(U.size() == 0)
  174. {
  175. // terrible initial guess.. should at least copy input mesh
  176. #ifndef NDEBUG
  177. cerr<<"arap_solve: Using terrible initial guess for U. Try U = V."<<endl;
  178. #endif
  179. U = MatrixXd::Zero(data.n,data.dim);
  180. }else
  181. {
  182. assert(U.cols() == data.dim && "U.cols() match data.dim");
  183. }
  184. // changes each arap iteration
  185. MatrixXd U_prev = U;
  186. // doesn't change for fixed with_dynamics timestep
  187. MatrixXd U0;
  188. if(data.with_dynamics)
  189. {
  190. U0 = U_prev;
  191. }
  192. while(iter < data.max_iter)
  193. {
  194. U_prev = U;
  195. // enforce boundary conditions exactly
  196. for(int bi = 0;bi<bc.rows();bi++)
  197. {
  198. U.row(data.b(bi)) = bc.row(bi);
  199. }
  200. const auto & Udim = U.replicate(data.dim,1);
  201. assert(U.cols() == data.dim);
  202. // As if U.col(2) was 0
  203. MatrixXd S = data.CSM * Udim;
  204. // THIS NORMALIZATION IS IMPORTANT TO GET SINGLE PRECISION SVD CODE TO WORK
  205. // CORRECTLY.
  206. S /= S.array().abs().maxCoeff();
  207. const int Rdim = data.dim;
  208. MatrixXd R(Rdim,data.CSM.rows());
  209. if(R.rows() == 2)
  210. {
  211. fit_rotations_planar(S,R);
  212. }else
  213. {
  214. fit_rotations(S,true,R);
  215. //#ifdef __SSE__ // fit_rotations_SSE will convert to float if necessary
  216. // fit_rotations_SSE(S,R);
  217. //#else
  218. // fit_rotations(S,true,R);
  219. //#endif
  220. }
  221. //for(int k = 0;k<(data.CSM.rows()/dim);k++)
  222. //{
  223. // R.block(0,dim*k,dim,dim) = MatrixXd::Identity(dim,dim);
  224. //}
  225. // Number of rotations: #vertices or #elements
  226. int num_rots = data.K.cols()/Rdim/Rdim;
  227. // distribute group rotations to vertices in each group
  228. MatrixXd eff_R;
  229. if(data.G.size() == 0)
  230. {
  231. // copy...
  232. eff_R = R;
  233. }else
  234. {
  235. eff_R.resize(Rdim,num_rots*Rdim);
  236. for(int r = 0;r<num_rots;r++)
  237. {
  238. eff_R.block(0,Rdim*r,Rdim,Rdim) =
  239. R.block(0,Rdim*data.G(r),Rdim,Rdim);
  240. }
  241. }
  242. MatrixXd Dl;
  243. if(data.with_dynamics)
  244. {
  245. assert(data.M.rows() == n &&
  246. "No mass matrix. Call arap_precomputation if changing with_dynamics");
  247. const double h = data.h;
  248. assert(h != 0);
  249. //Dl = 1./(h*h*h)*M*(-2.*V0 + Vm1) - fext;
  250. // data.vel = (V0-Vm1)/h
  251. // h*data.vel = (V0-Vm1)
  252. // -h*data.vel = -V0+Vm1)
  253. // -V0-h*data.vel = -2V0+Vm1
  254. Dl = 1./(h*h)*data.M*(-U0 - h*data.vel) - data.f_ext;
  255. }
  256. VectorXd Rcol;
  257. columnize(eff_R,num_rots,2,Rcol);
  258. VectorXd Bcol = -data.K * Rcol;
  259. assert(Bcol.size() == data.n*data.dim);
  260. for(int c = 0;c<data.dim;c++)
  261. {
  262. VectorXd Uc,Bc,bcc,Beq;
  263. Bc = Bcol.block(c*n,0,n,1);
  264. if(data.with_dynamics)
  265. {
  266. Bc += Dl.col(c);
  267. }
  268. if(bc.size()>0)
  269. {
  270. bcc = bc.col(c);
  271. }
  272. min_quad_with_fixed_solve(
  273. data.solver_data,
  274. Bc,bcc,Beq,
  275. Uc);
  276. U.col(c) = Uc;
  277. }
  278. iter++;
  279. }
  280. if(data.with_dynamics)
  281. {
  282. // Keep track of velocity for next time
  283. data.vel = (U-U0)/data.h;
  284. }
  285. return true;
  286. }
  287. #ifdef IGL_STATIC_LIBRARY
  288. template bool igl::arap_solve<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&, igl::ARAPData&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  289. template bool igl::arap_precomputation<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, igl::ARAPData&);
  290. #endif