704_SignedDistance.py 4.4 KB

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