import numpy as np from matplotlib.lines import Line2D def rotz(t): """Rotation about the z-axis.""" c = np.cos(t) s = np.sin(t) return np.array([[c, -s, 0], [s, c, 0], [0, 0, 1]]) def draw_box_and_pc(points, boxes, names=None): import matplotlib.pyplot as plt _, ax = plt.subplots(1, 1, figsize=(9, 9)) axes_limit = 40 # points: LiDAR points with shape [-1, 3] viz_points = points dists = np.sqrt(np.sum(viz_points[:, :2] ** 2, axis=1)) colors = np.minimum(1, dists / axes_limit / np.sqrt(2)) # prepare color_map points_color = np.array([[245, 150, 100]]).reshape(1, 3).repeat(points.shape[0], 0) points_color = points_color / 255.0 point_scale = 0.2 scatter = ax.scatter(viz_points[:, 0], viz_points[:, 1], c=points_color, s=point_scale) ax.plot(0, 0, "x", color="red") ax.set_xlim(-axes_limit, axes_limit) ax.set_ylim(-axes_limit, axes_limit) if names is None: draw_box_3d(boxes, ax) else: color_map = { "vehicle": "green", "bicycle": "blue", "pedestrian": "red", "traffic_cone": "darkred", "barrier": "peru", "czone_sign": "pink", "generic_object": "black", } colors = [color_map[name] for name in names] draw_box_3d(boxes, ax, linestyle="--", colors=colors) ax.axis("off") ax.set_aspect("equal") plt.savefig("dbg/dbg.png", bbox_inches="tight", pad_inches=0, dpi=200) def draw_box_3d(boxes, ax, linestyle="--", colors="cyan"): for o in range(len(boxes)): color = colors[o] obj = boxes[o] c_x, c_y, c_z, l, w, h, rz = obj[0], obj[1], obj[2], obj[3], obj[4], obj[5], obj[6] R = rotz(rz) # 3d bounding box corners x_corners = [l / 2, l / 2, -l / 2, -l / 2, l / 2, l / 2, -l / 2, -l / 2] y_corners = [w / 2, -w / 2, -w / 2, w / 2, w / 2, -w / 2, -w / 2, w / 2] z_corners = [h, h, h, h, 0, 0, 0, 0] # rotate and translate 3d bounding box corners_3d = np.dot(R, np.vstack([x_corners, y_corners, z_corners])) corners_3d[0, :] = corners_3d[0, :] + c_x corners_3d[1, :] = corners_3d[1, :] + c_y corners_3d[2, :] = corners_3d[2, :] + c_z corners_3d_velo = np.transpose(corners_3d) x1, x2, x3, x4 = corners_3d_velo[0:4, 0] y1, y2, y3, y4 = corners_3d_velo[0:4, 1] polygon = np.zeros([5, 2], dtype=np.float32) polygon[0, 0] = x1 polygon[1, 0] = x2 polygon[2, 0] = x3 polygon[3, 0] = x4 polygon[4, 0] = x1 polygon[0, 1] = y1 polygon[1, 1] = y2 polygon[2, 1] = y3 polygon[3, 1] = y4 polygon[4, 1] = y1 line1 = [(x1, y1), (x2, y2)] line2 = [(x2, y2), (x3, y3)] line3 = [(x3, y3), (x4, y4)] line4 = [(x4, y4), (x1, y1)] (line1_xs, line1_ys) = zip(*line1) (line2_xs, line2_ys) = zip(*line2) (line3_xs, line3_ys) = zip(*line3) (line4_xs, line4_ys) = zip(*line4) ax.add_line(Line2D(line1_xs, line1_ys, linewidth=1, linestyle=linestyle, color=color)) ax.add_line(Line2D(line2_xs, line2_ys, linewidth=1, linestyle=linestyle, color=color)) ax.add_line(Line2D(line3_xs, line3_ys, linewidth=1, linestyle=linestyle, color=color)) ax.add_line(Line2D(line4_xs, line4_ys, linewidth=1, linestyle=linestyle, color=color))