main.cpp 2.0 KB

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