arap.cpp 7.4 KB

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