main.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. U = ((U.array()-U.minCoeff())/(U.maxCoeff()-U.minCoeff())).eval();
  39. igl::opengl::glfw::Viewer viewer;
  40. viewer.callback_key_down = [&](igl::opengl::glfw::Viewer & viewer,unsigned char key,int)->bool
  41. {
  42. switch(key)
  43. {
  44. default:
  45. return false;
  46. case ' ':
  47. {
  48. U = U.rightCols(k).eval();
  49. // Rescale eigen vectors for visualization
  50. VectorXd Z =
  51. bbd*0.5*U.col(c);
  52. Eigen::MatrixXd C;
  53. igl::parula(U.col(c).eval(),false,C);
  54. c = (c+1)%U.cols();
  55. if(twod)
  56. {
  57. V.col(2) = Z;
  58. }
  59. viewer.selected_data().set_mesh(V,F);
  60. viewer.selected_data().compute_normals();
  61. viewer.selected_data().set_colors(C);
  62. return true;
  63. }
  64. }
  65. };
  66. viewer.callback_key_down(viewer,' ',0);
  67. viewer.selected_data().show_lines = false;
  68. viewer.launch();
  69. }