main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <igl/avg_edge_length.h>
  2. #include <igl/barycenter.h>
  3. #include <igl/jet.h>
  4. #include <igl/shapeup.h>
  5. #include <igl/quad_planarity.h>
  6. #include <igl/readDMAT.h>
  7. #include <igl/readOFF.h>
  8. #include <igl/opengl/glfw/Viewer.h>
  9. #include <igl/PI.h>
  10. #include <vector>
  11. #include <cstdlib>
  12. // Quad mesh loaded
  13. Eigen::MatrixXd VQC;
  14. Eigen::MatrixXi FQC;
  15. Eigen::MatrixXi E;
  16. Eigen::MatrixXi FQCtri;
  17. Eigen::MatrixXd PQC0, PQC1, PQC2, PQC3;
  18. // Euclidean-regular quad mesh
  19. Eigen::MatrixXd VQCregular;
  20. Eigen::MatrixXi FQCtriregular;
  21. Eigen::MatrixXd PQC0regular, PQC1regular, PQC2regular, PQC3regular;
  22. igl::ShapeupData su_data;
  23. // Scale for visualizing the fields
  24. double global_scale; //TODO: not used
  25. void quadAngleRegularity(const Eigen::MatrixXd& V, const Eigen::MatrixXi& Q, Eigen::VectorXd& angleRegularity)
  26. {
  27. angleRegularity.conservativeResize(Q.rows());
  28. angleRegularity.setZero();
  29. for (int i=0;i<Q.rows();i++){
  30. for (int j=0;j<4;j++){
  31. Eigen::RowVectorXd v21=(V.row(Q(i,j))-V.row(Q(i,(j+1)%4))).normalized();
  32. Eigen::RowVectorXd v23=(V.row(Q(i,(j+2)%4))-V.row(Q(i,(j+1)%4))).normalized();
  33. angleRegularity(i)+=(abs(acos(v21.dot(v23))-igl::PI/2.0)/(igl::PI/2.0))/4.0;
  34. }
  35. }
  36. }
  37. bool key_down(igl::opengl::glfw::Viewer& viewer, unsigned char key, int modifier)
  38. {
  39. using namespace std;
  40. using namespace Eigen;
  41. // Plot the original quad mesh
  42. if (key == '1')
  43. {
  44. viewer.data().clear();
  45. // Draw the triangulated quad mesh
  46. viewer.data().set_mesh(VQC, FQCtri);
  47. // Assign a color to each quad that corresponds to the average deviation of each angle from pi/2
  48. VectorXd angleRegularity(FQC.rows());
  49. quadAngleRegularity( VQC, FQC, angleRegularity);
  50. MatrixXd Ct;
  51. igl::jet(angleRegularity, 0.0, 0.05, Ct);
  52. MatrixXd C(FQCtri.rows(),3);
  53. C << Ct, Ct;
  54. viewer.data().set_colors(C);
  55. // Plot a line for each edge of the quad mesh
  56. viewer.data().add_edges(PQC0, PQC1, Eigen::RowVector3d(0,0,0));
  57. viewer.data().add_edges(PQC1, PQC2, Eigen::RowVector3d(0,0,0));
  58. viewer.data().add_edges(PQC2, PQC3, Eigen::RowVector3d(0,0,0));
  59. viewer.data().add_edges(PQC3, PQC0, Eigen::RowVector3d(0,0,0));
  60. }
  61. // Plot the planarized quad mesh
  62. if (key == '2')
  63. {
  64. viewer.data().clear();
  65. // Draw the triangulated quad mesh
  66. viewer.data().set_mesh(VQCregular, FQCtri);
  67. // Assign a color to each quad that corresponds to its planarity
  68. VectorXd angleRegularity(FQC.rows());
  69. quadAngleRegularity( VQCregular, FQC, angleRegularity);
  70. MatrixXd Ct;
  71. igl::jet(angleRegularity, 0, 0.05, Ct);
  72. MatrixXd C(FQCtri.rows(),3);
  73. C << Ct, Ct;
  74. viewer.data().set_colors(C);
  75. // Plot a line for each edge of the quad mesh
  76. viewer.data().add_edges(PQC0regular, PQC1regular, Eigen::RowVector3d(0,0,0));
  77. viewer.data().add_edges(PQC1regular, PQC2regular, Eigen::RowVector3d(0,0,0));
  78. viewer.data().add_edges(PQC2regular, PQC3regular, Eigen::RowVector3d(0,0,0));
  79. viewer.data().add_edges(PQC3regular, PQC0regular, Eigen::RowVector3d(0,0,0));
  80. }
  81. return false;
  82. }
  83. int main(int argc, char *argv[])
  84. {
  85. using namespace Eigen;
  86. using namespace std;
  87. // Load a quad mesh
  88. igl::readOFF(TUTORIAL_SHARED_PATH "/halftunnel.off", VQC, FQC);
  89. // Convert it in a triangle mesh
  90. FQCtri.resize(2*FQC.rows(), 3);
  91. FQCtri << FQC.col(0),FQC.col(1),FQC.col(2),
  92. FQC.col(2),FQC.col(3),FQC.col(0);
  93. PQC0 = VQC(FQC.col(0).eval(), Eigen::placeholders::all);
  94. PQC1 = VQC(FQC.col(1).eval(), Eigen::placeholders::all);
  95. PQC2 = VQC(FQC.col(2).eval(), Eigen::placeholders::all);
  96. PQC3 = VQC(FQC.col(3).eval(), Eigen::placeholders::all);
  97. // Create a planar version with ShapeUp
  98. //igl::planarize_quad_mesh(VQC, FQC, 100, 0.005, VQCregular);
  99. E.resize(FQC.size(),2);
  100. E.col(0)<<FQC.col(0),FQC.col(1),FQC.col(2),FQC.col(3);
  101. E.col(1)<<FQC.col(1),FQC.col(2),FQC.col(3),FQC.col(0);
  102. VectorXi b(1); b(0)=0; //setting the first vertex to be the same.
  103. VectorXd wShape=VectorXd::Constant(FQC.rows(),1.0);
  104. VectorXd wSmooth=VectorXd::Constant(E.rows(),1.0);
  105. MatrixXd bc(1,3); bc<<VQC.row(0);
  106. VectorXi array_of_fours=VectorXi::Constant(FQC.rows(),4);
  107. igl::shapeup_projection_function localFunction(igl::shapeup_regular_face_projection);
  108. su_data.maxIterations=200;
  109. shapeup_precomputation(VQC, array_of_fours,FQC,E,b,wShape, wSmooth,su_data);
  110. shapeup_solve(bc,localFunction, VQC,su_data, false,VQCregular);
  111. // Convert the planarized mesh to triangles
  112. PQC0regular = VQCregular(FQC.col(0).eval(), Eigen::placeholders::all);
  113. PQC1regular = VQCregular(FQC.col(1).eval(), Eigen::placeholders::all);
  114. PQC2regular = VQCregular(FQC.col(2).eval(), Eigen::placeholders::all);
  115. PQC3regular = VQCregular(FQC.col(3).eval(), Eigen::placeholders::all);
  116. // Launch the viewer
  117. igl::opengl::glfw::Viewer viewer;
  118. key_down(viewer,'1',0);
  119. viewer.data().invert_normals = true;
  120. viewer.data().show_lines = false;
  121. viewer.callback_key_down = &key_down;
  122. viewer.launch();
  123. }