main.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2020 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include <igl/opengl/glfw/Viewer.h>
  9. #include <igl/readMESH.h>
  10. #include <igl/barycenter.h>
  11. #include <igl/volume.h>
  12. #include <igl/exploded_view.h>
  13. #include <igl/slice.h>
  14. #include <igl/colormap.h>
  15. int main(int argc, char *argv[])
  16. {
  17. Eigen::MatrixXd V;
  18. Eigen::MatrixXi T,F;
  19. igl::readMESH(argc>1?argv[1]: TUTORIAL_SHARED_PATH "/octopus-low.mesh",V,T,F);
  20. // Some per-tet data
  21. Eigen::VectorXd D;
  22. {
  23. Eigen::MatrixXd BC;
  24. igl::barycenter(V,T,BC);
  25. Eigen::VectorXd vol;
  26. igl::volume(V,T,vol);
  27. const Eigen::RowVectorXd c = vol.transpose()*BC/vol.array().sum();
  28. D = (BC.rowwise()-c).rowwise().norm();
  29. }
  30. // Plot the mesh
  31. igl::opengl::glfw::Viewer viewer;
  32. double t = 1;
  33. double s = 1;
  34. const auto update = [&]()
  35. {
  36. Eigen::MatrixXd EV;
  37. Eigen::MatrixXi EF;
  38. Eigen::VectorXi I,J;
  39. igl::exploded_view(V,T,s,t,EV,EF,I,J);
  40. Eigen::VectorXd DJ;
  41. igl::slice(D,J,1,DJ);
  42. static bool first = true;
  43. if(first)
  44. {
  45. viewer.data().clear();
  46. viewer.data().set_mesh(EV,EF);
  47. first = false;
  48. }else
  49. {
  50. viewer.data().set_vertices(EV);
  51. }
  52. viewer.data().set_face_based(true);
  53. Eigen::MatrixXd C;
  54. igl::colormap(igl::COLOR_MAP_TYPE_VIRIDIS,DJ,true,C);
  55. viewer.data().set_colors(C);
  56. };
  57. int mod = 0;
  58. float prev_y;
  59. viewer.callback_mouse_move = [&](igl::opengl::glfw::Viewer &, int x, int y)->bool
  60. {
  61. if((mod & IGL_MOD_SHIFT)||(mod & IGL_MOD_ALT))
  62. {
  63. if(mod & IGL_MOD_SHIFT)
  64. {
  65. t = std::min(std::max(t+0.001*(y-prev_y),1.),2.);
  66. }
  67. if(mod & IGL_MOD_ALT)
  68. {
  69. s = std::min(std::max(s+0.001*(y-prev_y),0.),1.);
  70. }
  71. prev_y = y;
  72. update();
  73. return true;
  74. }
  75. return false;
  76. };
  77. // get modifier
  78. viewer.callback_key_down =
  79. [&](igl::opengl::glfw::Viewer &, unsigned char key, int _mod)->bool
  80. {
  81. prev_y = viewer.current_mouse_y;
  82. mod = _mod;
  83. return false;
  84. };
  85. viewer.callback_key_up =
  86. [&](igl::opengl::glfw::Viewer &, unsigned char key, int _mod)->bool
  87. {
  88. mod = _mod;
  89. return false;
  90. };
  91. update();
  92. viewer.launch();
  93. }