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 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
import os
from typing import Optional

import matplotlib
import matplotlib.pylab as plt
import numpy as np
from PIL import Image

import os
import torch
import matplotlib
from typing import Tuple, Optional, List, Dict, Any, Union
from matplotlib.patches import Circle, Polygon, RegularPolygon

from gpudrive.visualize.color import ROAD_GRAPH_COLORS, ROAD_GRAPH_TYPE_NAMES

def img_from_fig(fig: matplotlib.figure.Figure) -> np.ndarray:
    """Returns a [H, W, 3] uint8 np image from fig.canvas.tostring_rgb()."""
    # Adjusted margins to better accommodate 3D plots
    fig.subplots_adjust(
        left=0.0,    # Reduce left margin
        bottom=0.0,  # Reduce bottom margin
        right=1.0,   # Extend to right edge
        top=1.0,     # Extend to top edge
        wspace=0.0, 
        hspace=0.0
    )
    
    # Force render
    fig.canvas.draw()
    
    # Convert to numpy array
    data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
    img = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
    
    plt.close(fig)
    return img


def save_img_as_png(img: np.ndarray, filename: str = "/tmp/img.png"):
    """Saves np image to disk."""
    outdir = os.path.dirname(filename)
    os.makedirs(outdir, exist_ok=True)
    Image.fromarray(img).save(filename)


def plot_roadgraph_points(ax, observation_roadgraph, env_idx, agent_idx):
    """Plots the road graph points by their type, using names instead of type numbers."""

    # Extract road graph types and positions
    roadgraph_types = observation_roadgraph.type[env_idx, agent_idx, :]
    roadgraph_x = observation_roadgraph.x[env_idx, agent_idx, :]
    roadgraph_y = observation_roadgraph.y[env_idx, agent_idx, :]

    # Plot points by type, mapping types to names
    for road_type, color in ROAD_GRAPH_COLORS.items():
        # Filter points by road type
        idx = roadgraph_types == road_type
        if idx.sum() > 0:
            ax.plot(
                roadgraph_x[idx],
                roadgraph_y[idx],
                ".",  # Plot as dots
                color=color,
                label=ROAD_GRAPH_TYPE_NAMES.get(
                    road_type, f"Type {road_type}"
                ),
            )


def plot_numpy_bounding_boxes(
    ax: matplotlib.axes.Axes,
    bboxes: np.ndarray,
    color: np.ndarray,
    alpha: Optional[float] = 1.0,
    line_width_scale: float = 1.5,
    as_center_pts: bool = False,
    label: Optional[str] = None,
) -> None:
    """Plots multiple bounding boxes.

    Args:
      ax: Fig handles.
      bboxes: Shape (num_bbox, 5), with last dimension as (x, y, length, width,
        yaw).
      color: Shape (3,), represents RGB color for drawing.
      alpha: Alpha value for drawing, i.e. 0 means fully transparent.
      as_center_pts: If set to True, bboxes will be drawn as center points,
        instead of full bboxes.
      label: String, represents the meaning of the color for different boxes.
    """
    if bboxes.ndim != 2 or bboxes.shape[1] != 5:
        raise ValueError(
            (
                "Expect bboxes rank 2, last dimension of bbox 5"
                " got{}, {}, {} respectively"
            ).format(bboxes.ndim, bboxes.shape[1], color.shape)
        )

    if as_center_pts:
        ax.plot(
            bboxes[:, 0],
            bboxes[:, 1],
            "o",
            color=color,
            ms=2,
            alpha=alpha,
            linewidth=1.7 * line_width_scale,
            label=label,
        )
    else:
        c = np.cos(bboxes[:, 4])
        s = np.sin(bboxes[:, 4])
        pt = np.array((bboxes[:, 0], bboxes[:, 1]))  # (2, N)
        length, width = bboxes[:, 2], bboxes[:, 3]
        u = np.array((c, s))
        ut = np.array((s, -c))

        # Compute box corner coordinates.
        tl = pt + length / 2 * u - width / 2 * ut
        tr = pt + length / 2 * u + width / 2 * ut
        br = pt - length / 2 * u + width / 2 * ut
        bl = pt - length / 2 * u - width / 2 * ut

        # Compute heading arrow using center left/right/front.
        cl = pt - width / 2 * ut
        cr = pt + width / 2 * ut
        cf = pt + length / 2 * u

        # Draw bboxes.
        ax.plot(
            [tl[0, :], tr[0, :], br[0, :], bl[0, :], tl[0, :]],
            [tl[1, :], tr[1, :], br[1, :], bl[1, :], tl[1, :]],
            color=color,
            zorder=4,
            linewidth=1.7 * line_width_scale,
            alpha=alpha,
            label=label,
        )

        # Draw heading arrow.
        ax.plot(
            [cl[0, :], cr[0, :], cf[0, :], cl[0, :]],
            [cl[1, :], cr[1, :], cf[1, :], cl[1, :]],
            color=color,
            zorder=6,
            alpha=alpha,
            linewidth=1.5 * line_width_scale,
            label=label,
        )


