main.cpp 4.6 KB

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