main.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. {
  217. for (int n=0; n<2; ++n)
  218. {
  219. Eigen::MatrixXd VF = field.block(0,n*3,F.rows(),3);
  220. Eigen::VectorXd c = VF.rowwise().norm();
  221. Eigen::MatrixXd C2;
  222. igl::jet(c,1,1+rand_factor,C2);
  223. viewer.data.add_edges(B - global_scale*VF, B + global_scale*VF , C2);
  224. }
  225. }
  226. void drawConstraints(igl::viewer::Viewer &viewer)
  227. {
  228. for (int n=0; n<2; ++n)
  229. {
  230. Eigen::MatrixXd Bc = igl::slice(B, b, 1);
  231. Eigen::MatrixXd color;
  232. color.setZero(b.rows(),3);
  233. color.col(2).setOnes();
  234. for (int i =0; i<b.rows(); ++i)
  235. if (blevel[i] ==1 && n>0)
  236. color.row(i)<<0.7,0.7,0.7;
  237. // Eigen::RowVector3d color; color<<0.5,0.5,0.5;
  238. 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);
  239. }
  240. }
  241. void colorEdgeMeshFaces(const Eigen::VectorXd &values,
  242. const double &minimum,
  243. const double &maximum,
  244. Eigen::MatrixXd &C)
  245. {
  246. C.setConstant(Fbs.rows(),3,1);
  247. Eigen::MatrixXd colors;
  248. igl::jet(values, minimum, maximum, colors);
  249. for (int ei = 0; ei<E.rows(); ++ei)
  250. {
  251. const Eigen::RowVector3d &this_color = colors.row(ei);
  252. int f0 = E2F(ei,0);
  253. int f1 = E2F(ei,1);
  254. if(f0 != -1)
  255. {
  256. int i0 = -1;
  257. for (int k = 0; k<3; k++)
  258. if (F2E(f0,k)== ei)
  259. {
  260. i0 = k;
  261. break;
  262. }
  263. C.row(3*f0+i0) = this_color;
  264. }
  265. if(f1 != -1)
  266. {
  267. int i1 = -1;
  268. for (int k = 0; k<3; k++)
  269. if (F2E(f1,k)== ei)
  270. {
  271. i1 = k;
  272. break;
  273. }
  274. C.row(3*f1+i1) = this_color;
  275. }
  276. }
  277. }
  278. void update_display(igl::viewer::Viewer& viewer)
  279. {
  280. using namespace std;
  281. using namespace Eigen;
  282. viewer.data.clear();
  283. viewer.data.lines.resize(0,9);
  284. viewer.data.points.resize(0,6);
  285. viewer.core.show_texture = false;
  286. if (display_mode == 1)
  287. {
  288. cerr<< "Displaying original field, its singularities and its cuts" <<endl;
  289. viewer.data.set_mesh(V, F);
  290. // Highlight in red the constrained faces
  291. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  292. for (unsigned i=0; i<b.size();++i)
  293. C.row(b(i)) << 1, 0, 0;
  294. viewer.data.set_colors(C);
  295. //Draw constraints
  296. drawConstraints(viewer);
  297. // Draw Field
  298. drawField(viewer,two_pv_ori);
  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. drawField(viewer,two_pv);
  318. // Draw Cuts
  319. drawCuts(viewer,cuts);
  320. //Draw Singularities
  321. Eigen::MatrixXd singular_points = igl::slice(V, singularities, 1);
  322. viewer.data.add_points(singular_points,Eigen::RowVector3d(239./255.,205./255.,57./255.));
  323. }
  324. if (display_mode == 3)
  325. {
  326. cerr<< "Displaying original field and its curl" <<endl;
  327. viewer.data.set_mesh(Vbs, Fbs);
  328. Eigen::MatrixXd C;
  329. colorEdgeMeshFaces(curl_ori, 0, 0.2, C);
  330. viewer.data.set_colors(C);
  331. // Draw Field
  332. drawField(viewer,two_pv_ori);
  333. }
  334. if (display_mode == 4)
  335. {
  336. cerr<< "Displaying current field and its curl" <<endl;
  337. viewer.data.set_mesh(Vbs, Fbs);
  338. Eigen::MatrixXd C;
  339. colorEdgeMeshFaces(curl, 0, 0.2, C);
  340. viewer.data.set_colors(C);
  341. // Draw Field
  342. drawField(viewer,two_pv);
  343. }
  344. if (display_mode == 5)
  345. {
  346. cerr<< "Displaying original poisson-integrated field and original poisson error" <<endl;
  347. viewer.data.set_mesh(V, F);
  348. Eigen::MatrixXd C;
  349. igl::jet(poisson_error_ori, 0, 0.5, C);
  350. viewer.data.set_colors(C);
  351. // Draw Field
  352. drawField(viewer,two_pv_poisson_ori);
  353. }
  354. if (display_mode == 6)
  355. {
  356. cerr<< "Displaying current poisson-integrated field and current poisson error" <<endl;
  357. viewer.data.set_mesh(V, F);
  358. Eigen::MatrixXd C;
  359. igl::jet(poisson_error, 0, 0.5, C);
  360. viewer.data.set_colors(C);
  361. // Draw Field
  362. drawField(viewer,two_pv_poisson);
  363. }
  364. if (display_mode == 7)
  365. {
  366. cerr<< "Displaying original texture with cuts and singularities" <<endl;
  367. viewer.data.set_mesh(V, F);
  368. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  369. viewer.data.set_colors(C);
  370. viewer.data.set_uv(uv_scale*scalars_ori, Fcut_ori);
  371. viewer.data.set_texture(texture_R, texture_B, texture_G);
  372. viewer.core.show_texture = true;
  373. // Draw Cuts
  374. drawCuts(viewer,cuts_ori);
  375. //Draw Singularities
  376. Eigen::MatrixXd singular_points = igl::slice(V, singularities_ori, 1);
  377. viewer.data.add_points(singular_points,Eigen::RowVector3d(239./255.,205./255.,57./255.));
  378. }
  379. if (display_mode == 8)
  380. {
  381. cerr<< "Displaying current texture with cuts and singularities" <<endl;
  382. viewer.data.set_mesh(V, F);
  383. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  384. viewer.data.set_colors(C);
  385. viewer.data.set_uv(uv_scale*scalars, Fcut);
  386. viewer.data.set_texture(texture_R, texture_B, texture_G);
  387. viewer.core.show_texture = true;
  388. // Draw Cuts
  389. drawCuts(viewer,cuts);
  390. //Draw Singularities
  391. Eigen::MatrixXd singular_points = igl::slice(V, singularities, 1);
  392. viewer.data.add_points(singular_points,Eigen::RowVector3d(239./255.,205./255.,57./255.));
  393. }
  394. if (display_mode == 9)
  395. {
  396. cerr<< "Displaying original field overlayed onto the current integrated field" <<endl;
  397. viewer.data.set_mesh(V, F);
  398. // Highlight in red the constrained faces
  399. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  400. for (unsigned i=0; i<b.size();++i)
  401. C.row(b(i)) << 1, 0, 0;
  402. viewer.data.set_colors(C);
  403. // Draw Field
  404. drawField(viewer,two_pv_ori);
  405. // Draw Integrated Field
  406. drawField(viewer,two_pv_poisson_ori);
  407. }
  408. if (display_mode == 0)
  409. {
  410. cerr<< "Displaying current field overlayed onto the current integrated field" <<endl;
  411. viewer.data.set_mesh(V, F);
  412. // Highlight in red the constrained faces
  413. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  414. for (unsigned i=0; i<b.size();++i)
  415. C.row(b(i)) << 1, 0, 0;
  416. viewer.data.set_colors(C);
  417. // Draw Field
  418. drawField(viewer,two_pv);
  419. // Draw Integrated Field
  420. drawField(viewer,two_pv_poisson);
  421. }
  422. }
  423. bool key_down(igl::viewer::Viewer& viewer, unsigned char key, int modifier)
  424. {
  425. if (key == '1')
  426. {
  427. display_mode = 1;
  428. update_display(viewer);
  429. }
  430. if (key == '2')
  431. {
  432. display_mode = 2;
  433. update_display(viewer);
  434. }
  435. if (key == '3')
  436. {
  437. display_mode = 3;
  438. update_display(viewer);
  439. }
  440. if (key == '4')
  441. {
  442. display_mode = 4;
  443. update_display(viewer);
  444. }
  445. if (key == '5')
  446. {
  447. display_mode = 5;
  448. update_display(viewer);
  449. }
  450. if (key == '6')
  451. {
  452. display_mode = 6;
  453. update_display(viewer);
  454. }
  455. if (key == '7')
  456. {
  457. display_mode = 7;
  458. update_display(viewer);
  459. }
  460. if (key == '8')
  461. {
  462. display_mode = 8;
  463. update_display(viewer);
  464. }
  465. if (key == '9')
  466. {
  467. display_mode = 9;
  468. update_display(viewer);
  469. }
  470. if (key == '0')
  471. {
  472. display_mode = 0;
  473. update_display(viewer);
  474. }
  475. if (key == 'A')
  476. {
  477. //do a batch of iterations
  478. printf("--Improving Curl--\n");
  479. for (int bi = 0; bi<5; ++bi)
  480. {
  481. printf("\n\n **** Batch %d ****\n", iter);
  482. igl::integrable_polyvector_fields_solve(ipfdata, params, two_pv, iter ==0);
  483. iter++;
  484. params.wSmooth *= params.redFactor_wsmooth;
  485. }
  486. // Post process current field
  487. // Compute curl_minimizing matchings and curl
  488. printf("--Matchings and curl--\n");
  489. Eigen::MatrixXi match_ab, match_ba; // matchings across interior edges
  490. double avgCurl = igl::polyvector_field_matchings(two_pv, V, F, true, match_ab, match_ba, curl);
  491. double maxCurl = curl.maxCoeff();
  492. printf("curl -- max: %.5g, avg: %.5g\n", maxCurl, avgCurl);
  493. // Compute singularities
  494. printf("--Singularities--\n");
  495. igl::polyvector_field_singularities_from_matchings(V, F, match_ab, match_ba, singularities);
  496. printf("#singularities: %ld\n", singularities.rows());
  497. // Get mesh cuts based on singularities
  498. printf("--Cuts--\n");
  499. igl::polyvector_field_cut_mesh_with_singularities(V, F, singularities, cuts);
  500. // Comb field
  501. printf("--Combing--\n");
  502. Eigen::MatrixXd combed;
  503. igl::polyvector_field_comb_from_matchings_and_cuts(V, F, two_pv, match_ab, match_ba, cuts, combed);
  504. // Reconstruct integrable vector fields from combed field
  505. printf("--Cut mesh--\n");
  506. igl::cut_mesh(V, F, cuts, Vcut, Fcut);
  507. printf("--Poisson--\n");
  508. double avgPoisson = igl::polyvector_field_poisson_reconstruction(Vcut, Fcut, combed, scalars, two_pv_poisson, poisson_error);
  509. double maxPoisson = poisson_error.maxCoeff();
  510. printf("poisson error -- max: %.5g, avg: %.5g\n", maxPoisson, avgPoisson);
  511. update_display(viewer);
  512. }
  513. return false;
  514. }
  515. int main(int argc, char *argv[])
  516. {
  517. // Load a mesh
  518. igl::readOBJ("../shared/inspired_mesh.obj", V, F);
  519. printf("--Initialization--\n");
  520. V_border = igl::is_border_vertex(V,F);
  521. igl::adjacency_list(F, VV);
  522. igl::vertex_triangle_adjacency(V,F,VF,VFi);
  523. igl::triangle_triangle_adjacency(V,F,TT,TTi);
  524. igl::edge_topology(V,F,E,F2E,E2F);
  525. // Generate "subdivided" mesh for visualization of curl terms
  526. igl::false_barycentric_subdivision(V, F, Vbs, Fbs);
  527. // Compute scale for visualizing fields
  528. global_scale = .2*igl::avg_edge_length(V, F);
  529. //Compute scale for visualizing texture
  530. uv_scale = 0.6/igl::avg_edge_length(V, F);
  531. // Compute face barycenters
  532. igl::barycenter(V, F, B);
  533. // Compute local basis for faces
  534. igl::local_basis(V,F,B1,B2,B3);
  535. //Generate random vectors for constraints
  536. generate_constraints();
  537. // Interpolate a 2-PolyVector field to be used as the original field
  538. printf("--Initial solution--\n");
  539. igl::n_polyvector(V, F, b, bc, two_pv_ori);
  540. // Post process original field
  541. // Compute curl_minimizing matchings and curl
  542. Eigen::MatrixXi match_ab, match_ba; // matchings across interior edges
  543. printf("--Matchings and curl--\n");
  544. double avgCurl = igl::polyvector_field_matchings(two_pv_ori, V, F, true, match_ab, match_ba, curl_ori);
  545. double maxCurl = curl_ori.maxCoeff();
  546. printf("original curl -- max: %.5g, avg: %.5g\n", maxCurl, avgCurl);
  547. printf("--Singularities--\n");
  548. // Compute singularities
  549. igl::polyvector_field_singularities_from_matchings(V, F, V_border, VF, TT, E2F, F2E, match_ab, match_ba, singularities_ori);
  550. printf("original #singularities: %ld\n", singularities.rows());
  551. printf("--Cuts--\n");
  552. // Get mesh cuts based on singularities
  553. igl::polyvector_field_cut_mesh_with_singularities(V, F, VF, VV, TT, TTi, singularities_ori, cuts_ori);
  554. printf("--Combing--\n");
  555. // Comb field
  556. Eigen::MatrixXd combed;
  557. igl::polyvector_field_comb_from_matchings_and_cuts(V, F, TT, E2F, F2E, two_pv_ori, match_ab, match_ba, cuts_ori, combed);
  558. printf("--Cut mesh--\n");
  559. // Reconstruct integrable vector fields from combed field
  560. igl::cut_mesh(V, F, VF, VFi, TT, TTi, V_border, cuts_ori, Vcut_ori, Fcut_ori);
  561. printf("--Poisson--\n");
  562. double avgPoisson = igl::polyvector_field_poisson_reconstruction(Vcut_ori, Fcut_ori, combed, scalars_ori, two_pv_poisson_ori, poisson_error_ori);
  563. double maxPoisson = poisson_error_ori.maxCoeff();
  564. printf("poisson error -- max: %.5g, avg: %.5g\n", maxPoisson, avgPoisson);
  565. // Set the curl-free 2-PolyVector to equal the original field
  566. two_pv = two_pv_ori;
  567. singularities = singularities_ori;
  568. curl = curl_ori;
  569. cuts = cuts_ori;
  570. two_pv_poisson = two_pv_poisson_ori;
  571. poisson_error = poisson_error_ori;
  572. Vcut = Vcut_ori;
  573. Fcut = Fcut_ori;
  574. scalars = scalars_ori;
  575. printf("--Integrable - Precomputation--\n");
  576. // Precompute stuff for solver
  577. igl::integrable_polyvector_fields_precompute(V, F, b, bc, blevel, two_pv_ori, ipfdata);
  578. cerr<<"Done. Press keys 1-0 for various visualizations, 'A' to improve integrability." <<endl;
  579. igl::viewer::Viewer viewer;
  580. viewer.callback_key_down = &key_down;
  581. viewer.core.show_lines = false;
  582. key_down(viewer,'2',0);
  583. // Replace the standard texture with an integer shift invariant texture
  584. line_texture(texture_R, texture_G, texture_B);
  585. viewer.launch();
  586. return 0;
  587. }