File size: 2,945 Bytes
314ecfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Gated creep: restart a stalled ego only when nothing on its predicted path requires waiting.

The policy can stay at v = 0 on an empty road (an attractor of its speed input); the agent's fallback is a
blind creep after 40 s, which also pushes the ego into crossing traffic. The gate checks the object bank
(constant-velocity futures over a short horizon) against the model's own predicted route; the agent adds
the traffic-light check. Objects are bank raw rows in the ego frame (x forward, y right, half extents).
"""
import numpy as np

from dataloader.object_bank import RAW_SPEED, RAW_WIDTH, RAW_X, RAW_Y, RAW_YAW

EGO_HALF_WIDTH = 0.92


def resample_path(route_xy, length=22.0, step=0.5):
    """Predicted route from the ego origin, every `step` metres up to `length` (extended straight)."""
    pts = np.vstack([[0.0, 0.0], np.asarray(route_xy, dtype=np.float64).reshape(-1, 2)])
    pts = pts[np.concatenate([[True], np.hypot(*np.diff(pts, axis=0).T) > 1e-3])]
    s = np.concatenate([[0.0], np.cumsum(np.hypot(*np.diff(pts, axis=0).T))])
    if s[-1] < length:
        direction = pts[-1] - pts[-2] if len(pts) > 1 else np.array([1.0, 0.0])
        direction = direction / max(float(np.linalg.norm(direction)), 1e-6)
        pts = np.vstack([pts, pts[-1] + direction * (length - s[-1])])
        s = np.append(s, length)
    grid = np.arange(0.0, length + 1e-6, step)
    path = np.stack([np.interp(grid, s, pts[:, 0]), np.interp(grid, s, pts[:, 1])], axis=1)
    tangent = np.gradient(path, axis=0)
    return path, grid, np.arctan2(tangent[:, 1], tangent[:, 0])


def path_conflicts(rows, route_xy, horizon_s=3.0, dt=0.25, s_range=(1.0, 22.0), margin=0.3,
                   away_speed=1.0, away_min_s=8.0):
    """Bool mask over rows: the object's constant-velocity future within `horizon_s` comes within
    EGO_HALF_WIDTH + its half width + margin of the path, between s_range metres along it. Objects at
    least `away_min_s` ahead that move away along the path faster than `away_speed` do not count."""
    rows = np.asarray(rows, dtype=np.float64)
    if rows.ndim != 2 or len(rows) == 0:
        return np.zeros(0, dtype=bool)
    path, grid, tangent = resample_path(route_xy, s_range[1])
    ts = np.arange(0.0, horizon_s + 1e-6, dt)
    yaw, speed = rows[:, RAW_YAW], rows[:, RAW_SPEED]
    heading = np.stack([np.cos(yaw), np.sin(yaw)], axis=-1)
    pos = rows[:, None, [RAW_X, RAW_Y]] + (speed[:, None, None] * ts[None, :, None]) * heading[:, None, :]
    d = np.linalg.norm(pos[:, :, None, :] - path[None, None], axis=-1)  # [M, T, P]
    j = d.argmin(axis=-1)
    dmin = np.take_along_axis(d, j[..., None], axis=-1)[..., 0]
    s = grid[j]
    along = speed[:, None] * np.cos(yaw[:, None] - tangent[j])
    limit = EGO_HALF_WIDTH + rows[:, RAW_WIDTH] + margin
    hit = (dmin < limit[:, None]) & (s >= s_range[0]) & (s <= s_range[1])
    hit &= ~((along > away_speed) & (s >= away_min_s))
    return hit.any(axis=-1)