main.cpp 4.4 KB

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