-
Notifications
You must be signed in to change notification settings - Fork 0
/
realTimeRotToAvatarPos.py
663 lines (617 loc) · 32.6 KB
/
realTimeRotToAvatarPos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
'''
Goal: after mapping後的rotation需要apply到avatar的lower body上,
才能夠的到position資訊, 得到position資訊後才能與DB當中的motion做比較
'''
import numpy as np
import pandas as pd
import time
import timeit
import json
import pickle
import os
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib
from scipy.spatial.transform import Rotation as R
from positionAnalysis import jointsNames
from realTimeHandRotationCompute import jointsNames as handJointNames
usedLowerBodyJoints = [
jointsNames.LeftUpperLeg, jointsNames.LeftLowerLeg, jointsNames.LeftFoot,
jointsNames.RightUpperLeg, jointsNames.RightLowerLeg, jointsNames.RightFoot,
jointsNames.Hip
]
def loadTPosePosAndVecs(saveDirPath):
'''
Goal: load儲存的T pose position以及vectors資訊
'''
TPosePositions=None
TPoseVectors=None
with open(saveDirPath+'TPosePositions.pickle', 'rb') as inPickle:
TPosePositions = pickle.load(inPickle)
with open(saveDirPath+'TPoseVectors.pickle', 'rb') as inPickle:
TPoseVectors = pickle.load(inPickle)
return TPosePositions, TPoseVectors
def visualize3DVecs(startPts, vectors):
'''
(original version is in realTimeHandRotationCompute)
Goal: 顯示3D空間向量, 方便debug, 顯示部分joints以及vertex即可
Input:
:*args: 多個3d position array組成的list代表向量的起點 + 多個3d position array組成的list代表多個向量
以上兩者長度要相同
'''
# origin = [0, 0, 0]
# x = [1, 2, 3]
# y = [4, 5, 6]
# xCy = np.cross(x, y)
xyzZip = list(zip(*[startPts[i]+vectors[i] for i in range(len(vectors))]))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
colormap = matplotlib.cm.inferno
colors = list(range(len(vectors)))
norm = matplotlib.colors.Normalize()
norm.autoscale(colors)
for i in range(len(vectors)):
ax.quiver(xyzZip[0][i], xyzZip[1][i], xyzZip[2][i], xyzZip[3][i], xyzZip[4][i], xyzZip[5][i], arrow_length_ratio =0.2, color=colormap(norm(colors[i])), label=i)
ax.set_xlim([0, 2])
ax.set_ylim([0, 2])
ax.set_zlim([0, 2])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.legend()
def forwardKinematic(kinematicChain, forwardRotations):
'''
Goal: 給定一個kinematic chain(1 position, 2 vectors),
給予第一個joint的X, Z旋轉, 以及第二個joint的X旋轉,
求第2及第3個點的旋轉後position
Note, z軸旋轉要正負相反, 因為Unity與python的z軸是反過來的
TODO: 第一個vector的旋轉, 會影響到第2個vector
'''
outputKC=[kinematicChain[0], None, None]
# upperLegRotMat = R.from_euler('zyx', [forwardRotations[1], 0, forwardRotations[0]], degrees=True) # upper leg rotation matrix
# 看起來unity是以zxy的順序以extrinsic rotation進行旋轉
# ref: https://forum.unity.com/threads/which-euler-angles-convention-used-in-unity.41114/#post-6828104
upperLegRotMat = R.from_euler('zxy', [forwardRotations[1], forwardRotations[0], 0], degrees=True) # upper leg rotation matrix
upperLegRotMat = upperLegRotMat.as_matrix()
outputKC[1] = np.dot(upperLegRotMat, kinematicChain[1])
# print(upperLegRotMat)
# lowerLegRotMat = R.from_euler('zyx', [0, 0, forwardRotations[2]], degrees=True)
lowerLegRotMat = R.from_euler('zxy', [0, forwardRotations[2], 0], degrees=True)
lowerLegRotMat = lowerLegRotMat.as_matrix()
outputKC[2] = np.dot(lowerLegRotMat, kinematicChain[2])
outputKC[2] = np.dot(upperLegRotMat, outputKC[2])
# print(lowerLegRotMat)
# another method(maybe wrong) -> it's wrong, it assume the link(bone) is always on the x axis
# link1Length = np.linalg.norm(kinematicChain[1])
# link2Length = np.linalg.norm(kinematicChain[2])
# firstRotResult = np.dot(upperLegRotMat, np.array([link1Length, 0, 0]))
# newSecondPoint = firstRotResult + forwardRotations[0]
# outputKC[1] = firstRotResult
# secondRotResult = np.dot(lowerLegRotMat, np.array([link2Length, 0, 0]))
# secondRotResult = np.dot(upperLegRotMat, secondRotResult)
# newThirdPoint = secondRotResult + newSecondPoint
# outputKC[2] = secondRotResult
return outputKC
# 接收quaternion做forward kinematic的function
def forwardKinematicQuat(kinematicChain, quaternions):
'''
接收quaternion做forward kinematic的function
Input:
:quaternions: (List) 第1個element是upper leg的quaternion,
第2個element是lower leg (knee)的quaternion
Output:
:outputKC: 旋轉後的kinematic chain
'''
outputKC=[kinematicChain[0], None, None]
## upper leg rotation
upperLegRotMat = R.from_quat(quaternions[0]) # upper leg rotation matrix
upperLegRotMat = upperLegRotMat.as_matrix()
outputKC[1] = np.dot(upperLegRotMat, kinematicChain[1])
## lower leg rotation
lowerLegRotMat = R.from_quat(quaternions[1])
lowerLegRotMat = lowerLegRotMat.as_matrix()
outputKC[2] = np.dot(lowerLegRotMat, kinematicChain[2])
outputKC[2] = np.dot(upperLegRotMat, outputKC[2])
return outputKC
# For test, compare the position result in unity and python
if __name__=='__main01__':
rotApplyUnitySaveDirPath = 'positionData/fromAfterMappingHand/leftFrontKickCombinations/'
unityPosJson = None
with open(rotApplyUnitySaveDirPath+'leftFrontKick(True, False, False, False, True, True).json', 'r') as fileIn:
unityPosJson = json.load(fileIn)['results']
rotApplyPythonSaveDirPath = 'positionData/fromAfterMappingHand/'
pythonPosJson = None
with open(rotApplyPythonSaveDirPath+'leftFrontKickStream.json', 'r') as fileIn:
pythonPosJson = json.load(fileIn)
unityTimeCount = len(unityPosJson)
pythonTimeCount = len(pythonPosJson)
unityJointCount = len(unityPosJson[0]['data'])
pythonJointCount = len(pythonPosJson[0]['data'])
print('unity time count: ', unityTimeCount)
print('python time count: ', pythonTimeCount)
print('unity joint count: ', unityJointCount)
print('python joint count: ', pythonJointCount)
jointInd = 0
jointAxis = 'z'
unityData = [unityPosJson[t]['data'][jointInd][jointAxis] for t in range(unityTimeCount)]
pythonData = [pythonPosJson[t]['data'][str(jointInd)][jointAxis] for t in range(pythonTimeCount)]
plt.figure()
plt.plot(range(unityTimeCount), unityData, label='unity')
plt.plot(range(pythonTimeCount), pythonData, label='real time')
plt.legend()
plt.show()
# For test,
# apply unity收集到的animation rotation data到python的avatar
# 觀察結果是否合理
if __name__=='__main01__':
# 1. 讀取預存好的T pose position以及vectors
# 2. 讀取animation rotation
# 3. (real time)Apply rotation到T pose vectors
# 1.
saveDirPath='TPoseInfo/genericAvatar/'
TPosePositions, TPoseVectors = loadTPosePosAndVecs(saveDirPath)
# 2.
animationRotDirPath = 'bodyDBRotation/genericAvatar/'
animRotJson = None
with open(os.path.join(animationRotDirPath, 'leftFrontKick0.03_withoutHip.json')) as fileIn:
animRotJson = json.load(fileIn)['results']
timeCount = len(animRotJson)
print('timeCount: ', timeCount)
# 3.
leftKinematic = [
TPosePositions[jointsNames.LeftUpperLeg],
TPoseVectors[0],
TPoseVectors[1]
] # upper leg position, upper leg vector, lower leg vector
rightKinematic = [
TPosePositions[jointsNames.RightUpperLeg],
TPoseVectors[2],
TPoseVectors[3]
]
lowerBodyPosition = [{'time': t, 'data': {aJoint: None for aJoint in usedLowerBodyJoints}} for t in range(timeCount)]
testKinematic1 = None
rotApplyTimeLaps = np.zeros(timeCount)
for t in range(timeCount):
testKinematic1 = forwardKinematic(
leftKinematic,
[
animRotJson[t]['data'][0]['x'],
animRotJson[t]['data'][0]['z'],
animRotJson[t]['data'][1]['x']
]
)
lowerBodyPosition[t]['data'][jointsNames.LeftLowerLeg] = testKinematic1[0] + testKinematic1[1]
lowerBodyPosition[t]['data'][jointsNames.LeftFoot] = testKinematic1[0] + testKinematic1[1] + testKinematic1[2]
testKinematic2 = forwardKinematic(
rightKinematic,
[
animRotJson[t]['data'][2]['x'],
animRotJson[t]['data'][2]['z'],
animRotJson[t]['data'][3]['x']
]
)
lowerBodyPosition[t]['data'][jointsNames.RightLowerLeg] = testKinematic2[0] + testKinematic2[1]
lowerBodyPosition[t]['data'][jointsNames.RightFoot] = testKinematic2[0] + testKinematic2[1] + testKinematic2[2]
rotApplyTimeLaps[t] = time.time()
rotApplyCost = rotApplyTimeLaps[1:] - rotApplyTimeLaps[:-1]
print('rotation compute avg time: ', np.mean(rotApplyCost))
print('rotation compute time std: ', np.std(rotApplyCost))
print('rotation compute max time cost: ', np.max(rotApplyCost))
print('rotation compute min time cost: ', np.min(rotApplyCost))
testKinematic1 = forwardKinematic(leftKinematic, [90, 90, 90])
## 轉換資料成方便儲存的格式
for t in range(timeCount):
lowerBodyPosition[t]['data'][jointsNames.Hip] = TPosePositions[jointsNames.Hip]
lowerBodyPosition[t]['data'][jointsNames.LeftUpperLeg] = TPosePositions[jointsNames.LeftUpperLeg]
lowerBodyPosition[t]['data'][jointsNames.RightUpperLeg] = TPosePositions[jointsNames.RightUpperLeg]
for t in range(timeCount):
for aJoint in usedLowerBodyJoints:
if lowerBodyPosition[t]['data'][aJoint] is not None:
lowerBodyPosition[t]['data'][aJoint] = {k: lowerBodyPosition[t]['data'][aJoint][i] for i, k in enumerate(['x', 'y', 'z'])}
# Store data into file
rotApplySaveDirPath='positionData/'
with open(os.path.join(rotApplySaveDirPath, 'testLeftFrontKickAnimRotToAvatar.json'), 'w') as WFile:
# json.dump(lowerBodyPosition, WFile)
pass
# quaternion rotation apply to avatar
if __name__ == '__main01__':
# 1. 讀取預存好的T pose position以及vectors
# 2. 讀取mapped hand rotations
# 3. (real time)Apply mapped hand rotations到T pose position以及vectors上
# 4. Store the applied result(avatar lower body motions)
# 5. Store computation time cost
TPosesaveDirPath='TPoseInfo/genericAvatar/'
# mappedHandRotSaveFilePath = 'rotationMappingQuaternionData/leftFrontKick/leftFrontKick_quat_linear_TFTTTT.json'
# rotApplySaveFilePath = 'positionData/fromAfterMappingHand/newMappingMethods/leftFrontKick_quat_linear_TFTTTT.json'
# mappedHandRotSaveFilePath = 'rotationMappingQuaternionData/leftFrontKick/leftFrontKick_quat_BSpline_TFTTTT.json'
# rotApplySaveFilePath = 'positionData/fromAfterMappingHand/newMappingMethods/leftFrontKick_quat_BSpline_TFTTTT.json'
# mappedHandRotSaveFilePath = 'rotationMappingQuaternionData/leftSideKick/leftFrontKick_quat_BSpline_TFTTTT.json'
# rotApplySaveFilePath = 'positionData/fromAfterMappingHand/newMappingMethods/leftFrontKick_quat_BSpline_TFTTTT.json'
# mappedHandRotSaveFilePath = 'rotationMappingQuaternionData/leftSideKick/leftSideKick_quat_BSpline_FTTTFT.json'
# rotApplySaveFilePath = 'positionData/fromAfterMappingHand/newMappingMethods/leftSideKick_quat_BSpline_FTTTFT.json'
# mappedHandRotSaveFilePath = 'rotationMappingQuaternionData/runSprint/runSprint_quat_BSpline_TFTTFT.json'
# rotApplySaveFilePath = 'positionData/fromAfterMappingHand/newMappingMethods/runSprint_quat_BSpline_TFTTFT.json'
# mappedHandRotSaveFilePath = 'rotationMappingQuaternionData/hurdleJump/hurdleJump_quat_BSpline_TFTTFT.json'
# rotApplySaveFilePath = 'positionData/fromAfterMappingHand/newMappingMethods/hurdleJump_quat_BSpline_TFTTFT.json'
# mappedHandRotSaveFilePath = 'rotationMappingQuaternionData/walkInjured/walkInjured_quat_BSpline_TFTTFT.json'
# rotApplySaveFilePath = 'positionData/fromAfterMappingHand/newMappingMethods/walkInjured_quat_BSpline_TFTTFT.json'
# mappedHandRotSaveFilePath = './bodyDBRotation/genericAvatar/quaternion/leftFrontKick0.03_075_withHip.json'
# rotApplySaveFilePath = 'positionData/leftFrontKick0.03_withHip.json'
# mappedHandRotSaveFilePath = 'handRotaionAfterMapping/leftFrontKick_quat_directMapping.json'
# rotApplySaveFilePath = 'positionData/leftFrontKick_quat_directMapping.json'
# mappedHandRotSaveFilePath = './bodyDBRotation/genericAvatar/quaternion/leftSideKick0.03_075_withHip.json'
# rotApplySaveFilePath = 'positionData/leftSideKick0.03_withHip.json'
# mappedHandRotSaveFilePath = './handRotaionAfterMapping/leftSideKick_quat_directMapping.json'
# rotApplySaveFilePath = 'positionData/leftSideKick_quat_directMapping.json'
# mappedHandRotSaveFilePath = './bodyDBRotation/genericAvatar/quaternion/runSprint0.03_05_withHip.json'
# rotApplySaveFilePath = 'positionData/runSprint0.03_withHip.json'
# mappedHandRotSaveFilePath = './handRotaionAfterMapping/runSprint_quat_directMapping.json'
# rotApplySaveFilePath = 'positionData/runSprint_quat_directMapping.json'
# mappedHandRotSaveFilePath = './handRotaionAfterMapping/runSprint_leftToRight_quat_directMapping.json'
# rotApplySaveFilePath = 'positionData/runSprint_leftToRight_quat_directMapping.json'
# mappedHandRotSaveFilePath = './handRotaionAfterMapping/runSprint_leftToRightDoubleFinger_quat_directMapping.json'
# rotApplySaveFilePath = 'positionData/runSprint_leftToRightDoubleFinger_quat_directMapping.json'
# mappedHandRotSaveFilePath = './handRotaionAfterMapping/runSprint_rgb_2_15_1_quat_directMapping.json'
# rotApplySaveFilePath = 'positionData/runSprint_rgb_2_15_1_quat_directMapping.json'
# mappedHandRotSaveFilePath = './bodyDBRotation/genericAvatar/quaternion/runInjured0.03_05_withHip.json'
# rotApplySaveFilePath = 'positionData/runInjured0.03_withHip.json'
# mappedHandRotSaveFilePath = './handRotaionAfterMapping/runInjured_quat_directMapping.json'
# rotApplySaveFilePath = 'positionData/runInjured_quat_directMapping.json'
# mappedHandRotSaveFilePath = './bodyDBRotation/genericAvatar/quaternion/jumpJoy0.03_075_withHip.json'
# rotApplySaveFilePath = 'positionData/jumpJoy0.03_075_withHip.json'
# mappedHandRotSaveFilePath = './handRotaionAfterMapping/jumpJoy_quat_directMapping.json'
# rotApplySaveFilePath = 'positionData/jumpJoy_quat_directMapping.json'
# mappedHandRotSaveFilePath = './bodyDBRotation/genericAvatar/quaternion/twoLegJump0.03_05_withHip.json'
# rotApplySaveFilePath = 'positionData/twoLegJump0.03_05_withHip.json'
# mappedHandRotSaveFilePath = './handRotaionAfterMapping/twoLegJump_quat_directMapping.json'
# rotApplySaveFilePath = 'positionData/twoLegJump_quat_directMapping.json'
mappedHandRotSaveFilePath = './handRotaionAfterMapping/runSprintAndFrontKick_3_2_5_quat_directMapping.json'
rotApplySaveFilePath = 'positionData/runSprintAndFrontKick_3_2_5_quat_directMapping.json'
# 1.
TPosePositions, TPoseVectors = loadTPosePosAndVecs(TPosesaveDirPath)
print(TPosePositions)
print(TPoseVectors)
# 2.
mappedHandRotJson = None
with open(mappedHandRotSaveFilePath, 'r') as fileIn:
# mappedHandRotJson = json.load(fileIn)['results']
mappedHandRotJson = json.load(fileIn)
timeCount = len(mappedHandRotJson)
print('timeCount: ', timeCount)
# 3.
## 3.1 切成兩個kinematic chain(left and right), 並且接下來的處理都是以chain為單位, 兩個chain個別做旋轉
leftKinematic = [
TPosePositions[jointsNames.LeftUpperLeg],
TPoseVectors[0],
TPoseVectors[1]
] # upper leg position, upper leg vector, lower leg vector
rightKinematic = [
TPosePositions[jointsNames.RightUpperLeg],
TPoseVectors[2],
TPoseVectors[3]
]
## 3.2 forward kinematic
lowerBodyPosition = [{'time': t, 'data': {aJoint: None for aJoint in usedLowerBodyJoints}} for t in range(timeCount)]
testKinematic1 = None
rotApplyTimeLaps = np.zeros(timeCount)
for t in range(timeCount):
testKinematic1 = forwardKinematicQuat(
leftKinematic,
[
[
mappedHandRotJson[t]['data'][0]['x'],
mappedHandRotJson[t]['data'][0]['y'],
mappedHandRotJson[t]['data'][0]['z'],
mappedHandRotJson[t]['data'][0]['w']
],
[
mappedHandRotJson[t]['data'][1]['x'],
mappedHandRotJson[t]['data'][1]['y'],
mappedHandRotJson[t]['data'][1]['z'],
mappedHandRotJson[t]['data'][1]['w']
]
]
)
lowerBodyPosition[t]['data'][jointsNames.LeftLowerLeg] = testKinematic1[0] + testKinematic1[1]
lowerBodyPosition[t]['data'][jointsNames.LeftFoot] = testKinematic1[0] + testKinematic1[1] + testKinematic1[2]
testKinematic2 = forwardKinematicQuat(
rightKinematic,
[
[
mappedHandRotJson[t]['data'][2]['x'],
mappedHandRotJson[t]['data'][2]['y'],
mappedHandRotJson[t]['data'][2]['z'],
mappedHandRotJson[t]['data'][2]['w']
],
[
mappedHandRotJson[t]['data'][3]['x'],
mappedHandRotJson[t]['data'][3]['y'],
mappedHandRotJson[t]['data'][3]['z'],
mappedHandRotJson[t]['data'][3]['w']
]
]
)
lowerBodyPosition[t]['data'][jointsNames.RightLowerLeg] = testKinematic2[0] + testKinematic2[1]
lowerBodyPosition[t]['data'][jointsNames.RightFoot] = testKinematic2[0] + testKinematic2[1] + testKinematic2[2]
rotApplyTimeLaps[t] = timeit.default_timer()
rotApplyCost = rotApplyTimeLaps[1:] - rotApplyTimeLaps[:-1]
print('rotation compute avg time: ', np.mean(rotApplyCost))
print('rotation compute time std: ', np.std(rotApplyCost))
print('rotation compute max time cost: ', np.max(rotApplyCost))
print('rotation compute min time cost: ', np.min(rotApplyCost))
# 4.
# Unity store 7 joints
# (left/right) upper, lowerleg , foot, hips
for t in range(timeCount):
lowerBodyPosition[t]['data'][jointsNames.Hip] = TPosePositions[jointsNames.Hip]
lowerBodyPosition[t]['data'][jointsNames.LeftUpperLeg] = TPosePositions[jointsNames.LeftUpperLeg]
lowerBodyPosition[t]['data'][jointsNames.RightUpperLeg] = TPosePositions[jointsNames.RightUpperLeg]
for t in range(timeCount):
for aJoint in usedLowerBodyJoints:
if lowerBodyPosition[t]['data'][aJoint] is not None:
lowerBodyPosition[t]['data'][aJoint] = {k: lowerBodyPosition[t]['data'][aJoint][i] for i, k in enumerate(['x', 'y', 'z'])}
with open(rotApplySaveFilePath, 'w') as WFile:
json.dump(lowerBodyPosition, WFile)
# 5. Store computation time cost
# timeCostDirPath = 'timeConsume/frontKick/forwardKinematic.csv'
# timeCostDf = pd.DataFrame({
# 'ForwardKinematic': rotApplyCost
# })
# timeCostDf.to_csv(timeCostDirPath, index=False)
# eular rotation apply to avatar (刪除不必要的code and comment)
if __name__ == '__main01__':
# 1. 讀取預存好的T pose position以及vectors
# 2. 讀取mapped hand rotations
# 3. (real time)Apply mapped hand rotations到T pose position以及vectors上
# 4. Store the applied result(avatar lower body motions)
TPosesaveDirPath='TPoseInfo/genericAvatar/'
# mappedHandRotSaveFilePath = 'rotationMappingData/leftFrontKick/leftFrontKick_eular_linear_TFTTTT.json'
# rotApplySaveFilePath = 'positionData/fromAfterMappingHand/newMappingMethods/leftFrontKick_eular_linear_TFTTTT.json'
mappedHandRotSaveFilePath = 'rotationMappingData/leftFrontKick/leftFrontKick_eular_BSpline_TFTTTT.json'
rotApplySaveFilePath = 'positionData/fromAfterMappingHand/newMappingMethods/leftFrontKick_eular_BSpline_TFTTTT.json'
# 1.
TPosePositions, TPoseVectors = loadTPosePosAndVecs(TPosesaveDirPath)
print(TPosePositions)
print(TPoseVectors)
# 2.
mappedHandRotJson = None
with open(mappedHandRotSaveFilePath, 'r') as fileIn:
mappedHandRotJson = json.load(fileIn)
timeCount = len(mappedHandRotJson)
print('timeCount: ', timeCount)
# 3.
## 3.1 切成兩個kinematic chain(left and right), 並且接下來的處理都是以chain為單位, 兩個chain個別做旋轉
leftKinematic = [
TPosePositions[jointsNames.LeftUpperLeg],
TPoseVectors[0],
TPoseVectors[1]
] # upper leg position, upper leg vector, lower leg vector
rightKinematic = [
TPosePositions[jointsNames.RightUpperLeg],
TPoseVectors[2],
TPoseVectors[3]
]
## 3.2 forward kinematic
lowerBodyPosition = [{'time': t, 'data': {aJoint: None for aJoint in usedLowerBodyJoints}} for t in range(timeCount)]
testKinematic1 = None
rotApplyTimeLaps = np.zeros(timeCount)
for t in range(timeCount):
testKinematic1 = forwardKinematic(
leftKinematic,
[
mappedHandRotJson[t]['data'][0]['x'],
mappedHandRotJson[t]['data'][0]['z'],
mappedHandRotJson[t]['data'][1]['x']
]
)
lowerBodyPosition[t]['data'][jointsNames.LeftLowerLeg] = testKinematic1[0] + testKinematic1[1]
lowerBodyPosition[t]['data'][jointsNames.LeftFoot] = testKinematic1[0] + testKinematic1[1] + testKinematic1[2]
testKinematic2 = forwardKinematic(
rightKinematic,
[
mappedHandRotJson[t]['data'][2]['x'],
mappedHandRotJson[t]['data'][2]['z'],
mappedHandRotJson[t]['data'][3]['x']
]
)
lowerBodyPosition[t]['data'][jointsNames.RightLowerLeg] = testKinematic2[0] + testKinematic2[1]
lowerBodyPosition[t]['data'][jointsNames.RightFoot] = testKinematic2[0] + testKinematic2[1] + testKinematic2[2]
rotApplyTimeLaps[t] = time.time()
rotApplyCost = rotApplyTimeLaps[1:] - rotApplyTimeLaps[:-1]
print('rotation compute avg time: ', np.mean(rotApplyCost))
print('rotation compute time std: ', np.std(rotApplyCost))
print('rotation compute max time cost: ', np.max(rotApplyCost))
print('rotation compute min time cost: ', np.min(rotApplyCost))
# 4.
# Unity store 7 joints
# (left/right) upper, lowerleg , foot, hips
for t in range(timeCount):
lowerBodyPosition[t]['data'][jointsNames.Hip] = TPosePositions[jointsNames.Hip]
lowerBodyPosition[t]['data'][jointsNames.LeftUpperLeg] = TPosePositions[jointsNames.LeftUpperLeg]
lowerBodyPosition[t]['data'][jointsNames.RightUpperLeg] = TPosePositions[jointsNames.RightUpperLeg]
for t in range(timeCount):
for aJoint in usedLowerBodyJoints:
if lowerBodyPosition[t]['data'][aJoint] is not None:
lowerBodyPosition[t]['data'][aJoint] = {k: lowerBodyPosition[t]['data'][aJoint][i] for i, k in enumerate(['x', 'y', 'z'])}
with open(rotApplySaveFilePath, 'w') as WFile:
json.dump(lowerBodyPosition, WFile)
pass
# Implement rotation apply to avatar (old rotation apply to avatar method)
if __name__=='__main01__':
# 1. 讀取預存好的T pose position以及vectors
# 2. 讀取mapped hand rotations
# 3. (real time)Apply mapped hand rotations到T pose position以及vectors上
# 4. Store the applied result(avatar lower body motions)
# 1.
saveDirPath='TPoseInfo/genericAvatar/'
TPosePositions, TPoseVectors = loadTPosePosAndVecs(saveDirPath)
print(TPosePositions)
print(TPoseVectors)
# 2.
# mappedHandRotSaveDirPath='handRotaionAfterMapping/leftFrontKick/'
# mappedHandRotSaveDirPath='handRotaionAfterMapping/' # python real time版本計算的結果
mappedHandRotSaveDirPath='handRotaionAfterMapping/leftFrontKickStreamLinearMapping/'
# mappedHandRotSaveDirPath='handRotaionAfterMapping/leftSideKickStreamLinearMapping/'
# mappedHandRotSaveDirPath='handRotaionAfterMapping/runSprintStreamLinearMapping/'
# mappedHandRotSaveDirPath='handRotaionAfterMapping/walkInjuredStreamLinearMapping/'
mappedHandRotJson = None
# with open(mappedHandRotSaveDirPath+'leftFrontKick(True, False, False, False, True, True).json', 'r') as fileIn:
# with open(mappedHandRotSaveDirPath+'leftFrontKickStreamTFFFTT.json', 'r') as fileIn: # python real time版本計算的結果
with open(mappedHandRotSaveDirPath+'leftFrontKick(True, False, False, True, True, True).json', 'r') as fileIn:
# with open(mappedHandRotSaveDirPath+'leftSideKick(False, True, True, False, False, False).json', 'r') as fileIn:
# with open(mappedHandRotSaveDirPath+'runSprint(True, False, True, True, False, True).json', 'r') as fileIn:
# with open(mappedHandRotSaveDirPath+'walkInjured(True, False, True, True, False, True).json', 'r') as fileIn:
mappedHandRotJson = json.load(fileIn)
timeCount = len(mappedHandRotJson)
# print(mappedHandRotJson)
# 3.
# visualize 三個joints的heirarchy結構(origin position, two vectors)
# 這邊只會有兩個獨立的heirarchy結構: 左腿, 右腿
## 3.1 切成兩個kinematic chain(left and right), 並且接下來的處理都是以chain為單位
leftKinematic = [
TPosePositions[jointsNames.LeftUpperLeg],
TPoseVectors[0],
TPoseVectors[1]
] # upper leg position, upper leg vector, lower leg vector
rightKinematic = [
TPosePositions[jointsNames.RightUpperLeg],
TPoseVectors[2],
TPoseVectors[3]
]
# testKinematic = [
# np.array([0, 0, 0]),
# np.array([0, 0, 1]),
# np.array([1, 1, 0])
# ]
## 3.2 forward kinematic
# visualize3DVecs(
# [testKinematic[0].tolist(), (testKinematic[0]+testKinematic[1]).tolist()],
# [testKinematic[1].tolist(), testKinematic[2].tolist()]
# )
# visualize3DVecs(
# [leftKinematic[0].tolist(), (leftKinematic[0]+leftKinematic[1]).tolist(), leftKinematic[0].tolist(), leftKinematic[0].tolist(), leftKinematic[0].tolist()],
# [leftKinematic[1].tolist(), leftKinematic[2].tolist(), [1, 0, 0], [0, 1, 0], [0, 0, 1]]
# )
lowerBodyPosition = [{'time': t, 'data': {aJoint: None for aJoint in usedLowerBodyJoints}} for t in range(timeCount)]
testKinematic1 = None
rotApplyTimeLaps = np.zeros(timeCount)
for t in range(timeCount):
# print(mappedHandRotJson[t]['data'][0])
# print(mappedHandRotJson[t]['data'][1])
testKinematic1 = forwardKinematic(
leftKinematic,
[
mappedHandRotJson[t]['data'][0]['x'],
mappedHandRotJson[t]['data'][0]['z'],
mappedHandRotJson[t]['data'][1]['x']
]
)
lowerBodyPosition[t]['data'][jointsNames.LeftLowerLeg] = testKinematic1[0] + testKinematic1[1]
lowerBodyPosition[t]['data'][jointsNames.LeftFoot] = testKinematic1[0] + testKinematic1[1] + testKinematic1[2]
testKinematic2 = forwardKinematic(
rightKinematic,
[
mappedHandRotJson[t]['data'][2]['x'],
mappedHandRotJson[t]['data'][2]['z'],
mappedHandRotJson[t]['data'][3]['x']
]
)
lowerBodyPosition[t]['data'][jointsNames.RightLowerLeg] = testKinematic2[0] + testKinematic2[1]
lowerBodyPosition[t]['data'][jointsNames.RightFoot] = testKinematic2[0] + testKinematic2[1] + testKinematic2[2]
rotApplyTimeLaps[t] = time.time()
rotApplyCost = rotApplyTimeLaps[1:] - rotApplyTimeLaps[:-1]
print('rotation compute avg time: ', np.mean(rotApplyCost))
print('rotation compute time std: ', np.std(rotApplyCost))
print('rotation compute max time cost: ', np.max(rotApplyCost))
print('rotation compute min time cost: ', np.min(rotApplyCost))
testKinematic1 = forwardKinematic(leftKinematic, [90, 90, 90])
# visualize3DVecs(
# [testKinematic1[0].tolist(), (testKinematic1[0]+testKinematic1[1]).tolist()],
# [testKinematic1[1].tolist(), testKinematic1[2].tolist()]
# )
# visualize3DVecs(
# [[0,0,0], [0,0,1]],
# [[0,1,0], [0,1,1]]
# )
# plt.show()
# 4.
# Unity store 7 joints
# (left/right) upper, lowerleg , foot, hips
for t in range(timeCount):
lowerBodyPosition[t]['data'][jointsNames.Hip] = TPosePositions[jointsNames.Hip]
lowerBodyPosition[t]['data'][jointsNames.LeftUpperLeg] = TPosePositions[jointsNames.LeftUpperLeg]
lowerBodyPosition[t]['data'][jointsNames.RightUpperLeg] = TPosePositions[jointsNames.RightUpperLeg]
for t in range(timeCount):
for aJoint in usedLowerBodyJoints:
if lowerBodyPosition[t]['data'][aJoint] is not None:
lowerBodyPosition[t]['data'][aJoint] = {k: lowerBodyPosition[t]['data'][aJoint][i] for i, k in enumerate(['x', 'y', 'z'])}
rotApplySaveDirPath='positionData/fromAfterMappingHand/'
# with open(rotApplySaveDirPath+'leftFrontKickStream.json', 'w') as WFile:
with open(rotApplySaveDirPath+'leftFrontKickStreamLinearMapping_TFFTTT.json', 'w') as WFile:
# with open(rotApplySaveDirPath+'leftSideKickStreamLinearMapping_FTTFFF.json', 'w') as WFile:
# with open(rotApplySaveDirPath+'runSprintStreamLinearMapping_TFTTFT.json', 'w') as WFile:
# with open(rotApplySaveDirPath+'walkInjuredStreamLinearMapping_TFTTFT.json', 'w') as WFile:
json.dump(lowerBodyPosition, WFile)
pass
# 5.
# compare with unity result
# rotApplyUnitySaveDirPath = 'positionData/fromAfterMappingHand/leftFrontKickCombinations/'
rotApplyUnitySaveDirPath = 'positionData/fromAfterMappingHand/leftFrontKickStreamLinearMapping/'
# rotApplyUnitySaveDirPath = 'positionData/fromAfterMappingHand/leftSideKickStreamLinearMapping/'
# rotApplyUnitySaveDirPath = 'positionData/fromAfterMappingHand/runSprintStreamLinearMappingCombinations/'
unityPosJson = None
with open(rotApplyUnitySaveDirPath+'leftFrontKick(True, False, False, True, True, True).json', 'r') as fileIn:
# with open(rotApplyUnitySaveDirPath+'leftSideKick(False, True, True, False, False, False).json', 'r') as fileIn:
# with open(rotApplyUnitySaveDirPath+'runSprint(True, False, True, True, False, True).json', 'r') as fileIn:
unityPosJson = json.load(fileIn)['results']
unityTimeCount = len(unityPosJson)
pythonTimeCount = len(lowerBodyPosition)
print('unity time count: ', unityTimeCount)
print('python time count: ', pythonTimeCount)
vizJoint = 5
vizAxis = 'x'
unityData = [unityPosJson[t]['data'][vizJoint][vizAxis] for t in range(unityTimeCount)]
pythonData = [lowerBodyPosition[t]['data'][vizJoint][vizAxis] for t in range(pythonTimeCount)]
plt.figure()
plt.plot(range(unityTimeCount), unityData, label='unity')
plt.plot(range(pythonTimeCount), pythonData, label='real time')
plt.legend()
plt.show()
if __name__=='__main01__':
# 1. 讀取檔案, 得到TPose狀態下的position資訊
# 1.1 Hip, upper leg, lower leg, foot
# 2. 計算lower body的bone length(改為計算TPose時的向量就好, 他就包含了bone length的資訊)
# 2.1 upper leg, lower leg兩個bone lengths(vectors)
# 3. Store bone lengths and TPose positions
# 1.
saveDirPath = 'positionData/fromDB/'
saveDirPath = 'positionData/fromDB/genericAvatar/'
TPoseJson = None
with open(saveDirPath+'TPose.json', 'r') as fileIn:
TPoseJson = json.load(fileIn)['results']
jointCount = len(TPoseJson[0]['data'])
# print(TPoseJson[0])
# print('=======')
# print(TPoseJson[2])
print('joint count: ', jointCount)
# 1.1
# 只擷取一個時間點的TPose資訊即可, 特別是lower body的部分
# 擷取的時間點不要第一個時間點就好
TPosePositions = {aJoint: np.array([TPoseJson[2]['data'][aJoint][aAxis] for aAxis in ['x', 'y', 'z']]) for aJoint in usedLowerBodyJoints}
print(TPosePositions)
# 2.
# 4 vectors, (left/right)(upper/lower leg)
TPoseVectors = [
TPosePositions[jointsNames.LeftLowerLeg] - TPosePositions[jointsNames.LeftUpperLeg],
TPosePositions[jointsNames.LeftFoot] - TPosePositions[jointsNames.LeftLowerLeg],
TPosePositions[jointsNames.RightLowerLeg] - TPosePositions[jointsNames.RightUpperLeg],
TPosePositions[jointsNames.RightFoot] - TPosePositions[jointsNames.RightLowerLeg]
]
print(TPoseVectors)
# 3.
# saveDirPath='TPoseInfo/'
saveDirPath='TPoseInfo/genericAvatar/'
# with open(saveDirPath+'TPosePositions.pickle', 'wb') as outPickle:
# pickle.dump(TPosePositions, outPickle)
# with open(saveDirPath+'TPoseVectors.pickle', 'wb') as outPickle:
# pickle.dump(TPoseVectors, outPickle)