forked from argoverse/argoverse-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_interpolate.py
305 lines (230 loc) · 10.6 KB
/
test_interpolate.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
# <Copyright 2019, Argo AI, LLC. Released under the MIT license.>
"""Interpolate tools unit tests."""
import matplotlib.pyplot as plt
import numpy as np
from argoverse.utils.interpolate import compute_lane_width, compute_mid_pivot_arc, compute_midpoint_line
def test_compute_lane_width_straight():
"""
Compute the lane width of the following straight lane segment
(waypoints indicated with "o" symbol):
o o
| |
o o
| |
o o
We can swap boundaries for this lane, and the width should be identical.
"""
left_even_pts = np.array([[1, 1], [1, 0], [1, -1]])
right_even_pts = np.array([[-1, 1], [-1, 0], [-1, -1]])
lane_width = compute_lane_width(left_even_pts, right_even_pts)
gt_lane_width = 2.0
assert np.isclose(lane_width, gt_lane_width)
lane_width = compute_lane_width(right_even_pts, left_even_pts)
gt_lane_width = 2.0
assert np.isclose(lane_width, gt_lane_width)
def test_compute_lane_width_telescoping():
"""
Compute the lane width of the following straight lane segment
(waypoints indicated with "o" symbol):
o o
\\ //
o o
\\ //
o o
\\ //
o
We can swap boundaries for this lane, and the width should be identical.
"""
left_even_pts = np.array([[3, 2], [2, 1], [1, 0], [0, -1]])
right_even_pts = np.array([[-3, 2], [-2, 1], [-1, 0], [0, -1]])
lane_width = compute_lane_width(left_even_pts, right_even_pts)
gt_lane_width = (6.0 + 4.0 + 2.0 + 0.0) / 4
assert np.isclose(lane_width, gt_lane_width)
lane_width = compute_lane_width(right_even_pts, left_even_pts)
gt_lane_width = (6.0 + 4.0 + 2.0 + 0.0) / 4
assert np.isclose(lane_width, gt_lane_width)
def test_compute_lane_width_curved_width1():
"""
Compute the lane width of the following curved lane segment
Should have width 1 at each pair of boundary waypoints.
-------boundary
/ ----boundary
/ /
| |
| \\
\\ -----
\\-----
"""
left_even_pts = np.array([[0, 2], [-2, 2], [-3, 1], [-3, 0], [-2, -1], [0, -1]])
right_even_pts = np.array([[0, 3], [-2, 3], [-4, 1], [-4, 0], [-2, -2], [0, -2]])
lane_width = compute_lane_width(left_even_pts, right_even_pts)
gt_lane_width = 1.0
assert np.isclose(lane_width, gt_lane_width)
lane_width = compute_lane_width(right_even_pts, left_even_pts)
gt_lane_width = 1.0
assert np.isclose(lane_width, gt_lane_width)
def test_compute_lane_width_curved_not_width1():
"""
Compute the lane width of the following curved lane segment
-------
/ ----
/ /
| |
| \\
\\ -----
\\-----
We get waypoint distances of [1,1,1,1,0.707..., 1,1]
"""
left_even_pts = np.array([[0, 2], [-2, 2], [-3, 1], [-3, 0], [-2.5, -0.5], [-2, -1], [0, -1]])
right_even_pts = np.array([[0, 3], [-2, 3], [-4, 1], [-4, 0], [-3, -1], [-2, -2], [0, -2]])
lane_width = compute_lane_width(left_even_pts, right_even_pts)
gt_lane_width = 0.9581581115980783
assert np.isclose(lane_width, gt_lane_width)
lane_width = compute_lane_width(right_even_pts, left_even_pts)
gt_lane_width = 0.9581581115980783
assert np.isclose(lane_width, gt_lane_width)
def test_compute_mid_pivot_arc_3pt_cul_de_sac():
"""
Make sure we handle the cul-de-sac case correctly.
When mapping a cul-de-sac, we get a line of points on one boundary,
and a single point on the other side. This function produces the middle
arc we get by pivoting around the single point.
Waypoints are depicted below for the cul-de-sac center, and other boundary.
o
\
\
\
O o
/
/
/
o
"""
# Numpy array of shape (3,)
single_pt = np.array([0, 0])
# Numpy array of shape (N,3)
arc_pts = np.array([[0, 1], [1, 0], [0, -1]])
# centerline_pts: Numpy array of shape (N,3)
centerline_pts, lane_width = compute_mid_pivot_arc(single_pt, arc_pts)
gt_centerline_pts = np.array([[0, 0.5], [0.5, 0], [0, -0.5]])
gt_lane_width = 1.0
assert np.allclose(centerline_pts, gt_centerline_pts)
assert np.isclose(lane_width, gt_lane_width)
def test_compute_mid_pivot_arc_5pt_cul_de_sac():
"""
Make sure we handle the cul-de-sac case correctly.
When mapping a cul-de-sac, we get a line of points on one boundary,
and a single point on the other side. This function produces the middle
arc we get by pivoting around the single point.
Waypoints are depicted below for the cul-de-sac center, and other boundary.
o
\
o
\
O o
/
o
/
o
"""
# Numpy array of shape (3,)
single_pt = np.array([0, 0])
# Numpy array of shape (N,3)
arc_pts = np.array([[0, 2], [1, 1], [2, 0], [1, -1], [0, -2]])
# centerline_pts: Numpy array of shape (N,3)
centerline_pts, lane_width = compute_mid_pivot_arc(single_pt, arc_pts)
gt_centerline_pts = np.array([[0, 1], [0.5, 0.5], [1, 0], [0.5, -0.5], [0, -1]])
gt_lane_width = (2 + 2 + 2 + np.sqrt(2) + np.sqrt(2)) / 5
assert np.allclose(centerline_pts, gt_centerline_pts)
assert np.isclose(lane_width, gt_lane_width)
def test_compute_midpoint_line_cul_de_sac_right_onept():
"""
Make sure that if we provide left and right boundary polylines,
we can get the correct centerline by averaging left and right waypoints.
"""
left_ln_bnds = np.array([[0, 2], [1, 1], [2, 0], [1, -1], [0, -2]])
right_ln_bnds = np.array([[0, 0]])
centerline_pts, lane_width = compute_midpoint_line(left_ln_bnds, right_ln_bnds, num_interp_pts=5)
gt_centerline_pts = np.array([[0, 1], [0.5, 0.5], [1, 0], [0.5, -0.5], [0, -1]])
gt_lane_width = (2 + 2 + 2 + np.sqrt(2) + np.sqrt(2)) / 5
assert np.allclose(centerline_pts, gt_centerline_pts)
assert np.isclose(lane_width, gt_lane_width)
def test_compute_midpoint_line_cul_de_sac_left_onept():
"""
Make sure that if we provide left and right boundary polylines,
we can get the correct centerline by averaging left and right waypoints.
"""
right_ln_bnds = np.array([[0, 2], [1, 1], [2, 0], [1, -1], [0, -2]])
left_ln_bnds = np.array([[0, 0]])
centerline_pts, lane_width = compute_midpoint_line(left_ln_bnds, right_ln_bnds, num_interp_pts=5)
gt_centerline_pts = np.array([[0, 1], [0.5, 0.5], [1, 0], [0.5, -0.5], [0, -1]])
gt_lane_width = (2 + 2 + 2 + np.sqrt(2) + np.sqrt(2)) / 5
assert np.allclose(centerline_pts, gt_centerline_pts)
assert np.isclose(lane_width, gt_lane_width)
def test_compute_midpoint_line_straightline_maintain_5_waypts():
"""
Make sure that if we provide left and right boundary polylines,
we can get the correct centerline by averaging left and right waypoints.
"""
right_ln_bnds = np.array([[-1, 4], [-1, 2], [-1, 0], [-1, -2], [-1, -4]])
left_ln_bnds = np.array([[2, 4], [2, 2], [2, 0], [2, -2], [2, -4]])
centerline_pts, lane_width = compute_midpoint_line(left_ln_bnds, right_ln_bnds, num_interp_pts=5)
gt_centerline_pts = np.array([[0.5, 4], [0.5, 2], [0.5, 0], [0.5, -2], [0.5, -4]])
gt_lane_width = 3.0
assert np.allclose(centerline_pts, gt_centerline_pts)
assert np.isclose(lane_width, gt_lane_width)
def test_compute_midpoint_line_straightline_maintain_4_waypts():
"""
Make sure that if we provide left and right boundary polylines,
we can get the correct centerline by averaging left and right waypoints.
"""
right_ln_bnds = np.array([[-1, 4], [-1, 2], [-1, 0], [-1, -2], [-1, -4]])
left_ln_bnds = np.array([[2, 4], [2, 2], [2, 0], [2, -2], [2, -4]])
centerline_pts, lane_width = compute_midpoint_line(left_ln_bnds, right_ln_bnds, num_interp_pts=4)
gt_centerline_pts = np.array([[0.5, 4], [0.5, 4 / 3], [0.5, -4 / 3], [0.5, -4]])
gt_lane_width = 3.0
assert np.allclose(centerline_pts, gt_centerline_pts)
assert np.isclose(lane_width, gt_lane_width)
def test_compute_midpoint_line_straightline_maintain_3_waypts():
"""
Make sure that if we provide left and right boundary polylines,
we can get the correct centerline by averaging left and right waypoints.
"""
right_ln_bnds = np.array([[-1, 4], [-1, 2], [-1, 0], [-1, -2], [-1, -4]])
left_ln_bnds = np.array([[2, 4], [2, 2], [2, 0], [2, -2], [2, -4]])
centerline_pts, lane_width = compute_midpoint_line(left_ln_bnds, right_ln_bnds, num_interp_pts=3)
gt_centerline_pts = np.array([[0.5, 4], [0.5, 0], [0.5, -4]])
gt_lane_width = 3.0
assert np.allclose(centerline_pts, gt_centerline_pts)
assert np.isclose(lane_width, gt_lane_width)
def test_compute_midpoint_line_straightline_maintain_2_waypts():
"""
Make sure that if we provide left and right boundary polylines,
we can get the correct centerline by averaging left and right waypoints.
"""
right_ln_bnds = np.array([[-1, 4], [-1, 2], [-1, 0], [-1, -2], [-1, -4]])
left_ln_bnds = np.array([[2, 4], [2, 2], [2, 0], [2, -2], [2, -4]])
centerline_pts, lane_width = compute_midpoint_line(left_ln_bnds, right_ln_bnds, num_interp_pts=2)
gt_centerline_pts = np.array([[0.5, 4], [0.5, -4]])
gt_lane_width = 3.0
assert np.allclose(centerline_pts, gt_centerline_pts)
assert np.isclose(lane_width, gt_lane_width)
def test_compute_midpoint_line_curved_maintain_4_waypts():
"""
Make sure that if we provide left and right boundary polylines,
we can get the correct centerline by averaging left and right waypoints.
Note that because of the curve and the arc interpolation, the land width and centerline in the middle points will be shifted.
"""
right_ln_bnds = np.array([[-1, 3], [1, 3], [4, 0], [4, -2]])
left_ln_bnds = np.array([[-1, 1], [1, 1], [2, 0], [2, -2]])
centerline_pts, lane_width = compute_midpoint_line(left_ln_bnds, right_ln_bnds, num_interp_pts=4)
from argoverse.utils.mpl_plotting_utils import draw_polygon_mpl
fig = plt.figure(figsize=(22.5, 8))
ax = fig.add_subplot(111)
draw_polygon_mpl(ax, right_ln_bnds, "g")
draw_polygon_mpl(ax, left_ln_bnds, "b")
draw_polygon_mpl(ax, centerline_pts, "r")
gt_centerline_pts = np.array([[-1, 2], [1, 2], [3, 0], [3, -2]])
gt_lane_width = 2.0
assert np.allclose(centerline_pts[0], gt_centerline_pts[0])
assert np.allclose(centerline_pts[-1], gt_centerline_pts[-1])