-
Notifications
You must be signed in to change notification settings - Fork 74
/
trajectory_util.py
284 lines (194 loc) · 7.92 KB
/
trajectory_util.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
import numpy as np
import torch
from model.mesh_fusion.util import get_extrinsics
#######################
# PRIVATE HELPERS #
#######################
def _rot_x(x):
'''
positive: look up, negative: look down
:param x: rotation in degrees
'''
return np.array([x * np.pi / 180, 0, 0], dtype=np.float32)
def _rot_y(x):
'''
positive: look right, negative: look left
:param x: rotation in degrees
'''
return np.array([0, x * np.pi / 180, 0], dtype=np.float32)
def _rot_z(x):
'''
positive: tilt left, negative: tilt right
:param x: rotation in degrees
'''
return np.array([0, 0, x * np.pi / 180], dtype=np.float32)
def _trans_x(x):
'''
positive: right, negative: left
:param x: translation amount
'''
return np.array([x, 0, 0], dtype=np.float32)
def _trans_y(x):
'''
positive: down, negative: up
:param x: translation amount
'''
return np.array([0, x, 0], dtype=np.float32)
def _trans_z(x):
'''
positive: back, negative: front
:param x: translation amount
'''
return np.array([0, 0, x], dtype=np.float32)
def _config_fn(fn, **kwargs):
return lambda i, steps: fn(i, steps, **kwargs)
def _circle(i, steps=60, txmax=0, txmin=0, tymax=0, tymin=0, tzmax=0, tzmin=0, rxmax=0, rxmin=0, rymax=0, rymin=0, rzmax=0, rzmin=0):
tx_delta = (txmax - txmin) / (steps // 2)
ty_delta = (tymax - tymin) / (steps // 2)
tz_delta = (tzmax - tzmin) / (steps // 2)
rx_delta = (rxmax - rxmin) / (steps // 2)
ry_delta = (rymax - rymin) / (steps // 2)
rz_delta = (rzmax - rzmin) / (steps // 2)
f = i % (steps // 2)
tx = txmin + f * tx_delta
ty = tymin + f * ty_delta
tz = tzmin + f * tz_delta
rx = rxmin + f * rx_delta
ry = rymin + f * ry_delta
rz = rzmin + f * rz_delta
if i < steps // 2:
T = _trans_x(-tx)
T += _trans_z(tz)
T += _trans_y(ty)
R = _rot_y(ry)
R += _rot_x(rx)
R += _rot_z(rz)
else:
T = _trans_x(tx)
T += _trans_z(tz)
T += _trans_y(-ty)
R = _rot_y(-ry)
R += _rot_x(-rx)
R += _rot_z(-rz)
return get_extrinsics(R, T)
def _rot_left(i, steps=60, ty=0, rx=0):
angle = i * 360 // steps
T = _trans_x(0)
T += _trans_y(ty)
R = _rot_y(-angle)
R += _rot_x(rx)
return get_extrinsics(R, T)
def _back_and_forth(i, steps=20, txmax=0, txmin=0, tymax=0, tymin=0, tzmax=0, tzmin=0, rxmax=0, rxmin=0, rymax=0, rymin=0, rzmax=0, rzmin=0):
tx_delta = (txmax - txmin) / (steps // 2)
ty_delta = (tymax - tymin) / (steps // 2)
tz_delta = (tzmax - tzmin) / (steps // 2)
rx_delta = (rxmax - rxmin) / (steps // 2)
ry_delta = (rymax - rymin) / (steps // 2)
rz_delta = (rzmax - rzmin) / (steps // 2)
f = i % (steps // 2)
tx = txmin + f * tx_delta
ty = tymin + f * ty_delta
tz = tzmin + f * tz_delta
rx = rxmin + f * rx_delta
ry = rymin + f * ry_delta
rz = rzmin + f * rz_delta
if i < steps // 2:
T = _trans_x(-tx)
T += _trans_z(tz)
T += _trans_y(ty)
R = _rot_y(-ry)
R += _rot_x(rx)
R += _rot_z(rz)
else:
T = _trans_x(tx)
T += _trans_z(tz)
T += _trans_y(-ty)
R = _rot_y(ry)
R += _rot_x(-rx)
R += _rot_z(-rz)
return get_extrinsics(R, T)
trans_t = lambda t: torch.Tensor([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, t],
[0, 0, 0, 1]]).float()
rot_phi = lambda phi: torch.Tensor([
[1, 0, 0, 0],
[0, np.cos(phi), -np.sin(phi), 0],
[0, np.sin(phi), np.cos(phi), 0],
[0, 0, 0, 1]]).float()
rot_theta = lambda th: torch.Tensor([
[np.cos(th), 0, -np.sin(th), 0],
[0, 1, 0, 0],
[np.sin(th), 0, np.cos(th), 0],
[0, 0, 0, 1]]).float()
def pose_spherical(theta, phi, radius):
c2w = trans_t(radius)
c2w = rot_phi(phi / 180. * np.pi) @ c2w
c2w = rot_theta(theta / 180. * np.pi) @ c2w
# c2w = torch.Tensor(np.array([[-1,0,0,0],[0,0,1,0],[0,1,0,0],[0,0,0,1]])) @ c2w
c2w[0:3, 3] = -1 * c2w[0:3, 3]
return c2w
# def sample_points_on_unit_sphere(n_sample, radius=4.0, height=0.0):
# c2w = torch.stack([pose_spherical(angle, 0, radius) for angle in np.linspace(-180,180,n_sample)], dim=0)
# c2w[:, 1, 3] = height
# return c2w
# def spherical_trajector(steps, radius, height):
# c2w = sample_points_on_unit_sphere(n_sample, radius, height)
# return torch.inverse(c2w)
def _sphere_rot_xz(i, steps, radius=4.0, height=0.0, phi=20.0):
rot_angle = i * 360 / steps
rot_angle = rot_angle
phi = phi if height > 0 else -phi if height < 0 else 0
c2w = pose_spherical(rot_angle, phi, radius)
c2w[1, 3] = height
return torch.inverse(c2w)
def _double_sphere_rot_xz(i, steps, radius=4.0, height=0.0, phi=20.0):
rot_angle = i * 360 / steps
rot_angle = rot_angle
phi = phi if height > 0 else -phi if height < 0 else 0
c2w = pose_spherical(rot_angle, phi, radius)
if i % 2 == 0:
c2w[0:3, 3] = 0
else:
c2w[1, 3] = height
return torch.inverse(c2w)
#######################
# PUBLIC TRAJECTORIES #
#######################
def forward(height=0, rot=0, txmax=2):
return _config_fn(_circle, txmax=txmax, rymax=90, rymin=45, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def forward_small(height=0, rot=0):
return _config_fn(_circle, txmax=0.5, rymax=90, rymin=45, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def left_right(height=0, rot=0):
return _config_fn(_circle, tzmax=2, rymax=90, rymin=45, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def backward(height=0, rot=0):
return _config_fn(_circle, txmax=-2, rymax=90, rymin=45, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def backward2(height=0, rot=0, txmax=1):
return _config_fn(_circle, txmax=txmax, rymax=225, rymin=180, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def backward2_small(height=0, rot=0):
return _config_fn(_circle, txmax=0.5, rymax=225, rymin=180, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def backward3(height=0, rot=0, txmax=1):
return _config_fn(_circle, txmax=txmax, rymax=270, rymin=225, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def backward3_small(height=0, rot=0):
return _config_fn(_circle, txmax=0.5, rymax=270, rymin=225, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def rot_left_up_down(height=0, rot=0):
return _config_fn(_rot_left, ty=height, rx=rot)
def back_and_forth_forward(height=0, rot=0):
return _config_fn(_back_and_forth, txmax=0, tzmax=-2, rymax=60, rymin=15, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def back_and_forth_backward(height=0, rot=0):
return _config_fn(_back_and_forth, txmax=0, tzmax=-2, rymax=-120, rymin=-165, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def back_and_forth_forward_reverse(height=0, rot=0, tzmax=2):
return _config_fn(_back_and_forth, txmax=0, tzmax=tzmax, rymax=-15, rymin=-60, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def back_and_forth_forward_reverse_small(height=0, rot=0):
return _config_fn(_back_and_forth, txmax=0, tzmax=0.5, rymax=-15, rymin=-60, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def back_and_forth_backward_reverse(height=0, rot=0, tzmax=2):
return _config_fn(_back_and_forth, txmax=0, tzmax=tzmax, rymax=165, rymin=120, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def back_and_forth_backward_reverse_small(height=0, rot=0):
return _config_fn(_back_and_forth, txmax=0, tzmax=0.5, rymax=165, rymin=120, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def back_and_forth_backward_reverse2(height=0, rot=0):
return _config_fn(_back_and_forth, txmax=0, tzmax=3, rymax=165, rymin=60, tymin=height, tymax=height, rxmin=rot, rxmax=rot)
def sphere_rot(radius=4.0, height=0.0, phi=20.0):
return _config_fn(_sphere_rot_xz, radius=radius, height=height, phi=phi)
def double_rot(radius=4.0, height=0.0, phi=2.0):
return _config_fn(_double_sphere_rot_xz, radius=radius, height=height, phi=phi)