-
Notifications
You must be signed in to change notification settings - Fork 34
/
quaternion.py
333 lines (276 loc) · 10.5 KB
/
quaternion.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
# Copyright (c) 2018-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import numpy as np
import torch
import torch.nn.functional as F
# PyTorch-backed implementations
def qmul(q, r):
"""
Multiply quaternion(s) q with quaternion(s) r.
Expects two equally-sized tensors of shape (*, 4), where * denotes any number of dimensions.
Returns q*r as a tensor of shape (*, 4).
"""
assert q.shape[-1] == 4
assert r.shape[-1] == 4
original_shape = q.shape
# Compute outer product
terms = torch.bmm(r.view(-1, 4, 1), q.view(-1, 1, 4))
w = terms[:, 0, 0] - terms[:, 1, 1] - terms[:, 2, 2] - terms[:, 3, 3]
x = terms[:, 0, 1] + terms[:, 1, 0] - terms[:, 2, 3] + terms[:, 3, 2]
y = terms[:, 0, 2] + terms[:, 1, 3] + terms[:, 2, 0] - terms[:, 3, 1]
z = terms[:, 0, 3] - terms[:, 1, 2] + terms[:, 2, 1] + terms[:, 3, 0]
return torch.stack((w, x, y, z), dim=1).view(original_shape)
def qrot(q, v):
"""
Rotate vector(s) v about the rotation described by quaternion(s) q.
Expects a tensor of shape (*, 4) for q and a tensor of shape (*, 3) for v,
where * denotes any number of dimensions.
Returns a tensor of shape (*, 3).
"""
assert q.shape[-1] == 4
assert v.shape[-1] == 3
assert q.shape[:-1] == v.shape[:-1]
original_shape = list(v.shape)
q = q.view(-1, 4)
v = v.view(-1, 3)
qvec = q[:, 1:]
uv = torch.cross(qvec, v, dim=1)
uuv = torch.cross(qvec, uv, dim=1)
return (v + 2 * (q[:, :1] * uv + uuv)).view(original_shape)
def qeuler(q, order, epsilon=0):
"""
Convert quaternion(s) q to Euler angles.
Expects a tensor of shape (*, 4), where * denotes any number of dimensions.
Returns a tensor of shape (*, 3).
"""
assert q.shape[-1] == 4
original_shape = list(q.shape)
original_shape[-1] = 3
q = q.contiguous().view(-1, 4)
q0 = q[:, 0]
q1 = q[:, 1]
q2 = q[:, 2]
q3 = q[:, 3]
if order == 'xyz':
x = torch.atan2(2 * (q0 * q1 - q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2))
y = torch.asin(torch.clamp(2 * (q1 * q3 + q0 * q2), -1 + epsilon, 1 - epsilon))
z = torch.atan2(2 * (q0 * q3 - q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3))
elif order == 'yzx':
x = torch.atan2(2 * (q0 * q1 - q2 * q3), 1 - 2 * (q1 * q1 + q3 * q3))
y = torch.atan2(2 * (q0 * q2 - q1 * q3), 1 - 2 * (q2 * q2 + q3 * q3))
z = torch.asin(torch.clamp(2 * (q1 * q2 + q0 * q3), -1 + epsilon, 1 - epsilon))
elif order == 'zxy':
x = torch.asin(torch.clamp(2 * (q0 * q1 + q2 * q3), -1 + epsilon, 1 - epsilon))
y = torch.atan2(2 * (q0 * q2 - q1 * q3), 1 - 2 * (q1 * q1 + q2 * q2))
z = torch.atan2(2 * (q0 * q3 - q1 * q2), 1 - 2 * (q1 * q1 + q3 * q3))
elif order == 'xzy':
x = torch.atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q3 * q3))
y = torch.atan2(2 * (q0 * q2 + q1 * q3), 1 - 2 * (q2 * q2 + q3 * q3))
z = torch.asin(torch.clamp(2 * (q0 * q3 - q1 * q2), -1 + epsilon, 1 - epsilon))
elif order == 'yxz':
x = torch.asin(torch.clamp(2 * (q0 * q1 - q2 * q3), -1 + epsilon, 1 - epsilon))
y = torch.atan2(2 * (q1 * q3 + q0 * q2), 1 - 2 * (q1 * q1 + q2 * q2))
z = torch.atan2(2 * (q1 * q2 + q0 * q3), 1 - 2 * (q1 * q1 + q3 * q3))
elif order == 'zyx':
x = torch.atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2))
y = torch.asin(torch.clamp(2 * (q0 * q2 - q1 * q3), -1 + epsilon, 1 - epsilon))
z = torch.atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3))
else:
raise
return torch.stack((x, y, z), dim=1).view(original_shape)
def qinv(q: torch.Tensor):
"""
Invert quaternions
Expect a tensor of shape (*, 4)
Returns a tensor of shape (*, 4)
"""
ori_shape = q.shape
q = q.contiguous().view(-1, 4)
q_norm = torch.norm(q, p=2, dim=1)
q_inv = q.clone().contiguous().view(-1, 4)
q_inv[:, 1:] *= -1
q_inv /= q_norm.unsqueeze(1)
return q_inv.reshape(*ori_shape)
def qinv_np(q):
ori_shape = q.shape
q = q.reshape(-1, 4)
q_norm = np.linalg.norm(q, ord=2, axis=1)
q_inv = np.copy(q).reshape(-1, 4)
q_inv[:, 1:] *= -1
q_inv /= np.expand_dims(q_norm, axis=1)
return q_inv.reshape(*ori_shape)
def rotation_from_to(v_from: torch.Tensor, v_to: torch.Tensor):
"""
Calculate the shortest rotation from two vectors
Argument:
-- Both v_from, v_to is of shape (*, 3)
"""
assert v_from.shape == v_to.shape
assert v_from.shape[-1] == 3 and v_to.shape[-1] == 3
ori_shape = v_from.shape
out_shape = list(ori_shape)[:-1] + [4]
v_from = v_from.view(-1, 3)
v_to = v_to.view(-1, 3)
dot = torch.sum(v_from * v_to, dim=1)
xyz = torch.cross(v_from, v_to)
w = torch.norm(v_from, dim=1, p=2) * torch.norm(v_to, dim=1, p=2) + dot
w = w.unsqueeze(1)
rotations = torch.cat([w, xyz], dim=1)
rotations = F.normalize(rotations, p=2, dim=1)
return rotations.view(out_shape)
def geodesic_distance(v_from: torch.Tensor, v_to: torch.Tensor) -> torch.Tensor:
"""
calculate geodesic distance from v_from to v_to in the form of delta cosine value
:param v_from: (*, 4)
:param v_to: (*, 4)
:return: (*)
"""
assert v_from.shape == v_to.shape
assert v_from.shape[-1] == v_to.shape[-1] == 4 or v_from.shape[-1] == v_to.shape[-1] == 3
ori_shape = v_from.shape
v_from = v_from.contiguous().view(-1, v_from.shape[-1])
v_to = v_to.contiguous().view(-1, v_to.shape[-1])
if ori_shape[-1] == 4:
v_from_inv = qinv(v_from)
terms = torch.bmm(v_from_inv.view(-1, 4, 1), v_to.view(-1, 1, 4))
w = terms[:, 0, 0] - terms[:, 1, 1] - terms[:, 2, 2] - terms[:, 3, 3]
w = torch.clamp(w, -1, 1)
distance = torch.sub(1, w).view(ori_shape[:-1])
else:
v_from_inv = v_from.clone()
v_from_inv[:, 1:] *= -1
terms = v_from_inv * v_to
w = terms[:, 0] - terms[:, 1] - terms[:, 2]
w = torch.clamp(w, -1, 1)
distance = torch.sub(1, w).view(ori_shape[:-1])
return distance
# Numpy-backed implementations
def qmul_np(q, r):
q = torch.from_numpy(q).contiguous()
r = torch.from_numpy(r).contiguous()
return qmul(q, r).numpy()
def qrot_np(q, v):
q = torch.from_numpy(q).contiguous()
v = torch.from_numpy(v).contiguous()
return qrot(q, v).numpy()
def qeuler_np(q, order, epsilon=0, use_gpu=False):
if use_gpu:
q = torch.from_numpy(q).cuda()
return qeuler(q, order, epsilon).cpu().numpy()
else:
q = torch.from_numpy(q).contiguous()
return qeuler(q, order, epsilon).numpy()
def qfix(q):
"""
Enforce quaternion continuity across the time dimension by selecting
the representation (q or -q) with minimal distance (or, equivalently, maximal dot product)
between two consecutive frames.
Expects a tensor of shape (L, J, 4), where L is the sequence length and J is the number of joints.
Returns a tensor of the same shape.
"""
assert len(q.shape) == 3
assert q.shape[-1] == 4
result = q.copy()
dot_products = np.sum(q[1:] * q[:-1], axis=2)
mask = dot_products < 0
mask = (np.cumsum(mask, axis=0) % 2).astype(bool)
result[1:][mask] *= -1
return result
def expmap_to_quaternion(e):
"""
Convert axis-angle rotations (aka exponential maps) to quaternions.
Stable formula from "Practical Parameterization of Rotations Using the Exponential Map".
Expects a tensor of shape (*, 3), where * denotes any number of dimensions.
Returns a tensor of shape (*, 4).
"""
assert e.shape[-1] == 3
original_shape = list(e.shape)
original_shape[-1] = 4
e = e.reshape(-1, 3)
theta = np.linalg.norm(e, axis=1).reshape(-1, 1)
w = np.cos(0.5 * theta).reshape(-1, 1)
xyz = 0.5 * np.sinc(0.5 * theta / np.pi) * e
return np.concatenate((w, xyz), axis=1).reshape(original_shape)
def euler_to_quaternion(e, order):
"""
Convert Euler angles to quaternions.
"""
assert e.shape[-1] == 3
original_shape = list(e.shape)
original_shape[-1] = 4
e = e.reshape(-1, 3)
x = e[:, 0]
y = e[:, 1]
z = e[:, 2]
rx = np.stack((np.cos(x / 2), np.sin(x / 2), np.zeros_like(x), np.zeros_like(x)), axis=1)
ry = np.stack((np.cos(y / 2), np.zeros_like(y), np.sin(y / 2), np.zeros_like(y)), axis=1)
rz = np.stack((np.cos(z / 2), np.zeros_like(z), np.zeros_like(z), np.sin(z / 2)), axis=1)
result = None
for coord in order:
if coord == 'x':
r = rx
elif coord == 'y':
r = ry
elif coord == 'z':
r = rz
else:
raise Exception("Unknown axis in order")
if result is None:
result = r
else:
result = qmul_np(result, r)
# Reverse antipodal representation to have a non-negative "w"
if order in ['xyz', 'yzx', 'zxy']:
result *= -1
return result.reshape(original_shape)
def rotation_from_to_np(v_from, v_to, use_gpu=False):
if use_gpu:
v_from = torch.from_numpy(v_from).cuda()
v_to = torch.from_numpy(v_to).cuda()
return rotation_from_to(v_from, v_to).cpu().numpy()
else:
v_from = torch.from_numpy(v_from)
v_to = torch.from_numpy(v_to)
return rotation_from_to(v_from, v_to).numpy()
def average_quaternion(q: torch.Tensor, w: torch.Tensor) -> torch.Tensor:
"""
calculate the average of quaternions
:param q: (W, J, 4)
:param w: (W)
:return: (J, 4)
"""
w = w.view(-1, 1, 1, 1)
joint_num = q.shape[1]
q = q.reshape(-1, 4, 1)
q_t = q.reshape(-1, 1, 4)
qq_t = torch.bmm(q, q_t)
qq_t = qq_t.reshape(w.shape[0], joint_num, 4, 4)
averages = []
m = torch.sum(w * qq_t, dim=0)
for jm in m:
eigenvalues, eigenvectors = torch.eig(jm, eigenvectors=True)
q_avg = eigenvectors[:, torch.argmax(eigenvalues[:, 0])]
averages.append(q_avg)
return torch.stack(averages)
def average_quaternion_np(q, w = None, use_gpu = False):
"""
q: (W, 4)
w: (w)
"""
if w is None:
w = np.ones(len(q), dtype = np.float32) / len(q)
if use_gpu:
q_torch = torch.from_numpy(q).cuda().unsqueeze(1)
w_torch = torch.from_numpy(w).cuda()
avg = average_quaternion(q_torch, w_torch)
avg = avg.cpu().numpy()
else:
q_torch = torch.from_numpy(q).unsqueeze(1)
w_torch = torch.from_numpy(w)
avg = average_quaternion(q_torch, w_torch)
avg = avg.numpy()
return avg