707_SweptVolume.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. from math import pi, cos
  11. sys.path.insert(0, os.getcwd() + "/../")
  12. import pyigl as igl
  13. from shared import TUTORIAL_SHARED_PATH, check_dependencies, print_usage
  14. dependencies = ["copyleft", "viewer"]
  15. check_dependencies(dependencies)
  16. def key_down(viewer, key, modifier):
  17. global show_swept_volume, SV, SF, V, F
  18. if key == ord(' '):
  19. show_swept_volume = not show_swept_volume
  20. viewer.data.clear()
  21. if show_swept_volume:
  22. viewer.data.set_mesh(SV, SF)
  23. viewer.data.uniform_colors(igl.eigen.MatrixXd([0.2, 0.2, 0.2]), igl.eigen.MatrixXd([1.0, 1.0, 1.0]), igl.eigen.MatrixXd([1.0, 1.0, 1.0])) # TODO replace with constants from cpp
  24. else:
  25. viewer.data.set_mesh(V, F)
  26. viewer.core.is_animating = not show_swept_volume
  27. viewer.data.set_face_based(True)
  28. return True
  29. def pre_draw(viewer):
  30. global show_swept_volume, V
  31. if not show_swept_volume:
  32. T = transform(0.25 * igl.get_seconds())
  33. VT = V * T.matrix().block(0, 0, 3, 3).transpose()
  34. trans = T.matrix().block(0, 3, 3, 1).transpose()
  35. Vtrans = igl.eigen.MatrixXd(VT.rows(), VT.cols())
  36. Vtrans.rowwiseSet(trans)
  37. VT += Vtrans
  38. viewer.data.set_vertices(VT)
  39. viewer.data.compute_normals()
  40. return False
  41. # Define a rigid motion
  42. def transform(t):
  43. T = igl.eigen.Affine3d()
  44. T.setIdentity()
  45. T.rotate(t * 2 * pi, igl.eigen.MatrixXd([0, 1, 0]))
  46. T.translate(igl.eigen.MatrixXd([0, 0.125 * cos(2 * pi * t), 0]))
  47. return T
  48. if __name__ == "__main__":
  49. keys = {"space": "toggle between transforming original mesh and swept volume"}
  50. print_usage(keys)
  51. V = igl.eigen.MatrixXd()
  52. SV = igl.eigen.MatrixXd()
  53. VT = igl.eigen.MatrixXd()
  54. F = igl.eigen.MatrixXi()
  55. SF = igl.eigen.MatrixXi()
  56. show_swept_volume = False
  57. grid_size = 50
  58. time_steps = 200
  59. isolevel = 1
  60. igl.read_triangle_mesh(TUTORIAL_SHARED_PATH + "bunny.off", V, F)
  61. print("Computing swept volume...")
  62. igl.copyleft.swept_volume(V, F, transform, time_steps, grid_size, isolevel, SV, SF)
  63. print("...finished.")
  64. # Plot the generated mesh
  65. viewer = igl.viewer.Viewer()
  66. viewer.data.set_mesh(V, F)
  67. viewer.data.set_face_based(True)
  68. viewer.core.is_animating = not show_swept_volume
  69. viewer.callback_pre_draw = pre_draw
  70. viewer.callback_key_down = key_down
  71. viewer.launch()