-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make TopDownMultiChannel support ScenarioEnv #498
Changes from 6 commits
739fd07
3eac8b2
44ef311
94e1482
eda5586
2f141be
7854fac
75a8f4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -5,14 +5,23 @@ | |||
import numpy as np | ||||
|
||||
from metadrive.component.vehicle.base_vehicle import BaseVehicle | ||||
from metadrive.scenario.scenario_description import ScenarioDescription | ||||
from metadrive.utils.interpolating_line import InterpolatingLine | ||||
from metadrive.component.lane.point_lane import PointLane | ||||
from metadrive.constants import Decoration, DEFAULT_AGENT | ||||
from metadrive.obs.top_down_obs import TopDownObservation | ||||
from metadrive.obs.top_down_obs_impl import WorldSurface, COLOR_BLACK, VehicleGraphics, LaneGraphics, \ | ||||
ObservationWindowMultiChannel | ||||
from metadrive.utils import import_pygame, clip | ||||
|
||||
from metadrive.component.road_network.node_road_network import NodeRoadNetwork | ||||
from metadrive.component.vehicle_navigation_module.node_network_navigation import NodeNetworkNavigation | ||||
from metadrive.component.vehicle_navigation_module.edge_network_navigation import EdgeNetworkNavigation | ||||
from metadrive.component.vehicle_navigation_module.trajectory_navigation import TrajectoryNavigation | ||||
|
||||
pygame = import_pygame() | ||||
COLOR_WHITE = pygame.Color("white") | ||||
DEFAULT_TRAJECTORY_LANE_WIDTH = 5 | ||||
|
||||
|
||||
class TopDownMultiChannel(TopDownObservation): | ||||
|
@@ -106,16 +115,30 @@ def draw_map(self) -> pygame.Surface: | |||
self.canvas_background.move_display_window_to(centering_pos) | ||||
self.canvas_road_network.move_display_window_to(centering_pos) | ||||
|
||||
# self.draw_navigation(self.canvas_navigation) | ||||
self.draw_navigation(self.canvas_background, (64, 64, 64)) | ||||
if isinstance(self.target_vehicle.navigation, NodeNetworkNavigation): | ||||
self.draw_navigation_node(self.canvas_background, (64, 64, 64)) | ||||
elif isinstance(self.target_vehicle.navigation, EdgeNetworkNavigation): | ||||
# TODO: draw edge network navigation | ||||
pass | ||||
elif isinstance(self.target_vehicle.navigation, TrajectoryNavigation): | ||||
self.draw_navigation_trajectory(self.canvas_background, (64, 64, 64)) | ||||
|
||||
if isinstance(self.road_network, NodeRoadNetwork): | ||||
for _from in self.road_network.graph.keys(): | ||||
decoration = True if _from == Decoration.start else False | ||||
for _to in self.road_network.graph[_from].keys(): | ||||
for l in self.road_network.graph[_from][_to]: | ||||
two_side = True if l is self.road_network.graph[_from][_to][-1] or decoration else False | ||||
LaneGraphics.LANE_LINE_WIDTH = 0.5 | ||||
LaneGraphics.display(l, self.canvas_background, two_side) | ||||
elif hasattr(self.engine, "map_manager"): | ||||
for data in self.engine.map_manager.current_map.blocks[-1].map_data.values(): | ||||
if ScenarioDescription.POLYLINE in data: | ||||
LaneGraphics.display_scenario( | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't remember what the figure drawn with this code looks like, so I am not sure if
It gives you filled polygons to show maps and lines to represent lines like this: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||
InterpolatingLine(data[ScenarioDescription.POLYLINE]), data.get("type", None), | ||||
self.canvas_background | ||||
) | ||||
|
||||
for _from in self.road_network.graph.keys(): | ||||
decoration = True if _from == Decoration.start else False | ||||
for _to in self.road_network.graph[_from].keys(): | ||||
for l in self.road_network.graph[_from][_to]: | ||||
two_side = True if l is self.road_network.graph[_from][_to][-1] or decoration else False | ||||
LaneGraphics.LANE_LINE_WIDTH = 0.5 | ||||
LaneGraphics.display(l, self.canvas_background, two_side) | ||||
self.canvas_road_network.blit(self.canvas_background, (0, 0)) | ||||
self.obs_window.reset(self.canvas_runtime) | ||||
self._should_draw_map = False | ||||
|
@@ -142,7 +165,13 @@ def draw_scene(self): | |||
ego_heading = vehicle.heading_theta | ||||
ego_heading = ego_heading if abs(ego_heading) > 2 * np.pi / 180 else 0 | ||||
|
||||
for v in self.engine.traffic_manager.vehicles: | ||||
vehicles = [] | ||||
if hasattr(self.engine, "traffic_manager"): | ||||
vehicles = self.engine.traffic_manager.vehicles | ||||
elif hasattr(self.engine, "scenario_traffic_manager"): | ||||
vehicles = self.engine.scenario_traffic_manager.vehicles | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I said before, let's draw all other things besides vehicles. A suggestion here is that we can actually cancel this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||||
|
||||
for v in vehicles: | ||||
if v is vehicle: | ||||
continue | ||||
h = v.heading_theta | ||||
|
@@ -256,13 +285,17 @@ def observe(self, vehicle: BaseVehicle): | |||
img = np.clip(img, 0, 255) | ||||
return np.transpose(img, (1, 0, 2)) | ||||
|
||||
def draw_navigation(self, canvas, color=(128, 128, 128)): | ||||
def draw_navigation_node(self, canvas, color=(128, 128, 128)): | ||||
checkpoints = self.target_vehicle.navigation.checkpoints | ||||
for i, c in enumerate(checkpoints[:-1]): | ||||
lanes = self.road_network.graph[c][checkpoints[i + 1]] | ||||
for lane in lanes: | ||||
LaneGraphics.draw_drivable_area(lane, canvas, color=color) | ||||
|
||||
def draw_navigation_trajectory(self, canvas, color=(128, 128, 128)): | ||||
lane = PointLane(self.target_vehicle.navigation.checkpoints, DEFAULT_TRAJECTORY_LANE_WIDTH) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is already an ego car trajectory in the simulator called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense! |
||||
LaneGraphics.draw_drivable_area(lane, canvas, color=color) | ||||
|
||||
def _get_stack_indices(self, length, frame_skip=None): | ||||
frame_skip = frame_skip or self.frame_skip | ||||
num = int(math.ceil(length / frame_skip)) | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there are more objects in a traffic scenario now, drawing only vehicles is not enough. It would be better to draw all objects including cones, barriers, pedestrians, and bikes. So I suggest drawing all objects like what we did in the Top-Down renderer. This can be done by accessing all objects via
engine._spawned_objects
orengine.get_objects()
and draw them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have done so now!