main.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <igl/readOFF.h>
  2. #include <igl/combine.h>
  3. #include <igl/opengl/glfw/Viewer.h>
  4. //#include <igl/opengl/glfw/imgui/ImGuiPlugin.h>
  5. #include <igl/opengl/glfw/imgui/ImGuiMenu.h>
  6. #include <igl/fast_find_mesh_intersect.h>
  7. Eigen::MatrixXd V1,V2;
  8. Eigen::MatrixXi F1,F2;
  9. igl::AABB<Eigen::MatrixXd,3> tree;
  10. double min_z,max_z;
  11. double slice_z;
  12. void update_visualization(igl::opengl::glfw::Viewer & viewer)
  13. {
  14. Eigen::MatrixXi I;
  15. Eigen::MatrixXd edges;
  16. //shifted intersection object
  17. Eigen::MatrixXd V2_(V2.rows(),V2.cols());
  18. V2_<< V2.col(0), V2.col(1), V2.col(2).array()+slice_z;
  19. igl::fast_find_mesh_intersect(tree, V1,F1, V2_,F2, I,edges);
  20. Eigen::MatrixXi edges_link=Eigen::MatrixXi::NullaryExpr(edges.rows()/2,2, [](int i,int j) { return i*2+j;});
  21. // Plot the edges of the intersects
  22. viewer.data().set_edges ( edges, edges_link, Eigen::RowVector3d(1,0,0));
  23. // show faces which are intersected
  24. Eigen::VectorXd face_data=Eigen::VectorXd::Zero(F1.rows());
  25. for(int i=0; i<I.rows(); ++i)
  26. face_data(I(i,0)) = 1.0;
  27. viewer.data().set_data(face_data);
  28. }
  29. bool key_down(igl::opengl::glfw::Viewer& viewer, unsigned char key, int mod)
  30. {
  31. switch(key)
  32. {
  33. default:
  34. return false;
  35. case '.':
  36. slice_z = std::min(slice_z+0.01,max_z);
  37. break;
  38. case ',':
  39. slice_z = std::max(slice_z-0.01,min_z);
  40. break;
  41. }
  42. update_visualization(viewer);
  43. return true;
  44. }
  45. int main(int argc, char *argv[])
  46. {
  47. // Load two meshes
  48. igl::readOFF(TUTORIAL_SHARED_PATH "/cow.off", V1, F1);
  49. igl::readOFF(TUTORIAL_SHARED_PATH "/planexy.off", V2, F2);
  50. // initialize AABB tree with first mesh (it doesn't move)
  51. tree.init(V1,F1);
  52. std::cout<<"Usage:"<<std::endl;
  53. std::cout<<"'.'/',' push back/pull forward slicing plane."<<std::endl;
  54. std::cout<<std::endl;
  55. min_z=V1.col(2).minCoeff();
  56. max_z=V1.col(2).maxCoeff();
  57. slice_z=(max_z+min_z)/2;
  58. //center slicing object
  59. V2.col(2).array() -= V2.col(2).array().mean();
  60. // Plot the mesh
  61. igl::opengl::glfw::Viewer viewer;
  62. viewer.data().set_mesh(V1, F1);
  63. update_visualization(viewer);
  64. igl::opengl::glfw::imgui::ImGuiMenu menu;
  65. //plugin.widgets.push_back(&menu);
  66. menu.callback_draw_viewer_window = [](){};
  67. viewer.callback_key_down = &key_down;
  68. // show the cow closer
  69. viewer.core().camera_zoom = 2.0;
  70. // Launch the viewer
  71. viewer.launch();
  72. }