main.cpp 5.0 KB

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