705_MarchingCubes.py 2.7 KB

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