704_SignedDistance.py 4.4 KB

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