arap.cpp 7.8 KB

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