main.cpp 4.3 KB

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