2
0

main.cpp 3.6 KB

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