forked from mbrossar/RINS-W
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lie_algebra.py
482 lines (410 loc) · 16.2 KB
/
lie_algebra.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
from src.utils import *
import numpy as np
import matplotlib.pyplot as plt
class SO3:
# tolerance criterion
TOL = 1e-8
Id = torch.eye(3).cuda().float()
dId = torch.eye(3).cuda().double()
@classmethod
def Ad(cls, Rot):
return Rot
@classmethod
def exp(cls, phi):
angle = phi.norm(dim=1, keepdim=True)
mask = angle[:, 0] < cls.TOL
dim_batch = phi.shape[0]
Id = cls.Id.expand(dim_batch, 3, 3)
axis = phi[~mask] / angle[~mask]
c = angle[~mask].cos().unsqueeze(2)
s = angle[~mask].sin().unsqueeze(2)
Rot = phi.new_empty(dim_batch, 3, 3)
Rot[mask] = Id[mask] + SO3.wedge(phi[mask])
Rot[~mask] = c*Id[~mask] + \
(1-c)*bouter(axis, axis) + s*cls.wedge(axis)
return Rot
@classmethod
def inv_left_jacobian(cls, phi):
angle = phi.norm(dim=1)
mask = angle < cls.TOL
dim_batch = phi.shape[0]
Id = cls.Id.expand(dim_batch, 3, 3)
axis = (phi/angle.unsqueeze(1))
half_angle = angle.view(dim_batch, 1, 1)/2
cot = 1/half_angle.tan()
J = Id - 1/2 * cls.wedge(phi)
J1 = half_angle*cot*Id + (1-half_angle*cot)*bouter(axis, axis) - \
half_angle*cls.wedge(axis)
J[~mask] = J1[~mask]
return J
@classmethod
def left_jacobian(cls, phi):
angle = phi.norm(dim=1, keepdim=True)
mask = angle[:, 0] < cls.TOL
dim_batch = phi.shape[0]
Id = cls.Id.expand(dim_batch, 3, 3)
axis = phi[~mask] / angle[~mask]
angle = angle[~mask].unsqueeze(2)
s = angle.sin()
c = angle.cos()
J = torch.empty_like(Id)
# Near |phi|==0, use first order Taylor expansion
J[mask] = Id[mask] + 1/2*cls.wedge(phi[mask])
J[~mask] = (s/angle)*Id[~mask] + (1-s/angle)*bouter(axis, axis) +\
((1-c)/angle) * cls.wedge(axis)
return J
@classmethod
def log(cls, Rot):
dim_batch = Rot.shape[0]
Id = cls.Id.expand(dim_batch, 3, 3)
cos_angle = (0.5 * btrace(Rot) - 0.5).clamp(-1., 1.)
# Clip cos(angle) to its proper domain to avoid NaNs from rounding
# errors
angle = cos_angle.acos()
mask = angle < cls.TOL
if mask.sum() == 0:
angle = angle.unsqueeze(1).unsqueeze(1)
return cls.vee((0.5 * angle/angle.sin())*(Rot-Rot.transpose(1, 2)))
elif mask.sum() == dim_batch:
# If angle is close to zero, use first-order Taylor expansion
return cls.vee(Rot - Id)
phi = cls.vee(Rot - Id)
angle = angle
phi[~mask] = cls.vee((0.5 * angle[~mask]/angle[~mask].sin()).unsqueeze(
1).unsqueeze(2)*(Rot[~mask] - Rot[~mask].transpose(1, 2)))
return phi
@staticmethod
def vee(Phi):
return torch.stack((Phi[:, 2, 1],
Phi[:, 0, 2],
Phi[:, 1, 0]), dim=1)
@staticmethod
def wedge(phi):
dim_batch = phi.shape[0]
zero = phi.new_zeros(dim_batch)
return torch.stack((zero, -phi[:, 2], phi[:, 1],
phi[:, 2], zero, -phi[:, 0],
-phi[:, 1], phi[:, 0], zero), 1).view(dim_batch,
3, 3)
@classmethod
def from_rpy(cls, roll, pitch, yaw):
return cls.rotz(yaw).bmm(cls.roty(pitch).bmm(cls.rotx(roll)))
@classmethod
def rotx(cls, angle_in_radians):
c = angle_in_radians.cos()
s = angle_in_radians.sin()
mat = c.new_zeros((c.shape[0], 3, 3))
mat[:, 0, 0] = 1
mat[:, 1, 1] = c
mat[:, 2, 2] = c
mat[:, 1, 2] = -s
mat[:, 2, 1] = s
return mat
@classmethod
def roty(cls, angle_in_radians):
c = angle_in_radians.cos()
s = angle_in_radians.sin()
mat = c.new_zeros((c.shape[0], 3, 3))
mat[:, 1, 1] = 1
mat[:, 0, 0] = c
mat[:, 2, 2] = c
mat[:, 0, 2] = s
mat[:, 2, 0] = -s
return mat
@classmethod
def rotz(cls, angle_in_radians):
c = angle_in_radians.cos()
s = angle_in_radians.sin()
mat = c.new_zeros((c.shape[0], 3, 3))
mat[:, 2, 2] = 1
mat[:, 0, 0] = c
mat[:, 1, 1] = c
mat[:, 0, 1] = -s
mat[:, 1, 0] = s
return mat
@classmethod
def isclose(cls, x, y):
return (x-y).abs() < cls.TOL
@classmethod
def to_rpy(cls, Rots):
"""Convert a rotation matrix to RPY Euler angles."""
pitch = torch.atan2(-Rots[:, 2, 0],
torch.sqrt(Rots[:, 0, 0]**2 + Rots[:, 1, 0]**2))
yaw = pitch.new_empty(pitch.shape)
roll = pitch.new_empty(pitch.shape)
near_pi_over_two_mask = cls.isclose(pitch, np.pi / 2.)
near_neg_pi_over_two_mask = cls.isclose(pitch, -np.pi / 2.)
remainder_inds = ~(near_pi_over_two_mask | near_neg_pi_over_two_mask)
yaw[near_pi_over_two_mask] = 0
roll[near_pi_over_two_mask] = torch.atan2(
Rots[near_pi_over_two_mask, 0, 1],
Rots[near_pi_over_two_mask, 1, 1])
yaw[near_neg_pi_over_two_mask] = 0.
roll[near_neg_pi_over_two_mask] = -torch.atan2(
Rots[near_neg_pi_over_two_mask, 0, 1],
Rots[near_neg_pi_over_two_mask, 1, 1])
sec_pitch = 1/pitch[remainder_inds].cos()
remainder_mats = Rots[remainder_inds]
yaw = torch.atan2(remainder_mats[:, 1, 0] * sec_pitch,
remainder_mats[:, 0, 0] * sec_pitch)
roll = torch.atan2(remainder_mats[:, 2, 1] * sec_pitch,
remainder_mats[:, 2, 2] * sec_pitch)
rpys = torch.cat([roll.unsqueeze(dim=1),
pitch.unsqueeze(dim=1),
yaw.unsqueeze(dim=1)], dim=1)
return rpys
@classmethod
def from_quaternion(cls, quat, ordering='wxyz'):
"""Form a rotation matrix from a unit length quaternion.
Valid orderings are 'xyzw' and 'wxyz'.
"""
if ordering is 'xyzw':
qx = quat[:, 0]
qy = quat[:, 1]
qz = quat[:, 2]
qw = quat[:, 3]
elif ordering is 'wxyz':
qw = quat[:, 0]
qx = quat[:, 1]
qy = quat[:, 2]
qz = quat[:, 3]
# Form the matrix
mat = quat.new_empty(quat.shape[0], 3, 3)
qx2 = qx * qx
qy2 = qy * qy
qz2 = qz * qz
mat[:, 0, 0] = 1. - 2. * (qy2 + qz2)
mat[:, 0, 1] = 2. * (qx * qy - qw * qz)
mat[:, 0, 2] = 2. * (qw * qy + qx * qz)
mat[:, 1, 0] = 2. * (qw * qz + qx * qy)
mat[:, 1, 1] = 1. - 2. * (qx2 + qz2)
mat[:, 1, 2] = 2. * (qy * qz - qw * qx)
mat[:, 2, 0] = 2. * (qx * qz - qw * qy)
mat[:, 2, 1] = 2. * (qw * qx + qy * qz)
mat[:, 2, 2] = 1. - 2. * (qx2 + qy2)
return mat
@classmethod
def to_quaternion(cls, Rots, ordering='wxyz'):
"""Convert a rotation matrix to a unit length quaternion.
Valid orderings are 'xyzw' and 'wxyz'.
"""
tmp = 1 + Rots[:, 0, 0] + Rots[:, 1, 1] + Rots[:, 2, 2]
tmp[tmp < 0] = 0
qw = 0.5 * torch.sqrt(tmp)
qx = qw.new_empty(qw.shape[0])
qy = qw.new_empty(qw.shape[0])
qz = qw.new_empty(qw.shape[0])
near_zero_mask = qw.abs() < cls.TOL
if near_zero_mask.sum() > 0:
cond1_mask = near_zero_mask * \
(Rots[:, 0, 0] > Rots[:, 1, 1])*(Rots[:, 0, 0] > Rots[:, 2, 2])
cond1_inds = cond1_mask.nonzero()
if len(cond1_inds) > 0:
cond1_inds = cond1_inds.squeeze()
R_cond1 = Rots[cond1_inds].view(-1, 3, 3)
d = 2. * torch.sqrt(1. + R_cond1[:, 0, 0] -
R_cond1[:, 1, 1] - R_cond1[:, 2, 2]).view(-1)
qw[cond1_inds] = (R_cond1[:, 2, 1] - R_cond1[:, 1, 2]) / d
qx[cond1_inds] = 0.25 * d
qy[cond1_inds] = (R_cond1[:, 1, 0] + R_cond1[:, 0, 1]) / d
qz[cond1_inds] = (R_cond1[:, 0, 2] + R_cond1[:, 2, 0]) / d
cond2_mask = near_zero_mask * (Rots[:, 1, 1] > Rots[:, 2, 2])
cond2_inds = cond2_mask.nonzero()
if len(cond2_inds) > 0:
cond2_inds = cond2_inds.squeeze()
R_cond2 = Rots[cond2_inds].view(-1, 3, 3)
d = 2. * torch.sqrt(1. + R_cond2[:, 1, 1] -
R_cond2[:, 0, 0] - R_cond2[:, 2, 2]).squeeze()
tmp = (R_cond2[:, 0, 2] - R_cond2[:, 2, 0]) / d
qw[cond2_inds] = tmp
qx[cond2_inds] = (R_cond2[:, 1, 0] + R_cond2[:, 0, 1]) / d
qy[cond2_inds] = 0.25 * d
qz[cond2_inds] = (R_cond2[:, 2, 1] + R_cond2[:, 1, 2]) / d
cond3_mask = near_zero_mask & cond1_mask.logical_not() & cond2_mask.logical_not()
cond3_inds = cond3_mask
if len(cond3_inds) > 0:
R_cond3 = Rots[cond3_inds].view(-1, 3, 3)
d = 2. * \
torch.sqrt(1. + R_cond3[:, 2, 2] -
R_cond3[:, 0, 0] - R_cond3[:, 1, 1]).squeeze()
qw[cond3_inds] = (R_cond3[:, 1, 0] - R_cond3[:, 0, 1]) / d
qx[cond3_inds] = (R_cond3[:, 0, 2] + R_cond3[:, 2, 0]) / d
qy[cond3_inds] = (R_cond3[:, 2, 1] + R_cond3[:, 1, 2]) / d
qz[cond3_inds] = 0.25 * d
far_zero_mask = near_zero_mask.logical_not()
far_zero_inds = far_zero_mask
if len(far_zero_inds) > 0:
R_fz = Rots[far_zero_inds]
d = 4. * qw[far_zero_inds]
qx[far_zero_inds] = (R_fz[:, 2, 1] - R_fz[:, 1, 2]) / d
qy[far_zero_inds] = (R_fz[:, 0, 2] - R_fz[:, 2, 0]) / d
qz[far_zero_inds] = (R_fz[:, 1, 0] - R_fz[:, 0, 1]) / d
# Check ordering last
if ordering is 'xyzw':
quat = torch.stack([qx, qy, qz, qw], dim=1)
elif ordering is 'wxyz':
quat = torch.stack([qw, qx, qy, qz], dim=1)
return quat
@classmethod
def normalize(cls, Rots):
U, _, V = torch.svd(Rots)
S = cls.Id.clone().repeat(Rots.shape[0], 1, 1)
S[:, 2, 2] = torch.det(U) * torch.det(V)
return U.bmm(S).bmm(V.transpose(1, 2))
@classmethod
def dnormalize(cls, Rots):
U, _, V = torch.svd(Rots)
S = cls.dId.clone().repeat(Rots.shape[0], 1, 1)
S[:, 2, 2] = torch.det(U) * torch.det(V)
return U.bmm(S).bmm(V.transpose(1, 2))
@classmethod
def qmul(cls, q, r, ordering='wxyz'):
"""
Multiply quaternion(s) q with quaternion(s) r.
"""
terms = bouter(r, q)
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]
xyz = torch.stack((x, y, z), dim=1)
xyz[w < 0] *= -1
w[w < 0] *= -1
if ordering == 'wxyz':
q = torch.cat((w.unsqueeze(1), xyz), dim=1)
else:
q = torch.cat((xyz, w.unsqueeze(1)), dim=1)
return q / q.norm(dim=1, keepdim=True)
@staticmethod
def sinc(x):
return x.sin() / x
@classmethod
def qexp(cls, xi, ordering='wxyz'):
"""
Convert exponential maps to quaternions.
"""
theta = xi.norm(dim=1, keepdim=True)
w = (0.5*theta).cos()
xyz = 0.5*cls.sinc(0.5*theta/np.pi)*xi
return torch.cat((w, xyz), 1)
@classmethod
def qlog(cls, q, ordering='wxyz'):
"""
Applies the log map to quaternions.
"""
n = 0.5*torch.norm(q[:, 1:], p=2, dim=1, keepdim=True)
n = torch.clamp(n, min=1e-8)
q = q[:, 1:] * torch.acos(torch.clamp(q[:, :1], min=-1.0, max=1.0))
r = q / n
return r
@classmethod
def qinv(cls, q, ordering='wxyz'):
"Quaternion inverse"
r = torch.empty_like(q)
if ordering == 'wxyz':
r[:, 1:4] = -q[:, 1:4]
r[:, 0] = q[:, 0]
else:
r[:, :3] = -q[:, :3]
r[:, 3] = q[:, 3]
return r
@classmethod
def qnorm(cls, q):
"Quaternion normalization"
return q / q.norm(dim=1, keepdim=True)
@classmethod
def orinet_metrics(cls, gt_qs, hat_qs, N=11):
"""Compute losses of "OriNet: Robust 3D Orientation Estimation with a
Single Particular IMU".
Compute :
- MAE/RMSE of length of quaternion in a local time window
- MAE in each quaternion coordinate
- mean length of error
- mean distance between the estimated and ground-truth quaternions
"""
# normalize quaternion
gt_qs = (gt_qs / gt_qs.norm(dim=1, keepdim=True)).double()
hat_qs = (hat_qs / hat_qs.norm(dim=1, keepdim=True)).double()
# put estimated quaternion on the same sign as ground-truth quaternion
hat_qs[hat_qs*gt_qs < 0] *= - 1
# MAE/RMSE of length of quaternion in a local time window
tmp = (gt_qs[1:] - gt_qs[:-1]).unsqueeze(0).transpose(1, 2)
tmp = tmp.norm(dim=1, keepdim=True)
gt_DeltaQ = torch.nn.functional.conv1d(tmp, tmp.new_ones(1, 1, N))
tmp = (hat_qs[1:] - hat_qs[:-1]).unsqueeze(0).transpose(1, 2)
tmp = tmp.norm(dim=1, keepdim=True)
net_DeltaQ = torch.nn.functional.conv1d(tmp, tmp.new_ones(1, 1, N))
tmp = (gt_qs - hat_qs).norm(dim=1)
print("mean length of the quaternion error : {:.7f}".format(tmp.mean().item()))
tmp = (gt_qs*hat_qs).sum(dim=1).abs().clamp(0, 1)
theta_D = 180/np.pi * 2*tmp.acos()
print("mean distance {:.7f} (deg)".format(theta_D.mean().item()))
@classmethod
def qinterp(cls, qs, t, t_int):
idxs = np.searchsorted(t, t_int)
idxs0 = idxs-1
idxs0[idxs0 < 0] = 0
idxs1 = idxs
idxs1[idxs1 == t.shape[0]] = t.shape[0] - 1
q0 = qs[idxs0]
q1 = qs[idxs1]
tau = torch.zeros_like(t_int)
dt = (t[idxs1]-t[idxs0])[idxs0 != idxs1]
tau[idxs0 != idxs1] = (t_int-t[idxs0])[idxs0 != idxs1]/dt
return cls.slerp(q0, q1, tau)
@classmethod
def slerp(cls, q0, q1, tau, DOT_THRESHOLD = 0.9995):
"""Spherical linear interpolation."""
dot = (q0*q1).sum(dim=1)
q1[dot < 0] = -q1[dot < 0]
dot[dot < 0] = -dot[dot < 0]
q = torch.zeros_like(q0)
tmp = q0 + tau.unsqueeze(1) * (q1 - q0)
tmp = tmp[dot > DOT_THRESHOLD]
q[dot > DOT_THRESHOLD] = tmp / tmp.norm(dim=1, keepdim=True)
theta_0 = dot.acos()
sin_theta_0 = theta_0.sin()
theta = theta_0 * tau
sin_theta = theta.sin()
s0 = (theta.cos() - dot * sin_theta / sin_theta_0).unsqueeze(1)
s1 = (sin_theta / sin_theta_0).unsqueeze(1)
q[dot < DOT_THRESHOLD] = ((s0 * q0) + (s1 * q1))[dot < DOT_THRESHOLD]
return q / q.norm(dim=1, keepdim=True)
class CPUSO3:
# tolerance criterion
TOL = 1e-8
Id = torch.eye(3)
@classmethod
def qmul(cls, q, r):
"""
Multiply quaternion(s) q with quaternion(s) r.
"""
# Compute outer product
terms = cls.outer(r, q)
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))
@staticmethod
def outer(a, b):
return torch.einsum('i, j -> ij', a, b)
@classmethod
def exp(cls, phi):
angle = phi.norm()
if angle < cls.TOL:
return cls.Id + cls.wedge(phi)
axis = phi / angle
c = angle.cos()
s = angle.sin()
Rot = c*cls.Id + (1-c)*cls.outer(axis, axis) + s*cls.wedge(axis)
return Rot
@staticmethod
def wedge(phi):
return phi.new([[0, -phi[2], phi[1]],
[phi[2], 0, -phi[0]],
[-phi[1], phi[0], 0]])
@staticmethod
def outer(vec1, vec2):
"""outer product"""
return torch.einsum('i, j -> ij', vec1, vec2)