main.cpp 3.9 KB

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