def plot_bounding_box(
    ax: matplotlib.axes.Axes,
    center: Optional[Union[Tuple[float, float], torch.Tensor]],
    vehicle_length: Union[float, torch.Tensor],
    vehicle_width: Union[float, torch.Tensor],
    orientation: Union[float, torch.Tensor],
    color: str,
    alpha: Optional[float] = 1.0,
    label: Optional[str] = None,
) -> None:
    """Plots bounding boxes, supporting both single and multiple agents.

    Args:
        ax: Matplotlib Axes handle.
        center: Tuple (x, y) specifying a single bounding box center or
                a tensor of shape (num_agents, 2) with x, y positions for multiple agents.
        vehicle_length: Length of the bounding box (float or tensor of shape (num_agents,)).
        vehicle_width: Width of the bounding box (float or tensor of shape (num_agents,)).
        orientation: Orientation of the bounding box (float or tensor of shape (num_agents,)).
        color: Color for the bounding boxes.
        alpha: Transparency of the bounding boxes (0.0 to 1.0).
        label: Optional label for the bounding boxes (only used for single-agent plots).
    """
    if isinstance(center, torch.Tensor):
        # Multiple bounding boxes
        if center.shape[-1] != 2:
            raise ValueError(
                "Center tensor must have shape (num_agents, 2) for multiple bounding boxes."
            )

        num_agents = center.shape[0]
        for i in range(num_agents):
            cx, cy = center[i]
            length = vehicle_length[i].item()
            width = vehicle_width[i].item()
            angle = orientation[i].item()

            # Compute bounding box corners
            corners_x = [
                cx - length / 2,
                cx + length / 2,
                cx + length / 2,
                cx - length / 2,
                cx - length / 2,
            ]
            corners_y = [
                cy - width / 2,
                cy - width / 2,
                cy + width / 2,
                cy + width / 2,
                cy - width / 2,
            ]

            # Apply rotation
            rotated_corners = [
                (
                    (x - cx) * np.cos(angle) - (y - cy) * np.sin(angle) + cx,
                    (x - cx) * np.sin(angle) + (y - cy) * np.cos(angle) + cy,
                )
                for x, y in zip(corners_x, corners_y)
            ]

            rotated_corners_x, rotated_corners_y = zip(*rotated_corners)
            ax.plot(
                np.concatenate(
                    [rotated_corners_x]
                ),  # Use np.concatenate to fix the addition
                np.concatenate(
                    [rotated_corners_y]
                ),  # Use np.concatenate to fix the addition
                color=color,
                alpha=alpha,
                linestyle="-",
                linewidth=2,
                label=label if i == 0 else None,
            )
    else:
        # Single bounding box
        cx, cy = center
        corners_x = [
            cx - vehicle_length / 2,
            cx + vehicle_length / 2,
            cx + vehicle_length / 2,
            cx - vehicle_length / 2,
            cx - vehicle_length / 2,
        ]
        corners_y = [
            cy - vehicle_width / 2,
            cy - vehicle_width / 2,
            cy + vehicle_width / 2,
            cy + vehicle_width / 2,
            cy - vehicle_width / 2,
        ]

        # Apply rotation for single bounding box
        rotated_corners = [
            (
                (x - cx) * np.cos(orientation)
                - (y - cy) * np.sin(orientation)
                + cx,
                (x - cx) * np.sin(orientation)
                + (y - cy) * np.cos(orientation)
                + cy,
            )
            for x, y in zip(corners_x, corners_y)
        ]

        rotated_corners_x, rotated_corners_y = zip(*rotated_corners)
        ax.plot(
            np.concatenate([rotated_corners_x]),
            np.concatenate([rotated_corners_y]),
            color=color,
            alpha=alpha,
            linestyle="-",
            label=label,
            linewidth=2,
        )


