main.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <igl/opengl/glfw/Viewer.h>
  2. #include <igl/copyleft/tetgen/tetrahedralize.h>
  3. #include <igl/readOBJ.h>
  4. #include <igl/marching_tets.h>
  5. #include <Eigen/Core>
  6. #include "tutorial_shared_path.h"
  7. int main(int argc, char * argv[])
  8. {
  9. // Load a surface mesh which is a cube
  10. Eigen::MatrixXd surfaceV;
  11. Eigen::MatrixXi surfaceF;
  12. igl::readOBJ(TUTORIAL_SHARED_PATH "/cube.obj", surfaceV, surfaceF);
  13. // Find the centroid of the loaded mesh
  14. Eigen::RowVector3d surfaceCenter = surfaceV.colwise().sum() / surfaceV.rows();
  15. // Center the mesh about the origin
  16. surfaceV.rowwise() -= surfaceCenter;
  17. // Tetrahedralize the surface mesh
  18. Eigen::MatrixXd TV; // Tet mesh vertices
  19. Eigen::MatrixXi TF; // Tet mesh boundary face indices
  20. Eigen::MatrixXi TT; // Tet mesh tetrahedron indices
  21. igl::copyleft::tetgen::tetrahedralize(surfaceV, surfaceF, "pq1.414a0.0001", TV, TT, TF);
  22. // Compute a scalar at each tet vertex which is the distance from the vertex to the origin
  23. Eigen::VectorXd S = TV.rowwise().norm();
  24. // Compute a mesh (stored in SV, SF) representing the iso-level-set for the isovalue 0.5
  25. Eigen::MatrixXd SV;
  26. Eigen::MatrixXi SF;
  27. igl::marching_tets(TV, TT, S, 0.45, SV, SF);
  28. // Draw the mesh stored in (SV, SF)
  29. igl::opengl::glfw::Viewer viewer;
  30. viewer.data().set_mesh(SV, SF);
  31. viewer.callback_key_down =
  32. [&](igl::opengl::glfw::Viewer & viewer, unsigned char key, int mod)->bool
  33. {
  34. viewer.data().set_face_based(true);
  35. return true;
  36. };
  37. viewer.launch();
  38. }