-
Notifications
You must be signed in to change notification settings - Fork 5
/
Spline.py
267 lines (202 loc) · 8.44 KB
/
Spline.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
import torch
import numpy as np
delt = 0
def skew_symmetric(w):
w0,w1,w2 = w.unbind(dim=-1)
O = torch.zeros_like(w0)
wx = torch.stack([torch.stack([O,-w2,w1],dim=-1),
torch.stack([w2,O,-w0],dim=-1),
torch.stack([-w1,w0,O],dim=-1)],dim=-2)
return wx
def taylor_A(x,nth=10):
# Taylor expansion of sin(x)/x
ans = torch.zeros_like(x)
denom = 1.
for i in range(nth+1):
if i>0:
denom *= (2*i)*(2*i+1)
ans = ans+(-1)**i*x**(2*i)/denom
return ans
def taylor_B(x,nth=10):
# Taylor expansion of (1-cos(x))/x**2
ans = torch.zeros_like(x)
denom = 1.
for i in range(nth+1):
denom *= (2*i+1)*(2*i+2)
ans = ans+(-1)**i*x**(2*i)/denom
return ans
def taylor_C(x,nth=10):
# Taylor expansion of (x-sin(x))/x**3
ans = torch.zeros_like(x)
denom = 1.
for i in range(nth+1):
denom *= (2*i+2)*(2*i+3)
ans = ans+(-1)**i*x**(2*i)/denom
return ans
def exp_r2q_parallel(r):
x, y, z = r[..., 0], r[..., 1], r[..., 2]
theta = 0.5 * torch.sqrt(x ** 2 + y ** 2 + z ** 2)
lambda_ = torch.sin(theta) / (2. * theta)
Bool_criterion = (theta < 1e-20)
qx = Bool_criterion * (1. / 2. - 1. / 12. * theta ** 2 - 1. / 240. * theta ** 4) * x + (~Bool_criterion) * lambda_ * x
qy = Bool_criterion * (1. / 2. - 1. / 12. * theta ** 2 - 1. / 240. * theta ** 4) * y + (~Bool_criterion) * lambda_ * y
qz = Bool_criterion * (1. / 2. - 1. / 12. * theta ** 2 - 1. / 240. * theta ** 4) * z + (~Bool_criterion) * lambda_ * z
qw = Bool_criterion * (1. - 1. / 2. * theta ** 2 + 1. / 24. * theta ** 4) + (~Bool_criterion) * torch.cos(theta)
q_ = torch.stack([qx, qy, qz, qw], -1)
return q_
def q_to_R_parallel(q):
qb, qc, qd, qa = q.unbind(dim=-1)
R = torch.stack(
[torch.stack([1 - 2 * (qc ** 2 + qd ** 2), 2 * (qb * qc - qa * qd), 2 * (qa * qc + qb * qd)], dim=-1),
torch.stack([2 * (qb * qc + qa * qd), 1 - 2 * (qb ** 2 + qd ** 2), 2 * (qc * qd - qa * qb)], dim=-1),
torch.stack([2 * (qb * qd - qa * qc), 2 * (qa * qb + qc * qd), 1 - 2 * (qb ** 2 + qc ** 2)], dim=-1)],
dim=-2)
return R
def q_to_Q_parallel(q):
x, y, z, w = q[..., 0], q[..., 1], q[..., 2], q[..., 3]
Q_0 = torch.stack([w, -z, y, x], -1).unsqueeze(-2)
Q_1 = torch.stack([z, w, -x, y], -1).unsqueeze(-2)
Q_2 = torch.stack([-y, x, w, z], -1).unsqueeze(-2)
Q_3 = torch.stack([-x, -y, -z, w], -1).unsqueeze(-2)
Q_ = torch.cat([Q_0, Q_1, Q_2, Q_3], -2)
return Q_
def q_to_q_conj_parallel(q):
x, y, z, w = q[..., 0], q[..., 1], q[..., 2], q[..., 3]
q_conj_ = torch.stack([-x, -y, -z, w], -1)
return q_conj_
def log_q2r_parallel(q): # here fixme
x, y, z, w = q[..., 0], q[..., 1], q[..., 2], q[..., 3]
theta = torch.sqrt(x ** 2 + y ** 2 + z ** 2)
Bool_criterion_1 = (theta < 1e-20)
Bool_criterion_2 = (theta >= 1e-20) & (torch.abs(w) < 1e-10)
Bool_criterion_3 = (theta >= 1e-20) & (torch.abs(w) >= 1e-10)
lambda_ = Bool_criterion_1 * (2. / w - 2. / 3. * (theta ** 2) / (w * w * w)) + Bool_criterion_2 * (w / torch.abs(w) * torch.pi / theta) + Bool_criterion_3 * (2. * (torch.arctan(theta / w)) / theta)
r_ = torch.stack([lambda_ * x, lambda_ * y, lambda_ * z], -1)
return r_
def SE3_to_se3(Rt, eps=1e-8): # [...,3,4]
R, t = Rt.split([3, 1], dim=-1)
w = SO3_to_so3(R)
wx = skew_symmetric(w)
theta = w.norm(dim=-1)[..., None, None]
I = torch.eye(3, device=w.device, dtype=torch.float32)
A = taylor_A(theta)
B = taylor_B(theta)
invV = I - 0.5 * wx + (1 - A / (2 * B)) / (theta ** 2 + eps) * wx @ wx
u = (invV @ t)[..., 0]
wu = torch.cat([w, u], dim=-1)
return wu
def SO3_to_so3(R, eps=1e-7): # [...,3,3]
trace = R[..., 0, 0] + R[..., 1, 1] + R[..., 2, 2]
theta = ((trace - 1) / 2).clamp(-1 + eps, 1 - eps).acos_()[
..., None, None] % np.pi # ln(R) will explode if theta==pi
lnR = 1 / (2 * taylor_A(theta) + 1e-8) * (R - R.transpose(-2, -1)) # FIXME: wei-chiu finds it weird
w0, w1, w2 = lnR[..., 2, 1], lnR[..., 0, 2], lnR[..., 1, 0]
w = torch.stack([w0, w1, w2], dim=-1)
return w
def se3_to_SE3(wu): # [...,3]
w,u = wu.split([3,3],dim=-1)
wx = skew_symmetric(w) # wx=[0 -w(2) w(1);w(2) 0 -w(0);-w(1) w(0) 0]
theta = w.norm(dim=-1)[...,None,None] # theta=sqrt(w'*w)
I = torch.eye(3,device=w.device,dtype=torch.float32)
A = taylor_A(theta)
B = taylor_B(theta)
C = taylor_C(theta)
R = I+A*wx+B*wx@wx
V = I+B*wx+C*wx@wx
Rt = torch.cat([R,(V@u[...,None])],dim=-1)
return Rt
def SE3_to_se3_N(poses_rt):
poses_se3_list =[]
for i in range(poses_rt.shape[0]):
pose_se3 = SE3_to_se3(poses_rt[i])
poses_se3_list.append(pose_se3)
poses = torch.stack(poses_se3_list, 0)
return poses
def se3_to_SE3_N(poses_wu):
poses_se3_list =[]
for i in range(poses_wu.shape[0]):
pose_se3 = se3_to_SE3(poses_wu[i])
poses_se3_list.append(pose_se3)
poses = torch.stack(poses_se3_list, 0)
return poses
def se3_2_qt_parallel(wu):
w, u = wu.split([3, 3], dim=-1)
wx = skew_symmetric(w)
theta = w.norm(dim=-1)[..., None, None]
I = torch.eye(3, device=w.device, dtype=torch.float32)
# A = taylor_A(theta)
B = taylor_B(theta)
C = taylor_C(theta)
# R = I + A * wx + B * wx @ wx
V = I + B * wx + C * wx @ wx
t = V @ u[..., None]
q = exp_r2q_parallel(w)
return q, t.squeeze(-1)
def SplineN_linear(start_pose, end_pose, poses_number, NUM, device=None):
pose_time = poses_number / (NUM-1)
# parallel
pos_0 = torch.where(pose_time == 0)
pose_time[pos_0] = pose_time[pos_0] + 0.000001
pos_1 = torch.where(pose_time == 1)
pose_time[pos_1] = pose_time[pos_1] - 0.000001
q_start, t_start = se3_2_qt_parallel(start_pose)
q_end, t_end = se3_2_qt_parallel(end_pose)
# sample t_vector
t_t = (1 - pose_time)[..., None] * t_start + pose_time[..., None] * t_end
# sample rotation_vector
q_tau_0 = q_to_Q_parallel(q_to_q_conj_parallel(q_start)) @ q_end[..., None]
r = pose_time[..., None] * log_q2r_parallel(q_tau_0.squeeze(-1))
q_t_0 = exp_r2q_parallel(r)
q_t = q_to_Q_parallel(q_start) @ q_t_0[..., None]
# convert q&t to RT
R = q_to_R_parallel(q_t.squeeze(dim=-1))
t = t_t.unsqueeze(dim=-1)
pose_spline = torch.cat([R, t], -1)
poses = pose_spline.reshape([-1, 3, 4])
return poses
def SplineN_cubic(pose0, pose1, pose2, pose3, poses_number, NUM):
sample_time = poses_number / (NUM - 1)
# parallel
pos_0 = torch.where(sample_time == 0)
sample_time[pos_0] = sample_time[pos_0] + 0.000001
pos_1 = torch.where(sample_time == 1)
sample_time[pos_1] = sample_time[pos_1] - 0.000001
sample_time = sample_time.unsqueeze(-1)
q0, t0 = se3_2_qt_parallel(pose0)
q1, t1 = se3_2_qt_parallel(pose1)
q2, t2 = se3_2_qt_parallel(pose2)
q3, t3 = se3_2_qt_parallel(pose3)
u = sample_time
uu = sample_time ** 2
uuu = sample_time ** 3
one_over_six = 1. / 6.
half_one = 0.5
# t
coeff0 = one_over_six - half_one * u + half_one * uu - one_over_six * uuu
coeff1 = 4 * one_over_six - uu + half_one * uuu
coeff2 = one_over_six + half_one * u + half_one * uu - half_one * uuu
coeff3 = one_over_six * uuu
# spline t
t_t = coeff0 * t0 + coeff1 * t1 + coeff2 * t2 + coeff3 * t3
# R
coeff1_r = 5 * one_over_six + half_one * u - half_one * uu + one_over_six * uuu
coeff2_r = one_over_six + half_one * u + half_one * uu - 2 * one_over_six * uuu
coeff3_r = one_over_six * uuu
# spline R
q_01 = q_to_Q_parallel(q_to_q_conj_parallel(q0)) @ q1[..., None] # [1]
q_12 = q_to_Q_parallel(q_to_q_conj_parallel(q1)) @ q2[..., None] # [2]
q_23 = q_to_Q_parallel(q_to_q_conj_parallel(q2)) @ q3[..., None] # [3]
r_01 = log_q2r_parallel(q_01.squeeze(-1)) * coeff1_r # [4]
r_12 = log_q2r_parallel(q_12.squeeze(-1)) * coeff2_r # [5]
r_23 = log_q2r_parallel(q_23.squeeze(-1)) * coeff3_r # [6]
q_t_0 = exp_r2q_parallel(r_01) # [7]
q_t_1 = exp_r2q_parallel(r_12) # [8]
q_t_2 = exp_r2q_parallel(r_23) # [9]
q_product1 = q_to_Q_parallel(q_t_1) @ q_t_2[..., None] # [10]
q_product2 = q_to_Q_parallel(q_t_0) @ q_product1 # [10]
q_t = q_to_Q_parallel(q0) @ q_product2 # [10]
R = q_to_R_parallel(q_t.squeeze(-1))
t = t_t.unsqueeze(dim=-1)
pose_spline = torch.cat([R, t], -1)
poses = pose_spline.reshape([-1, 3, 4])
return poses