File size: 5,754 Bytes
38b191e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import torch
import os
import numpy as np
import trimesh
from common.utils.utils_hand import batch_euler2matzxy, coordtrans

from kornia.geometry.conversions import axis_angle_to_rotation_matrix, quaternion_to_rotation_matrix, euler_from_quaternion, rotation_matrix_to_quaternion, quaternion_from_euler, rotation_matrix_to_axis_angle


euler_coordtrans_RIGHT = np.array([
       [   15,  -6,  10,
           15,   1,  -5,
           15, -15,   0,
           0,  20,   2,
           0,  10,  -2,
           0,  10,  10,
           15,  25,  10,
           10,  37,   8,
           7,  35,   8,
           0,  12,   5,
           0,  25,  -5,
           0,  25,  15,
          -30, -75, -35,
          -30, -45, -30,
          -30, -45, -30]]) / 180 * np.pi
euler_coordtrans_LEFT = np.array([
       [   15,   6, -10,
           15,  -1,   5,
           15,  15,   0,
           0, -20,  -2,
           0, -10,   2,
           0, -10, -10,
          15, -25, -10,
          10, -37,  -8,
          7, -35,  -8,
           0, -12,  -5,
           0, -25,   5,
           0, -25, -15,
          -30,  75,  35,
          -30,  45,  30,
          -30,  45,  30]]) / 180 * np.pi

# range of motion

BOF_RIGHT = np.array([
       [ 
           0, -30, -40, # index0
           0,   0,   0, # index1
           0,   0, -5, # index2

           0, -22.5, -40, # middle0
           0,   0,   0, # middle1
           0,   0, -5, # middle2

           0, -25, -40, # pinky0
           0,   0,   0, # pinky1
           0,   0, -5, # pinky2
           
           0, -22.5, -40, # ring0
           0,   0,   0, # ring1
           0,   0, -5, # ring2
           
           -180, -60, -15, # thumb0
           -180,-5,  -180, # thumb1
           -180, -5,  -10], # thumb0

         # MAX VALUES  
        [
         
           0,   30,  90, # index0
           0,   0, 110, # index1
           0,   0,  90, # index2

           0,  22.5, 90, # middle0
           0,   0, 110, # middle1
           0,   0,  90, # middle2

           0,  25, 90, # pinky0
           0,   0, 135, # pinky1
           0,   0,  90, # pinky2

           0,  22.5,  90, # ring0
           0,   0, 120, # ring1
           0,   0,  90, # ring2

           180,  60,  90, # thumb0
           180,   5,   80,  # thumb1
           180,  5,   80]])

BOF_LEFT = np.array([
        [
         
           0,  -30, -90, # index0
           0,   0,-110, # index1
           0,   0, -90, # index2

           0, -22.5, -90, # middle0
           0,  0,  -110, # middle1
           0, 0,  -90, # middle2
           
           0, -25, -90, # pinky0
           0, 0, -135, # pinky1
           0,  0, -90, # pinky2
           
           0, -22.5,  -90, # ring0
           0,   0,   -120, # ring1
           0,   0, -90, # ring2
           
           -180, -60, -90, # thumb0
           -180, -5,   -80, # thumb1
           -180, -5,   -80], # thumb2

        [ 
         
           0,  30,   40, # index0
           0,   0,   0, # index1
           0,   0,   5, # index2

           0,  22.5, 40, # middle0
           0,   0,   0, # middle1
           0,   0,   5, # middle2
           
           0,  25,   40, # pinky0
           0,   0,   0, # pinky1
           0,   0,   5, # pinky2

           0,  22.5,  40, # ring0
           0,   0,   0, # ring1
           0,   0,   5, # ring2

           180,  60,  15, # thumb0
           180,  5,   180, # thumb1
           180,  5,   10], # thumb2
        ])

def do_clipping(input_angles, side, rotation_type):

    device = input_angles.device

    if side == "LEFT":
        max = torch.tensor(BOF_LEFT[1,:], dtype=torch.float32).reshape(15,3).to(device)
        min = torch.tensor(BOF_LEFT[0,:], dtype=torch.float32).reshape(15,3).to(device)
        min_np = torch.deg2rad(min) # shape (N, 3)
        max_np = torch.deg2rad(max)

    elif side == "RIGHT":
        max = torch.tensor(BOF_RIGHT[1,:], dtype=torch.float32).reshape(15,3).to(device)
        min = torch.tensor(BOF_RIGHT[0,:], dtype=torch.float32).reshape(15,3).to(device)
        min_np = torch.deg2rad(min)  # shape (N, 3)
        max_np = torch.deg2rad(max)

    input_angles = input_angles.reshape(-1,15,3)

    B, J, N = input_angles.shape
        
    if rotation_type == "quat":

        org_mats = quaternion_to_rotation_matrix(input_angles).float()

    elif rotation_type == "aa":

        org_mats = axis_angle_to_rotation_matrix(input_angles.reshape(-1, 3)).float()

        if len(org_mats.shape)!=4:

            org_mats = org_mats.unsqueeze(0)

    rotmat_mano = org_mats.clone()

    rotmat_mano = rotmat_mano.view(-1,15,3,3)

    name = f"euler_coordtrans_{side.upper()}"
    mat = globals()[name]   

    local2global = torch.from_numpy(mat).type(torch.float32).to(device)

    local2global = batch_euler2matzxy(local2global.view(-1,3)).view(-1,15,3,3).expand(1,-1,-1,-1)

    anatom_space_mats = coordtrans(rotmat_mano, local2global, 0)

    mat_to_quat = rotation_matrix_to_quaternion(anatom_space_mats)

    roll, pitch, yaw = euler_from_quaternion(mat_to_quat[:, :, 0], mat_to_quat[:, :, 1], mat_to_quat[:, :, 2], mat_to_quat[:, :, 3])

    euler_from_quat = torch.stack([roll, pitch, yaw], dim=-1)  # shape (1, 15, 3)

    euler_from_quat = torch.clip(euler_from_quat, min_np, max_np)  # root joint is always zero

    qw, qx, qy, qz = quaternion_from_euler(euler_from_quat[:,:, 0], euler_from_quat[:,:, 1], euler_from_quat[:,:, 2])

    rots_anat = torch.stack([qw, qx, qy, qz], dim=-1)  # shape (1, 15, 4)

    anat_mats = quaternion_to_rotation_matrix(rots_anat)

    corrected_mano_space = coordtrans(anat_mats, local2global, 1)

    corrected_aa = rotation_matrix_to_axis_angle(corrected_mano_space).reshape(B, J*3)

    return corrected_aa