def get_corners_polygon(x, y, length, width, orientation):
    """Calculate the four corners of a speed bump (can be any) polygon."""
    # Compute the direction vectors based on orientation
    # print(length)
    c = np.cos(orientation)
    s = np.sin(orientation)
    u = np.array((c, s))  # Unit vector along the orientation
    ut = np.array((-s, c))  # Unit vector perpendicular to the orientation

    # Center point of the speed bump
    pt = np.array([x, y])

    # corners
    tl = pt + (length / 2) * u - (width / 2) * ut
    tr = pt + (length / 2) * u + (width / 2) * ut
    br = pt - (length / 2) * u + (width / 2) * ut
    bl = pt - (length / 2) * u - (width / 2) * ut

    return [tl.tolist(), tr.tolist(), br.tolist(), bl.tolist()]


def get_stripe_polygon(
    x: float,
    y: float,
    length: float,
    width: float,
    orientation: float,
    index: int,
    num_stripes: int,
) -> np.ndarray:

    """Calculate the corners of a stripe within the speed bump polygon."""

    # Compute the direction vectors
    c = np.cos(orientation)
    s = np.sin(orientation)
    u = np.array([c, s])  # Unit vector along the orientation (lengthwise)
    ut = np.array([-s, c])  # Perpendicular unit vector (widthwise)

    # Total stripe height along the width
    stripe_width = length / num_stripes
    half_length = length / 2
    half_width = width / 2

    # Offset for the current stripe
    offset_start = -half_length + index * stripe_width
    offset_end = offset_start + stripe_width

    # Center of the speed bump
    center = np.array([x, y])

    # Calculate stripe corners
    stripe_corners = [
        center + u * offset_start + ut * half_width,  # Top-left
        center + u * offset_start - ut * half_width,  # Bottom-left
        center + u * offset_end - ut * half_width,  # Bottom-right
        center + u * offset_end + ut * half_width,  # Top-right
    ]

    return np.array(stripe_corners)


def plot_speed_bumps(
    x_coords: Union[float, np.ndarray],
    y_coords: Union[float, np.ndarray],
    segment_lengths: Union[float, torch.Tensor],
    segment_widths: Union[float, torch.Tensor],
    segment_orientations: Union[float, torch.Tensor],
    ax: matplotlib.axes.Axes,
    facecolor: str = None,
    edgecolor: str = None,
    alpha: float = None,
) -> None:
    facecolor = "xkcd:goldenrod"
    edgecolor = "xkcd:black"
    alpha = 0.5
    for x, y, length, width, orientation in zip(
        x_coords,
        y_coords,
        segment_lengths,
        segment_widths,
        segment_orientations,
    ):
        # method1: from waymax using hatch as diagonals
        points = get_corners_polygon(x, y, length, width, orientation)

        p = Polygon(
            points,
            facecolor=facecolor,
            edgecolor=edgecolor,
            linewidth=0,
            alpha=alpha,
            hatch=r"//",
            zorder=2,
        )

        ax.add_patch(p)

    pass


def plot_stop_sign(
    point: np.ndarray,
    ax: matplotlib.axes.Axes,
    radius: float = None,
    facecolor: str = None,
    edgecolor: str = None,
    linewidth: float = None,
    alpha: float = None,
) -> None:
    # Default configurations for the stop sign
    facecolor = "#c04000" if facecolor is None else facecolor
    edgecolor = "white" if edgecolor is None else edgecolor
    linewidth = 1.5 if linewidth is None else linewidth
    radius = 1.0 if radius is None else radius
    alpha = 1.0 if alpha is None else alpha

    point = np.array(point).reshape(-1)

    p = RegularPolygon(
        point,
        numVertices=6,  # For hexagonal stop sign
        radius=radius,
        facecolor=facecolor,
        edgecolor=edgecolor,
        linewidth=linewidth,
        alpha=alpha,
        zorder=2,
    )
    ax.add_patch(p)


