main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <igl/cat.h>
  2. #include <igl/edge_lengths.h>
  3. #include <igl/parula.h>
  4. #include <igl/per_edge_normals.h>
  5. #include <igl/per_face_normals.h>
  6. #include <igl/per_vertex_normals.h>
  7. #include <igl/point_mesh_squared_distance.h>
  8. #include <igl/readMESH.h>
  9. #include <igl/signed_distance.h>
  10. #include <igl/slice_mask.h>
  11. #include <igl/slice_tets.h>
  12. #include <igl/upsample.h>
  13. #include <igl/viewer/Viewer.h>
  14. #include <Eigen/Sparse>
  15. #include <iostream>
  16. Eigen::MatrixXd V;
  17. Eigen::MatrixXi T,F;
  18. igl::AABB<Eigen::MatrixXd,3> tree;
  19. Eigen::MatrixXd FN,VN,EN;
  20. Eigen::MatrixXi E;
  21. Eigen::VectorXi EMAP;
  22. double max_distance = 1;
  23. double slice_z = 0.5;
  24. bool overlay = false;
  25. void update_visualization(igl::Viewer & viewer)
  26. {
  27. using namespace Eigen;
  28. using namespace std;
  29. Eigen::Vector4d plane(
  30. 0,0,1,-((1-slice_z)*V.col(2).minCoeff()+slice_z*V.col(2).maxCoeff()));
  31. MatrixXd V_vis;
  32. MatrixXi F_vis;
  33. // Extract triangle mesh slice through volume mesh and subdivide nasty
  34. // triangles
  35. {
  36. VectorXi J;
  37. SparseMatrix<double> bary;
  38. igl::slice_tets(V,T,plane,V_vis,F_vis,J,bary);
  39. while(true)
  40. {
  41. MatrixXd l;
  42. igl::edge_lengths(V_vis,F_vis,l);
  43. l /= (V_vis.colwise().maxCoeff() - V_vis.colwise().minCoeff()).norm();
  44. const double max_l = 0.03;
  45. if(l.maxCoeff()<max_l)
  46. {
  47. break;
  48. }
  49. Array<bool,Dynamic,1> bad = l.array().rowwise().maxCoeff() > max_l;
  50. MatrixXi F_vis_bad, F_vis_good;
  51. igl::slice_mask(F_vis,bad,1,F_vis_bad);
  52. igl::slice_mask(F_vis,(bad!=true).eval(),1,F_vis_good);
  53. igl::upsample(V_vis,F_vis_bad);
  54. F_vis = igl::cat(1,F_vis_bad,F_vis_good);
  55. }
  56. }
  57. // Compute signed distance
  58. VectorXd S_vis;
  59. {
  60. VectorXi I;
  61. MatrixXd N,C;
  62. // Bunny is a watertight mesh so use pseudonormal for signing
  63. signed_distance_pseudonormal(V_vis,V,F,tree,FN,VN,EN,EMAP,S_vis,I,C,N);
  64. }
  65. // push to [0,1] range
  66. S_vis.array() = 0.5*(S_vis.array()/max_distance)+0.5;
  67. MatrixXd C_vis;
  68. // color without normalizing
  69. igl::parula(S_vis,false,C_vis);
  70. const auto & append_mesh = [&C_vis,&F_vis,&V_vis](
  71. const Eigen::MatrixXd & V,
  72. const Eigen::MatrixXi & F,
  73. const RowVector3d & color)
  74. {
  75. F_vis.conservativeResize(F_vis.rows()+F.rows(),3);
  76. F_vis.bottomRows(F.rows()) = F.array()+V_vis.rows();
  77. V_vis.conservativeResize(V_vis.rows()+V.rows(),3);
  78. V_vis.bottomRows(V.rows()) = V;
  79. C_vis.conservativeResize(C_vis.rows()+V.rows(),3);
  80. C_vis.bottomRows(V.rows()).rowwise() = color;
  81. };
  82. if(overlay)
  83. {
  84. append_mesh(V,F,RowVector3d(0.8,0.8,0.8));
  85. }
  86. viewer.data.clear();
  87. viewer.data.set_mesh(V_vis,F_vis);
  88. viewer.data.set_colors(C_vis);
  89. viewer.core.lighting_factor = overlay;
  90. }
  91. bool key_down(igl::Viewer& viewer, unsigned char key, int mod)
  92. {
  93. switch(key)
  94. {
  95. default:
  96. return false;
  97. case ' ':
  98. overlay ^= true;
  99. break;
  100. case '.':
  101. slice_z = std::min(slice_z+0.01,0.99);
  102. break;
  103. case ',':
  104. slice_z = std::max(slice_z-0.01,0.01);
  105. break;
  106. }
  107. update_visualization(viewer);
  108. return true;
  109. }
  110. int main(int argc, char *argv[])
  111. {
  112. using namespace Eigen;
  113. using namespace std;
  114. cout<<"Usage:"<<endl;
  115. cout<<"[space] toggle showing surface."<<endl;
  116. cout<<"'.'/',' push back/pull forward slicing plane."<<endl;
  117. cout<<endl;
  118. // Load mesh: (V,T) tet-mesh of convex hull, F contains original surface
  119. // triangles
  120. igl::readMESH("../shared/bunny.mesh",V,T,F);
  121. // Encapsulated call to point_mesh_squared_distance to determine bounds
  122. {
  123. VectorXd sqrD;
  124. VectorXi I;
  125. MatrixXd C;
  126. igl::point_mesh_squared_distance(V,V,F,sqrD,I,C);
  127. max_distance = sqrt(sqrD.maxCoeff());
  128. }
  129. // Precompute signed distance AABB tree
  130. tree.init(V,F);
  131. // Precompute vertex,edge and face normals
  132. igl::per_face_normals(V,F,FN);
  133. igl::per_vertex_normals(
  134. V,F,igl::PER_VERTEX_NORMALS_WEIGHTING_TYPE_ANGLE,FN,VN);
  135. igl::per_edge_normals(
  136. V,F,igl::PER_EDGE_NORMALS_WEIGHTING_TYPE_UNIFORM,FN,EN,E,EMAP);
  137. // Plot the generated mesh
  138. igl::Viewer viewer;
  139. update_visualization(viewer);
  140. viewer.callback_key_down = &key_down;
  141. viewer.core.show_lines = false;
  142. viewer.launch();
  143. }