main.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <igl/marching_cubes.h>
  2. #include <igl/signed_distance.h>
  3. #include <igl/read_triangle_mesh.h>
  4. #include <igl/voxel_grid.h>
  5. #include <igl/opengl/glfw/Viewer.h>
  6. #include <Eigen/Core>
  7. #include <iostream>
  8. int main(int argc, char * argv[])
  9. {
  10. using namespace Eigen;
  11. using namespace std;
  12. using namespace igl;
  13. MatrixXi F;
  14. MatrixXd V;
  15. // Read in inputs as double precision floating point meshes
  16. read_triangle_mesh(
  17. TUTORIAL_SHARED_PATH "/armadillo.obj",V,F);
  18. cout<<"Creating grid..."<<endl;
  19. // number of vertices on the largest side
  20. const int s = 100;
  21. // create grid
  22. MatrixXd GV;
  23. Eigen::RowVector3i res;
  24. igl::voxel_grid(V,0,s,1,GV,res);
  25. // compute values
  26. cout<<"Computing distances..."<<endl;
  27. VectorXd S,B;
  28. {
  29. VectorXi I;
  30. MatrixXd C,N;
  31. signed_distance(GV,V,F,SIGNED_DISTANCE_TYPE_PSEUDONORMAL,S,I,C,N);
  32. // Convert distances to binary inside-outside data --> aliasing artifacts
  33. B = S;
  34. for_each(B.data(),B.data()+B.size(),[](double& b){b=(b>0?1:(b<0?-1:0));});
  35. }
  36. cout<<"Marching cubes..."<<endl;
  37. MatrixXd SV,BV;
  38. MatrixXi SF,BF;
  39. igl::marching_cubes(S,GV,res(0),res(1),res(2),0,SV,SF);
  40. igl::marching_cubes(B,GV,res(0),res(1),res(2),0,BV,BF);
  41. cout<<R"(Usage:
  42. '1' Show original mesh.
  43. '2' Show marching cubes contour of signed distance.
  44. '3' Show marching cubes contour of indicator function.
  45. )";
  46. igl::opengl::glfw::Viewer viewer;
  47. viewer.data().set_mesh(SV,SF);
  48. viewer.callback_key_down =
  49. [&](igl::opengl::glfw::Viewer & viewer, unsigned char key, int mod)->bool
  50. {
  51. switch(key)
  52. {
  53. default:
  54. return false;
  55. case '1':
  56. viewer.data().clear();
  57. viewer.data().set_mesh(V,F);
  58. break;
  59. case '2':
  60. viewer.data().clear();
  61. viewer.data().set_mesh(SV,SF);
  62. break;
  63. case '3':
  64. viewer.data().clear();
  65. viewer.data().set_mesh(BV,BF);
  66. break;
  67. }
  68. viewer.data().set_face_based(true);
  69. return true;
  70. };
  71. viewer.launch();
  72. }