704_SignedDistance.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from __future__ import print_function
  2. # Add the igl library to the modules search path
  3. import sys, os
  4. sys.path.insert(0, os.getcwd() + "/../")
  5. import pyigl as igl
  6. from iglhelpers import e2p
  7. import math
  8. TUTORIAL_SHARED_PATH = "../../tutorial/shared/"
  9. global V, F, T, tree, FN, VN, EN, E, EMAP, max_distance, slice_z, overlay
  10. V = igl.eigen.MatrixXd()
  11. F = igl.eigen.MatrixXi()
  12. T = igl.eigen.MatrixXi()
  13. tree = igl.AABB()
  14. FN = igl.eigen.MatrixXd()
  15. VN = igl.eigen.MatrixXd()
  16. EN = igl.eigen.MatrixXd()
  17. E = igl.eigen.MatrixXi()
  18. EMAP = igl.eigen.MatrixXi()
  19. max_distance = 1
  20. slice_z = 0.5
  21. overlay = False
  22. viewer = igl.viewer.Viewer()
  23. def update_visualization(viewer):
  24. global V, F, T, tree, FN, VN, EN, E, EMAP, max_distance, slice_z, overlay
  25. plane = igl.eigen.MatrixXd([0.0, 0.0, 1.0, -((1-slice_z) * V.col(2).minCoeff() + slice_z * V.col(2).maxCoeff())])
  26. V_vis = igl.eigen.MatrixXd()
  27. F_vis = igl.eigen.MatrixXi()
  28. # Extract triangle mesh slice through volume mesh and subdivide nasty triangles
  29. J = igl.eigen.MatrixXi()
  30. bary = igl.eigen.SparseMatrixd()
  31. igl.slice_tets(V, T, plane, V_vis, F_vis, J, bary)
  32. max_l = 0.03
  33. # while True:
  34. # l = igl.eigen.MatrixXd()
  35. # igl.edge_lengths(V_vis, F_vis, l)
  36. # l /= (V_vis.colwise().maxCoeff() - V_vis.colwise().minCoeff()).norm()
  37. #
  38. # if l.maxCoeff() < max_l:
  39. # break
  40. #
  41. # bad = e2p(l.rowwiseMaxCoeff())
  42. # bad = bad > max_l
  43. # F_vis_bad = igl.eigen.MatrixXi()
  44. # F_vis_good = igl.eigen.MatrixXi()
  45. # igl::slice_mask(F_vis, bad, 1, F_vis_bad);
  46. # igl::slice_mask(F_vis, (bad!=true).eval(), 1, F_vis_good);
  47. # igl.upsample(V_vis, F_vis_bad)
  48. # F_vis = igl.cat(1, F_vis_bad, F_vis_good)
  49. # #Compute signed distance
  50. # S_vis = igl.eigen.MatrixXd()
  51. # I = igl.eigen.MatrixXi()
  52. # N = igl.eigen.MatrixXd()
  53. # C = igl.eigen.MatrixXd()
  54. # # Bunny is a watertight mesh so use pseudonormal for signing
  55. # igl.signed_distance_pseudonormal(V_vis, V, F, tree, FN, VN, EN, EMAP, S_vis, I, C, N)
  56. # # push to [0,1] range
  57. # S_vis.array() = 0.5*(S_vis.array()/max_distance)+0.5;
  58. # C_vis = igl.eigen.MatrixXi()
  59. # # color without normalizing
  60. # igl.parula(S_vis, False, C_vis)
  61. # const auto & append_mesh = [&C_vis,&F_vis,&V_vis](const Eigen::MatrixXd & V, const Eigen::MatrixXi & F, const RowVector3d & color)
  62. # F_vis.conservativeResize(F_vis.rows() + F.rows(), 3)
  63. # F_vis.bottomRows(F.rows()) = F.array() + V_vis.rows()
  64. # V_vis.conservativeResize(V_vis.rows() + V.rows(), 3)
  65. # V_vis.bottomRows(V.rows()) = V
  66. # C_vis.conservativeResize(C_vis.rows() + V.rows(), 3)
  67. # C_vis.bottomRows(V.rows()).rowwise() = color
  68. # if overlay:
  69. # append_mesh(V, F, RowVector3d(0.8,0.8,0.8))
  70. viewer.data.clear()
  71. viewer.data.set_mesh(V_vis, F_vis)
  72. # viewer.data.set_colors(C_vis)
  73. viewer.core.lighting_factor = overlay
  74. def key_down(viewer, key, modifier):
  75. global slice_z, overlay
  76. if key == ord(' '):
  77. overlay = not overlay
  78. elif key == ord('.'):
  79. slice_z = min(slice_z + 0.01, 0.99)
  80. elif key == ord(','):
  81. slice_z = max(slice_z - 0.01, 0.01)
  82. else:
  83. return False
  84. update_visualization(viewer)
  85. return True
  86. print("Press [space] to toggle showing surface.")
  87. print("Press '.'/',' to push back/pull forward slicing plane.")
  88. #Load mesh: (V,T) tet-mesh of convex hull, F contains original surface triangles
  89. igl.readMESH(TUTORIAL_SHARED_PATH + "bunny.mesh", V, T, F);
  90. #Call to point_mesh_squared_distance to determine bounds
  91. sqrD = igl.eigen.MatrixXd()
  92. I = igl.eigen.MatrixXi()
  93. C = igl.eigen.MatrixXd()
  94. igl.point_mesh_squared_distance(V, V, F, sqrD, I, C)
  95. max_distance = math.sqrt(sqrD.maxCoeff())
  96. #Precompute signed distance AABB tree
  97. tree.init(V, F)
  98. #Precompute vertex, edge and face normals
  99. igl.per_face_normals(V, F, FN)
  100. igl.per_vertex_normals(V, F, igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_ANGLE, FN, VN)
  101. igl.per_edge_normals(V, F, igl.PER_EDGE_NORMALS_WEIGHTING_TYPE_UNIFORM, FN, EN, E, EMAP)
  102. #Plot the generated mesh
  103. update_visualization(viewer);
  104. viewer.callback_key_down = key_down
  105. viewer.core.show_lines = False
  106. viewer.launch()