main.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. #include <igl/readOFF.h>
  2. #include <igl/readOBJ.h>
  3. #include <igl/n_polyvector.h>
  4. #include <igl/integrable_polyvector_fields.h>
  5. #include <igl/viewer/Viewer.h>
  6. #include <igl/local_basis.h>
  7. #include <igl/avg_edge_length.h>
  8. #include <igl/is_border_vertex.h>
  9. #include <igl/adjacency_list.h>
  10. #include <igl/vertex_triangle_adjacency.h>
  11. #include <igl/triangle_triangle_adjacency.h>
  12. #include <igl/edge_topology.h>
  13. #include <igl/jet.h>
  14. #include <igl/barycenter.h>
  15. #include <igl/polyvector_field_matchings.h>
  16. #include <igl/polyvector_field_singularities_from_matchings.h>
  17. #include <igl/polyvector_field_cut_mesh_with_singularities.h>
  18. #include <igl/polyvector_field_comb_from_matchings_and_cuts.h>
  19. #include <igl/polyvector_field_poisson_reconstruction.h>
  20. #include <igl/cut_mesh.h>
  21. #include <igl/slice.h>
  22. #include <igl/false_barycentric_subdivision.h>
  23. #include <iostream>
  24. #include <fstream>
  25. #include <igl/matlab_format.h>
  26. using namespace std;
  27. // Input mesh
  28. Eigen::MatrixXd V;
  29. Eigen::MatrixXi F;
  30. std::vector<bool> V_border;
  31. std::vector<std::vector<int> > VF, VFi;
  32. std::vector<std::vector<int> > VV;
  33. Eigen::MatrixXi TT, TTi;
  34. Eigen::MatrixXi E, E2F, F2E;
  35. // Per face bases (only needed to generate constraints)
  36. Eigen::MatrixXd B1,B2,B3;
  37. // "Subdivided" mesh obtained by splitting each triangle into 3 (only needed for display)
  38. Eigen::MatrixXd Vbs;
  39. Eigen::MatrixXi Fbs;
  40. // Scale for visualizing the fields
  41. double global_scale;
  42. // Scale for visualizing textures
  43. double uv_scale;
  44. // Data for original PolyVector field
  45. Eigen::MatrixXd two_pv_ori; // field
  46. Eigen::VectorXi singularities_ori; // singularities
  47. Eigen::VectorXd curl_ori; // curl per edge
  48. Eigen::MatrixXi cuts_ori; // cut edges
  49. Eigen::MatrixXd two_pv_poisson_ori; // field after poisson integration
  50. Eigen::VectorXf poisson_error_ori; // poisson integration error
  51. Eigen::MatrixXd scalars_ori;
  52. Eigen::MatrixXd Vcut_ori;
  53. Eigen::MatrixXi Fcut_ori;
  54. // Data for curl-free PolyVector field
  55. Eigen::MatrixXd two_pv; // field
  56. Eigen::VectorXi singularities; // singularities
  57. Eigen::VectorXd curl; // curl per edge
  58. Eigen::MatrixXi cuts; // cut edges
  59. Eigen::MatrixXd two_pv_poisson; // field after poisson integration
  60. Eigen::VectorXf poisson_error; // poisson integration error
  61. Eigen::MatrixXd scalars;
  62. Eigen::MatrixXd Vcut;
  63. Eigen::MatrixXi Fcut;
  64. // Vector of constrained faces
  65. Eigen::VectorXi b;
  66. // Matrix of constraints
  67. Eigen::MatrixXd bc;
  68. // "constraint level" flag (level=2 indicates that both directions are constrained,
  69. // level = 1 indicates a partially constrained face, i.e. only the first vector will
  70. // be constrained)
  71. Eigen::VectorXi blevel;
  72. // Face Barycenters (only needed for display)
  73. Eigen::MatrixXd B;
  74. // percentage of constrained faces
  75. double constraint_percentage = 0.002;
  76. // Random length factor
  77. double rand_factor = 5;
  78. // The set of parameters for calculating the curl-free fields
  79. igl::integrable_polyvector_fields_parameters params;
  80. // Solver data (needed for precomputation)
  81. igl::IntegrableFieldSolverData<Eigen::MatrixXd, Eigen::MatrixXi, Eigen::MatrixXd, Eigen::MatrixXd> ipfdata;
  82. //texture image
  83. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_R, texture_G, texture_B;
  84. int display_mode = 1;
  85. int iter = 0;
  86. // Create a texture that hides the integer translation in the parametrization
  87. void line_texture(Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> &texture_R,
  88. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> &texture_G,
  89. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> &texture_B)
  90. {
  91. unsigned size = 128;
  92. unsigned size2 = size/2;
  93. unsigned lineWidth = 3;
  94. texture_R.setConstant(size, size, 255);
  95. for (unsigned i=0; i<size; ++i)
  96. for (unsigned j=size2-lineWidth; j<=size2+lineWidth; ++j)
  97. texture_R(i,j) = 0;
  98. for (unsigned i=size2-lineWidth; i<=size2+lineWidth; ++i)
  99. for (unsigned j=0; j<size; ++j)
  100. texture_R(i,j) = 0;
  101. texture_G = texture_R;
  102. texture_B = texture_R;
  103. }
  104. // Create a random set of tangent vectors
  105. void generate_constraints()
  106. {
  107. b.resize(42,1);
  108. b<<
  109. 663,
  110. 513,
  111. 3872,
  112. 2601,
  113. 3549,
  114. 2721,
  115. 3796,
  116. 594,
  117. 868,
  118. 1730,
  119. 1581,
  120. 3081,
  121. 1471,
  122. 1650,
  123. 454,
  124. 2740,
  125. 2945,
  126. 3808,
  127. 3679,
  128. 3589,
  129. 450,
  130. 2656,
  131. 1791,
  132. 1792,
  133. 2917,
  134. 3744,
  135. 1536,
  136. 2809,
  137. 3866,
  138. 3658,
  139. 1665,
  140. 2670,
  141. 1601,
  142. 1793,
  143. 3614,
  144. 524,
  145. 2877,
  146. 449,
  147. 455,
  148. 3867,
  149. 3871,
  150. 2592;
  151. blevel.setOnes(b.rows(),1);
  152. bc.resize(b.rows(),6);
  153. bc<<
  154. -0.88046298335147721,0.27309862654264377,0.38755912468723769,-0.350632259447135,-0.92528970817792766,-0.14455440005410564,
  155. 0.91471470003012889,0.392936119054695,-0.094330397492144599,0.32234487030777614,-0.85027369799342767,-0.41608703787410195,
  156. 0.94335566040683105,0.073867667925654024,-0.32345581709658111,0.19950360079371404,-0.90525435056476755,0.37511714710727789,
  157. -0.92054671613540229,0.15077598183983737,0.36036141124232496,-0.27998315313687211,-0.89796618385425386,-0.33950871360506074,
  158. 0.88944399663239937,0.23035525634795684,-0.39474780902172396,0.27297422303141039,-0.96047177712172194,0.054580572670497061,
  159. -0.83112706922096102,-0.55599928943162547,0.0096221078617792517,0.52546831822766438,-0.79091522174894457,-0.31358596675362826,
  160. 0.90724658517664569,-0.41046292080872998,-0.091781394228251156,-0.34252813327252363,-0.84767620917196618,0.40511667741613094,
  161. -0.8932101465465786,0.23975524191487588,0.38038540729184012,-0.33645713296414853,-0.91759364410558497,-0.21170380718016926,
  162. -0.87839308390284521,0.27039404931731387,0.39409725734320344,-0.29712518405497651,-0.95481177255558192,-0.0071487054467167244,
  163. -0.91448048788760505,-0.17055891298176448,0.36692655188106316,0.29811257890714044,-0.89715315396744022,0.32595261714489804,
  164. 0.82798126471567035,-0.56074230404745851,0.003885065171440813,-0.53510484459763941,-0.78801608401899037,0.30445600111594384,
  165. -0.87831929581593793,0.25312706437601257,0.40556368658667746,-0.26531767440854004,-0.9637845762158106,0.026941089342378936,
  166. -0.87482003689209031,-0.27011021313654948,0.4021571531272935,0.32303198334357713,-0.94388894366288889,0.0687313594225408,
  167. 0.87408456883093666,-0.48487387939766946,-0.029554823793924323,-0.43846604347950752,-0.81368808449189478,0.38165328489525413,
  168. 0.94988212941972827,-0.041936960956176939,-0.30978255521381903,-0.16637246118261648,-0.90677959514398765,-0.3873899456497869,
  169. 0.87516493768857484,-0.27181042881473483,-0.40025669591913515,-0.36755520380602424,-0.91147911093961553,-0.18468622708756641,
  170. -0.87064244687577641,0.27922257819020818,0.40498948323008854,-0.32176729617260508,-0.94599403842079244,-0.039510585747255161,
  171. -0.91274615133859638,-0.1832657459904497,0.36511385835536858,0.29782083933521708,-0.91026141603074595,0.28762284704690655,
  172. 0.875611546674125,0.28258715176515403,-0.39172556846369444,0.36000975242683186,-0.92250014843287764,0.13923524804764778,
  173. 0.76763693171870195,-0.64088483679642994,0.00040868803559811201,-0.63058113310112196,-0.75518119878562417,0.17907761327874747,
  174. 0.963064265211517,0.17044712473620016,-0.20845862597111031,0.061832174999749308,-0.89345471128813481,-0.44487690546019126,
  175. -0.88228998376691692,-0.46837234310148523,0.046815945597605227,0.41604986062280985,-0.82249303168905052,-0.38782434980116298,
  176. -0.96608602970701829,0.11121907649833783,0.23304098400879364,0.010641270548624404,-0.88457418950525291,0.46627810008860171,
  177. -0.96329451047686887,0.055809409647239343,0.26258140810033831,0.07182051046944142,-0.88891411988025926,0.45240855623364673,
  178. -0.71244584326772997,-0.70122065397026967,-0.026655484539588895,0.70046172163981768,-0.70836773631021255,-0.086997279682342638,
  179. 0.88646445996853696,0.2549240118236365,-0.38625705094979518,0.35132981358631576,-0.91395520354543514,0.20310895597591658,
  180. -0.86109327343809683,-0.30822574449366841,0.40437020769461601,0.37896596246993836,-0.91928725525816557,0.10628142645421024,
  181. 0.86443027504389158,-0.29669958642983363,-0.40586901212079213,-0.37200560813855077,-0.92052106924988175,-0.11938504337027039,
  182. 0.95370728000967508,-0.24689991217686594,-0.17170572915195079,-0.14736898596800915,-0.88138264597997584,0.4488284898935197,
  183. -0.81439393313167019,0.57995723960933832,0.020300785774083896,-0.55494919604589421,-0.78855235001798585,0.26498411478639117,
  184. 0.89527216270596455,0.22395367264061938,-0.38513959442592183,0.33680943342191538,-0.90609008785063272,0.25604717974787594,
  185. -0.9003647006267198,0.20802946062196581,0.38218732236782926,-0.32431023000528064,-0.90640636884236436,-0.27064805418831556,
  186. -0.87050937437709508,-0.28614105672408718,0.40042068475344922,0.37746788793940733,-0.91025870352880611,0.17013843253251126,
  187. -0.95715079751532439,0.0030851788865496879,0.28957353554324744,0.12908381923211401,-0.89056292562302997,0.43615942397041058,
  188. -0.87324677619075319,-0.28591869514051466,0.39457644080913162,0.3438918663433696,-0.93530416305293196,0.083333707698197687,
  189. 0.91999856277124803,-0.1621255206103257,-0.35681642348085474,-0.27672206872177485,-0.91342693749618353,-0.2984562389005877,
  190. -0.8669467282645521,0.29036174243712859,0.40508447128995645,-0.34873789125620602,-0.93406639205959408,-0.07682355385522964,
  191. -0.9570365266718136,-0.22821899053183647,0.17887755302603078,0.12590409644663494,-0.88275887883510706,-0.45264215483728532,
  192. -0.94033215083998489,0.087395510869996196,0.32884262311388451,-0.2131320783418921,-0.90465024471116184,-0.36902933748646671,
  193. -0.96131014054749453,0.18866284908038999,0.20072155603578434,-0.08260532909072589,-0.89255302833360861,0.44331191188407898,
  194. -0.95240414686152941,-0.02752900142620229,0.30359264668538755,0.15128346580527452,-0.9073021943457209,0.39232134929083828,
  195. -0.94070423353276911,-0.31552769387286655,0.12457053990729766,0.22959741970407915,-0.86253407908715607,-0.45091017650802745;
  196. }
  197. void drawCuts(igl::viewer::Viewer& viewer,
  198. const Eigen::MatrixXi &cuts)
  199. {
  200. int maxCutNum = cuts.sum();
  201. Eigen::MatrixXd start(maxCutNum,3);
  202. Eigen::MatrixXd end(maxCutNum,3);
  203. int ind = 0;
  204. for (unsigned int i=0;i<F.rows();i++)
  205. for (int j=0;j<3;j++)
  206. if (cuts(i,j))
  207. {
  208. start.row(ind) = V.row(F(i,j));
  209. end.row(ind) = V.row(F(i,(j+1)%3));
  210. ind++;
  211. }
  212. viewer.data.add_edges(start, end , Eigen::RowVector3d(1.,0,1.));
  213. }
  214. void drawField(igl::viewer::Viewer &viewer,
  215. const Eigen::MatrixXd &field,
  216. const Eigen::RowVector3d &color)
  217. {
  218. for (int n=0; n<2; ++n)
  219. {
  220. Eigen::MatrixXd VF = field.block(0,n*3,F.rows(),3);
  221. Eigen::VectorXd c = VF.rowwise().norm();
  222. viewer.data.add_edges(B - global_scale*VF, B + global_scale*VF , color);
  223. }
  224. }
  225. void drawConstraints(igl::viewer::Viewer &viewer)
  226. {
  227. for (int n=0; n<2; ++n)
  228. {
  229. Eigen::MatrixXd Bc = igl::slice(B, b, 1);
  230. Eigen::MatrixXd color;
  231. color.setZero(b.rows(),3);
  232. color.col(2).setOnes();
  233. for (int i =0; i<b.rows(); ++i)
  234. if (blevel[i] ==1 && n>0)
  235. color.row(i)<<0.7,0.7,0.7;
  236. // Eigen::RowVector3d color; color<<0.5,0.5,0.5;
  237. viewer.data.add_edges(Bc - global_scale*bc.block(0,n*3,bc.rows(),3), Bc + global_scale*bc.block(0,n*3,bc.rows(),3) , color);
  238. }
  239. }
  240. void colorEdgeMeshFaces(const Eigen::VectorXd &values,
  241. const double &minimum,
  242. const double &maximum,
  243. Eigen::MatrixXd &C)
  244. {
  245. C.setConstant(Fbs.rows(),3,1);
  246. Eigen::MatrixXd colors;
  247. igl::jet(values, minimum, maximum, colors);
  248. for (int ei = 0; ei<E.rows(); ++ei)
  249. {
  250. const Eigen::RowVector3d &this_color = colors.row(ei);
  251. int f0 = E2F(ei,0);
  252. int f1 = E2F(ei,1);
  253. if(f0 != -1)
  254. {
  255. int i0 = -1;
  256. for (int k = 0; k<3; k++)
  257. if (F2E(f0,k)== ei)
  258. {
  259. i0 = k;
  260. break;
  261. }
  262. C.row(3*f0+i0) = this_color;
  263. }
  264. if(f1 != -1)
  265. {
  266. int i1 = -1;
  267. for (int k = 0; k<3; k++)
  268. if (F2E(f1,k)== ei)
  269. {
  270. i1 = k;
  271. break;
  272. }
  273. C.row(3*f1+i1) = this_color;
  274. }
  275. }
  276. }
  277. void update_display(igl::viewer::Viewer& viewer)
  278. {
  279. using namespace std;
  280. using namespace Eigen;
  281. viewer.data.clear();
  282. viewer.data.lines.resize(0,9);
  283. viewer.data.points.resize(0,6);
  284. viewer.core.show_texture = false;
  285. if (display_mode == 1)
  286. {
  287. cerr<< "Displaying original field, its singularities and its cuts" <<endl;
  288. viewer.data.set_mesh(V, F);
  289. // Highlight in red the constrained faces
  290. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  291. for (unsigned i=0; i<b.size();++i)
  292. C.row(b(i)) << 1, 0, 0;
  293. viewer.data.set_colors(C);
  294. //Draw constraints
  295. drawConstraints(viewer);
  296. // Draw Field
  297. Eigen::RowVector3d color; color<<0,0,1;
  298. drawField(viewer,two_pv_ori,color);
  299. // Draw Cuts
  300. drawCuts(viewer,cuts_ori);
  301. //Draw Singularities
  302. Eigen::MatrixXd singular_points = igl::slice(V, singularities_ori, 1);
  303. viewer.data.add_points(singular_points,Eigen::RowVector3d(239./255.,205./255.,57./255.));
  304. }
  305. if (display_mode == 2)
  306. {
  307. cerr<< "Displaying current field, its singularities and its cuts" <<endl;
  308. viewer.data.set_mesh(V, F);
  309. // Highlight in red the constrained faces
  310. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  311. for (unsigned i=0; i<b.size();++i)
  312. C.row(b(i)) << 1, 0, 0;
  313. viewer.data.set_colors(C);
  314. //Draw constraints
  315. drawConstraints(viewer);
  316. // Draw Field
  317. Eigen::RowVector3d color; color<<0,0,1;
  318. drawField(viewer,two_pv,color);
  319. // Draw Cuts
  320. drawCuts(viewer,cuts);
  321. //Draw Singularities
  322. Eigen::MatrixXd singular_points = igl::slice(V, singularities, 1);
  323. viewer.data.add_points(singular_points,Eigen::RowVector3d(239./255.,205./255.,57./255.));
  324. }
  325. if (display_mode == 3)
  326. {
  327. cerr<< "Displaying original field and its curl" <<endl;
  328. viewer.data.set_mesh(Vbs, Fbs);
  329. Eigen::MatrixXd C;
  330. colorEdgeMeshFaces(curl_ori, 0, 0.2, C);
  331. viewer.data.set_colors(C);
  332. // Draw Field
  333. Eigen::RowVector3d color; color<<1,1,1;
  334. drawField(viewer,two_pv_ori,color);
  335. }
  336. if (display_mode == 4)
  337. {
  338. cerr<< "Displaying current field and its curl" <<endl;
  339. viewer.data.set_mesh(Vbs, Fbs);
  340. Eigen::MatrixXd C;
  341. colorEdgeMeshFaces(curl, 0, 0.2, C);
  342. viewer.data.set_colors(C);
  343. // Draw Field
  344. Eigen::RowVector3d color; color<<1,1,1;
  345. drawField(viewer,two_pv,color);
  346. }
  347. if (display_mode == 5)
  348. {
  349. cerr<< "Displaying original poisson-integrated field and original poisson error" <<endl;
  350. viewer.data.set_mesh(V, F);
  351. Eigen::MatrixXd C;
  352. igl::jet(poisson_error_ori, 0, 0.5, C);
  353. viewer.data.set_colors(C);
  354. // Draw Field
  355. Eigen::RowVector3d color; color<<1,1,1;
  356. drawField(viewer,two_pv_poisson_ori,color);
  357. }
  358. if (display_mode == 6)
  359. {
  360. cerr<< "Displaying current poisson-integrated field and current poisson error" <<endl;
  361. viewer.data.set_mesh(V, F);
  362. Eigen::MatrixXd C;
  363. igl::jet(poisson_error, 0, 0.5, C);
  364. viewer.data.set_colors(C);
  365. // Draw Field
  366. Eigen::RowVector3d color; color<<1,1,1;
  367. drawField(viewer,two_pv_poisson,color);
  368. }
  369. if (display_mode == 7)
  370. {
  371. cerr<< "Displaying original texture with cuts and singularities" <<endl;
  372. viewer.data.set_mesh(V, F);
  373. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  374. viewer.data.set_colors(C);
  375. viewer.data.set_uv(uv_scale*scalars_ori, Fcut_ori);
  376. viewer.data.set_texture(texture_R, texture_B, texture_G);
  377. viewer.core.show_texture = true;
  378. // Draw Cuts
  379. drawCuts(viewer,cuts_ori);
  380. //Draw Singularities
  381. Eigen::MatrixXd singular_points = igl::slice(V, singularities_ori, 1);
  382. viewer.data.add_points(singular_points,Eigen::RowVector3d(239./255.,205./255.,57./255.));
  383. }
  384. if (display_mode == 8)
  385. {
  386. cerr<< "Displaying current texture with cuts and singularities" <<endl;
  387. viewer.data.set_mesh(V, F);
  388. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  389. viewer.data.set_colors(C);
  390. viewer.data.set_uv(uv_scale*scalars, Fcut);
  391. viewer.data.set_texture(texture_R, texture_B, texture_G);
  392. viewer.core.show_texture = true;
  393. // Draw Cuts
  394. drawCuts(viewer,cuts);
  395. //Draw Singularities
  396. Eigen::MatrixXd singular_points = igl::slice(V, singularities, 1);
  397. viewer.data.add_points(singular_points,Eigen::RowVector3d(239./255.,205./255.,57./255.));
  398. }
  399. if (display_mode == 9)
  400. {
  401. cerr<< "Displaying original field overlayed onto the current integrated field" <<endl;
  402. viewer.data.set_mesh(V, F);
  403. // Highlight in red the constrained faces
  404. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  405. for (unsigned i=0; i<b.size();++i)
  406. C.row(b(i)) << 1, 0, 0;
  407. viewer.data.set_colors(C);
  408. // Draw Field
  409. Eigen::RowVector3d color; color<<0,0,1;
  410. drawField(viewer,two_pv_ori,color);
  411. // Draw Integrated Field
  412. color<<.2,.2,.2;
  413. drawField(viewer,two_pv_poisson_ori,color);
  414. }
  415. if (display_mode == 0)
  416. {
  417. cerr<< "Displaying current field overlayed onto the current integrated field" <<endl;
  418. viewer.data.set_mesh(V, F);
  419. // Highlight in red the constrained faces
  420. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  421. for (unsigned i=0; i<b.size();++i)
  422. C.row(b(i)) << 1, 0, 0;
  423. viewer.data.set_colors(C);
  424. // Draw Field
  425. Eigen::RowVector3d color; color<<0,0,1;
  426. drawField(viewer,two_pv,color);
  427. // Draw Integrated Field
  428. color<<.2,.2,.2;
  429. drawField(viewer,two_pv_poisson,color);
  430. }
  431. }
  432. bool key_down(igl::viewer::Viewer& viewer, unsigned char key, int modifier)
  433. {
  434. if (key == '1')
  435. {
  436. display_mode = 1;
  437. update_display(viewer);
  438. }
  439. if (key == '2')
  440. {
  441. display_mode = 2;
  442. update_display(viewer);
  443. }
  444. if (key == '3')
  445. {
  446. display_mode = 3;
  447. update_display(viewer);
  448. }
  449. if (key == '4')
  450. {
  451. display_mode = 4;
  452. update_display(viewer);
  453. }
  454. if (key == '5')
  455. {
  456. display_mode = 5;
  457. update_display(viewer);
  458. }
  459. if (key == '6')
  460. {
  461. display_mode = 6;
  462. update_display(viewer);
  463. }
  464. if (key == '7')
  465. {
  466. display_mode = 7;
  467. update_display(viewer);
  468. }
  469. if (key == '8')
  470. {
  471. display_mode = 8;
  472. update_display(viewer);
  473. }
  474. if (key == '9')
  475. {
  476. display_mode = 9;
  477. update_display(viewer);
  478. }
  479. if (key == '0')
  480. {
  481. display_mode = 0;
  482. update_display(viewer);
  483. }
  484. if (key == 'A')
  485. {
  486. //do a batch of iterations
  487. printf("--Improving Curl--\n");
  488. for (int bi = 0; bi<5; ++bi)
  489. {
  490. printf("\n\n **** Batch %d ****\n", iter);
  491. igl::integrable_polyvector_fields_solve(ipfdata, params, two_pv, iter ==0);
  492. iter++;
  493. params.wSmooth *= params.redFactor_wsmooth;
  494. }
  495. // Post process current field
  496. // Compute curl_minimizing matchings and curl
  497. printf("--Matchings and curl--\n");
  498. Eigen::MatrixXi match_ab, match_ba; // matchings across interior edges
  499. double avgCurl = igl::polyvector_field_matchings(two_pv, V, F, true, match_ab, match_ba, curl);
  500. double maxCurl = curl.maxCoeff();
  501. printf("curl -- max: %.5g, avg: %.5g\n", maxCurl, avgCurl);
  502. // Compute singularities
  503. printf("--Singularities--\n");
  504. igl::polyvector_field_singularities_from_matchings(V, F, match_ab, match_ba, singularities);
  505. printf("#singularities: %ld\n", singularities.rows());
  506. // Get mesh cuts based on singularities
  507. printf("--Cuts--\n");
  508. igl::polyvector_field_cut_mesh_with_singularities(V, F, singularities, cuts);
  509. // Comb field
  510. printf("--Combing--\n");
  511. Eigen::MatrixXd combed;
  512. igl::polyvector_field_comb_from_matchings_and_cuts(V, F, two_pv, match_ab, match_ba, cuts, combed);
  513. // Reconstruct integrable vector fields from combed field
  514. printf("--Cut mesh--\n");
  515. igl::cut_mesh(V, F, cuts, Vcut, Fcut);
  516. printf("--Poisson--\n");
  517. double avgPoisson = igl::polyvector_field_poisson_reconstruction(Vcut, Fcut, combed, scalars, two_pv_poisson, poisson_error);
  518. double maxPoisson = poisson_error.maxCoeff();
  519. printf("poisson error -- max: %.5g, avg: %.5g\n", maxPoisson, avgPoisson);
  520. update_display(viewer);
  521. }
  522. return false;
  523. }
  524. int main(int argc, char *argv[])
  525. {
  526. // Load a mesh
  527. igl::readOBJ("../shared/inspired_mesh.obj", V, F);
  528. printf("--Initialization--\n");
  529. V_border = igl::is_border_vertex(V,F);
  530. igl::adjacency_list(F, VV);
  531. igl::vertex_triangle_adjacency(V,F,VF,VFi);
  532. igl::triangle_triangle_adjacency(V,F,TT,TTi);
  533. igl::edge_topology(V,F,E,F2E,E2F);
  534. // Generate "subdivided" mesh for visualization of curl terms
  535. igl::false_barycentric_subdivision(V, F, Vbs, Fbs);
  536. // Compute scale for visualizing fields
  537. global_scale = .2*igl::avg_edge_length(V, F);
  538. //Compute scale for visualizing texture
  539. uv_scale = 0.6/igl::avg_edge_length(V, F);
  540. // Compute face barycenters
  541. igl::barycenter(V, F, B);
  542. // Compute local basis for faces
  543. igl::local_basis(V,F,B1,B2,B3);
  544. //Generate random vectors for constraints
  545. generate_constraints();
  546. // Interpolate a 2-PolyVector field to be used as the original field
  547. printf("--Initial solution--\n");
  548. igl::n_polyvector(V, F, b, bc, two_pv_ori);
  549. // Post process original field
  550. // Compute curl_minimizing matchings and curl
  551. Eigen::MatrixXi match_ab, match_ba; // matchings across interior edges
  552. printf("--Matchings and curl--\n");
  553. double avgCurl = igl::polyvector_field_matchings(two_pv_ori, V, F, true, match_ab, match_ba, curl_ori);
  554. double maxCurl = curl_ori.maxCoeff();
  555. printf("original curl -- max: %.5g, avg: %.5g\n", maxCurl, avgCurl);
  556. printf("--Singularities--\n");
  557. // Compute singularities
  558. igl::polyvector_field_singularities_from_matchings(V, F, V_border, VF, TT, E2F, F2E, match_ab, match_ba, singularities_ori);
  559. printf("original #singularities: %ld\n", singularities.rows());
  560. printf("--Cuts--\n");
  561. // Get mesh cuts based on singularities
  562. igl::polyvector_field_cut_mesh_with_singularities(V, F, VF, VV, TT, TTi, singularities_ori, cuts_ori);
  563. printf("--Combing--\n");
  564. // Comb field
  565. Eigen::MatrixXd combed;
  566. igl::polyvector_field_comb_from_matchings_and_cuts(V, F, TT, E2F, F2E, two_pv_ori, match_ab, match_ba, cuts_ori, combed);
  567. printf("--Cut mesh--\n");
  568. // Reconstruct integrable vector fields from combed field
  569. igl::cut_mesh(V, F, VF, VFi, TT, TTi, V_border, cuts_ori, Vcut_ori, Fcut_ori);
  570. printf("--Poisson--\n");
  571. double avgPoisson = igl::polyvector_field_poisson_reconstruction(Vcut_ori, Fcut_ori, combed, scalars_ori, two_pv_poisson_ori, poisson_error_ori);
  572. double maxPoisson = poisson_error_ori.maxCoeff();
  573. printf("poisson error -- max: %.5g, avg: %.5g\n", maxPoisson, avgPoisson);
  574. // Set the curl-free 2-PolyVector to equal the original field
  575. two_pv = two_pv_ori;
  576. singularities = singularities_ori;
  577. curl = curl_ori;
  578. cuts = cuts_ori;
  579. two_pv_poisson = two_pv_poisson_ori;
  580. poisson_error = poisson_error_ori;
  581. Vcut = Vcut_ori;
  582. Fcut = Fcut_ori;
  583. scalars = scalars_ori;
  584. printf("--Integrable - Precomputation--\n");
  585. // Precompute stuff for solver
  586. igl::integrable_polyvector_fields_precompute(V, F, b, bc, blevel, two_pv_ori, ipfdata);
  587. cerr<<"Done. Press keys 1-0 for various visualizations, 'A' to improve integrability." <<endl;
  588. igl::viewer::Viewer viewer;
  589. viewer.callback_key_down = &key_down;
  590. viewer.core.show_lines = false;
  591. key_down(viewer,'2',0);
  592. // Replace the standard texture with an integer shift invariant texture
  593. line_texture(texture_R, texture_G, texture_B);
  594. viewer.launch();
  595. return 0;
  596. }