704_SignedDistance.py 4.1 KB

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