main.cpp 5.0 KB

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