main.cpp 4.9 KB

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