"vscode:/vscode.git/clone" did not exist on "65d234ac1ab964dddb1b14097bfc4c48a518362b"
utils.py 4.52 KB
Newer Older
Chengyu Wang's avatar
Chengyu Wang committed
1
# ==============================================================================
zhe chen's avatar
zhe chen committed
2
# Binaries and/or source for the following packages or projects
Chengyu Wang's avatar
Chengyu Wang committed
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
# are presented under one or more of the following open source licenses:
# utils.py    The OpenLane-V2 Dataset Authors    Apache License, Version 2.0
#
# Contact wanghuijie@pjlab.org.cn if you have any issue.
#
# Copyright (c) 2023 The OpenLane-v2 Dataset Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

import numpy as np

THICKNESS = 4

COLOR_DEFAULT = (0, 0, 255)
COLOR_DICT = {
zhe chen's avatar
zhe chen committed
29
30
31
32
33
34
35
36
37
38
    0: COLOR_DEFAULT,
    1: (255, 0, 0),
    2: (0, 255, 0),
    3: (255, 255, 0),
    4: (255, 0, 255),
    5: (0, 128, 128),
    6: (0, 128, 0),
    7: (128, 0, 0),
    8: (128, 0, 128),
    9: (128, 128, 0),
Chengyu Wang's avatar
Chengyu Wang committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    10: (0, 0, 128),
    11: (64, 64, 64),
    12: (192, 192, 192),
}


def interp_arc(points, t=1000):
    r'''
    Linearly interpolate equally-spaced points along a polyline, either in 2d or 3d.

    Parameters
    ----------
    points : List
        List of shape (N,2) or (N,3), representing 2d or 3d-coordinates.
    t : array_like
        Number of points that will be uniformly interpolated and returned.

    Returns
    -------
zhe chen's avatar
zhe chen committed
58
    array_like
Chengyu Wang's avatar
Chengyu Wang committed
59
60
61
62
63
64
65
        Numpy array of shape (N,2) or (N,3)

    Notes
    -----
    Adapted from https://github.com/johnwlambert/argoverse2-api/blob/main/src/av2/geometry/interpolate.py#L120

    '''
zhe chen's avatar
zhe chen committed
66

Chengyu Wang's avatar
Chengyu Wang committed
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
    # filter consecutive points with same coordinate
    temp = []
    for point in points:
        point = point.tolist()
        if temp == [] or point != temp[-1]:
            temp.append(point)
    if len(temp) <= 1:
        return None
    points = np.array(temp, dtype=points.dtype)

    assert points.ndim == 2

    # the number of points on the curve itself
    n, _ = points.shape

    # equally spaced in arclength -- the number of points that will be uniformly interpolated
    eq_spaced_points = np.linspace(0, 1, t)

    # Compute the chordal arclength of each segment.
    # Compute differences between each x coord, to get the dx's
    # Do the same to get dy's. Then the hypotenuse length is computed as a norm.
    chordlen = np.linalg.norm(np.diff(points, axis=0), axis=1)  # type: ignore
    # Normalize the arclengths to a unit total
    chordlen = chordlen / np.sum(chordlen)
    # cumulative arclength

    cumarc = np.zeros(len(chordlen) + 1)
    cumarc[1:] = np.cumsum(chordlen)

    # which interval did each point fall in, in terms of eq_spaced_points? (bin index)
    tbins = np.digitize(eq_spaced_points, bins=cumarc).astype(int)  # type: ignore

    # #catch any problems at the ends
    tbins[np.where((tbins <= 0) | (eq_spaced_points <= 0))] = 1  # type: ignore
    tbins[np.where((tbins >= n) | (eq_spaced_points >= 1))] = n - 1

    s = np.divide((eq_spaced_points - cumarc[tbins - 1]), chordlen[tbins - 1])
    anchors = points[tbins - 1, :]
    # broadcast to scale each row of `points` by a different row of s
    offsets = (points[tbins, :] - points[tbins - 1, :]) * s.reshape(-1, 1)
    points_interp = anchors + offsets

    return points_interp

zhe chen's avatar
zhe chen committed
111

Chengyu Wang's avatar
Chengyu Wang committed
112
113
114
115
116
117
118
def assign_attribute(annotation):
    topology_lcte = np.array(annotation['topology_lcte'], dtype=bool)
    for i in range(len(annotation['lane_centerline'])):
        annotation['lane_centerline'][i]['attributes'] = \
            set([ts['attribute'] for j, ts in enumerate(annotation['traffic_element']) if topology_lcte[i][j]])
    return annotation

zhe chen's avatar
zhe chen committed
119

Chengyu Wang's avatar
Chengyu Wang committed
120
121
122
123
124
125
126
127
128
129
130
131
def assign_topology(annotation):
    topology_lcte = np.array(annotation['topology_lcte'], dtype=bool)
    annotation['topology'] = []
    for i in range(topology_lcte.shape[0]):
        for j in range(topology_lcte.shape[1]):
            if topology_lcte[i][j]:
                annotation['topology'].append({
                    'lane_centerline': annotation['lane_centerline'][i]['points'],
                    'traffic_element': annotation['traffic_element'][j]['points'],
                    'attribute': annotation['traffic_element'][j]['attribute'],
                })
    return annotation