contours.cpp 6.0 KB

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