2
0

main.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include <igl/cat.h>
  2. #include <igl/edge_lengths.h>
  3. #include <igl/per_edge_normals.h>
  4. #include <igl/per_face_normals.h>
  5. #include <igl/per_vertex_normals.h>
  6. #include <igl/point_mesh_squared_distance.h>
  7. #include <igl/readMESH.h>
  8. #include <igl/signed_distance.h>
  9. #include <igl/slice_mask.h>
  10. #include <igl/marching_tets.h>
  11. #include <igl/upsample.h>
  12. #include <igl/opengl/glfw/Viewer.h>
  13. #include <igl/writeOBJ.h>
  14. #include <Eigen/Sparse>
  15. #include <iostream>
  16. Eigen::MatrixXd V;
  17. Eigen::MatrixXi T,F;
  18. igl::AABB<Eigen::MatrixXd,3> tree;
  19. igl::FastWindingNumberBVH fwn_bvh;
  20. Eigen::MatrixXd FN,VN,EN;
  21. Eigen::MatrixXi E;
  22. Eigen::VectorXi EMAP;
  23. double max_distance = 1;
  24. double slice_z = 0.5;
  25. bool overlay = false;
  26. bool useFastWindingNumber = false;
  27. const Eigen::MatrixXd CM =
  28. (Eigen::MatrixXd(50,3)<<
  29. 242,242,242,
  30. 247,251,253,
  31. 228,234,238,
  32. 233,243,249,
  33. 214,227,234,
  34. 217,234,244,
  35. 199,218,230,
  36. 203,226,240,
  37. 186,211,226,
  38. 187,217,236,
  39. 171,203,222,
  40. 173,209,232,
  41. 157,195,218,
  42. 158,201,228,
  43. 142,187,214,
  44. 143,193,223,
  45. 129,179,210,
  46. 128,185,219,
  47. 114,171,206,
  48. 112,176,215,
  49. 100,163,202,
  50. 98,168,211,
  51. 86,156,198,
  52. 82,159,207,
  53. 71,148,194,
  54. 255,247,223,
  55. 242,230,204,
  56. 255,235,206,
  57. 242,219,189,
  58. 255,225,191,
  59. 242,209,175,
  60. 255,214,176,
  61. 242,198,159,
  62. 255,203,160,
  63. 242,188,145,
  64. 255,192,145,
  65. 242,177,129,
  66. 255,181,128,
  67. 242,167,115,
  68. 255,170,113,
  69. 242,157,101,
  70. 255,159,97,
  71. 242,146,85,
  72. 255,148,82,
  73. 242,136,71,
  74. 255,137,65,
  75. 242,125,55,
  76. 255,126,50,
  77. 242,115,41,
  78. 255,116,36).finished()/255.0;
  79. void update_visualization(igl::opengl::glfw::Viewer & viewer)
  80. {
  81. using namespace Eigen;
  82. using namespace std;
  83. Eigen::Vector4d plane(
  84. 0,0,1,-((1-slice_z)*V.col(2).minCoeff()+slice_z*V.col(2).maxCoeff()));
  85. MatrixXd V_vis;
  86. MatrixXi F_vis;
  87. // Extract triangle mesh slice through volume mesh and subdivide nasty
  88. // triangles
  89. {
  90. VectorXi J;
  91. SparseMatrix<double> bary;
  92. {
  93. // Value of plane's implicit function at all vertices
  94. const VectorXd IV =
  95. (V.col(0)*plane(0) +
  96. V.col(1)*plane(1) +
  97. V.col(2)*plane(2)).array()
  98. + plane(3);
  99. igl::marching_tets(V,T,IV,V_vis,F_vis,J,bary);
  100. igl::writeOBJ("vis.obj",V_vis,F_vis);
  101. }
  102. while(true)
  103. {
  104. MatrixXd l;
  105. igl::edge_lengths(V_vis,F_vis,l);
  106. l /= (V_vis.colwise().maxCoeff() - V_vis.colwise().minCoeff()).norm();
  107. const double max_l = 0.03;
  108. if(l.maxCoeff()<max_l)
  109. {
  110. break;
  111. }
  112. Array<bool,Dynamic,1> bad = l.array().rowwise().maxCoeff() > max_l;
  113. MatrixXi F_vis_bad, F_vis_good;
  114. igl::slice_mask(F_vis,bad,1,F_vis_bad);
  115. igl::slice_mask(F_vis,(bad!=true).eval(),1,F_vis_good);
  116. igl::upsample(V_vis,F_vis_bad);
  117. F_vis = igl::cat(1,F_vis_bad,F_vis_good);
  118. }
  119. }
  120. // Compute signed distance
  121. VectorXd S_vis;
  122. if (!useFastWindingNumber)
  123. {
  124. VectorXi I;
  125. MatrixXd N,C;
  126. // Bunny is a watertight mesh so use pseudonormal for signing
  127. signed_distance_pseudonormal(V_vis,V,F,tree,FN,VN,EN,EMAP,S_vis,I,C,N);
  128. } else {
  129. signed_distance_fast_winding_number(V_vis, V, F, tree, fwn_bvh, S_vis);
  130. }
  131. const auto & append_mesh = [&F_vis,&V_vis](
  132. const Eigen::MatrixXd & V,
  133. const Eigen::MatrixXi & F,
  134. const RowVector3d & color)
  135. {
  136. F_vis.conservativeResize(F_vis.rows()+F.rows(),3);
  137. F_vis.bottomRows(F.rows()) = F.array()+V_vis.rows();
  138. V_vis.conservativeResize(V_vis.rows()+V.rows(),3);
  139. V_vis.bottomRows(V.rows()) = V;
  140. };
  141. if(overlay)
  142. {
  143. append_mesh(V,F,RowVector3d(0.8,0.8,0.8));
  144. }
  145. viewer.data().clear();
  146. viewer.data().set_mesh(V_vis,F_vis);
  147. viewer.data().set_colormap(CM);
  148. viewer.data().set_data(S_vis);
  149. viewer.core().lighting_factor = overlay;
  150. }
  151. bool key_down(igl::opengl::glfw::Viewer& viewer, unsigned char key, int mod)
  152. {
  153. switch(key)
  154. {
  155. default:
  156. return false;
  157. case ' ':
  158. overlay ^= true;
  159. break;
  160. case '.':
  161. slice_z = std::min(slice_z+0.01,0.99);
  162. break;
  163. case ',':
  164. slice_z = std::max(slice_z-0.01,0.01);
  165. break;
  166. case '1':
  167. useFastWindingNumber = true;
  168. break;
  169. case '2':
  170. useFastWindingNumber = false;
  171. break;
  172. }
  173. update_visualization(viewer);
  174. return true;
  175. }
  176. int main(int argc, char *argv[])
  177. {
  178. using namespace Eigen;
  179. using namespace std;
  180. cout<<"Usage:"<<endl;
  181. cout<<"[space] toggle showing surface."<<endl;
  182. cout<<"'.'/',' push back/pull forward slicing plane."<<endl;
  183. cout<< "1/2 toggle between fast winding number (1) and pseudonormal (2) signing. \n";
  184. cout<<endl;
  185. // Load mesh: (V,T) tet-mesh of convex hull, F contains original surface
  186. // triangles
  187. igl::readMESH(TUTORIAL_SHARED_PATH "/bunny.mesh",V,T,F);
  188. // Encapsulated call to point_mesh_squared_distance to determine bounds
  189. {
  190. VectorXd sqrD;
  191. VectorXi I;
  192. MatrixXd C;
  193. igl::point_mesh_squared_distance(V,V,F,sqrD,I,C);
  194. max_distance = sqrt(sqrD.maxCoeff());
  195. }
  196. // Fast winding and Pseudo normal depend on differnt AABB trees... We initialize both here.
  197. // Pseudonormal setup...
  198. // Precompute signed distance AABB tree
  199. tree.init(V,F);
  200. // Precompute vertex,edge and face normals
  201. igl::per_face_normals(V,F,FN);
  202. igl::per_vertex_normals(
  203. V,F,igl::PER_VERTEX_NORMALS_WEIGHTING_TYPE_ANGLE,FN,VN);
  204. igl::per_edge_normals(
  205. V,F,igl::PER_EDGE_NORMALS_WEIGHTING_TYPE_UNIFORM,FN,EN,E,EMAP);
  206. // fast winding number setup (just init fwn bvh)
  207. igl::fast_winding_number(V, F, 2, fwn_bvh);
  208. // Plot the generated mesh
  209. igl::opengl::glfw::Viewer viewer;
  210. update_visualization(viewer);
  211. viewer.callback_key_down = &key_down;
  212. viewer.data().show_lines = false;
  213. viewer.launch();
  214. }