main.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. cout<< S_vis.rows() << S_vis.cols() << endl;
  66. }
  67. // push to [0,1] range
  68. S_vis.array() = 0.5*(S_vis.array()/max_distance)+0.5;
  69. MatrixXd C_vis;
  70. // color without normalizing
  71. igl::parula(S_vis,false,C_vis);
  72. const auto & append_mesh = [&C_vis,&F_vis,&V_vis](
  73. const Eigen::MatrixXd & V,
  74. const Eigen::MatrixXi & F,
  75. const RowVector3d & color)
  76. {
  77. F_vis.conservativeResize(F_vis.rows()+F.rows(),3);
  78. F_vis.bottomRows(F.rows()) = F.array()+V_vis.rows();
  79. V_vis.conservativeResize(V_vis.rows()+V.rows(),3);
  80. V_vis.bottomRows(V.rows()) = V;
  81. C_vis.conservativeResize(C_vis.rows()+V.rows(),3);
  82. C_vis.bottomRows(V.rows()).rowwise() = color;
  83. };
  84. if(overlay)
  85. {
  86. append_mesh(V,F,RowVector3d(0.8,0.8,0.8));
  87. }
  88. viewer.data.clear();
  89. viewer.data.set_mesh(V_vis,F_vis);
  90. viewer.data.set_colors(C_vis);
  91. viewer.core.lighting_factor = overlay;
  92. }
  93. bool key_down(igl::viewer::Viewer& viewer, unsigned char key, int mod)
  94. {
  95. switch(key)
  96. {
  97. default:
  98. return false;
  99. case ' ':
  100. overlay ^= true;
  101. break;
  102. case '.':
  103. slice_z = std::min(slice_z+0.01,0.99);
  104. break;
  105. case ',':
  106. slice_z = std::max(slice_z-0.01,0.01);
  107. break;
  108. }
  109. update_visualization(viewer);
  110. return true;
  111. }
  112. int main(int argc, char *argv[])
  113. {
  114. using namespace Eigen;
  115. using namespace std;
  116. cout<<"Usage:"<<endl;
  117. cout<<"[space] toggle showing surface."<<endl;
  118. cout<<"'.'/',' push back/pull forward slicing plane."<<endl;
  119. cout<<endl;
  120. // Load mesh: (V,T) tet-mesh of convex hull, F contains original surface
  121. // triangles
  122. igl::readMESH(TUTORIAL_SHARED_PATH "/bunny.mesh",V,T,F);
  123. // Encapsulated call to point_mesh_squared_distance to determine bounds
  124. {
  125. VectorXd sqrD;
  126. VectorXi I;
  127. MatrixXd C;
  128. igl::point_mesh_squared_distance(V,V,F,sqrD,I,C);
  129. max_distance = sqrt(sqrD.maxCoeff());
  130. }
  131. // Precompute signed distance AABB tree
  132. tree.init(V,F);
  133. // Precompute vertex,edge and face normals
  134. igl::per_face_normals(V,F,FN);
  135. igl::per_vertex_normals(
  136. V,F,igl::PER_VERTEX_NORMALS_WEIGHTING_TYPE_ANGLE,FN,VN);
  137. igl::per_edge_normals(
  138. V,F,igl::PER_EDGE_NORMALS_WEIGHTING_TYPE_UNIFORM,FN,EN,E,EMAP);
  139. // Plot the generated mesh
  140. igl::viewer::Viewer viewer;
  141. update_visualization(viewer);
  142. viewer.callback_key_down = &key_down;
  143. viewer.core.show_lines = false;
  144. viewer.launch();
  145. }