File size: 3,426 Bytes
c263a1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
render.py
---------
Draws a trajectory record as a PNG.

VALID records: uses the dense "smooth_x"/"smooth_y" curve (60 points) for the
line, so it reads as a real, smooth physical arc — with the 6 official labeled
points marked on top as numbered dots.

INVALID records: drawn using straight segments between the 6 points, same as
before. Keeping violations sparse/angular is intentional — a sharp discontinuity
should look sharp; smoothing it would hide exactly what makes it invalid.
"""

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import CubicSpline


def render_trajectory(record, save_path, image_size=6):
    x = record["x"]
    y = record["y"]
    is_valid = record.get("valid", record.get("violation_type") is None)

    fig, ax = plt.subplots(figsize=(image_size, image_size))
    ax.axhline(0, color="saddlebrown", linewidth=3)

    if is_valid and "smooth_x" in record and "smooth_y" in record:
        # Real physics-based dense curve (60 points from the actual equations)
        curve_x, curve_y = record["smooth_x"], record["smooth_y"]
        ax.plot(curve_x, curve_y, linestyle="-", color="gray", linewidth=1.3, alpha=0.8, zorder=1)
    elif "jump_idx" in record:
        # Teleport: two genuinely separate smooth segments, no line connecting
        # them. A spline across the whole path would visually paper over the
        # exact thing this violation is testing — that no continuous path
        # connects the two positions. Leaving a real gap is the honest render.
        j = record["jump_idx"]
        seg1_x, seg1_y = x[:j], y[:j]      # points before the jump, unshifted
        seg2_x, seg2_y = x[j:], y[j:]      # points at/after the jump, shifted
        curve_x, curve_y = list(x), list(y)  # for axis limits below
        for seg_x, seg_y in [(seg1_x, seg1_y), (seg2_x, seg2_y)]:
            if len(seg_x) >= 2:
                s = np.arange(len(seg_x))
                s_fine = np.linspace(0, len(seg_x) - 1, 30)
                sx = CubicSpline(s, seg_x)(s_fine)
                sy = CubicSpline(s, seg_y)(s_fine)
                ax.plot(sx, sy, linestyle="-", color="gray", linewidth=1.3, alpha=0.8, zorder=1)
    else:
        # Parametric cubic spline through the 6 labeled points, using index
        # as the parameter (not x) so it stays well-behaved even when x
        # isn't monotonic (rewind case goes backward in x).
        s = np.arange(len(x))
        s_fine = np.linspace(0, len(x) - 1, 60)
        spline_x = CubicSpline(s, x)
        spline_y = CubicSpline(s, y)
        curve_x = spline_x(s_fine).tolist()
        curve_y = spline_y(s_fine).tolist()
        ax.plot(curve_x, curve_y, linestyle="-", color="gray", linewidth=1.3, alpha=0.8, zorder=1)

    # The 6 official labeled points, always marked the same way
    ax.scatter(x, y, color="crimson", s=80, zorder=2)
    for i, (px, py) in enumerate(zip(x, y)):
        ax.annotate(str(i + 1), (px, py), textcoords="offset points",
                    xytext=(0, 8), ha="center", fontsize=9)

    all_x = x + curve_x
    all_y = y + curve_y
    ax.set_xlim(min(all_x) - 1, max(all_x) + 2)
    ax.set_ylim(min(-3, min(all_y) - 1), max(all_y) + 2)
    ax.set_xticks([])
    ax.set_yticks([])
    for spine in ax.spines.values():
        spine.set_visible(False)

    plt.tight_layout()
    plt.savefig(save_path, dpi=100)
    plt.close(fig)