arap.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/group_sum_matrix.h>
  12. #include <igl/covariance_scatter_matrix.h>
  13. #include <igl/speye.h>
  14. #include <igl/mode.h>
  15. #include <igl/slice.h>
  16. #include <igl/arap_rhs.h>
  17. #include <igl/repdiag.h>
  18. #include <igl/columnize.h>
  19. #include "fit_rotations.h"
  20. #include <cassert>
  21. #include <iostream>
  22. template <
  23. typename DerivedV,
  24. typename DerivedF,
  25. typename Derivedb>
  26. IGL_INLINE bool igl::arap_precomputation(
  27. const Eigen::PlainObjectBase<DerivedV> & V,
  28. const Eigen::PlainObjectBase<DerivedF> & F,
  29. const Eigen::PlainObjectBase<Derivedb> & b,
  30. ARAPData & data)
  31. {
  32. using namespace igl;
  33. using namespace Eigen;
  34. typedef typename DerivedV::Scalar Scalar;
  35. // number of vertices
  36. const int n = V.rows();
  37. data.n = n;
  38. assert((b.size() == 0 || b.maxCoeff() < n) && "b out of bounds");
  39. assert((b.size() == 0 || b.minCoeff() >=0) && "b out of bounds");
  40. // remember b
  41. data.b = b;
  42. //assert(F.cols() == 3 && "For now only triangles");
  43. // dimension
  44. const int dim = V.cols();
  45. assert(dim == 3 && "Only 3d supported");
  46. // Defaults
  47. data.f_ext = Eigen::MatrixXd::Zero(n,dim);
  48. SparseMatrix<Scalar> L;
  49. cotmatrix(V,F,L);
  50. ARAPEnergyType eff_energy = data.energy;
  51. if(eff_energy == ARAP_ENERGY_TYPE_DEFAULT)
  52. {
  53. switch(F.cols())
  54. {
  55. case 3:
  56. if(dim == 3)
  57. {
  58. eff_energy = ARAP_ENERGY_TYPE_SPOKES_AND_RIMS;
  59. }else
  60. {
  61. eff_energy = ARAP_ENERGY_TYPE_ELEMENTS;
  62. }
  63. break;
  64. case 4:
  65. eff_energy = ARAP_ENERGY_TYPE_ELEMENTS;
  66. break;
  67. default:
  68. assert(false);
  69. }
  70. }
  71. // Get covariance scatter matrix, when applied collects the covariance
  72. // matrices used to fit rotations to during optimization
  73. covariance_scatter_matrix(V,F,eff_energy,data.CSM);
  74. // Get group sum scatter matrix, when applied sums all entries of the same
  75. // group according to G
  76. SparseMatrix<double> G_sum;
  77. if(data.G.size() == 0)
  78. {
  79. if(eff_energy == ARAP_ENERGY_TYPE_ELEMENTS)
  80. {
  81. speye(F.rows(),G_sum);
  82. }else
  83. {
  84. speye(n,G_sum);
  85. }
  86. }else
  87. {
  88. // groups are defined per vertex, convert to per face using mode
  89. if(eff_energy == ARAP_ENERGY_TYPE_ELEMENTS)
  90. {
  91. Eigen::Matrix<int,Eigen::Dynamic,1> GG;
  92. MatrixXi GF(F.rows(),F.cols());
  93. for(int j = 0;j<F.cols();j++)
  94. {
  95. Matrix<int,Eigen::Dynamic,1> GFj;
  96. slice(data.G,F.col(j),GFj);
  97. GF.col(j) = GFj;
  98. }
  99. mode<int>(GF,2,GG);
  100. data.G=GG;
  101. }
  102. //printf("group_sum_matrix()\n");
  103. group_sum_matrix(data.G,G_sum);
  104. }
  105. SparseMatrix<double> G_sum_dim;
  106. repdiag(G_sum,dim,G_sum_dim);
  107. assert(G_sum_dim.cols() == data.CSM.rows());
  108. data.CSM = (G_sum_dim * data.CSM).eval();
  109. arap_rhs(V,F,eff_energy,data.K);
  110. SparseMatrix<double> Q = (-0.5*L).eval();
  111. return min_quad_with_fixed_precompute(
  112. Q,b,SparseMatrix<double>(),true,data.solver_data);
  113. }
  114. template <
  115. typename Derivedbc,
  116. typename DerivedU>
  117. IGL_INLINE bool igl::arap_solve(
  118. const Eigen::PlainObjectBase<Derivedbc> & bc,
  119. ARAPData & data,
  120. Eigen::PlainObjectBase<DerivedU> & U)
  121. {
  122. using namespace igl;
  123. using namespace Eigen;
  124. using namespace std;
  125. assert(data.b.size() == bc.rows());
  126. const int dim = bc.cols();
  127. const int n = data.n;
  128. int iter = 0;
  129. if(U.size() == 0)
  130. {
  131. // terrible initial guess.. should at least copy input mesh
  132. U = MatrixXd::Zero(data.n,dim);
  133. }
  134. MatrixXd U_prev = U;
  135. while(iter < data.max_iter)
  136. {
  137. U_prev = U;
  138. // enforce boundary conditions exactly
  139. for(int bi = 0;bi<bc.rows();bi++)
  140. {
  141. U.row(data.b(bi)) = bc.row(bi);
  142. }
  143. MatrixXd S = data.CSM * U.replicate(dim,1);
  144. MatrixXd R(dim,data.CSM.rows());
  145. #ifdef __SSE__ // fit_rotations_SSE will convert to float if necessary
  146. fit_rotations_SSE(S,R);
  147. #else
  148. fit_rotations(S,R);
  149. #endif
  150. //for(int k = 0;k<(data.CSM.rows()/dim);k++)
  151. //{
  152. // R.block(0,dim*k,dim,dim) = MatrixXd::Identity(dim,dim);
  153. //}
  154. // Number of rotations: #vertices or #elements
  155. int num_rots = data.K.cols()/dim/dim;
  156. // distribute group rotations to vertices in each group
  157. MatrixXd eff_R;
  158. if(data.G.size() == 0)
  159. {
  160. // copy...
  161. eff_R = R;
  162. }else
  163. {
  164. eff_R.resize(dim,num_rots*dim);
  165. for(int r = 0;r<num_rots;r++)
  166. {
  167. eff_R.block(0,dim*r,dim,dim) =
  168. R.block(0,dim*data.G(r),dim,dim);
  169. }
  170. }
  171. VectorXd Rcol;
  172. columnize(eff_R,num_rots,2,Rcol);
  173. VectorXd Bcol = -data.K * Rcol;
  174. for(int c = 0;c<dim;c++)
  175. {
  176. VectorXd Uc,Bc,bcc,Beq;
  177. Bc = Bcol.block(c*n,0,n,1);
  178. bcc = bc.col(c);
  179. min_quad_with_fixed_solve(
  180. data.solver_data,
  181. Bc,bcc,Beq,
  182. Uc);
  183. U.col(c) = Uc;
  184. }
  185. iter++;
  186. }
  187. return true;
  188. }
  189. #ifndef IGL_HEADER_ONLY
  190. 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&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, igl::ARAPData&);
  191. 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> >&);
  192. #endif