605_Tetgen.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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
  15. dependencies = ["tetgen", "glfw"]
  16. check_dependencies(dependencies)
  17. # Input polygon
  18. V = igl.eigen.MatrixXd()
  19. F = igl.eigen.MatrixXi()
  20. B = igl.eigen.MatrixXd()
  21. # Tetrahedralized interior
  22. TV = igl.eigen.MatrixXd()
  23. TT = igl.eigen.MatrixXi()
  24. TF = igl.eigen.MatrixXi()
  25. viewer = igl.glfw.Viewer()
  26. def key_down(viewer, key, modifier):
  27. if key >= ord('1') and key <= ord('9'):
  28. t = float((key - ord('1')) + 1) / 9.0
  29. v = igl.eigen.MatrixXd()
  30. v = B.col(2) - B.col(2).minCoeff()
  31. v /= v.col(0).maxCoeff()
  32. s = []
  33. for i in range(v.size()):
  34. if v[i, 0] < t:
  35. s.append(i)
  36. V_temp = igl.eigen.MatrixXd(len(s) * 4, 3)
  37. F_temp = igl.eigen.MatrixXd(len(s) * 4, 3).castint()
  38. for i in range(len(s)):
  39. V_temp.setRow(i * 4 + 0, TV.row(TT[s[i], 0]))
  40. V_temp.setRow(i * 4 + 1, TV.row(TT[s[i], 1]))
  41. V_temp.setRow(i * 4 + 2, TV.row(TT[s[i], 2]))
  42. V_temp.setRow(i * 4 + 3, TV.row(TT[s[i], 3]))
  43. F_temp.setRow(i * 4 + 0, igl.eigen.MatrixXd([[(i*4)+0, (i*4)+1, (i*4)+3]]).castint())
  44. F_temp.setRow(i * 4 + 1, igl.eigen.MatrixXd([[(i*4)+0, (i*4)+2, (i*4)+1]]).castint())
  45. F_temp.setRow(i * 4 + 2, igl.eigen.MatrixXd([[(i*4)+3, (i*4)+2, (i*4)+0]]).castint())
  46. F_temp.setRow(i * 4 + 3, igl.eigen.MatrixXd([[(i*4)+1, (i*4)+2, (i*4)+3]]).castint())
  47. viewer.data().clear()
  48. viewer.data().set_mesh(V_temp, F_temp)
  49. viewer.data().set_face_based(True)
  50. else:
  51. return False
  52. return True
  53. # Load a surface mesh
  54. igl.readOFF(TUTORIAL_SHARED_PATH + "fertility.off", V, F)
  55. # Tetrahedralize the interior
  56. igl.tetgen.tetrahedralize(V, F, "pq1.414Y", TV, TT, TF)
  57. # Compute barycenters
  58. igl.barycenter(TV, TT, B)
  59. # Plot the generated mesh
  60. key_down(viewer, ord('5'), 0)
  61. viewer.callback_key_down = key_down
  62. viewer.launch()