frame_field_deformer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@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 "frame_field_deformer.h"
  9. #include <Eigen/Dense>
  10. #include <Eigen/Sparse>
  11. #include <vector>
  12. #include <igl/cotmatrix_entries.h>
  13. #include <igl/cotmatrix.h>
  14. #include <igl/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. using namespace Eigen;
  121. nfree = V.rows() - fixed; // free vertices (at the beginning ov m.V) - global
  122. nconst = V.rows()-nfree; // #constrained vertices
  123. igl::vertex_triangle_adjacency(V,F,VT,VTi); // compute vertex to face relationship
  124. igl::cotmatrix_entries(V,F,C); // cotangent matrix for opt. rotations - global
  125. igl::cotmatrix(V,F,L);
  126. SparseMatrix<double> MA; // internal matrix for ARAP-warping energy
  127. MatrixXd LfcVc; // RHS (partial) for ARAP-warping energy
  128. SparseMatrix<double> MS; // internal matrix for smoothing energy
  129. MatrixXd bS; // RHS (full) for smoothing energy
  130. precompute_ARAP(MA,LfcVc); // precompute terms for the ARAP-warp part
  131. precompute_SMOOTH(MS,bS); // precompute terms for the smoothing part
  132. compute_idealWarp(WW); // computes the ideal warps
  133. RW.resize(F.rows()); // init rotation matrices - global
  134. M = (1-Lambda)*MA + Lambda*MS; // matrix for linear system - global
  135. RHS = (1-Lambda)*LfcVc + Lambda*bS; // RHS (partial) for linear system - global
  136. solver.compute(M); // system pre-conditioning
  137. if (solver.info()!=Eigen::Success) {fprintf(stderr,"Decomposition failed in pre-conditioning!\n"); exit(-1);}
  138. fprintf(stdout,"Preconditioning done.\n");
  139. }
  140. IGL_INLINE void Frame_field_deformer::precompute_ARAP(Eigen::SparseMatrix<double> & Lff, Eigen::MatrixXd & LfcVc)
  141. {
  142. using namespace Eigen;
  143. fprintf(stdout,"Precomputing ARAP terms\n");
  144. SparseMatrix<double> LL = -4*L;
  145. Lff = SparseMatrix<double>(nfree,nfree);
  146. extractBlock(LL,0,0,nfree,nfree,Lff);
  147. SparseMatrix<double> Lfc = SparseMatrix<double>(nfree,nconst);
  148. extractBlock(LL,0,nfree,nfree,nconst,Lfc);
  149. LfcVc = - Lfc * V_w.block(nfree,0,nconst,3);
  150. }
  151. IGL_INLINE void Frame_field_deformer::precompute_SMOOTH(Eigen::SparseMatrix<double> & MS, Eigen::MatrixXd & bS)
  152. {
  153. using namespace Eigen;
  154. fprintf(stdout,"Precomputing SMOOTH terms\n");
  155. SparseMatrix<double> LL = 4*L*L;
  156. // top-left
  157. MS = SparseMatrix<double>(nfree,nfree);
  158. extractBlock(LL,0,0,nfree,nfree,MS);
  159. // top-right
  160. SparseMatrix<double> Mfc = SparseMatrix<double>(nfree,nconst);
  161. extractBlock(LL,0,nfree,nfree,nconst,Mfc);
  162. MatrixXd MfcVc = Mfc * V_w.block(nfree,0,nconst,3);
  163. bS = (LL*V).block(0,0,nfree,3)-MfcVc;
  164. }
  165. IGL_INLINE void Frame_field_deformer::extractBlock(Eigen::SparseMatrix<double> & mat, int r0, int c0, int r, int c, Eigen::SparseMatrix<double> & m1)
  166. {
  167. std::vector<Eigen::Triplet<double> > tripletList;
  168. for (int k=c0; k<c0+c; ++k)
  169. for (Eigen::SparseMatrix<double>::InnerIterator it(mat,k); it; ++it)
  170. {
  171. if (it.row()>=r0 && it.row()<r0+r)
  172. tripletList.push_back(Eigen::Triplet<double>(it.row()-r0,it.col()-c0,it.value()));
  173. }
  174. m1.setFromTriplets(tripletList.begin(), tripletList.end());
  175. }
  176. IGL_INLINE void Frame_field_deformer::compute_optimal_rotations()
  177. {
  178. using namespace Eigen;
  179. Matrix<double,3,3> r,S,P,PP,D;
  180. for (int i=0;i<F.rows();i++)
  181. {
  182. // input tri --- could be done once and saved in a matrix
  183. P.col(0) = (V.row(F(i,1))-V.row(F(i,0))).transpose();
  184. P.col(1) = (V.row(F(i,2))-V.row(F(i,1))).transpose();
  185. P.col(2) = (V.row(F(i,0))-V.row(F(i,2))).transpose();
  186. P = WW[i] * P; // apply ideal warp
  187. // current tri
  188. PP.col(0) = (V_w.row(F(i,1))-V_w.row(F(i,0))).transpose();
  189. PP.col(1) = (V_w.row(F(i,2))-V_w.row(F(i,1))).transpose();
  190. PP.col(2) = (V_w.row(F(i,0))-V_w.row(F(i,2))).transpose();
  191. // cotangents
  192. D << C(i,2), 0, 0,
  193. 0, C(i,0), 0,
  194. 0, 0, C(i,1);
  195. S = PP*D*P.transpose();
  196. Eigen::JacobiSVD<Matrix<double,3,3> > svd(S, Eigen::ComputeFullU | Eigen::ComputeFullV );
  197. Matrix<double,3,3> su = svd.matrixU();
  198. Matrix<double,3,3> sv = svd.matrixV();
  199. r = su*sv.transpose();
  200. if (r.determinant()<0) // correct reflections
  201. {
  202. su(0,2)=-su(0,2); su(1,2)=-su(1,2); su(2,2)=-su(2,2);
  203. r = su*sv.transpose();
  204. }
  205. RW[i] = r*WW[i]; // RW INCORPORATES IDEAL WARP WW!!!
  206. }
  207. }
  208. IGL_INLINE void Frame_field_deformer::compute_optimal_positions()
  209. {
  210. using namespace Eigen;
  211. // compute variable RHS of ARAP-warp part of the system
  212. MatrixXd b(nfree,3); // fx3 known term of the system
  213. MatrixXd X; // result
  214. int t; // triangles incident to edge (i,j)
  215. int vi,i1,i2; // index of vertex i wrt tri t0
  216. for (int i=0;i<nfree;i++)
  217. {
  218. b.row(i) << 0.0, 0.0, 0.0;
  219. for (int k=0;k<(int)VT[i].size();k++) // for all incident triangles
  220. {
  221. t = VT[i][k]; // incident tri
  222. vi = (i==F(t,0))?0:(i==F(t,1))?1:(i==F(t,2))?2:3; // index of i in t
  223. assert(vi!=3);
  224. i1 = F(t,(vi+1)%3);
  225. i2 = F(t,(vi+2)%3);
  226. b.row(i)+=(C(t,(vi+2)%3)*RW[t]*(V.row(i1)-V.row(i)).transpose()).transpose();
  227. b.row(i)+=(C(t,(vi+1)%3)*RW[t]*(V.row(i2)-V.row(i)).transpose()).transpose();
  228. }
  229. }
  230. b/=2.0;
  231. b=-4*b;
  232. b*=(1-Lambda); // blend
  233. b+=RHS; // complete known term
  234. X = solver.solve(b);
  235. if (solver.info()!=Eigen::Success) {printf("Solving linear system failed!\n"); return;}
  236. // copy result to mw.V
  237. for (int i=0;i<nfree;i++)
  238. V_w.row(i)=X.row(i);
  239. }
  240. IGL_INLINE void Frame_field_deformer::computeXField(std::vector< Eigen::Matrix<double,3,2> > & XF)
  241. {
  242. using namespace Eigen;
  243. Matrix<double,3,3> P,PP,DG;
  244. XF.resize(F.rows());
  245. for (int i=0;i<F.rows();i++)
  246. {
  247. int i0,i1,i2;
  248. // indexes of vertices of face i
  249. i0 = F(i,0); i1 = F(i,1); i2 = F(i,2);
  250. // input frame
  251. P.col(0) = (V.row(i1)-V.row(i0)).transpose();
  252. P.col(1) = (V.row(i2)-V.row(i0)).transpose();
  253. P.col(2) = P.col(0).cross(P.col(1));
  254. // output triangle brought to origin
  255. PP.col(0) = (V_w.row(i1)-V_w.row(i0)).transpose();
  256. PP.col(1) = (V_w.row(i2)-V_w.row(i0)).transpose();
  257. PP.col(2) = PP.col(0).cross(PP.col(1));
  258. // deformation gradient
  259. DG = PP * P.inverse();
  260. XF[i] = DG * FF[i];
  261. }
  262. }
  263. // computes in WW the ideal warp at each tri to make the frame field a cross
  264. IGL_INLINE void Frame_field_deformer::compute_idealWarp(std::vector< Eigen::Matrix<double,3,3> > & WW)
  265. {
  266. using namespace Eigen;
  267. WW.resize(F.rows());
  268. for (int i=0;i<(int)FF.size();i++)
  269. {
  270. Vector3d v0,v1,v2;
  271. v0 = FF[i].col(0);
  272. v1 = FF[i].col(1);
  273. v2=v0.cross(v1); v2.normalize(); // normal
  274. Matrix3d A,AI; // compute affine map A that brings:
  275. A << v0[0], v1[0], v2[0], // first vector of FF to x unary vector
  276. v0[1], v1[1], v2[1], // second vector of FF to xy plane
  277. v0[2], v1[2], v2[2]; // triangle normal to z unary vector
  278. AI = A.inverse();
  279. // polar decomposition to discard rotational component (unnecessary but makes it easier)
  280. Eigen::JacobiSVD<Matrix<double,3,3> > svd(AI, Eigen::ComputeFullU | Eigen::ComputeFullV );
  281. //Matrix<double,3,3> au = svd.matrixU();
  282. Matrix<double,3,3> av = svd.matrixV();
  283. DiagonalMatrix<double,3> as(svd.singularValues());
  284. WW[i] = av*as*av.transpose();
  285. }
  286. }
  287. }
  288. IGL_INLINE void igl::frame_field_deformer(
  289. const Eigen::MatrixXd& V,
  290. const Eigen::MatrixXi& F,
  291. const Eigen::MatrixXd& FF1,
  292. const Eigen::MatrixXd& FF2,
  293. Eigen::MatrixXd& V_d,
  294. Eigen::MatrixXd& FF1_d,
  295. Eigen::MatrixXd& FF2_d,
  296. const int iterations,
  297. const double lambda,
  298. const bool perturb_initial_guess)
  299. {
  300. using namespace Eigen;
  301. // Solvers
  302. Frame_field_deformer deformer;
  303. // Init optimizer
  304. deformer.init(V, F, FF1, FF2, lambda, perturb_initial_guess ? 0.1 : 0);
  305. // Optimize
  306. deformer.optimize(iterations,true);
  307. // Copy positions
  308. V_d = deformer.V_w;
  309. // Allocate
  310. FF1_d.resize(F.rows(),3);
  311. FF2_d.resize(F.rows(),3);
  312. // Copy frame field
  313. for(unsigned i=0; i<deformer.XF.size(); ++i)
  314. {
  315. FF1_d.row(i) = deformer.XF[i].col(0);
  316. FF2_d.row(i) = deformer.XF[i].col(1);
  317. }
  318. }
  319. #ifdef IGL_STATIC_LIBRARY
  320. // Explicit template specialization
  321. #endif