main.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/colormap.h>
  14. int main(int argc, char *argv[])
  15. {
  16. Eigen::MatrixXd V;
  17. Eigen::MatrixXi T,F;
  18. igl::readMESH(argc>1?argv[1]: TUTORIAL_SHARED_PATH "/octopus-low.mesh",V,T,F);
  19. // Some per-tet data
  20. Eigen::VectorXd D;
  21. {
  22. Eigen::MatrixXd BC;
  23. igl::barycenter(V,T,BC);
  24. Eigen::VectorXd vol;
  25. igl::volume(V,T,vol);
  26. const Eigen::RowVectorXd c = vol.transpose()*BC/vol.array().sum();
  27. D = (BC.rowwise()-c).rowwise().norm();
  28. }
  29. // Plot the mesh
  30. igl::opengl::glfw::Viewer viewer;
  31. double t = 1;
  32. double s = 1;
  33. const auto update = [&]()
  34. {
  35. Eigen::MatrixXd EV;
  36. Eigen::MatrixXi EF;
  37. Eigen::VectorXi I,J;
  38. igl::exploded_view(V,T,s,t,EV,EF,I,J);
  39. Eigen::VectorXd DJ = D(J);
  40. static bool first = true;
  41. if(first)
  42. {
  43. viewer.data().clear();
  44. viewer.data().set_mesh(EV,EF);
  45. first = false;
  46. }else
  47. {
  48. viewer.data().set_vertices(EV);
  49. }
  50. viewer.data().set_face_based(true);
  51. Eigen::MatrixXd C;
  52. igl::colormap(igl::COLOR_MAP_TYPE_VIRIDIS,DJ,true,C);
  53. viewer.data().set_colors(C);
  54. };
  55. int mod = 0;
  56. float prev_y;
  57. viewer.callback_mouse_move = [&](igl::opengl::glfw::Viewer &, int x, int y)->bool
  58. {
  59. if((mod & IGL_MOD_SHIFT)||(mod & IGL_MOD_ALT))
  60. {
  61. if(mod & IGL_MOD_SHIFT)
  62. {
  63. t = std::min(std::max(t+0.001*(y-prev_y),1.),2.);
  64. }
  65. if(mod & IGL_MOD_ALT)
  66. {
  67. s = std::min(std::max(s+0.001*(y-prev_y),0.),1.);
  68. }
  69. prev_y = y;
  70. update();
  71. return true;
  72. }
  73. return false;
  74. };
  75. // get modifier
  76. viewer.callback_key_down =
  77. [&](igl::opengl::glfw::Viewer &, unsigned char key, int _mod)->bool
  78. {
  79. prev_y = viewer.current_mouse_y;
  80. mod = _mod;
  81. return false;
  82. };
  83. viewer.callback_key_up =
  84. [&](igl::opengl::glfw::Viewer &, unsigned char key, int _mod)->bool
  85. {
  86. mod = _mod;
  87. return false;
  88. };
  89. update();
  90. viewer.launch();
  91. }