main.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <igl/jet.h>
  2. #include <igl/readOFF.h>
  3. #include <igl/cotmatrix.h>
  4. #include <igl/matlab/matlabinterface.h>
  5. #include <igl/viewer/Viewer.h>
  6. #include <iostream>
  7. // Base mesh
  8. Eigen::MatrixXd V;
  9. Eigen::MatrixXi F;
  10. // Matlab instance
  11. Engine* engine;
  12. // Eigenvectors of the laplacian
  13. Eigen::MatrixXd EV;
  14. void plotEV(igl::viewer::Viewer& viewer, int id)
  15. {
  16. Eigen::VectorXd v = EV.col(id);
  17. v = v.array() - v.minCoeff();
  18. v = v.array() / v.maxCoeff();
  19. // Map to colors using jet colorramp
  20. Eigen::MatrixXd C(V.rows(),3);
  21. for (unsigned i=0; i<V.rows(); ++i)
  22. {
  23. double r,g,b;
  24. igl::jet(v(i),r,g,b);
  25. C.row(i) << r,g,b;
  26. }
  27. viewer.data.set_colors(C);
  28. }
  29. // This function is called every time a keyboard button is pressed
  30. bool key_down(igl::viewer::Viewer& viewer, unsigned char key, int modifier)
  31. {
  32. if (key >= '1' && key <= '9')
  33. plotEV(viewer,(key - '1') + 1);
  34. return false;
  35. }
  36. int main(int argc, char *argv[])
  37. {
  38. // Load a mesh in OFF format
  39. igl::readOFF("../shared/3holes.off", V, F);
  40. // Launch MATLAB
  41. igl::matlab::mlinit(&engine);
  42. // Compute the discrete Laplacian operator
  43. Eigen::SparseMatrix<double> L;
  44. igl::cotmatrix(V,F,L);
  45. // Send Laplacian matrix to matlab
  46. igl::matlab::mlsetmatrix(&engine,"L",L);
  47. // Plot the laplacian matrix using matlab spy
  48. igl::matlab::mleval(&engine,"spy(L)");
  49. // Extract the first 10 eigenvectors
  50. igl::matlab::mleval(&engine,"[EV,~] = eigs(-L,10,'sm')");
  51. // Plot the size of EV (only for demostration purposes)
  52. std::cerr << igl::matlab::mleval(&engine,"size(EV)") << std::endl;
  53. // Retrieve the result
  54. igl::matlab::mlgetmatrix(&engine,"EV",EV);
  55. // Plot the mesh
  56. igl::viewer::Viewer viewer;
  57. viewer.callback_key_down = &key_down;
  58. viewer.data.set_mesh(V, F);
  59. // Plot the first non-trivial eigenvector
  60. plotEV(viewer,1);
  61. // Launch the viewer
  62. viewer.launch();
  63. }