main.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <igl/readOFF.h>
  2. //#undef IGL_STATIC_LIBRARY
  3. #include <igl/copyleft/cgal/mesh_boolean.h>
  4. #include <igl/opengl/glfw/Viewer.h>
  5. #include <Eigen/Core>
  6. #include <iostream>
  7. #include "tutorial_shared_path.h"
  8. Eigen::MatrixXd VA,VB,VC;
  9. Eigen::VectorXi J,I;
  10. Eigen::MatrixXi FA,FB,FC;
  11. igl::MeshBooleanType boolean_type(
  12. igl::MESH_BOOLEAN_TYPE_UNION);
  13. const char * MESH_BOOLEAN_TYPE_NAMES[] =
  14. {
  15. "Union",
  16. "Intersect",
  17. "Minus",
  18. "XOR",
  19. "Resolve",
  20. };
  21. void update(igl::opengl::glfw::Viewer &viewer)
  22. {
  23. igl::copyleft::cgal::mesh_boolean(VA,FA,VB,FB,boolean_type,VC,FC,J);
  24. Eigen::MatrixXd C(FC.rows(),3);
  25. for(size_t f = 0;f<C.rows();f++)
  26. {
  27. if(J(f)<FA.rows())
  28. {
  29. C.row(f) = Eigen::RowVector3d(1,0,0);
  30. }else
  31. {
  32. C.row(f) = Eigen::RowVector3d(0,1,0);
  33. }
  34. }
  35. viewer.data().clear();
  36. viewer.data().set_mesh(VC,FC);
  37. viewer.data().set_colors(C);
  38. std::cout<<"A "<<MESH_BOOLEAN_TYPE_NAMES[boolean_type]<<" B."<<std::endl;
  39. }
  40. bool key_down(igl::opengl::glfw::Viewer &viewer, unsigned char key, int mods)
  41. {
  42. switch(key)
  43. {
  44. default:
  45. return false;
  46. case '.':
  47. boolean_type =
  48. static_cast<igl::MeshBooleanType>(
  49. (boolean_type+1)% igl::NUM_MESH_BOOLEAN_TYPES);
  50. break;
  51. case ',':
  52. boolean_type =
  53. static_cast<igl::MeshBooleanType>(
  54. (boolean_type+igl::NUM_MESH_BOOLEAN_TYPES-1)%
  55. igl::NUM_MESH_BOOLEAN_TYPES);
  56. break;
  57. case '[':
  58. viewer.core().camera_dnear -= 0.1;
  59. return true;
  60. case ']':
  61. viewer.core().camera_dnear += 0.1;
  62. return true;
  63. }
  64. update(viewer);
  65. return true;
  66. }
  67. int main(int argc, char *argv[])
  68. {
  69. using namespace Eigen;
  70. using namespace std;
  71. igl::readOFF(TUTORIAL_SHARED_PATH "/cheburashka.off",VA,FA);
  72. igl::readOFF(TUTORIAL_SHARED_PATH "/decimated-knight.off",VB,FB);
  73. // Plot the mesh with pseudocolors
  74. igl::opengl::glfw::Viewer viewer;
  75. // Initialize
  76. update(viewer);
  77. viewer.data().show_lines = true;
  78. viewer.callback_key_down = &key_down;
  79. viewer.core().camera_dnear = 3.9;
  80. cout<<
  81. "Press '.' to switch to next boolean operation type."<<endl<<
  82. "Press ',' to switch to previous boolean operation type."<<endl<<
  83. "Press ']' to push near cutting plane away from camera."<<endl<<
  84. "Press '[' to pull near cutting plane closer to camera."<<endl<<
  85. "Hint: investigate _inside_ the model to see orientation changes."<<endl;
  86. viewer.launch();
  87. }