705_MarchingCubes.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 = ["copyleft", "glfw"]
  16. check_dependencies(dependencies)
  17. def key_down(viewer, key, modifier):
  18. if key == ord('1'):
  19. viewer.data().clear()
  20. viewer.data().set_mesh(V, F)
  21. elif key == ord('2'):
  22. viewer.data().clear()
  23. viewer.data().set_mesh(SV, SF)
  24. elif key == ord('3'):
  25. viewer.data().clear()
  26. viewer.data().set_mesh(BV, BF)
  27. return True
  28. if __name__ == "__main__":
  29. keys = {"1": "show original mesh",
  30. "2": "show marching cubes contour of signed distance",
  31. "3": "show marching cubes contour of indicator function"}
  32. print_usage(keys)
  33. V = igl.eigen.MatrixXd()
  34. F = igl.eigen.MatrixXi()
  35. # Read in inputs as double precision floating point meshes
  36. igl.read_triangle_mesh(TUTORIAL_SHARED_PATH + "armadillo.obj", V, F)
  37. # number of vertices on the largest side
  38. s = 50
  39. Vmin = V.colwiseMinCoeff()
  40. Vmax = V.colwiseMaxCoeff()
  41. h = (Vmax - Vmin).maxCoeff() / s
  42. res = (s * ((Vmax - Vmin) / (Vmax - Vmin).maxCoeff())).castint()
  43. def lerp(res, Vmin, Vmax, di, d):
  44. return Vmin[d] + float(di) / (res[d] - 1) * (Vmax[d] - Vmin[d])
  45. # create grid
  46. print("Creating grid...")
  47. GV = igl.eigen.MatrixXd(res[0] * res[1] * res[2], 3)
  48. for zi in range(res[2]):
  49. z = lerp(res, Vmin, Vmax, zi, 2)
  50. for yi in range(res[1]):
  51. y = lerp(res, Vmin, Vmax, yi, 1)
  52. for xi in range(res[0]):
  53. x = lerp(res, Vmin, Vmax, xi, 0)
  54. GV.setRow(xi + res[0] * (yi + res[1] * zi), igl.eigen.MatrixXd([[x, y, z]]))
  55. # compute values
  56. print("Computing distances...")
  57. S = igl.eigen.MatrixXd()
  58. B = igl.eigen.MatrixXd()
  59. I = igl.eigen.MatrixXi()
  60. C = igl.eigen.MatrixXd()
  61. N = igl.eigen.MatrixXd()
  62. igl.signed_distance(GV, V, F, igl.SIGNED_DISTANCE_TYPE_PSEUDONORMAL, S, I, C, N)
  63. # Convert distances to binary inside-outside data --> aliasing artifacts
  64. B = S.copy()
  65. for e in range(B.rows()):
  66. if B[e] > 0:
  67. B[e] = 1
  68. else:
  69. if B[e] < 0:
  70. B[e] = -1
  71. else:
  72. B[e] = 0
  73. print("Marching cubes...")
  74. SV = igl.eigen.MatrixXd()
  75. BV = igl.eigen.MatrixXd()
  76. SF = igl.eigen.MatrixXi()
  77. BF = igl.eigen.MatrixXi()
  78. igl.copyleft.marching_cubes(S, GV, res[0], res[1], res[2], SV, SF)
  79. igl.copyleft.marching_cubes(B, GV, res[0], res[1], res[2], BV, BF)
  80. # Plot the generated mesh
  81. viewer = igl.glfw.Viewer()
  82. viewer.data().set_mesh(SV, SF)
  83. viewer.callback_key_down = key_down
  84. viewer.launch()