From 904bb691f5eaa6c4e2bc07b2f36e5b56ace525a3 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Sun, 1 Nov 2026 11:25:41 -1000 Subject: [PATCH] ui: share ModelRenderer between tizi and mici (#39198) --- .../ui/mici/onroad/model_renderer.py | 252 ++---------------- .../selfdrive/ui/onroad/model_renderer.py | 15 +- 2 files changed, 30 insertions(+), 237 deletions(-) diff --git a/openpilot/selfdrive/ui/mici/onroad/model_renderer.py b/openpilot/selfdrive/ui/mici/onroad/model_renderer.py index 754c86d358..fc5eef0bfe 100644 --- a/openpilot/selfdrive/ui/mici/onroad/model_renderer.py +++ b/openpilot/selfdrive/ui/mici/onroad/model_renderer.py @@ -1,32 +1,18 @@ -import colorsys import numpy as np import pyray as rl -from openpilot.cereal import log, messaging -from opendbc.car.structs import car -from dataclasses import dataclass, field -from openpilot.common.params import Params +from openpilot.cereal import log from openpilot.common.filter_simple import FirstOrderFilter from openpilot.selfdrive.controls.radard import RADAR_TO_CAMERA from openpilot.selfdrive.locationd.calibrationd import HEIGHT_INIT from openpilot.selfdrive.ui.ui_state import ui_state, UIStatus from openpilot.selfdrive.ui.mici.onroad import blend_colors +from openpilot.selfdrive.ui.onroad.model_renderer import ModelRenderer as BaseModelRenderer, CLIP_MARGIN, MAX_DRAW_DISTANCE, STOCK_PATH_GRADIENT from openpilot.system.ui.lib.application import gui_app -from openpilot.system.ui.lib.shader_polygon import draw_polygon, Gradient -from openpilot.system.ui.widgets import Widget +from openpilot.system.ui.lib.shader_polygon import draw_polygon -CLIP_MARGIN = 500 -MIN_DRAW_DISTANCE = 10.0 -MAX_DRAW_DISTANCE = 100.0 LEAD_BAR_LENGTH = 12.0 # px LEAD_BAR_WIDTH = 1.8 # m -STOCK_PATH_GRADIENT = Gradient( - start=(0.0, 1.0), - end=(0.0, 0.0), - colors=[rl.Color(13, 248, 122, 102), rl.Color(114, 255, 92, 89), rl.Color(114, 255, 92, 0)], - stops=[0.0, 0.5, 1.0], -) - LANE_LINE_COLORS = { UIStatus.DISENGAGED: rl.Color(200, 200, 200, 255), UIStatus.OVERRIDE: rl.Color(255, 255, 255, 255), @@ -34,12 +20,6 @@ LANE_LINE_COLORS = { } -@dataclass -class ModelPoints: - raw_points: np.ndarray = field(default_factory=lambda: np.empty((0, 3), dtype=np.float32)) - projected_points: np.ndarray = field(default_factory=lambda: np.empty((0, 2), dtype=np.float32)) - - class LeadVehicle: def __init__(self): self.bar = np.empty((0, 2), dtype=np.float32) @@ -48,43 +28,18 @@ class LeadVehicle: self.fade_filter = FirstOrderFilter(0.0, 0.1, 1 / gui_app.target_fps) -class ModelRenderer(Widget): +class ModelRenderer(BaseModelRenderer): + LANE_LINE_WIDTHS = (0.12, 0.16, 0.16, 0.16) + ROAD_EDGE_WIDTH = 0.16 + def __init__(self): super().__init__() - self._longitudinal_control = False - self._lane_line_probs = np.zeros(4, dtype=np.float32) - self._road_edge_stds = np.zeros(2, dtype=np.float32) - self._lead_vehicles = [LeadVehicle(), LeadVehicle()] - self._path_offset_z = HEIGHT_INIT[0] - - # Initialize ModelPoints objects - self._path = ModelPoints() - self._lane_lines = [ModelPoints() for _ in range(4)] - self._road_edges = [ModelPoints() for _ in range(2)] - self._acceleration_x = np.empty((0,), dtype=np.float32) - + self._lead_bars = [LeadVehicle(), LeadVehicle()] self._torque_filter = FirstOrderFilter(0, 0.1, 1 / gui_app.target_fps) - # 3x3 car space -> rect-origin space (draw methods add rect.x/y) - self._car_space_transform = np.zeros((3, 3), dtype=np.float32) - self._transform_dirty = True - self._clip_region = None - - self._path_gradient = Gradient( - start=(0.0, 1.0), # Bottom of path - end=(0.0, 0.0), # Top of path - colors=[], - stops=[], - ) - - # Get longitudinal control setting from car parameters - if car_params := Params().get("CarParams"): - cp = messaging.log_from_bytes(car_params, car.CarParams) - self._longitudinal_control = cp.openpilotLongitudinalControl - - def set_transform(self, transform: np.ndarray): - self._car_space_transform = transform.astype(np.float32) - self._transform_dirty = True + def _draw_offset(self) -> np.ndarray: + # car space transform maps to rect-origin space + return np.array([self._rect.x, self._rect.y], dtype=np.float32) def _render(self, rect: rl.Rectangle): sm = ui_state.sm @@ -132,26 +87,12 @@ class ModelRenderer(Widget): self._draw_path() if render_lead_indicator: - self._update_leads(sm) + self._update_lead_bars(sm) self._draw_lead_indicator() else: - self._lead_vehicles = [LeadVehicle(), LeadVehicle()] + self._lead_bars = [LeadVehicle(), LeadVehicle()] - def _update_raw_points(self, model): - """Update raw 3D points from model data""" - self._path.raw_points = np.array([model.position.x, model.position.y, model.position.z], dtype=np.float32).T - - for i, lane_line in enumerate(model.laneLines): - self._lane_lines[i].raw_points = np.array([lane_line.x, lane_line.y, lane_line.z], dtype=np.float32).T - - for i, road_edge in enumerate(model.roadEdges): - self._road_edges[i].raw_points = np.array([road_edge.x, road_edge.y, road_edge.z], dtype=np.float32).T - - self._lane_line_probs = np.array(model.laneLineProbs, dtype=np.float32) - self._road_edge_stds = np.array(model.roadEdgeStds, dtype=np.float32) - self._acceleration_x = np.array(model.acceleration.x, dtype=np.float32) - - def _update_leads(self, sm): + def _update_lead_bars(self, sm): plan = sm['longitudinalPlan'] if plan.longitudinalPlanSource == log.LongitudinalPlan.LongitudinalPlanSource.e2e and len(sm['modelV2'].leadsV3) > 1: leads = [(lead.prob > 0.5, lead.x[0], -lead.y[0]) for lead in list(sm['modelV2'].leadsV3)[:2]] @@ -168,7 +109,7 @@ class ModelRenderer(Widget): available = ss.enabled or ss.engageable or cs.brakePressed lane = self._path.raw_points opacity = 0.4 if ui_state.status == UIStatus.DISENGAGED else 0.8 - for lead, (present, d_rel, y_rel) in zip(self._lead_vehicles, leads, strict=True): + for lead, (present, d_rel, y_rel) in zip(self._lead_bars, leads, strict=True): visible = available and present and d_rel < MAX_DRAW_DISTANCE and len(lane) > 0 # snap to a new vehicle instead of sliding over if not visible or abs(y_rel - lead.y_filter.x) > 3.0: @@ -191,77 +132,6 @@ class ModelRenderer(Widget): bar[[1, 2]] = far + (near - far) * np.clip(length, 3.0, LEAD_BAR_LENGTH) / length return bar.astype(np.float32) - def _update_model(self, lead, path_x_array): - """Update model visualization data based on model message""" - max_distance = np.clip(path_x_array[-1], MIN_DRAW_DISTANCE, MAX_DRAW_DISTANCE) - max_idx = self._get_path_length_idx(self._lane_lines[0].raw_points[:, 0], max_distance) - - # Update lane lines using raw points - line_width_factor = 0.12 - for i, lane_line in enumerate(self._lane_lines): - if i in (1, 2): - line_width_factor = 0.16 - lane_line.projected_points = self._map_line_to_polygon( - lane_line.raw_points, line_width_factor * self._lane_line_probs[i], 0.0, max_idx - ) - - # Update road edges using raw points - for road_edge in self._road_edges: - road_edge.projected_points = self._map_line_to_polygon(road_edge.raw_points, line_width_factor, 0.0, max_idx) - - # Update path using raw points - if lead and lead.present: - lead_d = lead.dRel * 2.0 - max_distance = np.clip(lead_d - min(lead_d * 0.35, 10.0), 0.0, max_distance) - - max_idx = self._get_path_length_idx(path_x_array, max_distance) - self._path.projected_points = self._map_line_to_polygon( - self._path.raw_points, 0.9, self._path_offset_z, max_idx, allow_invert=False - ) - - self._update_path_gradient() - - def _update_path_gradient(self): - if not self._longitudinal_control: - return - - path_pts = self._path.projected_points + np.array([self._rect.x, self._rect.y], dtype=np.float32) - max_len = min(len(path_pts) // 2, len(self._acceleration_x)) - - segment_colors = [] - gradient_stops = [] - - i = 0 - while i < max_len: - # Some points (screen space) are out of frame (rect space) - track_y = path_pts[i][1] - if track_y < self._rect.y or track_y > (self._rect.y + self._rect.height): - i += 1 - continue - - # Calculate color based on acceleration (0 is bottom, 1 is top) - lin_grad_point = 1 - (track_y - self._rect.y) / self._rect.height - - # speed up: 120, slow down: 0 - path_hue = np.clip(60 + self._acceleration_x[i] * 35, 0, 120) - - saturation = min(abs(self._acceleration_x[i] * 1.5), 1) - lightness = np.interp(saturation, [0.0, 1.0], [0.95, 0.62]) - alpha = np.interp(lin_grad_point, [0.75 / 2.0, 0.75], [0.4, 0.0]) - - # Use HSL to RGB conversion - color = self._hsla_to_color(path_hue / 360.0, saturation, lightness, alpha) - - gradient_stops.append(lin_grad_point) - segment_colors.append(color) - - # Skip a point, unless next is last - i += 1 + (1 if (i + 2) < max_len else 0) - - # Store the gradient in the path object - self._path_gradient.colors = segment_colors - self._path_gradient.stops = gradient_stops - def _get_ll_color(self, prob: float, adjacent: bool, left: bool): alpha = np.clip(prob, 0.0, 0.7) if adjacent: @@ -287,7 +157,7 @@ class ModelRenderer(Widget): def _draw_lane_lines(self): """Draw lane lines and road edges. Two closest lines should be green (lane line or road edges).""" - offset = np.array([self._rect.x, self._rect.y], dtype=np.float32) + offset = self._draw_offset() for i, lane_line in enumerate(self._lane_lines): if lane_line.projected_points.size == 0: @@ -308,7 +178,7 @@ class ModelRenderer(Widget): if not self._path.projected_points.size: return - path_pts = self._path.projected_points + np.array([self._rect.x, self._rect.y], dtype=np.float32) + path_pts = self._path.projected_points + self._draw_offset() if ui_state.status == UIStatus.DISENGAGED: draw_polygon(self._rect, path_pts, rl.Color(0, 0, 0, 90)) @@ -320,90 +190,6 @@ class ModelRenderer(Widget): draw_polygon(self._rect, path_pts, rl.Color(255, 255, 255, 30)) def _draw_lead_indicator(self): - offset = np.array([self._rect.x, self._rect.y], dtype=np.float32) - for lead in self._lead_vehicles: + offset = self._draw_offset() + for lead in self._lead_bars: draw_polygon(self._rect, lead.bar + offset, rl.Color(255, 255, 255, int(255 * lead.fade_filter.x))) - - @staticmethod - def _get_path_length_idx(pos_x_array: np.ndarray, path_height: float) -> int: - """Get the index corresponding to the given path height""" - if len(pos_x_array) == 0: - return 0 - indices = np.where(pos_x_array <= path_height)[0] - return indices[-1] if indices.size > 0 else 0 - - def _map_line_to_polygon(self, line: np.ndarray, y_off: float, z_off: float, max_idx: int, allow_invert: bool = True) -> np.ndarray: - """Convert 3D line to 2D polygon for rendering.""" - if line.shape[0] == 0: - return np.empty((0, 2), dtype=np.float32) - - # Slice points and filter non-negative x-coordinates - points = line[:max_idx + 1] - points = points[points[:, 0] >= 0] - if points.shape[0] == 0: - return np.empty((0, 2), dtype=np.float32) - - N = points.shape[0] - # Generate left and right 3D points in one array using broadcasting - offsets = np.array([[0, -y_off, z_off], [0, y_off, z_off]], dtype=np.float32) - points_3d = points[None, :, :] + offsets[:, None, :] # Shape: 2xNx3 - points_3d = points_3d.reshape(2 * N, 3) # Shape: (2*N)x3 - - # Transform all points to projected space in one operation - proj = self._car_space_transform @ points_3d.T # Shape: 3x(2*N) - proj = proj.reshape(3, 2, N) - left_proj = proj[:, 0, :] - right_proj = proj[:, 1, :] - - # Filter points where z is sufficiently large - valid_proj = (np.abs(left_proj[2]) >= 1e-6) & (np.abs(right_proj[2]) >= 1e-6) - if not np.any(valid_proj): - return np.empty((0, 2), dtype=np.float32) - - # Compute screen coordinates - left_screen = left_proj[:2, valid_proj] / left_proj[2, valid_proj][None, :] - right_screen = right_proj[:2, valid_proj] / right_proj[2, valid_proj][None, :] - - # Define clip region bounds - clip = self._clip_region - x_min, x_max = clip.x, clip.x + clip.width - y_min, y_max = clip.y, clip.y + clip.height - - # Filter points within clip region - left_in_clip = ( - (left_screen[0] >= x_min) & (left_screen[0] <= x_max) & - (left_screen[1] >= y_min) & (left_screen[1] <= y_max) - ) - right_in_clip = ( - (right_screen[0] >= x_min) & (right_screen[0] <= x_max) & - (right_screen[1] >= y_min) & (right_screen[1] <= y_max) - ) - both_in_clip = left_in_clip & right_in_clip - - if not np.any(both_in_clip): - return np.empty((0, 2), dtype=np.float32) - - # Select valid and clipped points - left_screen = left_screen[:, both_in_clip] - right_screen = right_screen[:, both_in_clip] - - # Handle Y-coordinate inversion on hills - if not allow_invert and left_screen.shape[1] > 1: - y = left_screen[1, :] # y-coordinates - keep = y == np.minimum.accumulate(y) - if not np.any(keep): - return np.empty((0, 2), dtype=np.float32) - left_screen = left_screen[:, keep] - right_screen = right_screen[:, keep] - - return np.vstack((left_screen.T, right_screen[:, ::-1].T)).astype(np.float32) - - @staticmethod - def _hsla_to_color(h, s, l, a): - rgb = colorsys.hls_to_rgb(h, l, s) - return rl.Color( - int(rgb[0] * 255), - int(rgb[1] * 255), - int(rgb[2] * 255), - int(a * 255) - ) diff --git a/openpilot/selfdrive/ui/onroad/model_renderer.py b/openpilot/selfdrive/ui/onroad/model_renderer.py index 6dc7dc51b0..66e8afc1dd 100644 --- a/openpilot/selfdrive/ui/onroad/model_renderer.py +++ b/openpilot/selfdrive/ui/onroad/model_renderer.py @@ -36,6 +36,9 @@ class LeadVehicle: class ModelRenderer(Widget): + LANE_LINE_WIDTHS = (0.025, 0.025, 0.025, 0.025) + ROAD_EDGE_WIDTH = 0.025 + def __init__(self): super().__init__() self._longitudinal_control = False @@ -71,6 +74,9 @@ class ModelRenderer(Widget): self._car_space_transform = transform.astype(np.float32) self._transform_dirty = True + def _draw_offset(self) -> np.ndarray: + return np.zeros(2, dtype=np.float32) + def _render(self, rect: rl.Rectangle): sm = ui_state.sm @@ -156,12 +162,12 @@ class ModelRenderer(Widget): # Update lane lines using raw points for i, lane_line in enumerate(self._lane_lines): lane_line.projected_points = self._map_line_to_polygon( - lane_line.raw_points, 0.025 * self._lane_line_probs[i], 0.0, max_idx, max_distance + lane_line.raw_points, self.LANE_LINE_WIDTHS[i] * self._lane_line_probs[i], 0.0, max_idx, max_distance ) # Update road edges using raw points for road_edge in self._road_edges: - road_edge.projected_points = self._map_line_to_polygon(road_edge.raw_points, 0.025, 0.0, max_idx, max_distance) + road_edge.projected_points = self._map_line_to_polygon(road_edge.raw_points, self.ROAD_EDGE_WIDTH, 0.0, max_idx, max_distance) # Update path using raw points if lead and lead.present: @@ -179,7 +185,8 @@ class ModelRenderer(Widget): if not self._longitudinal_control: return - max_len = min(len(self._path.projected_points) // 2, len(self._acceleration_x)) + path_pts = self._path.projected_points + self._draw_offset() + max_len = min(len(path_pts) // 2, len(self._acceleration_x)) segment_colors = [] gradient_stops = [] @@ -187,7 +194,7 @@ class ModelRenderer(Widget): i = 0 while i < max_len: # Some points (screen space) are out of frame (rect space) - track_y = self._path.projected_points[i][1] + track_y = path_pts[i][1] if track_y < self._rect.y or track_y > (self._rect.y + self._rect.height): i += 1 continue