main.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "tutorial_shared_path.h"
  2. #include <igl/readOFF.h>
  3. #include <igl/cotmatrix.h>
  4. #include <igl/matlab/matlabinterface.h>
  5. #include <igl/opengl/glfw/Viewer.h>
  6. #include <iostream>
  7. // On mac you may need to issue something like:
  8. //
  9. // PATH=$PATH:/Applications/MATLAB_R2019a.app/bin/ ./tutorial/602_Matlab_bin
  10. // Base mesh
  11. Eigen::MatrixXd V;
  12. Eigen::MatrixXi F;
  13. // Matlab instance
  14. Engine* engine;
  15. // Eigenvectors of the laplacian
  16. Eigen::MatrixXd EV;
  17. // This function is called every time a keyboard button is pressed
  18. bool key_down(igl::opengl::glfw::Viewer& viewer, unsigned char key, int modifier)
  19. {
  20. if (key >= '1' && key <= '9')
  21. {
  22. viewer.data().set_data(EV.col((key - '1') + 1));
  23. }
  24. return false;
  25. }
  26. int main(int argc, char *argv[])
  27. {
  28. // Load a mesh in OFF format
  29. igl::readOFF(TUTORIAL_SHARED_PATH "/3holes.off", V, F);
  30. // Launch MATLAB
  31. igl::matlab::mlinit(&engine);
  32. // Compute the discrete Laplacian operator
  33. Eigen::SparseMatrix<double> L;
  34. igl::cotmatrix(V,F,L);
  35. // Send Laplacian matrix to matlab
  36. igl::matlab::mlsetmatrix(&engine,"L",L);
  37. // Plot the laplacian matrix using matlab spy
  38. igl::matlab::mleval(&engine,"spy(L)");
  39. // Extract the first 10 eigenvectors
  40. igl::matlab::mleval(&engine,"[EV,~] = eigs(-L,10,'sm')");
  41. // Plot the size of EV (only for demonstration purposes)
  42. std::cerr << igl::matlab::mleval(&engine,"size(EV)") << std::endl;
  43. // Retrieve the result
  44. igl::matlab::mlgetmatrix(&engine,"EV",EV);
  45. // Plot the mesh
  46. igl::opengl::glfw::Viewer viewer;
  47. viewer.callback_key_down = &key_down;
  48. viewer.data().set_mesh(V, F);
  49. // Plot the first non-trivial eigenvector
  50. viewer.data().set_data(EV.col(1));
  51. // Launch the viewer
  52. viewer.launch();
  53. }