704_SignedDistance.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 append_mesh(C_vis, F_vis, V_vis, V, F, color):
  24. F_vis.conservativeResize(F_vis.rows() + F.rows(), 3)
  25. F_vis.setBottomRows(F.rows(), F + V_vis.rows())
  26. V_vis.conservativeResize(V_vis.rows() + V.rows(), 3)
  27. V_vis.setBottomRows(V.rows(), V)
  28. C_vis.conservativeResize(C_vis.rows() + V.rows(), 3)
  29. colorM = igl.eigen.MatrixXd(V.rows(), C_vis.cols())
  30. colorM.rowwiseSet(color)
  31. C_vis.setBottomRows(V.rows(), colorM)
  32. def update_visualization(viewer):
  33. global V, F, T, tree, FN, VN, EN, E, EMAP, max_distance, slice_z, overlay
  34. plane = igl.eigen.MatrixXd([0.0, 0.0, 1.0, -((1 - slice_z) * V.col(2).minCoeff() + slice_z * V.col(2).maxCoeff())])
  35. V_vis = igl.eigen.MatrixXd()
  36. F_vis = igl.eigen.MatrixXi()
  37. # Extract triangle mesh slice through volume mesh and subdivide nasty triangles
  38. J = igl.eigen.MatrixXi()
  39. bary = igl.eigen.SparseMatrixd()
  40. igl.slice_tets(V, T, plane, V_vis, F_vis, J, bary)
  41. max_l = 0.03
  42. while True:
  43. l = igl.eigen.MatrixXd()
  44. igl.edge_lengths(V_vis, F_vis, l)
  45. l /= (V_vis.colwiseMaxCoeff() - V_vis.colwiseMinCoeff()).norm()
  46. if l.maxCoeff() < max_l:
  47. break
  48. bad = l.rowwiseMaxCoeff() > max_l
  49. notbad = l.rowwiseMaxCoeff() <= max_l # TODO replace by ~ operator
  50. F_vis_bad = igl.eigen.MatrixXi()
  51. F_vis_good = igl.eigen.MatrixXi()
  52. igl.slice_mask(F_vis, bad, 1, F_vis_bad)
  53. igl.slice_mask(F_vis, notbad, 1, F_vis_good)
  54. igl.upsample(V_vis, F_vis_bad)
  55. F_vis = igl.cat(1, F_vis_bad, F_vis_good)
  56. # Compute signed distance
  57. S_vis = igl.eigen.MatrixXd()
  58. I = igl.eigen.MatrixXi()
  59. N = igl.eigen.MatrixXd()
  60. C = igl.eigen.MatrixXd()
  61. # Bunny is a watertight mesh so use pseudonormal for signing
  62. igl.signed_distance_pseudonormal(V_vis, V, F, tree, FN, VN, EN, EMAP, S_vis, I, C, N)
  63. # push to [0,1] range
  64. S_vis = 0.5 * (S_vis / max_distance) + 0.5
  65. C_vis = igl.eigen.MatrixXd()
  66. # color without normalizing
  67. igl.parula(S_vis, False, C_vis)
  68. if overlay:
  69. append_mesh(C_vis, F_vis, V_vis, V, F, igl.eigen.MatrixXd([[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()