main.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. Eigen::MatrixXd V;
  9. Eigen::MatrixXi F;
  10. void plotMeshDistance(igl::opengl::glfw::Viewer& viewer, const Eigen::MatrixXd& V, const Eigen::MatrixXi& F, const Eigen::VectorXd& d, const double strip_size )
  11. {
  12. // Rescale the function depending on the strip size
  13. Eigen::VectorXd f = (d/strip_size);
  14. // The function should be 1 on each integer coordinate
  15. f = (f*M_PI).array().sin().abs();
  16. // Compute per-vertex colors
  17. Eigen::MatrixXd C;
  18. igl::colormap(igl::COLOR_MAP_TYPE_INFERNO,f,false,C);
  19. // Plot the mesh
  20. viewer.data().set_mesh(V, F);
  21. viewer.data().set_colors(C);
  22. }
  23. int main(int argc, char *argv[])
  24. {
  25. using namespace Eigen;
  26. using namespace std;
  27. // Load a mesh in OFF format
  28. igl::readOBJ(TUTORIAL_SHARED_PATH "/armadillo.obj", V, F);
  29. igl::opengl::glfw::Viewer viewer;
  30. // Plot a distance when a vertex is picked
  31. viewer.callback_mouse_down =
  32. [&](igl::opengl::glfw::Viewer& viewer, int, int)->bool
  33. {
  34. int fid;
  35. Eigen::Vector3f bc;
  36. // Cast a ray in the view direction starting from the mouse position
  37. double x = viewer.current_mouse_x;
  38. double y = viewer.core.viewport(3) - viewer.current_mouse_y;
  39. if(igl::unproject_onto_mesh(Eigen::Vector2f(x,y), viewer.core.view * viewer.core.model,
  40. viewer.core.proj, viewer.core.viewport, V, F, fid, bc))
  41. {
  42. int max;
  43. bc.maxCoeff(&max);
  44. int vid = F(fid,max);
  45. Eigen::VectorXi VS,FS,VT,FT;
  46. // The selected vertex is the source
  47. VS.resize(1);
  48. VS << vid;
  49. // All vertices are the targets
  50. VT.setLinSpaced(V.rows(),0,V.rows()-1);
  51. Eigen::VectorXd d;
  52. igl::exact_geodesic(V,F,VS,FS,VT,FT,d);
  53. plotMeshDistance(viewer,V,F,d,0.05);
  54. }
  55. return false;
  56. };
  57. viewer.data().set_mesh(V,F);
  58. cout << "Press [space] to smooth." << endl;;
  59. cout << "Press [r] to reset." << endl;;
  60. return viewer.launch();
  61. }