integrable_polyvector_fields.cpp 43 KB

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