main.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <igl/eigs.h>
  2. #include <igl/cotmatrix.h>
  3. #include <igl/massmatrix.h>
  4. #include <igl/opengl/glfw/Viewer.h>
  5. #include <igl/parula.h>
  6. #include <igl/read_triangle_mesh.h>
  7. #include <Eigen/Sparse>
  8. #include <iostream>
  9. #include <queue>
  10. #include "tutorial_shared_path.h"
  11. Eigen::MatrixXd V,U;
  12. Eigen::MatrixXi F;
  13. int c=0;
  14. double bbd = 1;
  15. bool twod = 0;
  16. int main(int argc, char * argv[])
  17. {
  18. using namespace Eigen;
  19. using namespace std;
  20. using namespace igl;
  21. VectorXd D;
  22. if(!read_triangle_mesh(
  23. argc>1?argv[1]: TUTORIAL_SHARED_PATH "/beetle.off",V,F))
  24. {
  25. cout<<"failed to load mesh"<<endl;
  26. }
  27. twod = V.col(2).minCoeff()==V.col(2).maxCoeff();
  28. bbd = (V.colwise().maxCoeff()-V.colwise().minCoeff()).norm();
  29. SparseMatrix<double> L,M;
  30. cotmatrix(V,F,L);
  31. L = (-L).eval();
  32. massmatrix(V,F,MASSMATRIX_TYPE_DEFAULT,M);
  33. const size_t k = 5;
  34. if(!eigs(L,M,k+1,EIGS_TYPE_SM,U,D))
  35. {
  36. cout<<"failed."<<endl;
  37. }
  38. // Normalize
  39. U = ((U.array()-U.minCoeff())/(U.maxCoeff()-U.minCoeff())).eval();
  40. igl::opengl::glfw::Viewer viewer;
  41. viewer.callback_key_down = [&](igl::opengl::glfw::Viewer & viewer,unsigned char key,int)->bool
  42. {
  43. switch(key)
  44. {
  45. default:
  46. return false;
  47. case ' ':
  48. {
  49. U = U.rightCols(k).eval();
  50. // Rescale eigen vectors for visualization
  51. VectorXd Z =
  52. bbd*0.5*U.col(c);
  53. Eigen::MatrixXd C;
  54. igl::parula(U.col(c).eval(),false,C);
  55. c = (c+1)%U.cols();
  56. if(twod)
  57. {
  58. V.col(2) = Z;
  59. }
  60. viewer.data().set_mesh(V,F);
  61. viewer.data().compute_normals();
  62. viewer.data().set_colors(C);
  63. return true;
  64. }
  65. }
  66. };
  67. viewer.callback_key_down(viewer,' ',0);
  68. viewer.data().show_lines = false;
  69. std::cout<<
  70. R"(
  71. [space] Cycle through eigen modes
  72. )";
  73. viewer.launch();
  74. }