def plot_crosswalk(
    points,
    ax: plt.Axes = None,
    facecolor: str = None,
    edgecolor: str = None,
    alpha: float = None,
):
    if ax is None:
        ax = plt.gca()
    # override default config
    facecolor = (
        crosswalk_config["facecolor"] if facecolor is None else facecolor
    )
    edgecolor = (
        crosswalk_config["edgecolor"] if edgecolor is None else edgecolor
    )
    alpha = crosswalk_config["alpha"] if alpha is None else alpha

    p = Polygon(
        points,
        facecolor=facecolor,
        edgecolor=edgecolor,
        linewidth=2,
        alpha=alpha,
        hatch=r"//",
        zorder=1,
    )

    ax.add_patch(p)


def plot_numpy_bounding_boxes_multiple_policy(
    ax: matplotlib.axes.Axes,
    bboxes_s: List[np.ndarray],
    colors: List[np.ndarray],
    alpha: Optional[float] = 1.0,
    line_width_scale: float = 1.5,
    as_center_pts: bool = False,
    label: Optional[str] = None,
) -> None:
    """Plots multiple bounding boxes.

    Args:
      ax: Fig handles.
      bboxes_s: Shape (num_policies,bboxes)
      bboxes: Shape (num_bbox, 5), with last dimension as (x, y, length, width,
        yaw).
      colors: (num_policies,color)
      color: Shape (3,), represents RGB color for drawing.
      alpha: Alpha value for drawing, i.e. 0 means fully transparent.
      as_center_pts: If set to True, bboxes will be drawn as center points,
        instead of full bboxes.
      label: String, represents the meaning of the color for different boxes.
    """

    for bboxes,color in zip(bboxes_s,colors):
        if bboxes.ndim != 2 or bboxes.shape[1] != 5:
            raise ValueError(
                (
                    "Expect bboxes rank 2, last dimension of bbox 5"
                    " got{}, {}, {} respectively"
                ).format(bboxes.ndim, bboxes.shape[1], color.shape)
            )

        if as_center_pts:
            ax.plot(
                bboxes[:, 0],
                bboxes[:, 1],
                "o",
                color=color,
                ms=2,
                alpha=alpha,
                linewidth=1.7 * line_width_scale,
                label=label,
            )
        else:
            c = np.cos(bboxes[:, 4])
            s = np.sin(bboxes[:, 4])
            pt = np.array((bboxes[:, 0], bboxes[:, 1]))  # (2, N)
            length, width = bboxes[:, 2], bboxes[:, 3]
            u = np.array((c, s))
            ut = np.array((s, -c))

            # Compute box corner coordinates.
            tl = pt + length / 2 * u - width / 2 * ut
            tr = pt + length / 2 * u + width / 2 * ut
            br = pt - length / 2 * u + width / 2 * ut
            bl = pt - length / 2 * u - width / 2 * ut

            # Compute heading arrow using center left/right/front.
            cl = pt - width / 2 * ut
            cr = pt + width / 2 * ut
            cf = pt + length / 2 * u

            # Draw bboxes.
            ax.plot(
                [tl[0, :], tr[0, :], br[0, :], bl[0, :], tl[0, :]],
                [tl[1, :], tr[1, :], br[1, :], bl[1, :], tl[1, :]],
                color=color,
                zorder=4,
                linewidth=1.7 * line_width_scale,
                alpha=alpha,
                label=label,
            )

            # Draw heading arrow.
            ax.plot(
                [cl[0, :], cr[0, :], cf[0, :], cl[0, :]],
                [cl[1, :], cr[1, :], cf[1, :], cl[1, :]],
                color=color,
                zorder=4,
                alpha=alpha,
                linewidth=1.5 * line_width_scale,
                label=label,
            )