main.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <igl/readOFF.h>
  2. #define IGL_NO_CORK
  3. #include <igl/boolean/mesh_boolean.h>
  4. #include <igl/viewer/Viewer.h>
  5. #include <Eigen/Core>
  6. #include <iostream>
  7. Eigen::MatrixXd VA,VB,VC;
  8. Eigen::VectorXi J;
  9. Eigen::MatrixXi FA,FB,FC;
  10. igl::MeshBooleanType boolean_type(igl::MESH_BOOLEAN_TYPE_UNION);
  11. const char * MESH_BOOLEAN_TYPE_NAMES[] =
  12. {
  13. "Union",
  14. "Intersect",
  15. "Minus",
  16. "XOR",
  17. "Resolve",
  18. };
  19. void update(igl::Viewer &viewer)
  20. {
  21. igl::mesh_boolean(VA,FA,VB,FB,boolean_type,VC,FC,J);
  22. Eigen::MatrixXd C(FC.rows(),3);
  23. for(size_t f = 0;f<C.rows();f++)
  24. {
  25. if(J(f)<FA.rows())
  26. {
  27. C.row(f) = Eigen::RowVector3d(1,0,0);
  28. }else
  29. {
  30. C.row(f) = Eigen::RowVector3d(0,1,0);
  31. }
  32. }
  33. viewer.data.clear();
  34. viewer.data.set_mesh(VC,FC);
  35. viewer.data.set_colors(C);
  36. }
  37. bool key_down(igl::Viewer &viewer, unsigned char key, int mods)
  38. {
  39. switch(key)
  40. {
  41. default:
  42. return false;
  43. case '.':
  44. boolean_type =
  45. static_cast<igl::MeshBooleanType>(
  46. (boolean_type+1)% igl::NUM_MESH_BOOLEAN_TYPES);
  47. break;
  48. case ',':
  49. boolean_type =
  50. static_cast<igl::MeshBooleanType>(
  51. (boolean_type+igl::NUM_MESH_BOOLEAN_TYPES-1)%
  52. igl::NUM_MESH_BOOLEAN_TYPES);
  53. break;
  54. case '[':
  55. viewer.core.camera_dnear -= 0.1;
  56. return true;
  57. case ']':
  58. viewer.core.camera_dnear += 0.1;
  59. return true;
  60. }
  61. std::cout<<"A "<<MESH_BOOLEAN_TYPE_NAMES[boolean_type]<<" B."<<std::endl;
  62. igl::mesh_boolean(VA,FA,VB,FB,boolean_type,VC,FC);
  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("../shared/cheburashka.off",VA,FA);
  71. igl::readOFF("../shared/decimated-knight.off",VB,FB);
  72. // Plot the mesh with pseudocolors
  73. igl::Viewer viewer;
  74. // Initialize
  75. update(viewer);
  76. viewer.core.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. }