diff --git a/metadrive/component/pgblock/pg_block.py b/metadrive/component/pgblock/pg_block.py index c2f1af836..07266de14 100644 --- a/metadrive/component/pgblock/pg_block.py +++ b/metadrive/component/pgblock/pg_block.py @@ -22,6 +22,7 @@ class PGBlockSocket: Positive_road is right road, and Negative road is left road on which cars drive in reverse direction BlockSocket is a part of block used to connect other blocks """ + def __init__(self, positive_road: Road, negative_road: Road = None): self.positive_road = positive_road self.negative_road = negative_road if negative_road else None @@ -74,16 +75,17 @@ class PGBlock(BaseBlock): When single-direction block created, road_2 in block socket is useless. But it's helpful when a town is created. """ + def __init__( - self, - block_index: int, - pre_block_socket: PGBlockSocket, - global_network: NodeRoadNetwork, - random_seed, - ignore_intersection_checking=False, - remove_negative_lanes=False, - side_lane_line_type=None, - center_line_type=None, + self, + block_index: int, + pre_block_socket: PGBlockSocket, + global_network: NodeRoadNetwork, + random_seed, + ignore_intersection_checking=False, + remove_negative_lanes=False, + side_lane_line_type=None, + center_line_type=None, ): # Specify the lane line type @@ -250,7 +252,6 @@ def create_in_world(self): for _from, to_dict in graph.items(): for _to, lanes in to_dict.items(): for _id, lane in enumerate(lanes): - self._construct_lane(lane, (_from, _to, _id)) # choose_side is a two-elemental list, the first element is for left side, @@ -278,24 +279,14 @@ def _construct_broken_line(self, lane, lateral, line_color, line_type): ) self._node_path_list.extend(node_path_list) - def _construct_continuous_line(self, lane, lateral, line_color, line_type): + def _construct_continuous_line(self, points, line_color, line_type): """ We process straight line to several pieces by default, which can be optimized through overriding this function Lateral: left[-1/2 * width] or right[1/2 * width] """ - segment_num = int(lane.length / PGDrivableAreaProperty.LANE_SEGMENT_LENGTH) - if segment_num == 0: - start = lane.position(0, lateral) - end = lane.position(lane.length, lateral) - node_path_list = self._construct_lane_line_segment(start, end, line_color, line_type) - self._node_path_list.extend(node_path_list) - for segment in range(segment_num): - start = lane.position(PGDrivableAreaProperty.LANE_SEGMENT_LENGTH * segment, lateral) - if segment == segment_num - 1: - end = lane.position(lane.length, lateral) - else: - end = lane.position((segment + 1) * PGDrivableAreaProperty.LANE_SEGMENT_LENGTH, lateral) - node_path_list = self._construct_lane_line_segment(start, end, line_color, line_type) + for p_1_index, p_1 in enumerate(points[:-1]): + p_2 = points[p_1_index + 1] + node_path_list = self._construct_lane_line_segment(p_1, p_2, line_color, line_type) self._node_path_list.extend(node_path_list) def _generate_sidewalk_from_line(self, lane, sidewalk_height=None, lateral_direction=1): @@ -345,16 +336,17 @@ def _construct_lane_line_in_block(self, lane, construct_left_right=(True, True)) for idx, line_type, line_color, need, in zip([-1, 1], lane.line_types, lane.line_colors, construct_left_right): if not need: continue - lateral = idx * lane.width_at(0) / 2 + seg_len = PGDrivableAreaProperty.LANE_SEGMENT_LENGTH + lateral = idx * lane.width / 2 if line_type == PGLineType.CONTINUOUS: - self._construct_continuous_line(lane, lateral, line_color, line_type) + self._construct_continuous_line(lane.get_polyline(seg_len, lateral=lateral), line_color, line_type) elif line_type == PGLineType.BROKEN: self._construct_broken_line(lane, lateral, line_color, line_type) elif line_type == PGLineType.SIDE: - self._construct_continuous_line(lane, lateral, line_color, line_type) + self._construct_continuous_line(lane.get_polyline(seg_len, lateral=lateral), line_color, line_type) self._generate_sidewalk_from_line(lane) elif line_type == PGLineType.GUARDRAIL: - self._construct_continuous_line(lane, lateral, line_color, line_type) + self._construct_continuous_line(lane.get_polyline(seg_len, lateral=lateral), line_color, line_type) self._generate_sidewalk_from_line( lane, sidewalk_height=PGDrivableAreaProperty.GUARDRAIL_HEIGHT, lateral_direction=idx )