702_WindingNumber.py 3.6 KB

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