704_SignedDistance.py 4.0 KB

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