Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
246 lines (211 loc) · 6.45 KB
/
Copy pathutils.py
File metadata and controls
246 lines (211 loc) · 6.45 KB
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
import numpy as np
import matplotlib.pyplot as plt
from transforms3d.euler import mat2euler
def load_data(file_name):
'''
function to read visual features, IMU measurements, and calibration parameters
Input:
file_name: the input data file. Should look like "XX.npz"
Output:
t (1 x t): time stamps
features (4 x n x t): visual feature point coordinates in stereo images (n is number of features)
linear_velocity (3 x t): linear velocity measurements in IMU frame
angular_velocity (3 x t): angular velocity measurements in IMU frame
K (3 x 3): camera intrinsic matrix
b (1,): stereo camera baseline
imu_T_cam (4 x 4): extrinsic transformation from (left) camera to imu frame, in SE(3).
'''
with np.load(file_name) as data:
t = data["time_stamps"] # time_stamps
features = data["features"] # 4 x num_features : pixel coordinates of the visual features
linear_velocity = data["linear_velocity"] # linear velocity in body-frame coordinates
angular_velocity = data["angular_velocity"] # angular velocity in body-frame coordinates
K = data["K"] # intrinsic calibration matrix
b = data["b"] # baseline
imu_T_cam = data["imu_T_cam"] # transformation from left camera frame to imu frame
return t,features,linear_velocity,angular_velocity,K,b,imu_T_cam
def visualize_trajectory_2d(pose,path_name="Unknown",show_ori=False):
'''
function to visualize the trajectory in 2D
Input:
pose: 4x4xN matrix of N poses in SE(3)
'''
fig,ax = plt.subplots(figsize=(5,5))
n_pose = pose.shape[2]
ax.plot(pose[0,3,:],pose[1,3,:],'r-',label=path_name)
ax.scatter(pose[0,3,0],pose[1,3,0],marker='s',label="start")
ax.scatter(pose[0,3,-1],pose[1,3,-1],marker='o',label="end")
if show_ori:
select_ori_index = list(range(0,n_pose,max(int(n_pose/50), 1)))
yaw_list = []
for i in select_ori_index:
_,_,yaw = mat2euler(pose[:3,:3,i])
yaw_list.append(yaw)
dx = np.cos(yaw_list)
dy = np.sin(yaw_list)
dx,dy = [dx,dy]/np.sqrt(dx**2+dy**2)
ax.quiver(pose[0,3,select_ori_index],pose[1,3,select_ori_index],dx,dy,\
color="b",units="xy",width=1)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.axis('equal')
ax.grid(False)
ax.legend()
plt.show(block=True)
return fig, ax
# -------------------Parallized functions for batch processing of multiple points-------------------
def projection(ph):
'''
ph = n x 4 = homogeneous point coordinates
r = n x 4 = ph/ph[...,2] = normalized z axis coordinates
'''
return ph/ph[...,2,None]
def projectionJacobian(ph):
'''
input:
ph = n x 4 = homogeneous point coordinates
output:
J = n x 4 x 4 = Jacobian of projection
'''
J = np.zeros(ph.shape+(4,))
iph2 = 1.0/ph[...,2]
ph2ph2 = ph[...,2]**2
J[...,0,0], J[...,1,1],J[...,3,3] = iph2,iph2,iph2
J[...,0,2] = -ph[...,0]/ph2ph2
J[...,1,2] = -ph[...,1]/ph2ph2
J[...,3,2] = -ph[...,3]/ph2ph2
return J
def inversePose(T):
'''
input:
T = n x 4 x 4 = n elements of SE(3)
output:
iT = n x 4 x 4 = inverse of T
'''
iT = np.empty_like(T)
iT[...,0,0], iT[...,0,1], iT[...,0,2] = T[...,0,0], T[...,1,0], T[...,2,0]
iT[...,1,0], iT[...,1,1], iT[...,1,2] = T[...,0,1], T[...,1,1], T[...,2,1]
iT[...,2,0], iT[...,2,1], iT[...,2,2] = T[...,0,2], T[...,1,2], T[...,2,2]
iT[...,:3,3] = -np.squeeze(iT[...,:3,:3] @ T[...,:3,3,None])
iT[...,3,:] = T[...,3,:]
return iT
def axangle2skew(a):
'''
input:
a (n x 3) = n elements of axis-angle
output:
S (n x 3 x 3) = n elements of so(3)
'''
S = np.empty(a.shape[:-1]+(3,3))
S[...,0,0].fill(0)
S[...,0,1] =-a[...,2]
S[...,0,2] = a[...,1]
S[...,1,0] = a[...,2]
S[...,1,1].fill(0)
S[...,1,2] =-a[...,0]
S[...,2,0] =-a[...,1]
S[...,2,1] = a[...,0]
S[...,2,2].fill(0)
return S
def axangle2twist(x):
'''
input:
x = n x 6 = n elements of position and axis-angle
output:
T = n x 4 x 4 = n elements of se(3)
'''
T = np.zeros(x.shape[:-1]+(4,4))
T[...,0,1] =-x[...,5]
T[...,0,2] = x[...,4]
T[...,0,3] = x[...,0]
T[...,1,0] = x[...,5]
T[...,1,2] =-x[...,3]
T[...,1,3] = x[...,1]
T[...,2,0] =-x[...,4]
T[...,2,1] = x[...,3]
T[...,2,3] = x[...,2]
return T
def twist2axangle(T):
'''
input:
T (n x 4 x 4): n elements of se(3)
output:
x (n x 6): n elements of position and axis-angle
'''
return T[...,[0,1,2,2,0,1],[3,3,3,1,2,0]]
def axangle2adtwist(x):
'''
input:
x (n x 6): n elements of position and axis-angle
output:
A (n x 6 x 6): n elements of ad(se(3))
'''
A = np.zeros(x.shape+(6,))
A[...,0,1] =-x[...,5]
A[...,0,2] = x[...,4]
A[...,0,4] =-x[...,2]
A[...,0,5] = x[...,1]
A[...,1,0] = x[...,5]
A[...,1,2] =-x[...,3]
A[...,1,3] = x[...,2]
A[...,1,5] =-x[...,0]
A[...,2,0] =-x[...,4]
A[...,2,1] = x[...,3]
A[...,2,3] =-x[...,1]
A[...,2,4] = x[...,0]
A[...,3,4] =-x[...,5]
A[...,3,5] = x[...,4]
A[...,4,3] = x[...,5]
A[...,4,5] =-x[...,3]
A[...,5,3] =-x[...,4]
A[...,5,4] = x[...,3]
return A
def twist2pose(T):
'''
input:
T (n x 4 x 4): n elements of se(3)
output:
pose (n x 4 x 4): n elements of SE(3)
'''
rotang = np.sqrt(np.sum(T[...,[2,0,1],[1,2,0]]**2,axis=-1)[...,None,None]) # n x 1
Tn = np.nan_to_num(T / rotang)
Tn2 = Tn@Tn
Tn3 = Tn@Tn2
eye = np.zeros_like(T)
eye[...,[0,1,2,3],[0,1,2,3]] = 1.0
return eye + T + (1.0 - np.cos(rotang))*Tn2 + (rotang - np.sin(rotang))*Tn3
def axangle2pose(x):
'''
input:
x (n x 6): n elements of position and axis-angle
output:
T (n x 4 x 4): n elements of SE(3)
'''
return twist2pose(axangle2twist(x))
def pose2adpose(T):
'''
input:
T (n x 4 x 4): n elements of SE(3)
output:
calT (n x 6 x 6): n elements of ad(SE(3))
'''
calT = np.empty(T.shape[:-2]+(6,6))
calT[...,:3,:3] = T[...,:3,:3]
calT[...,:3,3:] = axangle2skew(T[...,:3,3]) @ T[...,:3,:3]
calT[...,3:,:3] = np.zeros(T.shape[:-2]+(3,3))
calT[...,3:,3:] = T[...,:3,:3]
return calT
def pose2tr(XYZH):
'''
input:
XYZH (4 x 1): homogeneous point coordinates
output:
M_tr (4 x 6): point jacobian w.r.t. pose(X,Y,Z)
'''
XYZH = XYZH.reshape(4,1)
I = np.eye(3)
S = axangle2skew(XYZH[:3,:].T)[0,:,:]
M_tr = np.zeros((4,6))
M_tr[:3,:3] = I
M_tr[:3,3:] = -S
return M_tr