mysigner commited on
Commit
38b191e
·
verified ·
1 Parent(s): 31fa9bd

Upload 2 files

Browse files
body_angular_constraints.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from scipy.spatial.transform import Rotation as R
2
+ import torch, numpy as np
3
+
4
+ BOF_body = np.array([
5
+ [-120.0, -130.0, -80.0, # left shoulder
6
+ -120.0, 0.0, -80.0, # right shoulder
7
+ -180.0, -160.0, -180.0, # left elbow
8
+ -180.0, 0.0, -180.0, # right elbow
9
+ -120.0, -50.0, -90.0, # left wrist
10
+ -120.0, -50.0, -90.0, # right wrist
11
+ ],
12
+ [90.0, 0.0, 80.0, # left shoulder
13
+ 90.0, 130.0, 80.0, # right shoulder
14
+ 180.0, 0.0, 180.0, # left elbow
15
+ 180.0, 160.0, 180.0, # right elbow
16
+ 90.0, 50.0, 90.0, # left wrist
17
+ 90.0, 50.0, 90.0]]) / 180 * np.pi
18
+
19
+
20
+ def _to_numpy_flat_last3(x: torch.Tensor):
21
+ dev, dt = x.device, x.dtype
22
+ x_np = x.detach().cpu().numpy().reshape(-1, 3)
23
+ return x_np, x.shape, dev, dt
24
+
25
+ def _from_numpy(x_np: np.ndarray, shape, dev, dt):
26
+ y = torch.from_numpy(x_np.reshape(*shape))
27
+ if dt in (torch.float32, torch.float64): y = y.to(dt)
28
+ return y.to(dev)
29
+
30
+
31
+
32
+ def euler_XYZ_to_axis_angle_scipy(e: torch.Tensor, degrees: bool = False) -> torch.Tensor:
33
+ e_np, shape, dev, dt = _to_numpy_flat_last3(e)
34
+ aa_np = R.from_euler('XYZ', e_np, degrees=degrees).as_rotvec()
35
+ return _from_numpy(aa_np, shape, dev, dt)
36
+
37
+ def axis_angle_to_euler_XYZ_scipy(aa: torch.Tensor):
38
+ aa_np, shape, dev, dt = _to_numpy_flat_last3(aa)
39
+ e_np = R.from_rotvec(aa_np).as_euler('XYZ', degrees=False)
40
+ return _from_numpy(e_np, shape, dev, dt)
41
+
42
+
43
+
44
+
45
+ def apply_angular_constraints(body_pose): # body_pose: (B, 63) axis-angle for 21 joints
46
+ device = body_pose.device
47
+ B = body_pose.shape[0]
48
+ body_pose = body_pose.view(B, 21, 3)
49
+
50
+ # your bounds (6×3) in radians, defined for joints [15..20] in intrinsic 'XYZ'
51
+ minC = torch.tensor(BOF_body[0], dtype=body_pose.dtype, device=device).view(6,3)
52
+ maxC = torch.tensor(BOF_body[1], dtype=body_pose.dtype, device=device).view(6,3)
53
+
54
+ aa_arms = body_pose[:, 15:, :]
55
+
56
+ e_arms = axis_angle_to_euler_XYZ_scipy(aa_arms) # (B,6,3) intrinsic 'XYZ'
57
+
58
+ e_clamp = torch.clamp(e_arms, minC, maxC)
59
+
60
+ aa_new = euler_XYZ_to_axis_angle_scipy(e_clamp)
61
+
62
+ body_pose[:, 15:, :] = aa_new
63
+
64
+ return body_pose.view(B, -1)
65
+
66
+
67
+
hand_angular_constraints.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import torch
3
+ import os
4
+ import numpy as np
5
+ import trimesh
6
+ from common.utils.utils_hand import batch_euler2matzxy, coordtrans
7
+
8
+ 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
9
+
10
+
11
+ euler_coordtrans_RIGHT = np.array([
12
+ [ 15, -6, 10,
13
+ 15, 1, -5,
14
+ 15, -15, 0,
15
+ 0, 20, 2,
16
+ 0, 10, -2,
17
+ 0, 10, 10,
18
+ 15, 25, 10,
19
+ 10, 37, 8,
20
+ 7, 35, 8,
21
+ 0, 12, 5,
22
+ 0, 25, -5,
23
+ 0, 25, 15,
24
+ -30, -75, -35,
25
+ -30, -45, -30,
26
+ -30, -45, -30]]) / 180 * np.pi
27
+ euler_coordtrans_LEFT = np.array([
28
+ [ 15, 6, -10,
29
+ 15, -1, 5,
30
+ 15, 15, 0,
31
+ 0, -20, -2,
32
+ 0, -10, 2,
33
+ 0, -10, -10,
34
+ 15, -25, -10,
35
+ 10, -37, -8,
36
+ 7, -35, -8,
37
+ 0, -12, -5,
38
+ 0, -25, 5,
39
+ 0, -25, -15,
40
+ -30, 75, 35,
41
+ -30, 45, 30,
42
+ -30, 45, 30]]) / 180 * np.pi
43
+
44
+ # range of motion
45
+
46
+ BOF_RIGHT = np.array([
47
+ [
48
+ 0, -30, -40, # index0
49
+ 0, 0, 0, # index1
50
+ 0, 0, -5, # index2
51
+
52
+ 0, -22.5, -40, # middle0
53
+ 0, 0, 0, # middle1
54
+ 0, 0, -5, # middle2
55
+
56
+ 0, -25, -40, # pinky0
57
+ 0, 0, 0, # pinky1
58
+ 0, 0, -5, # pinky2
59
+
60
+ 0, -22.5, -40, # ring0
61
+ 0, 0, 0, # ring1
62
+ 0, 0, -5, # ring2
63
+
64
+ -180, -60, -15, # thumb0
65
+ -180,-5, -180, # thumb1
66
+ -180, -5, -10], # thumb0
67
+
68
+ # MAX VALUES
69
+ [
70
+
71
+ 0, 30, 90, # index0
72
+ 0, 0, 110, # index1
73
+ 0, 0, 90, # index2
74
+
75
+ 0, 22.5, 90, # middle0
76
+ 0, 0, 110, # middle1
77
+ 0, 0, 90, # middle2
78
+
79
+ 0, 25, 90, # pinky0
80
+ 0, 0, 135, # pinky1
81
+ 0, 0, 90, # pinky2
82
+
83
+ 0, 22.5, 90, # ring0
84
+ 0, 0, 120, # ring1
85
+ 0, 0, 90, # ring2
86
+
87
+ 180, 60, 90, # thumb0
88
+ 180, 5, 80, # thumb1
89
+ 180, 5, 80]])
90
+
91
+ BOF_LEFT = np.array([
92
+ [
93
+
94
+ 0, -30, -90, # index0
95
+ 0, 0,-110, # index1
96
+ 0, 0, -90, # index2
97
+
98
+ 0, -22.5, -90, # middle0
99
+ 0, 0, -110, # middle1
100
+ 0, 0, -90, # middle2
101
+
102
+ 0, -25, -90, # pinky0
103
+ 0, 0, -135, # pinky1
104
+ 0, 0, -90, # pinky2
105
+
106
+ 0, -22.5, -90, # ring0
107
+ 0, 0, -120, # ring1
108
+ 0, 0, -90, # ring2
109
+
110
+ -180, -60, -90, # thumb0
111
+ -180, -5, -80, # thumb1
112
+ -180, -5, -80], # thumb2
113
+
114
+ [
115
+
116
+ 0, 30, 40, # index0
117
+ 0, 0, 0, # index1
118
+ 0, 0, 5, # index2
119
+
120
+ 0, 22.5, 40, # middle0
121
+ 0, 0, 0, # middle1
122
+ 0, 0, 5, # middle2
123
+
124
+ 0, 25, 40, # pinky0
125
+ 0, 0, 0, # pinky1
126
+ 0, 0, 5, # pinky2
127
+
128
+ 0, 22.5, 40, # ring0
129
+ 0, 0, 0, # ring1
130
+ 0, 0, 5, # ring2
131
+
132
+ 180, 60, 15, # thumb0
133
+ 180, 5, 180, # thumb1
134
+ 180, 5, 10], # thumb2
135
+ ])
136
+
137
+ def do_clipping(input_angles, side, rotation_type):
138
+
139
+ device = input_angles.device
140
+
141
+ if side == "LEFT":
142
+ max = torch.tensor(BOF_LEFT[1,:], dtype=torch.float32).reshape(15,3).to(device)
143
+ min = torch.tensor(BOF_LEFT[0,:], dtype=torch.float32).reshape(15,3).to(device)
144
+ min_np = torch.deg2rad(min) # shape (N, 3)
145
+ max_np = torch.deg2rad(max)
146
+
147
+ elif side == "RIGHT":
148
+ max = torch.tensor(BOF_RIGHT[1,:], dtype=torch.float32).reshape(15,3).to(device)
149
+ min = torch.tensor(BOF_RIGHT[0,:], dtype=torch.float32).reshape(15,3).to(device)
150
+ min_np = torch.deg2rad(min) # shape (N, 3)
151
+ max_np = torch.deg2rad(max)
152
+
153
+ input_angles = input_angles.reshape(-1,15,3)
154
+
155
+ B, J, N = input_angles.shape
156
+
157
+ if rotation_type == "quat":
158
+
159
+ org_mats = quaternion_to_rotation_matrix(input_angles).float()
160
+
161
+ elif rotation_type == "aa":
162
+
163
+ org_mats = axis_angle_to_rotation_matrix(input_angles.reshape(-1, 3)).float()
164
+
165
+ if len(org_mats.shape)!=4:
166
+
167
+ org_mats = org_mats.unsqueeze(0)
168
+
169
+ rotmat_mano = org_mats.clone()
170
+
171
+ rotmat_mano = rotmat_mano.view(-1,15,3,3)
172
+
173
+ name = f"euler_coordtrans_{side.upper()}"
174
+ mat = globals()[name]
175
+
176
+ local2global = torch.from_numpy(mat).type(torch.float32).to(device)
177
+
178
+ local2global = batch_euler2matzxy(local2global.view(-1,3)).view(-1,15,3,3).expand(1,-1,-1,-1)
179
+
180
+ anatom_space_mats = coordtrans(rotmat_mano, local2global, 0)
181
+
182
+ mat_to_quat = rotation_matrix_to_quaternion(anatom_space_mats)
183
+
184
+ roll, pitch, yaw = euler_from_quaternion(mat_to_quat[:, :, 0], mat_to_quat[:, :, 1], mat_to_quat[:, :, 2], mat_to_quat[:, :, 3])
185
+
186
+ euler_from_quat = torch.stack([roll, pitch, yaw], dim=-1) # shape (1, 15, 3)
187
+
188
+ euler_from_quat = torch.clip(euler_from_quat, min_np, max_np) # root joint is always zero
189
+
190
+ qw, qx, qy, qz = quaternion_from_euler(euler_from_quat[:,:, 0], euler_from_quat[:,:, 1], euler_from_quat[:,:, 2])
191
+
192
+ rots_anat = torch.stack([qw, qx, qy, qz], dim=-1) # shape (1, 15, 4)
193
+
194
+ anat_mats = quaternion_to_rotation_matrix(rots_anat)
195
+
196
+ corrected_mano_space = coordtrans(anat_mats, local2global, 1)
197
+
198
+ corrected_aa = rotation_matrix_to_axis_angle(corrected_mano_space).reshape(B, J*3)
199
+
200
+ return corrected_aa
201
+