main.cpp 3.6 KB

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