main.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <igl/copyleft/marching_cubes.h>
  2. #include <igl/sparse_voxel_grid.h>
  3. #include <igl/opengl/glfw/Viewer.h>
  4. #include <Eigen/Core>
  5. #include <iostream>
  6. #include "tutorial_shared_path.h"
  7. int main(int argc, char * argv[])
  8. {
  9. // An implicit function which is zero on the surface of a sphere centered at the origin with radius 1
  10. // This function is negative inside the surface and positive outside the surface
  11. std::function<double(const Eigen::RowVector3d&)> scalar_func = [](const Eigen::RowVector3d& pt) -> double {
  12. return pt.norm() - 1.0;
  13. };
  14. // We know that the point (0, 0, 1) lies on the implicit surface
  15. Eigen::RowVector3d p0(0., 0., 1.);
  16. // Construct a sparse voxel grid whose cubes have edge length eps = 0.1.
  17. // The cubes will form a thin shell around the implicit surface
  18. const double eps = 0.1;
  19. // CS will hold one scalar value at each cube vertex corresponding
  20. // the value of the implicit at that vertex
  21. Eigen::VectorXd CS;
  22. // CV will hold the positions of the corners of the sparse voxel grid
  23. Eigen::MatrixXd CV;
  24. // CI is a #cubes x 8 matrix of indices where each row contains the
  25. // indices into CV of the 8 corners of a cube
  26. Eigen::MatrixXi CI;
  27. // Construct the voxel grid, populating CS, CV, and CI
  28. igl::sparse_voxel_grid(p0, scalar_func, eps, 1024 /*expected_number_of_cubes*/, CS, CV, CI);
  29. // Given the sparse voxel grid, use Marching Cubes to construct a triangle mesh of the surface
  30. Eigen::MatrixXi F;
  31. Eigen::MatrixXd V;
  32. igl::copyleft::marching_cubes(CS, CV, CI, V, F);
  33. // Draw the meshed implicit surface
  34. igl::opengl::glfw::Viewer viewer;
  35. viewer.data().clear();
  36. viewer.data().set_mesh(V,F);
  37. viewer.data().set_face_based(true);
  38. viewer.launch();
  39. }