main.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 <igl/PI.h>
  7. #include <iostream>
  8. #include "tutorial_shared_path.h"
  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. const double strip_size = 0.05;
  30. // The function should be 1 on each integer coordinate
  31. d = (d/strip_size*igl::PI).array().sin().abs().eval();
  32. // Compute per-vertex colors
  33. Eigen::MatrixXd C;
  34. igl::colormap(igl::COLOR_MAP_TYPE_INFERNO,d,false,C);
  35. // Plot the mesh
  36. viewer.data().set_mesh(V, F);
  37. viewer.data().set_colors(C);
  38. };
  39. // Plot a distance when a vertex is picked
  40. viewer.callback_mouse_down =
  41. [&](igl::opengl::glfw::Viewer& viewer, int, int)->bool
  42. {
  43. int fid;
  44. Eigen::Vector3f bc;
  45. // Cast a ray in the view direction starting from the mouse position
  46. double x = viewer.current_mouse_x;
  47. double y = viewer.core().viewport(3) - viewer.current_mouse_y;
  48. if(igl::unproject_onto_mesh(
  49. Eigen::Vector2f(x,y),
  50. viewer.core().view,
  51. viewer.core().proj,
  52. viewer.core().viewport,
  53. V,
  54. F,
  55. fid,
  56. bc))
  57. {
  58. int max;
  59. bc.maxCoeff(&max);
  60. int vid = F(fid,max);
  61. update_distance(vid);
  62. return true;
  63. }
  64. return false;
  65. };
  66. viewer.data().set_mesh(V,F);
  67. cout << "Click on mesh to define new source.\n" << std::endl;
  68. update_distance(0);
  69. return viewer.launch();
  70. }