frame_field_deformer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>
  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 "frame_field_deformer.h"
  9. #include <Eigen/Dense>
  10. #include <Eigen/Sparse>
  11. #include <vector>
  12. #include "cotmatrix_entries.h"
  13. #include "cotmatrix.h"
  14. #include "vertex_triangle_adjacency.h"
  15. namespace igl
  16. {
  17. class Frame_field_deformer
  18. {
  19. public:
  20. IGL_INLINE Frame_field_deformer();
  21. IGL_INLINE ~Frame_field_deformer();
  22. // Initialize the optimizer
  23. IGL_INLINE void init(const Eigen::MatrixXd& _V, const Eigen::MatrixXi& _F, const Eigen::MatrixXd& _D1, const Eigen::MatrixXd& _D2, double _Lambda, double _perturb_rotations, int _fixed = 1);
  24. // Run N optimization steps
  25. IGL_INLINE void optimize(int N, bool reset = false);
  26. // Reset optimization
  27. IGL_INLINE void reset_opt();
  28. // Precomputation of all components
  29. IGL_INLINE void precompute_opt();
  30. // Precomputation for deformation energy
  31. IGL_INLINE void precompute_ARAP(Eigen::SparseMatrix<double> & Lff, Eigen::MatrixXd & LfcVc);
  32. // Precomputation for regularization
  33. IGL_INLINE void precompute_SMOOTH(Eigen::SparseMatrix<double> & MS, Eigen::MatrixXd & bS);
  34. // extracts a r x c block from sparse matrix mat into sparse matrix m1
  35. // (r0,c0) is upper left entry of block
  36. IGL_INLINE void extractBlock(Eigen::SparseMatrix<double> & mat, int r0, int c0, int r, int c, Eigen::SparseMatrix<double> & m1);
  37. // computes optimal rotations for faces of m wrt current coords in mw.V
  38. // returns a 3x3 matrix
  39. IGL_INLINE void compute_optimal_rotations();
  40. // global optimization step - linear system
  41. IGL_INLINE void compute_optimal_positions();
  42. // compute the output XField from deformation gradient
  43. IGL_INLINE void computeXField(std::vector< Eigen::Matrix<double,3,2> > & XF);
  44. // computes in WW the ideal warp at each tri to make the frame field a cross
  45. IGL_INLINE void compute_idealWarp(std::vector< Eigen::Matrix<double,3,3> > & WW);
  46. // -------------------------------- Variables ----------------------------------------------------
  47. // Mesh I/O:
  48. Eigen::MatrixXd V; // Original mesh - vertices
  49. Eigen::MatrixXi F; // Original mesh - faces
  50. std::vector<std::vector<int> > VT; // Vertex to triangle topology
  51. std::vector<std::vector<int> > VTi; // Vertex to triangle topology
  52. Eigen::MatrixXd V_w; // Warped mesh - vertices
  53. std::vector< Eigen::Matrix<double,3,2> > FF; // frame field FF in 3D (parallel to m.F)
  54. std::vector< Eigen::Matrix<double,3,3> > WW; // warping matrices to make a cross field (parallel to m.F)
  55. std::vector< Eigen::Matrix<double,3,2> > XF; // pseudo-cross field from solution (parallel to m.F)
  56. int fixed;
  57. double perturb_rotations; // perturbation to rotation matrices
  58. // Numerics
  59. int nfree,nconst; // number of free/constrained vertices in the mesh - default all-but-1/1
  60. Eigen::MatrixXd C; // cotangent matrix of m
  61. Eigen::SparseMatrix<double> L; // Laplacian matrix of m
  62. Eigen::SparseMatrix<double> M; // matrix for global optimization - pre-conditioned
  63. Eigen::MatrixXd RHS; // pre-computed part of known term in global optimization
  64. std::vector< Eigen::Matrix<double,3,3> > RW; // optimal rotation-warping matrices (parallel to m.F) -- INCORPORATES WW
  65. Eigen::SimplicialCholesky<Eigen::SparseMatrix<double> > solver; // solver for linear system in global opt.
  66. // Parameters
  67. private:
  68. double Lambda = 0.1; // weight of energy regularization
  69. };
  70. IGL_INLINE Frame_field_deformer::Frame_field_deformer() {}
  71. IGL_INLINE Frame_field_deformer::~Frame_field_deformer() {}
  72. IGL_INLINE void Frame_field_deformer::init(const Eigen::MatrixXd& _V,
  73. const Eigen::MatrixXi& _F,
  74. const Eigen::MatrixXd& _D1,
  75. const Eigen::MatrixXd& _D2,
  76. double _Lambda,
  77. double _perturb_rotations,
  78. int _fixed)
  79. {
  80. V = _V;
  81. F = _F;
  82. assert(_D1.rows() == _D2.rows());
  83. FF.clear();
  84. for (unsigned i=0; i < _D1.rows(); ++i)
  85. {
  86. Eigen::Matrix<double,3,2> ff;
  87. ff.col(0) = _D1.row(i);
  88. ff.col(1) = _D2.row(i);
  89. FF.push_back(ff);
  90. }
  91. fixed = _fixed;
  92. Lambda = _Lambda;
  93. perturb_rotations = _perturb_rotations;
  94. reset_opt();
  95. precompute_opt();
  96. }
  97. IGL_INLINE void Frame_field_deformer::optimize(int N, bool reset)
  98. {
  99. //Reset optimization
  100. if (reset)
  101. reset_opt();
  102. // Iterative Local/Global optimization
  103. for (int i=0; i<N;i++)
  104. {
  105. compute_optimal_rotations();
  106. compute_optimal_positions();
  107. computeXField(XF);
  108. }
  109. }
  110. IGL_INLINE void Frame_field_deformer::reset_opt()
  111. {
  112. V_w = V;
  113. for (unsigned i=0; i<V_w.rows(); ++i)
  114. for (unsigned j=0; j<V_w.cols(); ++j)
  115. V_w(i,j) += (double(rand())/double(RAND_MAX))*10e-4*perturb_rotations;
  116. }
  117. // precomputation of all components
  118. IGL_INLINE void Frame_field_deformer::precompute_opt()
  119. {
  120. nfree = V.rows() - fixed; // free vertices (at the beginning ov m.V) - global
  121. nconst = V.rows()-nfree; // #constrained vertices
  122. igl::vertex_triangle_adjacency(V,F,VT,VTi); // compute vertex to face relationship
  123. igl::cotmatrix_entries(V,F,C); // cotangent matrix for opt. rotations - global
  124. igl::cotmatrix(V,F,L);
  125. Eigen::SparseMatrix<double> MA; // internal matrix for ARAP-warping energy
  126. Eigen::MatrixXd LfcVc; // RHS (partial) for ARAP-warping energy
  127. Eigen::SparseMatrix<double> MS; // internal matrix for smoothing energy
  128. Eigen::MatrixXd bS; // RHS (full) for smoothing energy
  129. precompute_ARAP(MA,LfcVc); // precompute terms for the ARAP-warp part
  130. precompute_SMOOTH(MS,bS); // precompute terms for the smoothing part
  131. compute_idealWarp(WW); // computes the ideal warps
  132. RW.resize(F.rows()); // init rotation matrices - global
  133. M = (1-Lambda)*MA + Lambda*MS; // matrix for linear system - global
  134. RHS = (1-Lambda)*LfcVc + Lambda*bS; // RHS (partial) for linear system - global
  135. solver.compute(M); // system pre-conditioning
  136. if (solver.info()!=Eigen::Success) {fprintf(stderr,"Decomposition failed in pre-conditioning!\n"); exit(-1);}
  137. fprintf(stdout,"Preconditioning done.\n");
  138. }
  139. IGL_INLINE void Frame_field_deformer::precompute_ARAP(Eigen::SparseMatrix<double> & Lff, Eigen::MatrixXd & LfcVc)
  140. {
  141. fprintf(stdout,"Precomputing ARAP terms\n");
  142. Eigen::SparseMatrix<double> LL = -4*L;
  143. Lff = Eigen::SparseMatrix<double>(nfree,nfree);
  144. extractBlock(LL,0,0,nfree,nfree,Lff);
  145. Eigen::SparseMatrix<double> Lfc = Eigen::SparseMatrix<double>(nfree,nconst);
  146. extractBlock(LL,0,nfree,nfree,nconst,Lfc);
  147. LfcVc = - Lfc * V_w.block(nfree,0,nconst,3);
  148. }
  149. IGL_INLINE void Frame_field_deformer::precompute_SMOOTH(Eigen::SparseMatrix<double> & MS, Eigen::MatrixXd & bS)
  150. {
  151. fprintf(stdout,"Precomputing SMOOTH terms\n");
  152. Eigen::SparseMatrix<double> LL = 4*L*L;
  153. // top-left
  154. MS = Eigen::SparseMatrix<double>(nfree,nfree);
  155. extractBlock(LL,0,0,nfree,nfree,MS);
  156. // top-right
  157. Eigen::SparseMatrix<double> Mfc = Eigen::SparseMatrix<double>(nfree,nconst);
  158. extractBlock(LL,0,nfree,nfree,nconst,Mfc);
  159. Eigen::MatrixXd MfcVc = Mfc * V_w.block(nfree,0,nconst,3);
  160. bS = (LL*V).block(0,0,nfree,3)-MfcVc;
  161. }
  162. IGL_INLINE void Frame_field_deformer::extractBlock(Eigen::SparseMatrix<double> & mat, int r0, int c0, int r, int c, Eigen::SparseMatrix<double> & m1)
  163. {
  164. std::vector<Eigen::Triplet<double> > tripletList;
  165. for (int k=c0; k<c0+c; ++k)
  166. for (Eigen::SparseMatrix<double>::InnerIterator it(mat,k); it; ++it)
  167. {
  168. if (it.row()>=r0 && it.row()<r0+r)
  169. tripletList.push_back(Eigen::Triplet<double>(it.row()-r0,it.col()-c0,it.value()));
  170. }
  171. m1.setFromTriplets(tripletList.begin(), tripletList.end());
  172. }
  173. IGL_INLINE void Frame_field_deformer::compute_optimal_rotations()
  174. {
  175. Eigen::Matrix<double,3,3> r,S,P,PP,D;
  176. for (int i=0;i<F.rows();i++)
  177. {
  178. // input tri --- could be done once and saved in a matrix
  179. P.col(0) = (V.row(F(i,1))-V.row(F(i,0))).transpose();
  180. P.col(1) = (V.row(F(i,2))-V.row(F(i,1))).transpose();
  181. P.col(2) = (V.row(F(i,0))-V.row(F(i,2))).transpose();
  182. P = WW[i] * P; // apply ideal warp
  183. // current tri
  184. PP.col(0) = (V_w.row(F(i,1))-V_w.row(F(i,0))).transpose();
  185. PP.col(1) = (V_w.row(F(i,2))-V_w.row(F(i,1))).transpose();
  186. PP.col(2) = (V_w.row(F(i,0))-V_w.row(F(i,2))).transpose();
  187. // cotangents
  188. D << C(i,2), 0, 0,
  189. 0, C(i,0), 0,
  190. 0, 0, C(i,1);
  191. S = PP*D*P.transpose();
  192. Eigen::JacobiSVD<Eigen::Matrix<double,3,3> > svd(S, Eigen::ComputeFullU | Eigen::ComputeFullV );
  193. Eigen::Matrix<double,3,3> su = svd.matrixU();
  194. Eigen::Matrix<double,3,3> sv = svd.matrixV();
  195. r = su*sv.transpose();
  196. if (r.determinant()<0) // correct reflections
  197. {
  198. su(0,2)=-su(0,2); su(1,2)=-su(1,2); su(2,2)=-su(2,2);
  199. r = su*sv.transpose();
  200. }
  201. RW[i] = r*WW[i]; // RW INCORPORATES IDEAL WARP WW!!!
  202. }
  203. }
  204. IGL_INLINE void Frame_field_deformer::compute_optimal_positions()
  205. {
  206. // compute variable RHS of ARAP-warp part of the system
  207. Eigen::MatrixXd b(nfree,3); // fx3 known term of the system
  208. Eigen::MatrixXd X; // result
  209. int t; // triangles incident to edge (i,j)
  210. int vi,i1,i2; // index of vertex i wrt tri t0
  211. for (int i=0;i<nfree;i++)
  212. {
  213. b.row(i) << 0.0, 0.0, 0.0;
  214. for (int k=0;k<(int)VT[i].size();k++) // for all incident triangles
  215. {
  216. t = VT[i][k]; // incident tri
  217. vi = (i==F(t,0))?0:(i==F(t,1))?1:(i==F(t,2))?2:3; // index of i in t
  218. assert(vi!=3);
  219. i1 = F(t,(vi+1)%3);
  220. i2 = F(t,(vi+2)%3);
  221. b.row(i)+=(C(t,(vi+2)%3)*RW[t]*(V.row(i1)-V.row(i)).transpose()).transpose();
  222. b.row(i)+=(C(t,(vi+1)%3)*RW[t]*(V.row(i2)-V.row(i)).transpose()).transpose();
  223. }
  224. }
  225. b/=2.0;
  226. b=-4*b;
  227. b*=(1-Lambda); // blend
  228. b+=RHS; // complete known term
  229. X = solver.solve(b);
  230. if (solver.info()!=Eigen::Success) {printf("Solving linear system failed!\n"); return;}
  231. // copy result to mw.V
  232. for (int i=0;i<nfree;i++)
  233. V_w.row(i)=X.row(i);
  234. }
  235. IGL_INLINE void Frame_field_deformer::computeXField(std::vector< Eigen::Matrix<double,3,2> > & XF)
  236. {
  237. Eigen::Matrix<double,3,3> P,PP,DG;
  238. XF.resize(F.rows());
  239. for (int i=0;i<F.rows();i++)
  240. {
  241. int i0,i1,i2;
  242. // indexes of vertices of face i
  243. i0 = F(i,0); i1 = F(i,1); i2 = F(i,2);
  244. // input frame
  245. P.col(0) = (V.row(i1)-V.row(i0)).transpose();
  246. P.col(1) = (V.row(i2)-V.row(i0)).transpose();
  247. P.col(2) = P.col(0).cross(P.col(1));
  248. // output triangle brought to origin
  249. PP.col(0) = (V_w.row(i1)-V_w.row(i0)).transpose();
  250. PP.col(1) = (V_w.row(i2)-V_w.row(i0)).transpose();
  251. PP.col(2) = PP.col(0).cross(PP.col(1));
  252. // deformation gradient
  253. DG = PP * P.inverse();
  254. XF[i] = DG * FF[i];
  255. }
  256. }
  257. // computes in WW the ideal warp at each tri to make the frame field a cross
  258. IGL_INLINE void Frame_field_deformer::compute_idealWarp(std::vector< Eigen::Matrix<double,3,3> > & WW)
  259. {
  260. WW.resize(F.rows());
  261. for (int i=0;i<(int)FF.size();i++)
  262. {
  263. Eigen::Vector3d v0,v1,v2;
  264. v0 = FF[i].col(0);
  265. v1 = FF[i].col(1);
  266. v2=v0.cross(v1); v2.normalize(); // normal
  267. Eigen::Matrix3d A,AI; // compute affine map A that brings:
  268. A << v0[0], v1[0], v2[0], // first vector of FF to x unary vector
  269. v0[1], v1[1], v2[1], // second vector of FF to xy plane
  270. v0[2], v1[2], v2[2]; // triangle normal to z unary vector
  271. AI = A.inverse();
  272. // polar decomposition to discard rotational component (unnecessary but makes it easier)
  273. Eigen::JacobiSVD<Eigen::Matrix<double,3,3> > svd(AI, Eigen::ComputeFullU | Eigen::ComputeFullV );
  274. //Matrix<double,3,3> au = svd.matrixU();
  275. Eigen::Matrix<double,3,3> av = svd.matrixV();
  276. Eigen::DiagonalMatrix<double,3> as(svd.singularValues());
  277. WW[i] = av*as*av.transpose();
  278. }
  279. }
  280. }
  281. IGL_INLINE void igl::frame_field_deformer(
  282. const Eigen::MatrixXd& V,
  283. const Eigen::MatrixXi& F,
  284. const Eigen::MatrixXd& FF1,
  285. const Eigen::MatrixXd& FF2,
  286. Eigen::MatrixXd& V_d,
  287. Eigen::MatrixXd& FF1_d,
  288. Eigen::MatrixXd& FF2_d,
  289. const int iterations,
  290. const double lambda,
  291. const bool perturb_initial_guess)
  292. {
  293. // Solvers
  294. Frame_field_deformer deformer;
  295. // Init optimizer
  296. deformer.init(V, F, FF1, FF2, lambda, perturb_initial_guess ? 0.1 : 0);
  297. // Optimize
  298. deformer.optimize(iterations,true);
  299. // Copy positions
  300. V_d = deformer.V_w;
  301. // Allocate
  302. FF1_d.resize(F.rows(),3);
  303. FF2_d.resize(F.rows(),3);
  304. // Copy frame field
  305. for(unsigned i=0; i<deformer.XF.size(); ++i)
  306. {
  307. FF1_d.row(i) = deformer.XF[i].col(0);
  308. FF2_d.row(i) = deformer.XF[i].col(1);
  309. }
  310. }
  311. #ifdef IGL_STATIC_LIBRARY
  312. // Explicit template instantiation
  313. #endif