main.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <igl/readOBJ.h>
  2. #include <igl/opengl/glfw/Viewer.h>
  3. #include <igl/exact_geodesic.h>
  4. #include <igl/colormap.h>
  5. #include <igl/unproject_onto_mesh.h>
  6. #include <iostream>
  7. #include "tutorial_shared_path.h"
  8. int main(int argc, char *argv[])
  9. {
  10. using namespace Eigen;
  11. using namespace std;
  12. Eigen::MatrixXd V;
  13. Eigen::MatrixXi F;
  14. igl::opengl::glfw::Viewer viewer;
  15. // Load a mesh in OFF format
  16. igl::readOBJ(TUTORIAL_SHARED_PATH "/armadillo.obj", V, F);
  17. const auto update_distance = [&](const int vid)
  18. {
  19. Eigen::VectorXi VS,FS,VT,FT;
  20. // The selected vertex is the source
  21. VS.resize(1);
  22. VS << vid;
  23. // All vertices are the targets
  24. VT.setLinSpaced(V.rows(),0,V.rows()-1);
  25. Eigen::VectorXd d;
  26. std::cout<<"Computing geodesic distance to vertex "<<vid<<"..."<<std::endl;
  27. igl::exact_geodesic(V,F,VS,FS,VT,FT,d);
  28. const double strip_size = 0.05;
  29. // The function should be 1 on each integer coordinate
  30. d = (d/strip_size*igl::PI).array().sin().abs().eval();
  31. // Compute per-vertex colors
  32. Eigen::MatrixXd C;
  33. igl::colormap(igl::COLOR_MAP_TYPE_INFERNO,d,false,C);
  34. // Plot the mesh
  35. viewer.data().set_mesh(V, F);
  36. viewer.data().set_colors(C);
  37. };
  38. // Plot a distance when a vertex is picked
  39. viewer.callback_mouse_down =
  40. [&](igl::opengl::glfw::Viewer& viewer, int, int)->bool
  41. {
  42. int fid;
  43. Eigen::Vector3f bc;
  44. // Cast a ray in the view direction starting from the mouse position
  45. double x = viewer.current_mouse_x;
  46. double y = viewer.core.viewport(3) - viewer.current_mouse_y;
  47. if(igl::unproject_onto_mesh(
  48. Eigen::Vector2f(x,y),
  49. viewer.core.view,
  50. viewer.core.proj,
  51. viewer.core.viewport,
  52. V,
  53. F,
  54. fid,
  55. bc))
  56. {
  57. int max;
  58. bc.maxCoeff(&max);
  59. int vid = F(fid,max);
  60. update_distance(vid);
  61. return true;
  62. }
  63. return false;
  64. };
  65. viewer.data().set_mesh(V,F);
  66. cout << "Click on mesh to define new source.\n" << std::endl;
  67. update_distance(0);
  68. return viewer.launch();
  69. }