main.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <igl/read_triangle_mesh.h>
  2. #include <igl/AABB.h>
  3. #include <igl/unique.h>
  4. #include <igl/opengl/glfw/Viewer.h>
  5. #include <igl/fast_find_intersections.h>
  6. Eigen::MatrixXd V1,V2;
  7. Eigen::MatrixXi F1,F2;
  8. igl::AABB<Eigen::MatrixXd,3> tree;
  9. double min_z,max_z;
  10. double slice_z;
  11. void update_visualization(igl::opengl::glfw::Viewer & viewer)
  12. {
  13. //shifted intersection object
  14. Eigen::MatrixXd V2_(V2.rows(),V2.cols());
  15. V2_<< V2.col(0), V2.col(1), V2.col(2).array()+slice_z;
  16. Eigen::MatrixXi IF,EE;
  17. Eigen::MatrixXd EV;
  18. Eigen::VectorXi EI;
  19. igl::fast_find_intersections(tree, V1,F1, V2_,F2,false,false,IF,EV,EE,EI);
  20. // Plot the edges of the intersects
  21. viewer.data().set_edges( EV,EE, Eigen::RowVector3d(1,0,0));
  22. // show faces which are intersected
  23. Eigen::VectorXi I;
  24. igl::unique(IF,I);
  25. Eigen::VectorXd D = Eigen::MatrixXd::Zero(F1.rows(),1);
  26. D(I).setConstant(1.0);
  27. viewer.data().set_data(D,0,1,igl::COLOR_MAP_TYPE_PARULA);
  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::read_triangle_mesh(
  49. argc<=1?TUTORIAL_SHARED_PATH "/cow.off" :argv[1],V1,F1);
  50. igl::read_triangle_mesh(
  51. argc<=2?TUTORIAL_SHARED_PATH "/planexy.off":argv[2],V2,F2);
  52. // initialize AABB tree with first mesh (it doesn't move)
  53. tree.init(V1,F1);
  54. std::cout<<"Usage:"<<std::endl;
  55. std::cout<<"'.'/',' push back/pull forward slicing plane."<<std::endl;
  56. std::cout<<std::endl;
  57. min_z=V1.col(2).minCoeff();
  58. max_z=V1.col(2).maxCoeff();
  59. slice_z=(max_z+min_z)/2;
  60. //center slicing object
  61. V2.col(2).array() -= V2.col(2).array().mean();
  62. // Plot the mesh
  63. igl::opengl::glfw::Viewer viewer;
  64. viewer.data().set_mesh(V1, F1);
  65. viewer.data().set_face_based(true);
  66. update_visualization(viewer);
  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. }