main.cpp 1.7 KB

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