main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #undef IGL_STATIC_LIBRARY
  2. #include <igl/readOBJ.h>
  3. #include <igl/readDMAT.h>
  4. #include <igl/viewer/Viewer.h>
  5. #include <igl/barycenter.h>
  6. #include <igl/avg_edge_length.h>
  7. #include <vector>
  8. #include <igl/n_polyvector.h>
  9. #include <igl/conjugate_frame_fields.h>
  10. #include <stdlib.h>
  11. #include <igl/readOFF.h>
  12. #include <igl/jet.h>
  13. #include <igl/quad_planarity.h>
  14. #include <igl/planarize_quad_mesh.h>
  15. // Input mesh
  16. Eigen::MatrixXd V;
  17. Eigen::MatrixXi F;
  18. // Face barycenters
  19. Eigen::MatrixXd B;
  20. // Quad mesh generated from smooth field
  21. Eigen::MatrixXd VQS;
  22. Eigen::MatrixXi FQS;
  23. Eigen::MatrixXi FQStri;
  24. // Quad mesh generated from conjugate field
  25. Eigen::MatrixXd VQC;
  26. Eigen::MatrixXi FQC;
  27. Eigen::MatrixXi FQCtri;
  28. Eigen::MatrixXd VQCplan;
  29. // Scale for visualizing the fields
  30. double global_scale;
  31. // Input constraints
  32. Eigen::VectorXi isConstrained;
  33. Eigen::MatrixXd constraints;
  34. Eigen::MatrixXd smooth_pvf;
  35. Eigen::MatrixXd conjugate_pvf;
  36. igl::ConjugateFFSolverData<Eigen::MatrixXd, Eigen::MatrixXi> *csdata;
  37. int conjIter = 2;
  38. int totalConjIter = 0;
  39. double lambdaOrtho = .1;
  40. double lambdaInit = 100;
  41. double lambdaMultFactor = 1.01;
  42. bool doHardConstraints = true;
  43. bool key_down(igl::Viewer& viewer, unsigned char key, int modifier)
  44. {
  45. using namespace std;
  46. using namespace Eigen;
  47. if (key <'1' || key >'6')
  48. return false;
  49. viewer.clear_mesh();
  50. viewer.options.show_lines = false;
  51. viewer.options.show_texture = false;
  52. if (key <= '3')
  53. {
  54. viewer.set_mesh(V, F);
  55. // Highlight in red the constrained faces
  56. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  57. for (unsigned i=0; i<F.rows();++i)
  58. if (isConstrained[i])
  59. C.row(i) << 1, 0, 0;
  60. viewer.set_colors(C);
  61. }
  62. if (key == '1')
  63. {
  64. // Frame field constraints
  65. MatrixXd F1_t = MatrixXd::Zero(F.rows(),3);
  66. MatrixXd F2_t = MatrixXd::Zero(F.rows(),3);
  67. for (unsigned i=0; i<F.rows();++i)
  68. if (isConstrained[i])
  69. {
  70. F1_t.row(i) = constraints.block(i,0,1,3);
  71. F2_t.row(i) = constraints.block(i,3,1,3);
  72. }
  73. viewer.add_edges (B - global_scale*F1_t, B + global_scale*F1_t , Eigen::RowVector3d(0,0,1));
  74. viewer.add_edges (B - global_scale*F2_t, B + global_scale*F2_t , Eigen::RowVector3d(0,0,1));
  75. }
  76. if (key == '2')
  77. {
  78. viewer.add_edges (B - global_scale*smooth_pvf.block(0,0,F.rows(),3),
  79. B + global_scale*smooth_pvf.block(0,0,F.rows(),3),
  80. Eigen::RowVector3d(0,1,0));
  81. viewer.add_edges (B - global_scale*smooth_pvf.block(0,3,F.rows(),3),
  82. B + global_scale*smooth_pvf.block(0,3,F.rows(),3),
  83. Eigen::RowVector3d(0,1,0));
  84. }
  85. if (key == '3')
  86. {
  87. if (totalConjIter <50)
  88. {
  89. double lambdaOut;
  90. igl::conjugate_frame_fields(*csdata, isConstrained, conjugate_pvf, conjugate_pvf, conjIter, lambdaOrtho, lambdaInit, lambdaMultFactor, doHardConstraints,
  91. &lambdaOut);
  92. totalConjIter += 2;
  93. lambdaInit = lambdaOut;
  94. }
  95. viewer.add_edges (B - global_scale*conjugate_pvf.block(0,0,F.rows(),3),
  96. B + global_scale*conjugate_pvf.block(0,0,F.rows(),3),
  97. Eigen::RowVector3d(0,1,0));
  98. viewer.add_edges (B - global_scale*conjugate_pvf.block(0,3,F.rows(),3),
  99. B + global_scale*conjugate_pvf.block(0,3,F.rows(),3),
  100. Eigen::RowVector3d(0,1,0));
  101. }
  102. if (key == '4')
  103. {
  104. viewer.set_mesh(VQS, FQStri);
  105. // show planarity
  106. VectorXd planarity;
  107. igl::quad_planarity( VQS, FQS, planarity);
  108. MatrixXd Ct;
  109. igl::jet(planarity, 0, 0.02, Ct);
  110. MatrixXd C(FQStri.rows(),3);
  111. C << Ct, Ct;
  112. viewer.set_colors(C);
  113. }
  114. if (key == '5')
  115. {
  116. viewer.set_mesh(VQC, FQCtri);
  117. // show planarity
  118. VectorXd planarity;
  119. igl::quad_planarity( VQC, FQC, planarity);
  120. MatrixXd Ct;
  121. igl::jet(planarity, 0, 0.02, Ct);
  122. MatrixXd C(FQCtri.rows(),3);
  123. C << Ct, Ct;
  124. viewer.set_colors(C);
  125. }
  126. if (key == '6')
  127. {
  128. igl ::planarize_quad_mesh(VQC, FQC, 50, 0.01, VQCplan);
  129. viewer.set_mesh(VQCplan, FQCtri);
  130. // show planarity
  131. VectorXd planarity;
  132. igl::quad_planarity( VQCplan, FQC, planarity);
  133. MatrixXd Ct;
  134. igl::jet(planarity, 0, 0.02, Ct);
  135. MatrixXd C(FQCtri.rows(),3);
  136. C << Ct, Ct;
  137. viewer.set_colors(C);
  138. }
  139. return false;
  140. }
  141. int main(int argc, char *argv[])
  142. {
  143. using namespace Eigen;
  144. using namespace std;
  145. // Load a mesh in OBJ format
  146. igl::readOBJ("../shared/inspired_mesh.obj", V, F);
  147. // Compute face barycenters
  148. igl::barycenter(V, F, B);
  149. // Compute scale for visualizing fields
  150. global_scale = .2*igl::avg_edge_length(V, F);
  151. // Load constraints
  152. MatrixXd temp;
  153. igl::readDMAT("../shared/inspired_mesh.dmat",temp);
  154. isConstrained = temp.block(0,0,temp.rows(),1).cast<int>();
  155. constraints = temp.block(0,1,temp.rows(),temp.cols()-1);
  156. // Interpolate to get a smooth field
  157. igl::n_polyvector(V, F, isConstrained, constraints, smooth_pvf);
  158. // Initialize conjugate field with smooth field
  159. csdata = new igl::ConjugateFFSolverData<Eigen::MatrixXd,Eigen::MatrixXi>(V,F);
  160. conjugate_pvf = smooth_pvf;
  161. // Load quad mesh generated by smooth field
  162. igl::readOFF("../shared/inspired_mesh_quads_Smooth.off", VQS, FQS);
  163. FQStri.resize(2*FQS.rows(), 3);
  164. FQStri << FQS.col(0),FQS.col(1),FQS.col(2),
  165. FQS.col(2),FQS.col(3),FQS.col(0);
  166. // Load quad mesh generated by conjugate field
  167. igl::readOFF("../shared/inspired_mesh_quads_Conjugate.off", VQC, FQC);
  168. FQCtri.resize(2*FQC.rows(), 3);
  169. FQCtri << FQC.col(0),FQC.col(1),FQC.col(2),
  170. FQC.col(2),FQC.col(3),FQC.col(0);
  171. igl::Viewer viewer;
  172. // Plot the original mesh with a texture parametrization
  173. key_down(viewer,'1',0);
  174. // Launch the viewer
  175. viewer.callback_key_down = &key_down;
  176. viewer.launch();
  177. }