integrable_polyvector_fields.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Olga Diamanti <olga.diam@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 <igl/integrable_polyvector_fields.h>
  9. #include <igl/field_local_global_conversions.h>
  10. #include <igl/parallel_transport_angles.h>
  11. #include <igl/local_basis.h>
  12. #include <igl/edge_topology.h>
  13. #include <igl/sparse.h>
  14. #include <igl/sort.h>
  15. #include <igl/slice.h>
  16. #include <igl/slice_into.h>
  17. #include <igl/sort_vectors_ccw.h>
  18. #include <iostream>
  19. IGL_INLINE igl::integrable_polyvector_fields_parameters::integrable_polyvector_fields_parameters():
  20. numIter(5),
  21. wBarrier(0.1),
  22. sBarrier(0.9),
  23. wCurl(10),
  24. wQuotCurl(10),
  25. wSmooth(1.),
  26. wCloseUnconstrained(1e-3),
  27. wCloseConstrained(100),
  28. redFactor_wsmooth(.8),
  29. gamma(0.1),
  30. tikh_gamma(1e-8)
  31. {}
  32. namespace igl {
  33. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  34. class IntegrableFieldSolver
  35. {
  36. private:
  37. IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC> &data;
  38. //Symbolic calculations
  39. IGL_INLINE void rj_barrier_face(const Eigen::RowVectorXd &vec2D_a,
  40. const double &s,
  41. Eigen::VectorXd &residuals,
  42. bool do_jac = false,
  43. Eigen::MatrixXd &J = *(Eigen::MatrixXd*)NULL);
  44. IGL_INLINE void rj_polycurl_edge(const Eigen::RowVectorXd &vec2D_a,
  45. const Eigen::RowVector2d &ea,
  46. const Eigen::RowVectorXd &vec2D_b,
  47. const Eigen::RowVector2d &eb,
  48. Eigen::VectorXd &residuals,
  49. bool do_jac = false,
  50. Eigen::MatrixXd &Jac = *(Eigen::MatrixXd*)NULL);
  51. IGL_INLINE void rj_quotcurl_edge_polyversion(const Eigen::RowVectorXd &vec2D_a,
  52. const Eigen::RowVector2d &ea,
  53. const Eigen::RowVectorXd &vec2D_b,
  54. const Eigen::RowVector2d &eb,
  55. Eigen::VectorXd &residuals,
  56. bool do_jac = false,
  57. Eigen::MatrixXd &Jac = *(Eigen::MatrixXd*)NULL);
  58. IGL_INLINE void rj_smoothness_edge(const Eigen::RowVectorXd &vec2D_a,
  59. const Eigen::RowVectorXd &vec2D_b,
  60. const double &k,
  61. const int nA,
  62. const int nB,
  63. Eigen::VectorXd &residuals,
  64. bool do_jac = false,
  65. Eigen::MatrixXd &Jac = *(Eigen::MatrixXd*)NULL);
  66. public:
  67. IGL_INLINE IntegrableFieldSolver(IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC> &cffsoldata);
  68. IGL_INLINE bool solve(integrable_polyvector_fields_parameters &params,
  69. Eigen::PlainObjectBase<DerivedFF>& current_field,
  70. bool current_field_is_not_ccw);
  71. IGL_INLINE void solveGaussNewton(integrable_polyvector_fields_parameters &params,
  72. const Eigen::VectorXd &x_initial,
  73. Eigen::VectorXd &x);
  74. //Compute residuals and Jacobian for Gauss Newton
  75. IGL_INLINE double RJ(const Eigen::VectorXd &x,
  76. const Eigen::VectorXd &x0,
  77. const integrable_polyvector_fields_parameters &params,
  78. bool doJacs = false);
  79. IGL_INLINE void RJ_Smoothness(const Eigen::MatrixXd &sol2D,
  80. const double &wSmoothSqrt,
  81. const int startRowInJacobian,
  82. bool doJacs = false,
  83. const int startIndexInVectors = 0);
  84. IGL_INLINE void RJ_Barrier(const Eigen::MatrixXd &sol2D,
  85. const double &s,
  86. const double &wBarrierSqrt,
  87. const int startRowInJacobian,
  88. bool doJacs = false,
  89. const int startIndexInVectors = 0);
  90. IGL_INLINE void RJ_Closeness(const Eigen::MatrixXd &sol2D,
  91. const Eigen::MatrixXd &sol02D,
  92. const double &wCloseUnconstrainedSqrt,
  93. const double &wCloseConstrainedSqrt,
  94. const int startRowInJacobian,
  95. bool doJacs = false,
  96. const int startIndexInVectors = 0);
  97. IGL_INLINE void RJ_Curl(const Eigen::MatrixXd &sol2D,
  98. const double &wCASqrt,
  99. const double &wCBSqrt,
  100. const int startRowInJacobian,
  101. bool doJacs = false,
  102. const int startIndexInVectors = 0);
  103. IGL_INLINE void RJ_QuotCurl(const Eigen::MatrixXd &sol2D,
  104. const double &wQuotCurlSqrt,
  105. const int startRowInJacobian,
  106. bool doJacs = false,
  107. const int startIndexInVectors = 0);
  108. };
  109. };
  110. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  111. IGL_INLINE igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::IntegrableFieldSolverData()
  112. {}
  113. template<typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  114. IGL_INLINE void igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::
  115. precomputeMesh(const Eigen::PlainObjectBase<DerivedV> &_V,
  116. const Eigen::PlainObjectBase<DerivedF> &_F)
  117. {
  118. numV = _V.rows();
  119. numF = _F.rows();
  120. numVariables = 2*2*numF;
  121. //Mesh stuff
  122. igl::edge_topology(_V,_F,E,F2E,E2F);
  123. numE = E.rows();
  124. igl::local_basis(_V,_F,B1,B2,FN);
  125. computeInteriorEdges();
  126. igl::parallel_transport_angles(_V, _F, FN, E2F, F2E, K);
  127. EVecNorm.setZero(numE,3);
  128. for (int k = 0; k<numE; ++k)
  129. EVecNorm.row(k) = (_V.row(E(k,1))-_V.row(E(k,0))).normalized();
  130. }
  131. template<typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  132. IGL_INLINE void igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::
  133. initializeConstraints(const Eigen::VectorXi& b,
  134. const Eigen::PlainObjectBase<DerivedC>& bc,
  135. const Eigen::VectorXi& constraint_level)
  136. {
  137. //save constrained
  138. Eigen::VectorXi iSorted, constrained_unsorted;
  139. constrained_unsorted.resize(2*2*b.size());
  140. is_constrained_face.setZero(numF, 1);
  141. int ind = 0;
  142. indInConstrained.setConstant(numF,1,-1);
  143. for (int k =0; k<b.size(); ++k)
  144. {
  145. is_constrained_face[b[k]] = constraint_level[k];
  146. for (int i=0; i<2;++i)
  147. {
  148. int xi = 2*2*b[k] + 2*i +0;
  149. int yi = 2*2*b[k] + 2*i +1;
  150. constrained_unsorted[ind++] = xi;
  151. constrained_unsorted[ind++] = yi;
  152. }
  153. indInConstrained[b[k]] = k;
  154. }
  155. //sort in descending order (so removing rows will work)
  156. igl::sort(constrained_unsorted, 1, false, constrained, iSorted);
  157. constrained_vec3 = bc.template cast<double>();
  158. }
  159. template<typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  160. IGL_INLINE void igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::
  161. makeFieldCCW(Eigen::MatrixXd &sol3D)
  162. {
  163. //sort ccw
  164. Eigen::RowVectorXd t;
  165. Eigen::RowVectorXd all(1,2*2*3);
  166. Eigen::VectorXi order;
  167. for (int fi=0; fi<numF; ++fi)
  168. {
  169. //take all 4 vectors (including opposites) and pick two that are in ccw order
  170. all << sol3D.row(fi), -sol3D.row(fi);
  171. igl::sort_vectors_ccw(all, FN.row(fi).eval(), order, true, t);
  172. //if we are in a constrained face, we need to make sure that the first vector is always the same vector as in the constraints
  173. if(is_constrained_face[fi])
  174. {
  175. const Eigen::RowVector3d &constraint = constrained_vec3.block(indInConstrained[fi], 0, 1, 3);;
  176. int best_i = -1; double best_score = std::numeric_limits<double>::max();
  177. for (int i = 0; i<2*2; ++i)
  178. {
  179. double score = (t.segment(i*3,3) - constraint).norm();
  180. if (score<best_score)
  181. {
  182. best_score = score;
  183. best_i = i;
  184. }
  185. }
  186. //do a circshift
  187. Eigen::RowVectorXd temp = t.segment(0, 3*best_i);
  188. int s1 = 3*best_i;
  189. int n2 = 3*best_i;
  190. int n1 = 3*2*2-n2;
  191. t.segment(0,n1) = t.segment(s1,n1);
  192. t.segment(n1, n2) = temp;
  193. }
  194. sol3D.row(fi) = t.segment(0, 2*3);
  195. }
  196. }
  197. template<typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  198. IGL_INLINE void igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::
  199. initializeOriginalVariable(const Eigen::PlainObjectBase<DerivedFF>& original_field)
  200. {
  201. Eigen::MatrixXd sol2D;
  202. Eigen::MatrixXd sol3D = original_field.template cast<double>();
  203. makeFieldCCW(sol3D);
  204. igl::global2local(B1, B2, sol3D, sol2D);
  205. xOriginal.setZero(numVariables);
  206. for (int i =0; i<numF; i++)
  207. xOriginal.segment(i*2*2, 2*2) = sol2D.row(i);
  208. }
  209. template<typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  210. IGL_INLINE void igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::
  211. computeInteriorEdges()
  212. {
  213. Eigen::VectorXi isBorderEdge;
  214. // Flag border edges
  215. numInteriorEdges = 0;
  216. isBorderEdge.setZero(numE,1);
  217. indFullToInterior = -1.*Eigen::VectorXi::Ones(numE,1);
  218. for(unsigned i=0; i<numE; ++i)
  219. {
  220. if ((E2F(i,0) == -1) || ((E2F(i,1) == -1)))
  221. isBorderEdge[i] = 1;
  222. else
  223. {
  224. indFullToInterior[i] = numInteriorEdges;
  225. numInteriorEdges++;
  226. }
  227. }
  228. E2F_int.resize(numInteriorEdges, 2);
  229. indInteriorToFull.setZero(numInteriorEdges,1);
  230. int ii = 0;
  231. for (int k=0; k<numE; ++k)
  232. {
  233. if (isBorderEdge[k])
  234. continue;
  235. E2F_int.row(ii) = E2F.row(k);
  236. indInteriorToFull[ii] = k;
  237. ii++;
  238. }
  239. }
  240. template<typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  241. IGL_INLINE void igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::
  242. add_Jacobian_to_svector(const int &toplace,
  243. const Eigen::MatrixXd &tJac,
  244. Eigen::VectorXd &SS_Jac)
  245. {
  246. int numInnerRows = tJac.rows();
  247. int numInnerCols = tJac.cols();
  248. int ind = toplace;
  249. for (int j=0; j<numInnerRows; ++j)
  250. for (int k=0; k<numInnerCols; ++k, ++ind)
  251. SS_Jac(ind) = tJac(j,k);
  252. }
  253. template<typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  254. IGL_INLINE void igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::
  255. add_jac_indices_face(const int numInnerRows,
  256. const int numInnerCols,
  257. const int startRowInJacobian,
  258. const int startIndexInVectors,
  259. Eigen::VectorXi &Rows,
  260. Eigen::VectorXi &Columns)
  261. {
  262. for (int fi=0; fi<numF; ++fi)
  263. {
  264. int startRow = startRowInJacobian+numInnerRows*fi;
  265. int startIndex = startIndexInVectors+numInnerRows*numInnerCols*fi;
  266. face_Jacobian_indices(startRow, startIndex, fi, 2, numInnerRows, numInnerCols, Rows, Columns);
  267. }
  268. }
  269. template<typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  270. IGL_INLINE void igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::
  271. face_Jacobian_indices(const int &startRow,
  272. const int &toplace,
  273. const int& fi,
  274. const int& half_degree,
  275. const int &numInnerRows,
  276. const int &numInnerCols,
  277. Eigen::VectorXi &rows,
  278. Eigen::VectorXi &columns)
  279. {
  280. int ind = toplace;
  281. for (int j=0; j<numInnerRows; ++j)
  282. {
  283. for (int k=0; k<numInnerCols; ++k, ++ind)
  284. {
  285. int iv = k/2;//which vector (0..half_degree-1) am i at
  286. int ixy = k%2;//which part (real/imag) am i at
  287. rows(ind) = startRow+j;
  288. columns(ind) = 2*half_degree*fi + 2*iv +ixy;
  289. }
  290. }
  291. }
  292. template<typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  293. IGL_INLINE void igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::
  294. add_jac_indices_edge(const int numInnerRows,
  295. const int numInnerCols,
  296. const int startRowInJacobian,
  297. const int startIndexInVectors,
  298. Eigen::VectorXi &Rows,
  299. Eigen::VectorXi &Columns)
  300. {
  301. for (int ii=0; ii<numInteriorEdges; ++ii)
  302. {
  303. // the two faces of the flap
  304. int a = E2F_int(ii,0);
  305. int b = E2F_int(ii,1);
  306. int startRow = startRowInJacobian+numInnerRows*ii;
  307. int startIndex = startIndexInVectors+numInnerRows*numInnerCols*ii;
  308. edge_Jacobian_indices(startRow, startIndex, a, b, 2, numInnerRows, numInnerCols, Rows, Columns);
  309. }
  310. }
  311. template<typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  312. IGL_INLINE void igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::
  313. edge_Jacobian_indices(const int &startRow,
  314. const int &toplace,
  315. const int& a,
  316. const int& b,
  317. const int& half_degree,
  318. const int &numInnerRows,
  319. const int &numInnerCols,
  320. Eigen::VectorXi &rows,
  321. Eigen::VectorXi &columns)
  322. {
  323. int ind = toplace;
  324. for (int j=0; j<numInnerRows; ++j)
  325. {
  326. for (int k=0; k<numInnerCols; ++k, ++ind)
  327. {
  328. int f = (k<2*half_degree)?a:b;//which face i am at
  329. int iv = (k%(2*half_degree))/2;//which vector (0..half_degree-1) am i at
  330. int ixy = k%2;//which part (real/imag) am i at
  331. rows(ind) = startRow+j;
  332. columns(ind) = 2*half_degree*f + 2*iv +ixy;
  333. }
  334. }
  335. }
  336. template<typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  337. IGL_INLINE void igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::
  338. computeJacobianPattern()
  339. {
  340. num_residuals_smooth = 4*numInteriorEdges;
  341. num_residuals_close = 4*numF;
  342. num_residuals_polycurl = 2*numInteriorEdges;
  343. num_residuals_quotcurl = numInteriorEdges;
  344. num_residuals_barrier = numF;
  345. num_residuals = num_residuals_smooth + num_residuals_close + num_residuals_polycurl + num_residuals_quotcurl + num_residuals_barrier;
  346. residuals.setZero(num_residuals,1);
  347. //per edge
  348. numJacElements_smooth = numInteriorEdges*numInnerJacCols_edge*numInnerJacRows_smooth;
  349. numJacElements_polycurl = numInteriorEdges*numInnerJacCols_edge*numInnerJacRows_polycurl;
  350. numJacElements_quotcurl = numInteriorEdges*numInnerJacCols_edge*numInnerJacRows_quotcurl;
  351. //per face
  352. numJacElements_barrier = numF*numInnerJacCols_face*numInnerJacRows_barrier;
  353. numJacElements_close = numF*numInnerJacCols_face*numInnerJacRows_close;
  354. numJacElements = (numJacElements_smooth +numJacElements_polycurl + numJacElements_quotcurl) + (numJacElements_barrier +numJacElements_close);
  355. //allocate
  356. II_Jac.setZero(numJacElements);
  357. JJ_Jac.setZero(numJacElements);
  358. SS_Jac.setOnes(numJacElements);
  359. //set stuff (attention: order !)
  360. int startRowInJacobian = 0;
  361. int startIndexInVectors = 0;
  362. //smoothness
  363. add_jac_indices_edge(numInnerJacRows_smooth,
  364. numInnerJacCols_edge,
  365. startRowInJacobian,
  366. startIndexInVectors,
  367. II_Jac,
  368. JJ_Jac);
  369. startRowInJacobian += num_residuals_smooth;
  370. startIndexInVectors += numJacElements_smooth;
  371. //closeness
  372. add_jac_indices_face(numInnerJacRows_close,
  373. numInnerJacCols_face,
  374. startRowInJacobian,
  375. startIndexInVectors,
  376. II_Jac,
  377. JJ_Jac);
  378. startRowInJacobian += num_residuals_close;
  379. startIndexInVectors += numJacElements_close;
  380. //barrier
  381. add_jac_indices_face(numInnerJacRows_barrier,
  382. numInnerJacCols_face,
  383. startRowInJacobian,
  384. startIndexInVectors,
  385. II_Jac,
  386. JJ_Jac);
  387. startRowInJacobian += num_residuals_barrier;
  388. startIndexInVectors += numJacElements_barrier;
  389. //polycurl
  390. add_jac_indices_edge(numInnerJacRows_polycurl,
  391. numInnerJacCols_edge,
  392. startRowInJacobian,
  393. startIndexInVectors,
  394. II_Jac,
  395. JJ_Jac);
  396. startRowInJacobian += num_residuals_polycurl;
  397. startIndexInVectors += numJacElements_polycurl;
  398. //quotcurl
  399. add_jac_indices_edge(numInnerJacRows_quotcurl,
  400. numInnerJacCols_edge,
  401. startRowInJacobian,
  402. startIndexInVectors,
  403. II_Jac,
  404. JJ_Jac);
  405. igl::sparse(II_Jac, JJ_Jac, SS_Jac, Jac);
  406. }
  407. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  408. IGL_INLINE void igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::
  409. computeHessianPattern()
  410. {
  411. //II_Jac is sorted in ascending order already
  412. int starti = 0;
  413. int currI = II_Jac(0);
  414. for (int ii = 0; ii<II_Jac.rows(); ++ii)
  415. {
  416. if(currI != II_Jac(ii))
  417. {
  418. starti = ii;
  419. currI = II_Jac(ii);
  420. }
  421. int k1 = II_Jac(ii);
  422. for (int jj = starti; jj<II_Jac.rows(); ++jj)
  423. {
  424. int k2 = II_Jac(jj);
  425. if (k1 !=k2)
  426. break;
  427. indInSS_Hess_1_vec.push_back(ii);
  428. indInSS_Hess_2_vec.push_back(jj);
  429. Hess_triplets.push_back(Eigen::Triplet<double> (JJ_Jac(ii),
  430. JJ_Jac(jj),
  431. SS_Jac(ii)*SS_Jac(jj)
  432. )
  433. );
  434. }
  435. }
  436. Hess.resize(Jac.cols(),Jac.cols());
  437. Hess.setFromTriplets(Hess_triplets.begin(), Hess_triplets.end());
  438. Hess.makeCompressed();
  439. }
  440. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  441. IGL_INLINE void igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC>::
  442. computeNewHessValues()
  443. {
  444. for (int i =0; i<Hess_triplets.size(); ++i)
  445. Hess_triplets[i] = Eigen::Triplet<double>(Hess_triplets[i].row(),
  446. Hess_triplets[i].col(),
  447. SS_Jac(indInSS_Hess_1_vec[i])*SS_Jac(indInSS_Hess_2_vec[i])
  448. );
  449. Hess.setFromTriplets(Hess_triplets.begin(), Hess_triplets.end());
  450. }
  451. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  452. IGL_INLINE igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC>::IntegrableFieldSolver( IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC> &cffsoldata):
  453. data(cffsoldata)
  454. { };
  455. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  456. IGL_INLINE bool igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC>::
  457. solve(igl::integrable_polyvector_fields_parameters &params,
  458. Eigen::PlainObjectBase<DerivedFF>& current_field,
  459. bool current_field_is_not_ccw)
  460. {
  461. Eigen::MatrixXd sol2D;
  462. Eigen::MatrixXd sol3D = current_field.template cast<double>();
  463. if (current_field_is_not_ccw)
  464. data.makeFieldCCW(sol3D);
  465. igl::global2local(data.B1, data.B2, sol3D, sol2D);
  466. Eigen::VectorXd x;
  467. x.setZero(data.numVariables);
  468. for (int i =0; i<data.numF; i++)
  469. x.segment(i*2*2, 2*2) = sol2D.row(i);
  470. //get x
  471. solveGaussNewton(params, data.xOriginal, x);
  472. //get output from x
  473. for (int i =0; i<data.numF; i++)
  474. sol2D.row(i) = x.segment(i*2*2, 2*2);
  475. igl::local2global(data.B1, data.B2, sol2D, sol3D);
  476. current_field = sol3D.cast<typename DerivedFF::Scalar>();
  477. return true;
  478. }
  479. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  480. IGL_INLINE void igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC>::
  481. solveGaussNewton(integrable_polyvector_fields_parameters &params,
  482. const Eigen::VectorXd &x_initial,
  483. Eigen::VectorXd &x)
  484. {
  485. bool converged = false;
  486. double F;
  487. Eigen::VectorXd xprev = x;
  488. Eigen::VectorXd xc = igl::slice(x_initial, data.constrained, 1);
  489. // double ESmooth, EClose, ECurl, EQuotCurl, EBarrier;
  490. for (int innerIter = 0; innerIter<params.numIter; ++innerIter)
  491. {
  492. //set constrained entries to those of the initial
  493. igl::slice_into(xc, data.constrained, 1, xprev);
  494. //get function, gradients and Hessians
  495. F = RJ(x, xprev, params, true);
  496. printf("IntegrableFieldSolver -- Iteration %d\n", innerIter);
  497. if((data.residuals.array() == std::numeric_limits<double>::infinity()).any())
  498. {
  499. std::cerr<<"IntegrableFieldSolver -- residuals: got infinity somewhere"<<std::endl;
  500. exit(1);
  501. };
  502. if((data.residuals.array() != data.residuals.array()).any())
  503. {
  504. std::cerr<<"IntegrableFieldSolver -- residuals: got infinity somewhere"<<std::endl;
  505. exit(1);
  506. };
  507. converged = false;
  508. Eigen::VectorXd rhs = data.Jac.transpose()*data.residuals;
  509. bool success;
  510. data.solver.factorize(data.Hess);
  511. success = data.solver.info() == Eigen::Success;
  512. if(!success)
  513. std::cerr<<"IntegrableFieldSolver -- Could not do LU"<<std::endl;
  514. Eigen::VectorXd direction;
  515. double error;
  516. direction = data.solver.solve(rhs);
  517. error = (data.Hess*direction - rhs).cwiseAbs().maxCoeff();
  518. if(error> 1e-4)
  519. {
  520. std::cerr<<"IntegrableFieldSolver -- Could not solve"<<std::endl;
  521. }
  522. // adaptive backtracking
  523. bool repeat = true;
  524. int run = 0;
  525. Eigen::VectorXd cx;
  526. Eigen::VectorXd tRes;
  527. double newF;
  528. while(repeat)
  529. {
  530. cx = x - params.gamma*direction;
  531. newF = RJ(cx, xprev, params);
  532. if(newF < F)
  533. {
  534. repeat = false;
  535. if(run == 0)
  536. params.gamma *= 2;
  537. }
  538. else
  539. {
  540. params.gamma *= 0.5f;
  541. if(params.gamma<1e-30)
  542. {
  543. repeat = false;
  544. converged = true;
  545. }
  546. }
  547. run++;
  548. }
  549. if (!converged)
  550. {
  551. xprev = x;
  552. x = cx;
  553. }
  554. else
  555. {
  556. std::cerr<<"IntegrableFieldSolver -- Converged"<<std::endl;
  557. break;
  558. }
  559. }
  560. }
  561. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  562. IGL_INLINE double igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC>::
  563. RJ(const Eigen::VectorXd &x,
  564. const Eigen::VectorXd &x0,
  565. const integrable_polyvector_fields_parameters &params,
  566. bool doJacs)
  567. {
  568. Eigen::MatrixXd sol2D(data.numF,4), sol02D(data.numF,4);
  569. for (int i =0; i<data.numF; i++)
  570. sol2D.row(i) = x.segment(i*2*2, 2*2);
  571. for (int i =0; i<data.numF; i++)
  572. sol02D.row(i) = x0.segment(i*2*2, 2*2);
  573. data.SS_Jac.setZero(data.numJacElements);
  574. //set stuff (attention: order !)
  575. int startRowInJacobian = 0;
  576. int startIndexInVectors = 0;
  577. //smoothness
  578. RJ_Smoothness(sol2D, sqrt(params.wSmooth), startRowInJacobian, doJacs, startIndexInVectors);
  579. startRowInJacobian += data.num_residuals_smooth;
  580. startIndexInVectors += data.numJacElements_smooth;
  581. //closeness
  582. RJ_Closeness(sol2D, sol02D, sqrt(params.wCloseUnconstrained), sqrt(params.wCloseConstrained), startRowInJacobian, doJacs, startIndexInVectors);
  583. startRowInJacobian += data.num_residuals_close;
  584. startIndexInVectors += data.numJacElements_close;
  585. //barrier
  586. RJ_Barrier(sol2D, params.sBarrier, sqrt(params.wBarrier), startRowInJacobian, doJacs, startIndexInVectors);
  587. startRowInJacobian += data.num_residuals_barrier;
  588. startIndexInVectors += data.numJacElements_barrier;
  589. //polycurl
  590. RJ_Curl(sol2D, params.wCurl, powl(params.wCurl, 2), startRowInJacobian, doJacs, startIndexInVectors);
  591. startRowInJacobian += data.num_residuals_polycurl;
  592. startIndexInVectors += data.numJacElements_polycurl;
  593. //quotcurl
  594. RJ_QuotCurl(sol2D, sqrt(params.wQuotCurl), startRowInJacobian, doJacs, startIndexInVectors);
  595. if(doJacs)
  596. {
  597. for (int i =0; i<data.numJacElements; ++i)
  598. data.Jac.coeffRef(data.II_Jac(i), data.JJ_Jac(i)) = data.SS_Jac(i);
  599. data.computeNewHessValues();
  600. }
  601. return data.residuals.transpose()*data.residuals;
  602. }
  603. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  604. IGL_INLINE void igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC>::
  605. rj_smoothness_edge(const Eigen::RowVectorXd &vec2D_a,
  606. const Eigen::RowVectorXd &vec2D_b,
  607. const double &k,
  608. const int nA,
  609. const int nB,
  610. Eigen::VectorXd &residuals,
  611. bool do_jac,
  612. Eigen::MatrixXd &Jac)
  613. {
  614. const Eigen::RowVector2d &ua = vec2D_a.segment(0, 2);
  615. const Eigen::RowVector2d &va = vec2D_a.segment(2, 2);
  616. const Eigen::RowVector2d &ub = vec2D_b.segment(0, 2);
  617. const Eigen::RowVector2d &vb = vec2D_b.segment(2, 2);
  618. const double &xua=ua[0], &yua=ua[1], &xva=va[0], &yva=va[1];
  619. const double &xub=ub[0], &yub=ub[1], &xvb=vb[0], &yvb=vb[1];
  620. double xua_2 = xua*xua;
  621. double xva_2 = xva*xva;
  622. double yua_2 = yua*yua;
  623. double yva_2 = yva*yva;
  624. double xub_2 = xub*xub;
  625. double xvb_2 = xvb*xvb;
  626. double yub_2 = yub*yub;
  627. double yvb_2 = yvb*yvb;
  628. double sA = sin(nA*k);
  629. double cA = cos(nA*k);
  630. double sB = sin(nB*k);
  631. double cB = cos(nB*k);
  632. double t1 = xua*yua;
  633. double t2 = xva*yva;
  634. double t3 = xub*xvb;
  635. double t4 = yub*yvb;
  636. double t5 = xua*xva;
  637. double t6 = xub*yub;
  638. double t7 = yua*yva;
  639. double t8 = xvb*yvb;
  640. double t9 = xva_2 - yva_2;
  641. double t10 = xua_2 - yua_2;
  642. double t11 = xvb_2 - yvb_2;
  643. double t12 = xub_2 - yub_2;
  644. double t13 = 2*t1 + 2*t2;
  645. double t17 = (2*t1*t9 + 2*t2*t10);
  646. double t19 = (t10*t9 - 4*t5*t7);
  647. residuals.resize(4, 1);
  648. residuals <<
  649. cA*(t10 + t9) - sA*(t13) - t12 - t11,
  650. sA*(t10 + t9) - 2*t8 - 2*t6 + cA*(t13),
  651. cB*t19 - (t12)*(t11) - sB*t17 + 4*t3*t4,
  652. cB*t17 + sB*t19 - 2*t6*(t11) - 2*t8*(t12);
  653. if (do_jac)
  654. {
  655. double t20 = 2*yua*t9 + 4*xua*t2;
  656. double t21 = 2*xua*t9 - 4*xva*t7;
  657. double t22 = 2*yva*t10 + 4*t5*yua;
  658. double t23 = 2*xva*t10 - 4*t1*yva;
  659. Jac.resize(4,8);
  660. Jac << 2*xua*cA - 2*yua*sA, - 2*yua*cA - 2*xua*sA, 2*xva*cA - 2*yva*sA, - 2*yva*cA - 2*xva*sA, -2*xub, 2*yub, -2*xvb, 2*yvb,
  661. 2*yua*cA + 2*xua*sA, 2*xua*cA - 2*yua*sA, 2*yva*cA + 2*xva*sA, 2*xva*cA - 2*yva*sA, -2*yub, -2*xub, -2*yvb, -2*xvb,
  662. cB*(t21) - sB*(t20), - cB*(t20) - sB*(t21), cB*(t23) - sB*(t22), - cB*(t22) - sB*(t23), 4*xvb*t4 - 2*xub*t11, 2*yub*t11 + 4*t3*yvb, 4*xub*t4 - 2*xvb*t12, 2*yvb*t12 + 4*t3*yub,
  663. cB*(t20) + sB*(t21), cB*(t21) - sB*(t20), cB*(t22) + sB*(t23), cB*(t23) - sB*(t22), - 2*yub*t11 - 4*t3*yvb, 4*xvb*t4 - 2*xub*t11, - 2*yvb*t12 - 4*t3*yub, 4*xub*t4 - 2*xvb*t12;
  664. }
  665. }
  666. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  667. IGL_INLINE void igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC>::
  668. RJ_Smoothness(const Eigen::MatrixXd &sol2D,
  669. const double &wSmoothSqrt,
  670. const int startRowInJacobian,
  671. bool doJacs,
  672. const int startIndexInVectors)
  673. {
  674. if (wSmoothSqrt ==0)
  675. return;
  676. for (int ii=0; ii<data.numInteriorEdges; ++ii)
  677. {
  678. // the two faces of the flap
  679. int a = data.E2F_int(ii,0);
  680. int b = data.E2F_int(ii,1);
  681. int k = data.indInteriorToFull[ii];
  682. Eigen::MatrixXd tJac;
  683. Eigen::VectorXd tRes;
  684. rj_smoothness_edge(sol2D.row(a),
  685. sol2D.row(b),
  686. data.K[k],
  687. 2*(0+1), //degree of first coefficient
  688. 2*(1+1), //degree of second coefficient
  689. tRes,
  690. doJacs,
  691. tJac);
  692. int startRow = startRowInJacobian+data.numInnerJacRows_smooth*ii;
  693. data.residuals.segment(startRow,data.numInnerJacRows_smooth) = wSmoothSqrt*tRes;
  694. if(doJacs)
  695. {
  696. int startIndex = startIndexInVectors+data.numInnerJacRows_smooth*data.numInnerJacCols_edge*ii;
  697. data.add_Jacobian_to_svector(startIndex, wSmoothSqrt*tJac,data.SS_Jac);
  698. }
  699. }
  700. }
  701. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  702. IGL_INLINE void igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC>::
  703. rj_barrier_face(const Eigen::RowVectorXd &vec2D_a,
  704. const double &s,
  705. Eigen::VectorXd &residuals,
  706. bool do_jac,
  707. Eigen::MatrixXd &Jac)
  708. {
  709. const Eigen::RowVector2d &ua = vec2D_a.segment(0, 2);
  710. const Eigen::RowVector2d &va = vec2D_a.segment(2, 2);
  711. const double &xua=ua[0], &yua=ua[1], &xva=va[0], &yva=va[1];
  712. double xva_2 = xva*xva;
  713. double yua_2 = yua*yua;
  714. double xua_2 = xua*xua;
  715. double yva_2 = yva*yva;
  716. double s_2 = s*s;
  717. double s_3 = s*s_2;
  718. double t00 = xua*yva;
  719. double t01 = xva*yua;
  720. double t05 = t00 - t01;
  721. double t05_2 = t05*t05;
  722. double t05_3 = t05*t05_2;
  723. if (do_jac)
  724. Jac.setZero(1,4);
  725. residuals.resize(1, 1);
  726. if (t05>=s)
  727. residuals << 0;
  728. else if (t05<0)
  729. residuals << std::numeric_limits<double>::infinity();
  730. else
  731. {
  732. residuals << 1/((3*t00 - 3*t01)/s - (3*t05_2)/s_2 + t05_3/s_3) - 1;
  733. double t03 = (s - t05)*(s - t05);
  734. double t06 = (3*s_2 - 3*s*t00 + 3*s*t01 + xua_2*yva_2 - 2*xua*t01*yva + xva_2*yua_2);
  735. double t04 = t06*t06;
  736. if (do_jac)
  737. Jac<<
  738. -(3*s_3*yva*t03)/(t05_2*t04),
  739. (3*s_3*xva*t03)/(t05_2*t04),
  740. (3*s_3*yua*t03)/(t05_2*t04),
  741. -(3*s_3*xua*t03)/(t05_2*t04);
  742. }
  743. }
  744. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  745. IGL_INLINE void igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC>::
  746. RJ_Barrier(const Eigen::MatrixXd &sol2D,
  747. const double &s,
  748. const double &wBarrierSqrt,
  749. const int startRowInJacobian,
  750. bool doJacs,
  751. const int startIndexInVectors)
  752. {
  753. if (wBarrierSqrt ==0)
  754. return;
  755. for (int fi=0; fi<data.numF; ++fi)
  756. {
  757. Eigen::MatrixXd tJac;
  758. Eigen::VectorXd tRes;
  759. rj_barrier_face(sol2D.row(fi),
  760. s,
  761. tRes,
  762. doJacs,
  763. tJac);
  764. int startRow = startRowInJacobian+ data.numInnerJacRows_barrier * fi;
  765. data.residuals.segment(startRow,data.numInnerJacRows_barrier) = wBarrierSqrt*tRes;
  766. if(doJacs)
  767. {
  768. int startIndex = startIndexInVectors+data.numInnerJacRows_barrier*data.numInnerJacCols_face*fi;
  769. data.add_Jacobian_to_svector(startIndex, wBarrierSqrt*tJac,data.SS_Jac);
  770. }
  771. }
  772. }
  773. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  774. IGL_INLINE void igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC>::
  775. RJ_Closeness(const Eigen::MatrixXd &sol2D,
  776. const Eigen::MatrixXd &sol02D,
  777. const double &wCloseUnconstrainedSqrt,
  778. const double &wCloseConstrainedSqrt,
  779. const int startRowInJacobian,
  780. bool doJacs,
  781. const int startIndexInVectors)
  782. {
  783. if (wCloseUnconstrainedSqrt ==0 && wCloseConstrainedSqrt ==0)
  784. return;
  785. for (int fi=0; fi<data.numF; ++fi)
  786. {
  787. Eigen::Vector4d weights;
  788. if (!data.is_constrained_face[fi])
  789. weights.setConstant(wCloseUnconstrainedSqrt);
  790. else
  791. {
  792. if (data.is_constrained_face[fi]==1)
  793. //only constrain the first vector
  794. weights<<wCloseConstrainedSqrt,wCloseConstrainedSqrt,wCloseUnconstrainedSqrt,wCloseUnconstrainedSqrt;
  795. else
  796. //either not partial, or partial with 2 constraints
  797. weights.setConstant(wCloseConstrainedSqrt);
  798. }
  799. Eigen::MatrixXd tJac;
  800. Eigen::VectorXd tRes;
  801. tJac = Eigen::MatrixXd::Identity(4,4);
  802. tRes.resize(4, 1); tRes<<(sol2D.row(fi)-sol02D.row(fi)).transpose();
  803. int startRow = startRowInJacobian+data.numInnerJacRows_close*fi;
  804. data.residuals.segment(startRow,data.numInnerJacRows_close) = weights.array()*tRes.array();
  805. if(doJacs)
  806. {
  807. int startIndex = startIndexInVectors+data.numInnerJacRows_close*data.numInnerJacCols_face*fi;
  808. data.add_Jacobian_to_svector(startIndex, weights.asDiagonal()*tJac,data.SS_Jac);
  809. }
  810. }
  811. }
  812. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  813. IGL_INLINE void igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC>::
  814. rj_polycurl_edge(const Eigen::RowVectorXd &vec2D_a,
  815. const Eigen::RowVector2d &ea,
  816. const Eigen::RowVectorXd &vec2D_b,
  817. const Eigen::RowVector2d &eb,
  818. Eigen::VectorXd &residuals,
  819. bool do_jac,
  820. Eigen::MatrixXd &Jac)
  821. {
  822. const Eigen::RowVector2d &ua = vec2D_a.segment(0, 2);
  823. const Eigen::RowVector2d &va = vec2D_a.segment(2, 2);
  824. const Eigen::RowVector2d &ub = vec2D_b.segment(0, 2);
  825. const Eigen::RowVector2d &vb = vec2D_b.segment(2, 2);
  826. const double &xua=ua[0], &yua=ua[1], &xva=va[0], &yva=va[1];
  827. const double &xub=ub[0], &yub=ub[1], &xvb=vb[0], &yvb=vb[1];
  828. const double &xea=ea[0], &yea=ea[1];
  829. const double &xeb=eb[0], &yeb=eb[1];
  830. const double dua = (xea*xua + yea*yua);
  831. const double dub = (xeb*xub + yeb*yub);
  832. const double dva = (xea*xva + yea*yva);
  833. const double dvb = (xeb*xvb + yeb*yvb);
  834. const double dua_2 = dua*dua;
  835. const double dva_2 = dva*dva;
  836. const double dub_2 = dub*dub;
  837. const double dvb_2 = dvb*dvb;
  838. residuals.resize(2, 1);
  839. residuals << dua_2 - dub_2 + dva_2 - dvb_2,
  840. dua_2*dva_2 - dub_2*dvb_2 ;
  841. if (do_jac)
  842. {
  843. Jac.resize(2,8);
  844. Jac << 2*xea*dua, 2*yea*dua, 2*xea*dva, 2*yea*dva, -2*xeb*dub, -2*yeb*dub, -2*xeb*dvb, -2*yeb*dvb,
  845. 2*xea*dua*dva_2, 2*yea*dua*dva_2, 2*xea*dua_2*dva, 2*yea*dua_2*dva, -2*xeb*dub*dvb_2, -2*yeb*dub*dvb_2, -2*xeb*dub_2*dvb, -2*yeb*dub_2*dvb;
  846. }
  847. }
  848. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  849. IGL_INLINE void igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC>::
  850. RJ_Curl(const Eigen::MatrixXd &sol2D,
  851. const double &wCASqrt,
  852. const double &wCBSqrt,
  853. const int startRowInJacobian,
  854. bool doJacs,
  855. const int startIndexInVectors)
  856. {
  857. if((wCASqrt==0) &&(wCBSqrt==0))
  858. return;
  859. for (int ii=0; ii<data.numInteriorEdges; ++ii)
  860. {
  861. // the two faces of the flap
  862. int a = data.E2F_int(ii,0);
  863. int b = data.E2F_int(ii,1);
  864. int k = data.indInteriorToFull[ii];
  865. // the common edge, a 3 vector
  866. const Eigen::RowVector3d &ce = data.EVecNorm.row(k);
  867. // the common edge expressed in local coordinates in the two faces
  868. // x/y denotes real/imaginary
  869. double xea = data.B1.row(a).dot(ce);
  870. double yea = data.B2.row(a).dot(ce);
  871. Eigen::RowVector2d ea; ea<<xea, yea;
  872. double xeb = data.B1.row(b).dot(ce);
  873. double yeb = data.B2.row(b).dot(ce);
  874. Eigen::RowVector2d eb; eb<<xeb, yeb;
  875. Eigen::MatrixXd tJac;
  876. Eigen::VectorXd tRes;
  877. rj_polycurl_edge(sol2D.row(a),
  878. ea,
  879. sol2D.row(b),
  880. eb,
  881. tRes,
  882. doJacs,
  883. tJac);
  884. tRes[0] = tRes[0]*wCASqrt;
  885. tRes[1] = tRes[1]*wCBSqrt;
  886. int startRow = startRowInJacobian+data.numInnerJacRows_polycurl*ii;
  887. data.residuals.segment(startRow,data.numInnerJacRows_polycurl) = tRes;
  888. if(doJacs)
  889. {
  890. tJac.row(0) = tJac.row(0)*wCASqrt;
  891. tJac.row(1) = tJac.row(1)*wCBSqrt;
  892. int startIndex = startIndexInVectors+data.numInnerJacRows_polycurl*data.numInnerJacCols_edge*ii;
  893. data.add_Jacobian_to_svector(startIndex, tJac,data.SS_Jac);
  894. }
  895. }
  896. }
  897. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  898. IGL_INLINE void igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC>::
  899. rj_quotcurl_edge_polyversion(const Eigen::RowVectorXd &vec2D_a,
  900. const Eigen::RowVector2d &ea,
  901. const Eigen::RowVectorXd &vec2D_b,
  902. const Eigen::RowVector2d &eb,
  903. Eigen::VectorXd &residuals,
  904. bool do_jac,
  905. Eigen::MatrixXd &Jac)
  906. {
  907. const Eigen::RowVector2d &ua = vec2D_a.segment(0, 2);
  908. const Eigen::RowVector2d &va = vec2D_a.segment(2, 2);
  909. const Eigen::RowVector2d &ub = vec2D_b.segment(0, 2);
  910. const Eigen::RowVector2d &vb = vec2D_b.segment(2, 2);
  911. const double &xua=ua[0], &yua=ua[1], &xva=va[0], &yva=va[1];
  912. const double &xub=ub[0], &yub=ub[1], &xvb=vb[0], &yvb=vb[1];
  913. const double &xea=ea[0], &yea=ea[1];
  914. const double &xeb=eb[0], &yeb=eb[1];
  915. double dua = (xea*xua + yea*yua);
  916. double dva = (xea*xva + yea*yva);
  917. double dub = (xeb*xub + yeb*yub);
  918. double dvb = (xeb*xvb + yeb*yvb);
  919. double dua_2 = dua * dua;
  920. double dva_2 = dva * dva;
  921. double dub_2 = dub * dub;
  922. double dvb_2 = dvb * dvb;
  923. double t00 = (dub_2 - dvb_2);
  924. double t01 = (dua_2 - dva_2);
  925. residuals.resize(1, 1);
  926. residuals << dua*dva*t00 - dub*dvb*t01;
  927. if (do_jac)
  928. {
  929. Jac.resize(1,8);
  930. Jac << xea*dva*t00 - 2*xea*dua*dub*dvb, yea*dva*t00 - 2*yea*dua*dub*dvb, xea*dua*t00 + 2*xea*dub*dva*dvb, yea*dua*t00 + 2*yea*dub*dva*dvb, 2*xeb*dua*dub*dva - xeb*dvb*t01, 2*yeb*dua*dub*dva - yeb*dvb*t01, - xeb*dub*t01 - 2*xeb*dua*dva*dvb, - yeb*dub*t01 - 2*yeb*dua*dva*dvb;
  931. }
  932. }
  933. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  934. IGL_INLINE void igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC>::
  935. RJ_QuotCurl(const Eigen::MatrixXd &sol2D,
  936. const double &wQuotCurlSqrt,
  937. const int startRowInJacobian,
  938. bool doJacs,
  939. const int startIndexInVectors)
  940. {
  941. for (int ii=0; ii<data.numInteriorEdges; ++ii)
  942. {
  943. // the two faces of the flap
  944. int a = data.E2F_int(ii,0);
  945. int b = data.E2F_int(ii,1);
  946. int k = data.indInteriorToFull[ii];
  947. // the common edge, a 3 vector
  948. const Eigen::RowVector3d &ce = data.EVecNorm.row(k);
  949. // the common edge expressed in local coordinates in the two faces
  950. // x/y denotes real/imaginary
  951. double xea = data.B1.row(a).dot(ce);
  952. double yea = data.B2.row(a).dot(ce);
  953. Eigen::RowVector2d ea; ea<<xea, yea;
  954. double xeb = data.B1.row(b).dot(ce);
  955. double yeb = data.B2.row(b).dot(ce);
  956. Eigen::RowVector2d eb; eb<<xeb, yeb;
  957. Eigen::MatrixXd tJac;
  958. Eigen::VectorXd tRes;
  959. rj_quotcurl_edge_polyversion(sol2D.row(a),
  960. ea,
  961. sol2D.row(b),
  962. eb,
  963. tRes,
  964. doJacs,
  965. tJac);
  966. int startRow = startRowInJacobian+ data.numInnerJacRows_quotcurl*ii;
  967. data.residuals.segment(startRow,data.numInnerJacRows_quotcurl) = wQuotCurlSqrt*tRes;
  968. if(doJacs)
  969. {
  970. int startIndex = startIndexInVectors+data.numInnerJacRows_quotcurl*data.numInnerJacCols_edge*ii;
  971. data.add_Jacobian_to_svector(startIndex, wQuotCurlSqrt*tJac,data.SS_Jac);
  972. }
  973. }
  974. }
  975. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  976. IGL_INLINE void igl::integrable_polyvector_fields_precompute(
  977. const Eigen::PlainObjectBase<DerivedV>& V,
  978. const Eigen::PlainObjectBase<DerivedF>& F,
  979. const Eigen::VectorXi& b,
  980. const Eigen::PlainObjectBase<DerivedC>& bc,
  981. const Eigen::VectorXi& constraint_level,
  982. const Eigen::PlainObjectBase<DerivedFF>& original_field,
  983. igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC> &data)
  984. {
  985. data.precomputeMesh(V,F);
  986. data.computeJacobianPattern();
  987. data.computeHessianPattern();
  988. data.solver.analyzePattern(data.Hess);
  989. data.initializeConstraints(b,bc,constraint_level);
  990. data.initializeOriginalVariable(original_field);
  991. };
  992. template <typename DerivedV, typename DerivedF, typename DerivedFF, typename DerivedC>
  993. IGL_INLINE void igl::integrable_polyvector_fields_solve(igl::IntegrableFieldSolverData<DerivedV, DerivedF, DerivedFF, DerivedC> &cffsoldata,
  994. integrable_polyvector_fields_parameters &params,
  995. Eigen::PlainObjectBase<DerivedFF>& current_field,
  996. bool current_field_is_not_ccw)
  997. {
  998. igl::IntegrableFieldSolver<DerivedV, DerivedF, DerivedFF, DerivedC> cffs(cffsoldata);
  999. cffs.solve(params, current_field, current_field_is_not_ccw);
  1000. };
  1001. #ifdef IGL_STATIC_LIBRARY
  1002. // Explicit template specialization
  1003. template igl::IntegrableFieldSolverData<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >::IntegrableFieldSolverData();
  1004. template void igl::integrable_polyvector_fields_solve<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(igl::IntegrableFieldSolverData<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, igl::integrable_polyvector_fields_parameters&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, bool);
  1005. template void igl::integrable_polyvector_fields_precompute<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, 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&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, igl::IntegrableFieldSolverData<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  1006. #endif