forked from ChiWeiHsiao/DeepVO-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
96 lines (82 loc) · 2.83 KB
/
helper.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
'''
角度,旋转矩阵,欧拉角之间的变换和检查
'''
import numpy as np
import math
# epsilon for testing whether a number is close to zero
_EPS = np.finfo(float).eps * 4.0
def isRotationMatrix(R) :
Rt = np.transpose(R)
shouldBeIdentity = np.dot(Rt, R)
I = np.identity(3, dtype = R.dtype)
n = np.linalg.norm(I - shouldBeIdentity)
return n < 1e-6
def R_to_angle(Rt):
# Ground truth pose is present as [R | t]
# R: Rotation Matrix, t: translation vector
# transform matrix to angles
Rt = np.reshape(np.array(Rt), (3,4))
t = Rt[:,-1]
R = Rt[:,:3]
assert(isRotationMatrix(R))
x, y, z = euler_from_matrix(R)
theta = [x, y, z]
pose_15 = np.concatenate((theta, t, R.flatten()))
assert(pose_15.shape == (15,))
return pose_15
def eulerAnglesToRotationMatrix(theta) :
R_x = np.array([[1, 0, 0 ],
[0, np.cos(theta[0]), -np.sin(theta[0]) ],
[0, np.sin(theta[0]), np.cos(theta[0]) ]
])
R_y = np.array([[np.cos(theta[1]), 0, np.sin(theta[1]) ],
[0, 1, 0 ],
[-np.sin(theta[1]), 0, np.cos(theta[1]) ]
])
R_z = np.array([[np.cos(theta[2]), -np.sin(theta[2]), 0],
[np.sin(theta[2]), np.cos(theta[2]), 0],
[0, 0, 1]
])
R = np.dot(R_z, np.dot( R_y, R_x ))
return R
def euler_from_matrix(matrix):
# y-x-z Tait–Bryan angles intrincic
# the method code is taken from https://github.com/awesomebytes/delta_robot/blob/master/src/transformations.py
i = 2
j = 0
k = 1
repetition = 0
frame = 1
parity = 0
M = np.array(matrix, dtype=np.float64, copy=False)[:3, :3]
if repetition:
sy = math.sqrt(M[i, j]*M[i, j] + M[i, k]*M[i, k])
if sy > _EPS:
ax = math.atan2( M[i, j], M[i, k])
ay = math.atan2( sy, M[i, i])
az = math.atan2( M[j, i], -M[k, i])
else:
ax = math.atan2(-M[j, k], M[j, j])
ay = math.atan2( sy, M[i, i])
az = 0.0
else:
cy = math.sqrt(M[i, i]*M[i, i] + M[j, i]*M[j, i])
if cy > _EPS:
ax = math.atan2( M[k, j], M[k, k])
ay = math.atan2(-M[k, i], cy)
az = math.atan2( M[j, i], M[i, i])
else:
ax = math.atan2(-M[j, k], M[j, j])
ay = math.atan2(-M[k, i], cy)
az = 0.0
if parity:
ax, ay, az = -ax, -ay, -az
if frame:
ax, az = az, ax
return ax, ay, az
def normalize_angle_delta(angle):
if(angle > np.pi):
angle = angle - 2 * np.pi
elif(angle < -np.pi):
angle = 2 * np.pi + angle
return angle