File size: 2,530 Bytes
6686473
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""3D scene overlay for the planning stack (toggleable, off by default).



:class:`GuiOverlay` draws directly onto the live PyBullet scene with

``addUserDebugLine``: each object's true footprint hull (red), the A* route on

the surface (green), and the executed U-shaped trajectory (blue). No extra

dependency, and nothing is drawn unless ``--viz`` / ``:viz`` is enabled.

"""

import logging

import pybullet as pb

from . import config

logger = logging.getLogger(__name__)

_DRAW_Z = config.TABLE_TOP_Z + 0.003   # a sliver above the table top


class GuiOverlay:
    """Accumulates PyBullet debug-line items so they can be cleared as a group."""

    def __init__(self):
        self._ids = []

    def clear(self):
        for item in self._ids:
            try:
                pb.removeUserDebugItem(item)
            except Exception:
                pass
        self._ids = []

    # -- primitives ----------------------------------------------------- #
    def _line(self, a, b, color, width=2.0):
        self._ids.append(pb.addUserDebugLine(a, b, color, lineWidth=width))

    def _polygon(self, poly, color, z=None, width=2.0):
        z = _DRAW_Z if z is None else z
        pts = [[x, y, z] for x, y in poly]
        for a, b in zip(pts, pts[1:] + pts[:1]):
            self._line(a, b, color, width)

    # -- high-level draws ----------------------------------------------- #
    def draw_obstacles(self, scene, exclude_ids=None):
        """Red outline of each object's true (mesh convex-hull) footprint."""
        exclude = set(exclude_ids or [])
        for oid in scene.object_ids:
            if oid in exclude:
                continue
            poly = scene.footprint_polygon(oid)
            if len(poly) >= 2:
                self._polygon(poly, [1.0, 0.1, 0.1])

    def draw_path(self, xy_path, color=(0.1, 1.0, 0.1), z=None):
        """Green polyline of the A* route on the surface."""
        z = _DRAW_Z if z is None else z
        pts = [[x, y, z] for x, y in xy_path]
        for a, b in zip(pts[:-1], pts[1:]):
            self._line(a, b, list(color), width=4.0)
        for p in pts:
            self._ids.append(pb.addUserDebugPoints([p], [list(color)], pointSize=6))

    def draw_trajectory(self, waypoints, color=(0.2, 0.4, 1.0)):
        """Blue polyline of the executed U-shaped TCP trajectory."""
        for a, b in zip(waypoints[:-1], waypoints[1:]):
            self._line(list(a), list(b), list(color), width=2.0)