arap.cpp 8.3 KB

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