705_MarchingCubes.py 3.1 KB

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