main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <igl/barycenter.h>
  2. #include <igl/boundary_facets.h>
  3. #include <igl/parula.h>
  4. #include <igl/readMESH.h>
  5. #include <igl/slice.h>
  6. #include <igl/marching_tets.h>
  7. #include <igl/winding_number.h>
  8. #include <igl/opengl/glfw/Viewer.h>
  9. #include <Eigen/Sparse>
  10. #include <iostream>
  11. Eigen::MatrixXd V,BC;
  12. Eigen::VectorXd W;
  13. Eigen::MatrixXi T,F,G;
  14. double slice_z = 0.5;
  15. enum OverLayType
  16. {
  17. OVERLAY_NONE = 0,
  18. OVERLAY_INPUT = 1,
  19. OVERLAY_OUTPUT = 2,
  20. NUM_OVERLAY = 3,
  21. } overlay = OVERLAY_NONE;
  22. void update_visualization(igl::opengl::glfw::Viewer & viewer)
  23. {
  24. using namespace Eigen;
  25. using namespace std;
  26. Eigen::Vector4d plane(
  27. 0,0,1,-((1-slice_z)*V.col(2).minCoeff()+slice_z*V.col(2).maxCoeff()));
  28. MatrixXd V_vis;
  29. MatrixXi F_vis;
  30. VectorXi J;
  31. {
  32. SparseMatrix<double> bary;
  33. // Value of plane's implicit function at all vertices
  34. const VectorXd IV =
  35. (V.col(0)*plane(0) +
  36. V.col(1)*plane(1) +
  37. V.col(2)*plane(2)).array()
  38. + plane(3);
  39. igl::marching_tets(V,T,IV,V_vis,F_vis,J,bary);
  40. }
  41. VectorXd W_vis;
  42. igl::slice(W,J,W_vis);
  43. MatrixXd C_vis;
  44. // color without normalizing
  45. igl::parula(W_vis,false,C_vis);
  46. const auto & append_mesh = [&C_vis,&F_vis,&V_vis](
  47. const Eigen::MatrixXd & V,
  48. const Eigen::MatrixXi & F,
  49. const RowVector3d & color)
  50. {
  51. F_vis.conservativeResize(F_vis.rows()+F.rows(),3);
  52. F_vis.bottomRows(F.rows()) = F.array()+V_vis.rows();
  53. V_vis.conservativeResize(V_vis.rows()+V.rows(),3);
  54. V_vis.bottomRows(V.rows()) = V;
  55. C_vis.conservativeResize(C_vis.rows()+F.rows(),3);
  56. C_vis.bottomRows(F.rows()).rowwise() = color;
  57. };
  58. switch(overlay)
  59. {
  60. case OVERLAY_INPUT:
  61. append_mesh(V,F,RowVector3d(1.,0.894,0.227));
  62. break;
  63. case OVERLAY_OUTPUT:
  64. append_mesh(V,G,RowVector3d(0.8,0.8,0.8));
  65. break;
  66. default:
  67. break;
  68. }
  69. viewer.data().clear();
  70. viewer.data().set_mesh(V_vis,F_vis);
  71. viewer.data().set_colors(C_vis);
  72. viewer.data().set_face_based(true);
  73. }
  74. bool key_down(igl::opengl::glfw::Viewer& viewer, unsigned char key, int mod)
  75. {
  76. switch(key)
  77. {
  78. default:
  79. return false;
  80. case ' ':
  81. overlay = (OverLayType)((1+(int)overlay)%NUM_OVERLAY);
  82. break;
  83. case '.':
  84. slice_z = std::min(slice_z+0.01,0.99);
  85. break;
  86. case ',':
  87. slice_z = std::max(slice_z-0.01,0.01);
  88. break;
  89. }
  90. update_visualization(viewer);
  91. return true;
  92. }
  93. int main(int argc, char *argv[])
  94. {
  95. using namespace Eigen;
  96. using namespace std;
  97. cout<<"Usage:"<<endl;
  98. cout<<"[space] toggle showing input mesh, output mesh or slice "<<endl;
  99. cout<<" through tet-mesh of convex hull."<<endl;
  100. cout<<"'.'/',' push back/pull forward slicing plane."<<endl;
  101. cout<<endl;
  102. // Load mesh: (V,T) tet-mesh of convex hull, F contains facets of input
  103. // surface mesh _after_ self-intersection resolution
  104. igl::readMESH(TUTORIAL_SHARED_PATH "/big-sigcat.mesh",V,T,F);
  105. // Compute barycenters of all tets
  106. igl::barycenter(V,T,BC);
  107. // Compute generalized winding number at all barycenters
  108. cout<<"Computing winding number over all "<<T.rows()<<" tets..."<<endl;
  109. igl::winding_number(V,F,BC,W);
  110. // Extract interior tets
  111. MatrixXi CT((W.array()>0.5).count(),4);
  112. {
  113. size_t k = 0;
  114. for(size_t t = 0;t<T.rows();t++)
  115. {
  116. if(W(t)>0.5)
  117. {
  118. CT.row(k) = T.row(t);
  119. k++;
  120. }
  121. }
  122. }
  123. // find bounary facets of interior tets
  124. igl::boundary_facets(CT,G);
  125. // boundary_facets seems to be reversed...
  126. G = G.rowwise().reverse().eval();
  127. // normalize
  128. W = (W.array() - W.minCoeff())/(W.maxCoeff()-W.minCoeff());
  129. // Plot the generated mesh
  130. igl::opengl::glfw::Viewer viewer;
  131. update_visualization(viewer);
  132. viewer.callback_key_down = &key_down;
  133. viewer.launch();
  134. }