404_DualQuaternionSkinning.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. from math import sin, cos, pi
  12. # Add the igl library to the modules search path
  13. import math
  14. sys.path.insert(0, os.getcwd() + "/../")
  15. import pyigl as igl
  16. from shared import TUTORIAL_SHARED_PATH, check_dependencies, print_usage
  17. dependencies = ["glfw"]
  18. check_dependencies(dependencies)
  19. def pre_draw(viewer):
  20. global recompute, anim_t, poses, C, BE, P, U, M, anim_t_dir
  21. if recompute:
  22. # Find pose interval
  23. begin = int(math.floor(anim_t)) % len(poses)
  24. end = int(math.floor(anim_t) + 1) % len(poses)
  25. t = anim_t - math.floor(anim_t)
  26. # Interpolate pose and identity
  27. anim_pose = igl.RotationList()
  28. for e in range(len(poses[begin])):
  29. anim_pose.append(poses[begin][e].slerp(t, poses[end][e]))
  30. # Propagate relative rotations via FK to retrieve absolute transformations
  31. vQ = igl.RotationList()
  32. vT = []
  33. igl.forward_kinematics(C, BE, P, anim_pose, vQ, vT)
  34. dim = C.cols()
  35. T = igl.eigen.MatrixXd(BE.rows() * (dim + 1), dim)
  36. for e in range(BE.rows()):
  37. a = igl.eigen.Affine3d.Identity()
  38. a.translate(vT[e])
  39. a.rotate(vQ[e])
  40. T.setBlock(e * (dim + 1), 0, dim + 1, dim, a.matrix().transpose().block(0, 0, dim + 1, dim))
  41. # Compute deformation via LBS as matrix multiplication
  42. if use_dqs:
  43. igl.dqs(V, W, vQ, vT, U)
  44. else:
  45. U = M * T
  46. # Also deform skeleton edges
  47. CT = igl.eigen.MatrixXd()
  48. BET = igl.eigen.MatrixXi()
  49. igl.deform_skeleton(C, BE, T, CT, BET)
  50. viewer.data().set_vertices(U)
  51. viewer.data().set_edges(CT, BET, sea_green)
  52. viewer.data().compute_normals()
  53. if viewer.core.is_animating:
  54. anim_t += anim_t_dir
  55. else:
  56. recompute = False
  57. return False
  58. def key_down(viewer, key, mods):
  59. global recompute, use_dqs, animation
  60. recompute = True
  61. if key == ord('D') or key == ord('d'):
  62. use_dqs = not use_dqs
  63. viewer.core.is_animating = False
  64. animation = False
  65. if use_dqs:
  66. print("Switched to Dual Quaternion Skinning")
  67. else:
  68. print("Switched to Linear Blend Skinning")
  69. elif key == ord(' '):
  70. if animation:
  71. viewer.core.is_animating = False
  72. animation = False
  73. else:
  74. viewer.core.is_animating = True
  75. animation = True
  76. return False
  77. if __name__ == "__main__":
  78. keys = {"d": "toggle between LBS and DQS",
  79. "space": "toggle animation"}
  80. print_usage(keys)
  81. V = igl.eigen.MatrixXd()
  82. F = igl.eigen.MatrixXi()
  83. C = igl.eigen.MatrixXd()
  84. BE = igl.eigen.MatrixXi()
  85. P = igl.eigen.MatrixXi()
  86. W = igl.eigen.MatrixXd()
  87. M = igl.eigen.MatrixXd()
  88. sea_green = igl.eigen.MatrixXd([[70. / 255., 252. / 255., 167. / 255.]])
  89. anim_t = 0.0
  90. anim_t_dir = 0.015
  91. use_dqs = False
  92. recompute = True
  93. animation = False # Flag needed as there is some synchronization problem with viewer.core.is_animating
  94. poses = [[]]
  95. igl.readOBJ(TUTORIAL_SHARED_PATH + "arm.obj", V, F)
  96. U = igl.eigen.MatrixXd(V)
  97. igl.readTGF(TUTORIAL_SHARED_PATH + "arm.tgf", C, BE)
  98. # retrieve parents for forward kinematics
  99. igl.directed_edge_parents(BE, P)
  100. rest_pose = igl.RotationList()
  101. igl.directed_edge_orientations(C, BE, rest_pose)
  102. poses = [[igl.eigen.Quaterniond.Identity() for i in range(4)] for j in range(4)]
  103. twist = igl.eigen.Quaterniond(pi, igl.eigen.MatrixXd([1, 0, 0]))
  104. poses[1][2] = rest_pose[2] * twist * rest_pose[2].conjugate()
  105. bend = igl.eigen.Quaterniond(-pi * 0.7, igl.eigen.MatrixXd([0, 0, 1]))
  106. poses[3][2] = rest_pose[2] * bend * rest_pose[2].conjugate()
  107. igl.readDMAT(TUTORIAL_SHARED_PATH + "arm-weights.dmat", W)
  108. igl.lbs_matrix(V, W, M)
  109. # Plot the mesh with pseudocolors
  110. viewer = igl.glfw.Viewer()
  111. viewer.data().set_mesh(U, F)
  112. viewer.data().set_edges(C, BE, sea_green)
  113. viewer.data().show_lines = False
  114. viewer.data().show_overlay_depth = False
  115. viewer.data().line_width = 1
  116. viewer.core.trackball_angle.normalize()
  117. viewer.callback_pre_draw = pre_draw
  118. viewer.callback_key_down = key_down
  119. viewer.core.is_animating = False
  120. viewer.core.camera_zoom = 2.5
  121. viewer.core.animation_max_fps = 30.0
  122. viewer.launch()