main.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. double x = viewer.current_mouse_x;
  20. double y = viewer.viewport(3) - viewer.current_mouse_y;
  21. bool hit = unproject_in_mesh(Vector2f(x,y),
  22. F,
  23. viewer.view * viewer.model,
  24. viewer.proj,
  25. viewer.viewport,
  26. *ei,
  27. fid,
  28. vid);
  29. // If the ray hits a face, assign a red color to the closest vertex
  30. if (hit)
  31. {
  32. cerr << "Picked face(vertex): " << fid << " (" << vid << ")" << endl;
  33. C.row(vid) << 1,0,0;
  34. viewer.set_colors(C);
  35. return true;
  36. }
  37. return false;
  38. }
  39. int main(int argc, char *argv[])
  40. {
  41. // Load a mesh in OFF format
  42. igl::readOFF("../shared/fertility.off", V, F);
  43. // Create a BVH for raycasting
  44. ei = new igl::EmbreeIntersector();
  45. ei->init(V.cast<float>(),F);
  46. // Initialize the colors to white for all vertices
  47. C = MatrixXd::Constant(V.rows(),3,1);
  48. // Show mesh
  49. igl::Viewer viewer;
  50. viewer.set_mesh(V, F);
  51. viewer.callback_mouse_down = &mouse_down;
  52. viewer.set_colors(C);
  53. viewer.options.show_lines = false;
  54. viewer.launch();
  55. }