702_WindingNumber.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import sys, os
  2. # Add the igl library to the modules search path
  3. sys.path.insert(0, os.getcwd() + "/../")
  4. import pyigl as igl
  5. from shared import TUTORIAL_SHARED_PATH, check_dependencies, print_usage
  6. dependencies = ["viewer"]
  7. check_dependencies(dependencies)
  8. def append_mesh(C_vis, F_vis, V_vis, V, F, color):
  9. F_vis.conservativeResize(F_vis.rows() + F.rows(), 3)
  10. F_vis.setBottomRows(F.rows(), F + V_vis.rows())
  11. V_vis.conservativeResize(V_vis.rows() + V.rows(), 3)
  12. V_vis.setBottomRows(V.rows(), V)
  13. C_vis.conservativeResize(C_vis.rows() + F.rows(), 3)
  14. colorM = igl.eigen.MatrixXd(F.rows(), C_vis.cols())
  15. colorM.rowwiseSet(color)
  16. C_vis.setBottomRows(F.rows(), colorM)
  17. def update(viewer):
  18. global V, F, T, W, slice_z, overlay
  19. plane = igl.eigen.MatrixXd([0, 0, 1, -((1 - slice_z) * V.col(2).minCoeff() + slice_z * V.col(2).maxCoeff())])
  20. V_vis = igl.eigen.MatrixXd()
  21. F_vis = igl.eigen.MatrixXi()
  22. J = igl.eigen.MatrixXi()
  23. bary = igl.eigen.SparseMatrixd()
  24. igl.slice_tets(V, T, plane, V_vis, F_vis, J, bary)
  25. W_vis = igl.eigen.MatrixXd()
  26. igl.slice(W, J, W_vis)
  27. C_vis = igl.eigen.MatrixXd()
  28. igl.parula(W_vis, False, C_vis)
  29. if overlay == 1: # OVERLAY_INPUT
  30. append_mesh(C_vis, F_vis, V_vis, V, F, igl.eigen.MatrixXd([[1., 0.894, 0.227]]))
  31. elif overlay == 2: # OVERLAY_OUTPUT
  32. append_mesh(C_vis, F_vis, V_vis, V, F, igl.eigen.MatrixXd([[0.8, 0.8, 0.8]]))
  33. viewer.data.clear()
  34. viewer.data.set_mesh(V_vis, F_vis)
  35. viewer.data.set_colors(C_vis)
  36. viewer.data.set_face_based(True)
  37. def key_down(viewer, key, modifier):
  38. global overlay, slice_z
  39. if key == ord(' '):
  40. overlay = (overlay + 1) % 3
  41. elif key == ord('.'):
  42. slice_z = min(slice_z + 0.01, 0.99)
  43. elif key == ord(','):
  44. slice_z = max(slice_z - 0.01, 0.01)
  45. update(viewer)
  46. return False
  47. if __name__ == "__main__":
  48. keys = {"space": "toggle showing input mesh, output mesh or slice through tet-mesh of convex hull",
  49. ". / ,": "push back/pull forward slicing plane"}
  50. print_usage(keys)
  51. V = igl.eigen.MatrixXd()
  52. BC = igl.eigen.MatrixXd()
  53. W = igl.eigen.MatrixXd()
  54. T = igl.eigen.MatrixXi()
  55. F = igl.eigen.MatrixXi()
  56. G = igl.eigen.MatrixXi()
  57. slice_z = 0.5
  58. overlay = 0
  59. # Load mesh: (V,T) tet-mesh of convex hull, F contains facets of input
  60. # surface mesh _after_ self-intersection resolution
  61. igl.readMESH(TUTORIAL_SHARED_PATH + "big-sigcat.mesh", V, T, F)
  62. # Compute barycenters of all tets
  63. igl.barycenter(V, T, BC)
  64. # Compute generalized winding number at all barycenters
  65. print("Computing winding number over all %i tets..." % T.rows())
  66. igl.winding_number(V, F, BC, W)
  67. # Extract interior tets
  68. Wt = sum(W > 0.5)
  69. CT = igl.eigen.MatrixXi(Wt, 4)
  70. k = 0
  71. for t in range(T.rows()):
  72. if W[t] > 0.5:
  73. CT.setRow(k, T.row(t))
  74. k += 1
  75. # find bounary facets of interior tets
  76. igl.boundary_facets(CT, G)
  77. # boundary_facets seem to be reversed...
  78. G = G.rowwiseReverse()
  79. # normalize
  80. W = (W - W.minCoeff()) / (W.maxCoeff() - W.minCoeff())
  81. # Plot the generated mesh
  82. viewer = igl.viewer.Viewer()
  83. update(viewer)
  84. viewer.callback_key_down = key_down
  85. viewer.launch()