main.cpp 2.0 KB

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