main.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <igl/avg_edge_length.h>
  2. #include <igl/barycenter.h>
  3. #include <igl/grad.h>
  4. #include <igl/jet.h>
  5. #include <igl/readDMAT.h>
  6. #include <igl/readOFF.h>
  7. #include <igl/opengl/glfw/Viewer.h>
  8. #include <iostream>
  9. int main(int argc, char *argv[])
  10. {
  11. using namespace Eigen;
  12. using namespace std;
  13. MatrixXd V;
  14. MatrixXi F;
  15. // Load a mesh in OFF format
  16. igl::readOFF(TUTORIAL_SHARED_PATH "/cheburashka.off", V, F);
  17. // Read scalar function values from a file, U: #V by 1
  18. VectorXd U;
  19. igl::readDMAT(TUTORIAL_SHARED_PATH "/cheburashka-scalar.dmat",U);
  20. // Compute gradient operator: #F*3 by #V
  21. SparseMatrix<double> G;
  22. igl::grad(V,F,G);
  23. // Compute gradient of U
  24. MatrixXd GU = Map<const MatrixXd>((G*U).eval().data(),F.rows(),3);
  25. // Compute gradient magnitude
  26. const VectorXd GU_mag = GU.rowwise().norm();
  27. igl::opengl::glfw::Viewer viewer;
  28. viewer.data().set_mesh(V, F);
  29. viewer.data().set_data(U);
  30. // Average edge length divided by average gradient (for scaling)
  31. const double max_size = igl::avg_edge_length(V,F) / GU_mag.mean();
  32. // Draw a black segment in direction of gradient at face barycenters
  33. MatrixXd BC;
  34. igl::barycenter(V,F,BC);
  35. const RowVector3d black(0,0,0);
  36. viewer.data().add_edges(BC,BC+max_size*GU, black);
  37. // Hide wireframe
  38. viewer.data().show_lines = false;
  39. viewer.launch();
  40. }