main.cpp 2.3 KB

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