main.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include <igl/unique_simplices.h>
  2. #include <igl/dual_contouring.h>
  3. #include <igl/get_seconds.h>
  4. #include <igl/grid.h>
  5. #include <igl/marching_cubes.h>
  6. #include <igl/per_corner_normals.h>
  7. #include <igl/parallel_for.h>
  8. #include <igl/per_corner_normals.h>
  9. #include <igl/per_face_normals.h>
  10. #include <igl/polygon_corners.h>
  11. #include <igl/slice.h>
  12. #include <igl/sparse_voxel_grid.h>
  13. #include <igl/opengl/glfw/Viewer.h>
  14. int main(int argc, char * argv[])
  15. {
  16. const auto & tictoc = []()
  17. {
  18. static double t_start = igl::get_seconds();
  19. double diff = igl::get_seconds()-t_start;
  20. t_start += diff;
  21. return diff;
  22. };
  23. // Create an interesting shape with sharp features using SDF CSG with spheres.
  24. const auto & sphere = [](
  25. const Eigen::RowVector3d & c,
  26. const double r,
  27. const Eigen::RowVector3d & x)->double
  28. {
  29. return (x-c).norm() - r;
  30. };
  31. const std::function<double(const Eigen::RowVector3d & x)> f =
  32. [&](const Eigen::RowVector3d & x)->double
  33. {
  34. return
  35. std::min(
  36. std::min(std::max(
  37. sphere(Eigen::RowVector3d(-0.2,0,-0.2),0.5,x),
  38. -sphere(Eigen::RowVector3d(+0.2,0,0.2),0.5,x)),
  39. sphere(Eigen::RowVector3d(-0.15,0,-0.15),0.3,x)
  40. ),
  41. std::max(
  42. std::max(
  43. sphere(Eigen::RowVector3d(-0.2,-0.5,-0.2),0.6,x),x(1)+0.45),-0.6-x(1))
  44. );
  45. };
  46. Eigen::RowVector3d p0(-0.2,0.5,-0.2);
  47. assert(abs(f(p0)) < 1e-10 && "p0 should be on zero level-set");
  48. // Simple finite difference gradients
  49. const auto & fd = [](
  50. const std::function<double(const Eigen::RowVector3d&)> & f,
  51. const Eigen::RowVector3d & x)
  52. {
  53. const double eps = 1e-10;
  54. Eigen::RowVector3d g;
  55. for(int c = 0;c<3;c++)
  56. {
  57. const Eigen::RowVector3d xp = x+eps*Eigen::RowVector3d(c==0,c==1,c==2);
  58. const double fp = f(xp);
  59. const Eigen::RowVector3d xn = x-eps*Eigen::RowVector3d(c==0,c==1,c==2);
  60. const double fn = f(xn);
  61. g(c) = (fp-fn)/(2*eps);
  62. }
  63. return g;
  64. };
  65. const auto & f_grad = [&fd,&f](const Eigen::RowVector3d & x)
  66. {
  67. return fd(f,x).normalized();
  68. };
  69. Eigen::MatrixXd V;
  70. Eigen::MatrixXi Q,F;
  71. Eigen::MatrixXd mcV,mcN;
  72. Eigen::MatrixXi mcF;
  73. // Grid parameters
  74. const Eigen::RowVector3d min_corner(-2,-2,-2);
  75. const Eigen::RowVector3d max_corner(+2,+2,+2);
  76. const int s = 256;
  77. int nx = s+1;
  78. int ny = s+1;
  79. int nz = s+1;
  80. const Eigen::RowVector3d step =
  81. (max_corner-min_corner).array()/(Eigen::RowVector3d(nx,ny,nz).array()-1);
  82. // Sparse grid below assumes regular grid
  83. assert((step(0) == step(1))&&(step(0) == step(2)));
  84. // Dual contouring parameters
  85. bool constrained = false;
  86. bool triangles = false;
  87. bool root_finding = true;
  88. for(int pass = 0;pass<2;pass++)
  89. {
  90. const bool sparse = pass == 1;
  91. printf("Using %s grid..\n",sparse?"sparse":"dense");
  92. if(sparse)
  93. {
  94. // igl::sparse_voxel_grid assumes (0,0,0) lies on the grid. But dense igl::grid
  95. // below won't necessarily do that depending on nx,ny,nz.
  96. tictoc();
  97. Eigen::MatrixXd GV;
  98. Eigen::VectorXd Gf;
  99. Eigen::Matrix<int,Eigen::Dynamic,8> GI;
  100. igl::sparse_voxel_grid(p0,f,step(0),16.*pow(step(0),-2.),Gf,GV,GI);
  101. const auto t_Gf = tictoc();
  102. printf(" %5f secs to populate sparse grid of %ld cells\n",t_Gf+tictoc(),GI.rows());
  103. // Dual contouring requires list of sparse edges (not cells)
  104. // extract _all_ edges from sparse_voxel_grid (conservative)
  105. Eigen::Matrix<int,Eigen::Dynamic,2> GI2;
  106. {
  107. Eigen::Matrix<int,Eigen::Dynamic,2> all_GI2(GI.rows()*12,2);
  108. all_GI2 <<
  109. // front
  110. GI.col(0),GI.col(1),
  111. GI.col(1),GI.col(2),
  112. GI.col(2),GI.col(3),
  113. GI.col(3),GI.col(0),
  114. // back
  115. GI.col(4+0),GI.col(4+1),
  116. GI.col(4+1),GI.col(4+2),
  117. GI.col(4+2),GI.col(4+3),
  118. GI.col(4+3),GI.col(4+0),
  119. // sides
  120. GI.col(0),GI.col(4+0),
  121. GI.col(1),GI.col(4+1),
  122. GI.col(2),GI.col(4+2),
  123. GI.col(3),GI.col(4+3);
  124. Eigen::VectorXi _1,_2;
  125. igl::unique_simplices(all_GI2,GI2,_1,_2);
  126. }
  127. tictoc();
  128. Eigen::RowVector3d step =
  129. (max_corner-min_corner).array()/(Eigen::RowVector3d(nx,ny,nz).array()-1);
  130. igl::dual_contouring(
  131. f,f_grad,step,Gf,GV,GI2,constrained,triangles,root_finding,V,Q);
  132. printf(" %5f secs dual contouring\n",t_Gf+tictoc());
  133. tictoc();
  134. igl::marching_cubes(Gf,GV,GI,0.0,mcV,mcF);
  135. printf(" %5f secs marching cubes\n",t_Gf+tictoc());
  136. }else
  137. {
  138. tictoc();
  139. igl::dual_contouring(
  140. f,f_grad,min_corner,max_corner,nx,ny,nz,constrained,triangles,root_finding,V,Q);
  141. printf(" %5f secs dual contouring\n",tictoc());
  142. // build and sample grid
  143. tictoc();
  144. Eigen::MatrixXd GV;
  145. igl::grid(Eigen::RowVector3i(nx,ny,nz),GV);
  146. Eigen::VectorXd Gf(GV.rows());
  147. igl::parallel_for(GV.rows(),[&](const int i)
  148. {
  149. GV.row(i).array() *= (max_corner-min_corner).array();
  150. GV.row(i) += min_corner;
  151. Gf(i) = f(GV.row(i));
  152. },1000ul);
  153. const auto t_grid = tictoc();
  154. igl::marching_cubes(Gf,GV,nx,ny,nz,0,mcV,mcF);
  155. const auto t_mc = tictoc();
  156. printf(" %5f secs (%5f + %5f) marching cubes\n",t_grid+t_mc,t_grid,t_mc);
  157. }
  158. }
  159. // Crisp (as possible) rendering of resulting MC triangle mesh
  160. igl::per_corner_normals(mcV,mcF,20,mcN);
  161. // Crisp rendering of resulting DC quad mesh with edges
  162. Eigen::MatrixXi E;
  163. Eigen::MatrixXd VV,N,NN;
  164. Eigen::VectorXi J;
  165. Eigen::MatrixXi FF;
  166. if(triangles)
  167. {
  168. VV = V;
  169. FF = Q;
  170. E.resize(Q.rows()*3,2);
  171. E<<
  172. Q.col(0), Q.col(1),
  173. Q.col(1), Q.col(2),
  174. Q.col(2), Q.col(0);
  175. }else
  176. {
  177. Eigen::VectorXi I,C;
  178. igl::polygon_corners(Q,I,C);
  179. E.resize(Q.rows()*4,2);
  180. E<<
  181. Q.col(0), Q.col(1),
  182. Q.col(1), Q.col(2),
  183. Q.col(2), Q.col(3),
  184. Q.col(3), Q.col(0);
  185. igl::per_face_normals(V,I,C,N,VV,FF,J);
  186. igl::slice(N,J,1,NN);
  187. igl::per_corner_normals(V,I,C,20,N,VV,FF,J,NN);
  188. }
  189. igl::opengl::glfw::Viewer vr;
  190. bool show_edges = true;
  191. bool use_dc = true;
  192. const auto update = [&]()
  193. {
  194. const bool was_face_based = vr.data().face_based ;
  195. vr.data().clear();
  196. if(use_dc)
  197. {
  198. vr.data().set_mesh(VV,FF);
  199. vr.data().show_lines = false;
  200. vr.data().set_normals(NN);
  201. if(show_edges)
  202. {
  203. vr.data().clear_edges();
  204. vr.data().set_edges(V,E,Eigen::RowVector3d(0,0,0));
  205. }
  206. }else
  207. {
  208. vr.data().set_mesh(mcV,mcF);
  209. vr.data().set_normals(mcN);
  210. vr.data().show_lines = show_edges;
  211. }
  212. vr.data().face_based = was_face_based;
  213. };
  214. update();
  215. vr.data().face_based = true;
  216. vr.callback_key_pressed = [&](decltype(vr) &,unsigned int key, int mod)
  217. {
  218. switch(key)
  219. {
  220. case ' ': use_dc=!use_dc; update();return true;
  221. case 'L': case 'l': show_edges=!show_edges; update();return true;
  222. }
  223. return false;
  224. };
  225. std::cout<<R"(
  226. [space] Toggle between dual contouring and marching cubes
  227. )";
  228. vr.launch();
  229. }