main.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <igl/eigs.h>
  2. #include <igl/cotmatrix.h>
  3. #include <igl/massmatrix.h>
  4. #include <igl/viewer/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(TUTORIAL_SHARED_PATH "/beetle.off",V,F))
  23. {
  24. cout<<"failed to load mesh"<<endl;
  25. }
  26. twod = V.col(2).minCoeff()==V.col(2).maxCoeff();
  27. bbd = (V.colwise().maxCoeff()-V.colwise().minCoeff()).norm();
  28. SparseMatrix<double> L,M;
  29. cotmatrix(V,F,L);
  30. L = (-L).eval();
  31. massmatrix(V,F,MASSMATRIX_TYPE_DEFAULT,M);
  32. const size_t k = 5;
  33. if(!eigs(L,M,k+1,EIGS_TYPE_SM,U,D))
  34. {
  35. cout<<"failed."<<endl;
  36. }
  37. U = ((U.array()-U.minCoeff())/(U.maxCoeff()-U.minCoeff())).eval();
  38. igl::viewer::Viewer viewer;
  39. viewer.callback_key_down = [&](igl::viewer::Viewer & viewer,unsigned char key,int)->bool
  40. {
  41. switch(key)
  42. {
  43. default:
  44. return false;
  45. case ' ':
  46. {
  47. U = U.rightCols(k).eval();
  48. // Rescale eigen vectors for visualization
  49. VectorXd Z =
  50. bbd*0.5*U.col(c);
  51. Eigen::MatrixXd C;
  52. igl::parula(U.col(c).eval(),false,C);
  53. c = (c+1)%U.cols();
  54. if(twod)
  55. {
  56. V.col(2) = Z;
  57. }
  58. viewer.data.set_mesh(V,F);
  59. viewer.data.compute_normals();
  60. viewer.data.set_colors(C);
  61. return true;
  62. }
  63. }
  64. };
  65. viewer.callback_key_down(viewer,' ',0);
  66. viewer.core.show_lines = false;
  67. viewer.launch();
  68. }