main.cpp 1.9 KB

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