707_SweptVolume.py 2.8 KB

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