main.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. if(twod)
  54. {
  55. V.col(2) = Z;
  56. viewer.data().set_mesh(V,F);
  57. viewer.data().compute_normals();
  58. }
  59. viewer.data().set_data(U.col(c).eval());
  60. c = (c+1)%U.cols();
  61. return true;
  62. }
  63. }
  64. };
  65. viewer.data().set_mesh(V,F);
  66. viewer.callback_key_down(viewer,' ',0);
  67. viewer.data().show_lines = false;
  68. std::cout<<
  69. R"(
  70. [space] Cycle through eigen modes
  71. )";
  72. viewer.launch();
  73. }