main.cpp 5.0 KB

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