2
0

main.cpp 1.9 KB

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