702_WindingNumber.py 3.6 KB

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