main.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <igl/readOFF.h>
  2. #include <igl/viewer/Viewer.h>
  3. #include <igl/embree/EmbreeIntersector.h>
  4. #include <igl/embree/unproject_in_mesh.h>
  5. #include <algorithm>
  6. using namespace Eigen;
  7. using namespace std;
  8. // Mesh
  9. Eigen::MatrixXd V;
  10. Eigen::MatrixXi F;
  11. // Per-vertex color
  12. MatrixXd C;
  13. igl::EmbreeIntersector* ei;
  14. vector<int> picked_vertices;
  15. bool mouse_down(igl::Viewer& viewer, int button, int modifier)
  16. {
  17. int vid, fid;
  18. // Cast a ray in the view direction starting from the mouse position
  19. bool hit = unproject_in_mesh(Vector2f(viewer.current_mouse_x,viewer.viewport(3) - viewer.current_mouse_y),
  20. F,
  21. viewer.view * viewer.model,
  22. viewer.proj,
  23. viewer.viewport,
  24. *ei,
  25. fid,
  26. vid);
  27. // If the ray hits a face, assign a red color to the closest vertex
  28. if (hit)
  29. {
  30. cerr << "Picked face(vertex): " << fid << " (" << vid << ")" << endl;
  31. C.row(vid) << 1,0,0;
  32. viewer.set_colors(C);
  33. return true;
  34. }
  35. return false;
  36. }
  37. int main(int argc, char *argv[])
  38. {
  39. // Load a mesh in OFF format
  40. igl::readOFF("../shared/fertility.off", V, F);
  41. // Create a BVH for raycasting
  42. ei = new igl::EmbreeIntersector();
  43. ei->init(V.cast<float>(),F);
  44. // Initialize the colors to white for all vertices
  45. C = MatrixXd::Constant(V.rows(),3,1);
  46. // Show mesh
  47. igl::Viewer viewer;
  48. viewer.set_mesh(V, F);
  49. viewer.callback_mouse_down = &mouse_down;
  50. viewer.set_colors(C);
  51. viewer.options.show_lines = false;
  52. viewer.launch();
  53. }