704_SignedDistance.py 4.0 KB

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