404_DualQuaternionSkinning.py 4.3 KB

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