main.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <igl/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 =
  12. [](const Eigen::RowVector3d& x) -> double {
  13. const double R = x.norm();
  14. const double s = atan2(x.head(2).norm(),x(2));
  15. const double p = atan2(x(1),x(0));
  16. return pow(sin(s),2.)*(pow(cos(12.*s),3.)*0.1+pow(sin(6.*p),2)*0.2)+(R-0.5);
  17. //return R-0.5;
  18. };
  19. // We know that the point (0, 0, 0.5) lies on the implicit surface
  20. Eigen::RowVector3d p0(0., 0., 0.5);
  21. // Construct a sparse voxel grid whose cubes have edge length eps
  22. // The cubes will form a thin shell around the implicit surface
  23. const double eps = 0.01;
  24. // CS will hold one scalar value at each cube vertex corresponding
  25. // the value of the implicit at that vertex
  26. Eigen::VectorXd CS;
  27. // CV will hold the positions of the corners of the sparse voxel grid
  28. Eigen::MatrixXd CV;
  29. // CI is a #cubes x 8 matrix of indices where each row contains the
  30. // indices into CV of the 8 corners of a cube
  31. Eigen::MatrixXi CI;
  32. // Construct the voxel grid, populating CS, CV, and CI
  33. igl::sparse_voxel_grid(p0, scalar_func, eps, 200000/*expected_number_of_cubes*/, CS, CV, CI);
  34. // Given the sparse voxel grid, use Marching Cubes to construct a triangle mesh of the surface
  35. Eigen::MatrixXi F;
  36. Eigen::MatrixXd V;
  37. igl::marching_cubes(CS, CV, CI, 0, V, F);
  38. // Draw the meshed implicit surface
  39. igl::opengl::glfw::Viewer viewer;
  40. viewer.data().clear();
  41. viewer.data().set_mesh(V,F);
  42. viewer.data().set_face_based(true);
  43. viewer.launch();
  44. }