404_DualQuaternionSkinning.py 4.7 KB

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