Ejemplo n.º 1
0
    def __init__(self, global_network: RoadNetwork, lane_width: float,
                 lane_num: int, render_root_np: NodePath,
                 pg_physics_world: PGPhysicsWorld, random_seed):
        place_holder = BlockSocket(Road(Decoration.start, Decoration.end),
                                   Road(Decoration.start, Decoration.end))
        super(FirstBlock, self).__init__(0, place_holder, global_network,
                                         random_seed)
        basic_lane = StraightLane([0, lane_width * (lane_num - 1)],
                                  [10, lane_width * (lane_num - 1)],
                                  line_types=(LineType.STRIPED, LineType.SIDE),
                                  width=lane_width)
        ego_v_born_road = Road(self.NODE_1, self.NODE_2)
        CreateRoadFrom(basic_lane, lane_num, ego_v_born_road,
                       self.block_network, self._global_network)
        CreateAdverseRoad(ego_v_born_road, self.block_network,
                          self._global_network)

        next_lane = ExtendStraightLane(basic_lane, 40,
                                       [LineType.STRIPED, LineType.SIDE])
        other_v_born_road = Road(self.NODE_2, self.NODE_3)
        CreateRoadFrom(next_lane, lane_num, other_v_born_road,
                       self.block_network, self._global_network)
        CreateAdverseRoad(other_v_born_road, self.block_network,
                          self._global_network)

        self._create_in_world()
        global_network += self.block_network
        socket = self.create_socket_from_positive_road(other_v_born_road)
        socket.index = 0
        self.add_sockets(socket)
        self.attach_to_pg_world(render_root_np, pg_physics_world)
        self._reborn_roads = [other_v_born_road]
Ejemplo n.º 2
0
    def _try_plug_into_previous_block(self) -> bool:
        parameters = self.get_config()
        basic_lane = self.positive_basic_lane
        lane_num = self.positive_lane_num

        # part 1
        start_node = self._pre_block_socket.positive_road.end_node
        end_node = self.add_road_node()
        positive_road = Road(start_node, end_node)
        length = parameters[Parameter.length]
        direction = parameters[Parameter.dir]
        angle = parameters[Parameter.angle]
        radius = parameters[Parameter.radius]
        curve, straight = sharpbend(basic_lane, length, radius,
                                    np.deg2rad(angle), direction,
                                    basic_lane.width,
                                    (LineType.STRIPED, LineType.SIDE))
        no_cross = CreateRoadFrom(curve, lane_num, positive_road,
                                  self.block_network, self._global_network)
        no_cross = CreateAdverseRoad(positive_road, self.block_network,
                                     self._global_network) and no_cross

        # part 2
        start_node = end_node
        end_node = self.add_road_node()
        positive_road = Road(start_node, end_node)
        no_cross = CreateRoadFrom(straight, lane_num, positive_road,
                                  self.block_network,
                                  self._global_network) and no_cross
        no_cross = CreateAdverseRoad(positive_road, self.block_network,
                                     self._global_network) and no_cross

        # common properties
        self.add_sockets(self.create_socket_from_positive_road(positive_road))
        return no_cross
Ejemplo n.º 3
0
    def __init__(self,
                 global_network: RoadNetwork,
                 lane_width: float,
                 lane_num: int,
                 render_root_np: NodePath,
                 pg_physics_world: PGPhysicsWorld,
                 random_seed,
                 length: float = 50):
        place_holder = BlockSocket(Road(Decoration.start, Decoration.end),
                                   Road(Decoration.start, Decoration.end))
        super(FirstBlock, self).__init__(0, place_holder, global_network,
                                         random_seed)
        assert length > self.ENTRANCE_LENGTH
        basic_lane = StraightLane(
            [0, lane_width * (lane_num - 1)],
            [self.ENTRANCE_LENGTH, lane_width * (lane_num - 1)],
            line_types=(LineType.BROKEN, LineType.SIDE),
            width=lane_width)
        ego_v_spawn_road = Road(self.NODE_1, self.NODE_2)
        CreateRoadFrom(basic_lane, lane_num, ego_v_spawn_road,
                       self.block_network, self._global_network)
        CreateAdverseRoad(ego_v_spawn_road, self.block_network,
                          self._global_network)

        next_lane = ExtendStraightLane(basic_lane,
                                       length - self.ENTRANCE_LENGTH,
                                       [LineType.BROKEN, LineType.SIDE])
        other_v_spawn_road = Road(self.NODE_2, self.NODE_3)
        CreateRoadFrom(next_lane, lane_num, other_v_spawn_road,
                       self.block_network, self._global_network)
        CreateAdverseRoad(other_v_spawn_road, self.block_network,
                          self._global_network)

        self._create_in_world()

        # global_network += self.block_network
        global_network.add(self.block_network)

        socket = self.create_socket_from_positive_road(other_v_spawn_road)
        socket.set_index(self._block_name, 0)

        self.add_sockets(socket)
        self.attach_to_pg_world(render_root_np, pg_physics_world)
        self._respawn_roads = [other_v_spawn_road]
Ejemplo n.º 4
0
 def _try_plug_into_previous_block(self) -> bool:
     para = self.get_config()
     no_cross = True
     attach_road = self._pre_block_socket.positive_road
     for i in range(4):
         exit_road, success = self._create_circular_part(
             attach_road, i, para[Parameter.radius_exit],
             para[Parameter.radius_inner], para[Parameter.angle])
         no_cross = no_cross and success
         if i < 3:
             no_cross = CreateAdverseRoad(exit_road, self.block_network,
                                          self._global_network) and no_cross
             attach_road = -exit_road
     self.add_reborn_roads(
         [socket.negative_road for socket in self._sockets])
     return no_cross
Ejemplo n.º 5
0
    def _try_plug_into_previous_block(self) -> bool:
        self.set_part_idx(0)  # only one part in simple block like straight, and curve
        para = self.get_config()
        length = para[Parameter.length]
        basic_lane = self.positive_basic_lane
        assert isinstance(basic_lane, StraightLane), "Straight road can only connect straight type"
        new_lane = ExtendStraightLane(basic_lane, length, [LineType.STRIPED, LineType.SIDE])
        start = self._pre_block_socket.positive_road.end_node
        end = self.add_road_node()
        socket = Road(start, end)
        _socket = -socket

        # create positive road
        no_cross = CreateRoadFrom(new_lane, self.positive_lane_num, socket, self.block_network, self._global_network)
        # create negative road
        no_cross = CreateAdverseRoad(socket, self.block_network, self._global_network) and no_cross
        self.add_sockets(BlockSocket(socket, _socket))
        return no_cross
Ejemplo n.º 6
0
    def _try_plug_into_previous_block(self) -> bool:
        para = self.get_config()
        decrease_increase = -1 if para[Parameter.decrease_increase] == 0 else 1
        if self.positive_lane_num <= 1:
            decrease_increase = 1
        elif self.positive_lane_num >= 4:
            decrease_increase = -1
        self.lane_num_intersect = self.positive_lane_num + decrease_increase * para[
            Parameter.change_lane_num]
        no_cross = True
        attach_road = self.pre_block_socket.positive_road
        _attach_road = self.pre_block_socket.negative_road
        attach_lanes = attach_road.get_lanes(self._global_network)
        # right straight left node name, rotate it to fit different part
        intersect_nodes = deque([
            self.road_node(0, 0),
            self.road_node(1, 0),
            self.road_node(2, 0), _attach_road.start_node
        ])

        for i in range(4):
            right_lane, success = self._create_part(attach_lanes, attach_road,
                                                    para[Parameter.radius],
                                                    intersect_nodes, i)
            no_cross = no_cross and success
            if i != 3:
                lane_num = self.positive_lane_num if i == 1 else self.lane_num_intersect
                exit_road = Road(self.road_node(i, 0), self.road_node(i, 1))
                no_cross = CreateRoadFrom(right_lane, lane_num, exit_road,
                                          self.block_network,
                                          self._global_network) and no_cross
                no_cross = CreateAdverseRoad(exit_road, self.block_network,
                                             self._global_network) and no_cross
                socket = BlockSocket(exit_road, -exit_road)
                self.add_respawn_roads(socket.negative_road)
                self.add_sockets(socket)
                attach_road = -exit_road
                attach_lanes = attach_road.get_lanes(self.block_network)
        return no_cross
Ejemplo n.º 7
0
    def _try_plug_into_previous_block(self) -> bool:
        acc_lane_len = self.get_config()[Parameter.length]
        no_cross = True
        fork_lane_num = self.get_config()[Parameter.lane_num] + 1

        # extend road and acc raod part, part 0
        self.set_part_idx(0)
        sin_angle = np.sin(np.deg2rad(self.ANGLE))
        cos_angle = np.cos(np.deg2rad(self.ANGLE))
        longitude_len = sin_angle * self.RADIUS * 2 + cos_angle * self.CONNECT_PART_LEN + self.RAMP_LEN

        extend_lane = ExtendStraightLane(
            self.positive_basic_lane, longitude_len + self.EXTRA_PART,
            [LineType.STRIPED, LineType.CONTINUOUS])
        extend_road = Road(self._pre_block_socket.positive_road.end_node,
                           self.add_road_node())
        no_cross = CreateRoadFrom(extend_lane, self.positive_lane_num,
                                  extend_road, self.block_network,
                                  self._global_network) and no_cross
        no_cross = CreateAdverseRoad(extend_road, self.block_network,
                                     self._global_network) and no_cross

        acc_side_lane = ExtendStraightLane(
            extend_lane, acc_lane_len + self.lane_width,
            [LineType.STRIPED, LineType.CONTINUOUS])
        acc_road = Road(extend_road.end_node, self.add_road_node())
        no_cross = CreateRoadFrom(acc_side_lane, self.positive_lane_num,
                                  acc_road, self.block_network,
                                  self._global_network) and no_cross
        no_cross = CreateAdverseRoad(acc_road, self.block_network,
                                     self._global_network) and no_cross
        acc_road.get_lanes(self.block_network)[-1].line_types = [
            LineType.STRIPED, LineType.STRIPED
        ]
        self.add_sockets(Block.create_socket_from_positive_road(acc_road))

        # ramp part, part 1
        self.set_part_idx(1)
        lateral_dist = (1 - cos_angle
                        ) * self.RADIUS * 2 + sin_angle * self.CONNECT_PART_LEN
        end_point = extend_lane.position(self.EXTRA_PART + self.RAMP_LEN,
                                         lateral_dist + self.lane_width)
        start_point = extend_lane.position(self.EXTRA_PART,
                                           lateral_dist + self.lane_width)
        straight_part = StraightLane(start_point,
                                     end_point,
                                     self.lane_width,
                                     self.LANE_TYPE,
                                     speed_limit=self.SPEED_LIMIT)
        straight_road = Road(self.add_road_node(), self.add_road_node())
        self.block_network.add_lane(straight_road.start_node,
                                    straight_road.end_node, straight_part)
        no_cross = (not check_lane_on_road(self._global_network, straight_part,
                                           0.95)) and no_cross
        self.add_reborn_roads(straight_road)

        # p1 road 0, 1
        bend_1, connect_part = sharpbend(straight_part,
                                         self.CONNECT_PART_LEN,
                                         self.RADIUS,
                                         np.deg2rad(self.ANGLE),
                                         False,
                                         self.lane_width,
                                         self.LANE_TYPE,
                                         speed_limit=self.SPEED_LIMIT)
        bend_1_road = Road(straight_road.end_node, self.add_road_node())
        connect_road = Road(bend_1_road.end_node, self.add_road_node())
        self.block_network.add_lane(bend_1_road.start_node,
                                    bend_1_road.end_node, bend_1)
        self.block_network.add_lane(connect_road.start_node,
                                    connect_road.end_node, connect_part)
        no_cross = CreateRoadFrom(bend_1, fork_lane_num, bend_1_road,
                                  self.block_network, self._global_network,
                                  False) and no_cross
        no_cross = CreateRoadFrom(connect_part, fork_lane_num, connect_road,
                                  self.block_network, self._global_network,
                                  False) and no_cross

        # p1, road 2, 3
        bend_2, acc_lane = sharpbend(connect_part,
                                     acc_lane_len,
                                     self.RADIUS,
                                     np.deg2rad(self.ANGLE),
                                     True,
                                     self.lane_width,
                                     self.LANE_TYPE,
                                     speed_limit=self.SPEED_LIMIT)
        acc_lane.line_types = [LineType.STRIPED, LineType.CONTINUOUS]
        bend_2_road = Road(connect_road.end_node, self.road_node(
            0, 0))  # end at part1 road 0, extend road
        acc_road = Road(self.road_node(0, 0), self.road_node(0, 1))
        self.block_network.add_lane(bend_2_road.start_node,
                                    bend_2_road.end_node, bend_2)
        self.block_network.add_lane(acc_road.start_node, acc_road.end_node,
                                    acc_lane)
        no_cross = CreateRoadFrom(bend_2, fork_lane_num, bend_2_road,
                                  self.block_network, self._global_network,
                                  False) and no_cross
        no_cross = CreateRoadFrom(acc_lane, fork_lane_num, acc_road,
                                  self.block_network, self._global_network,
                                  False) and no_cross
        return no_cross
Ejemplo n.º 8
0
    def _try_plug_into_previous_block(self) -> bool:
        no_cross = True
        sin_angle = np.sin(np.deg2rad(self.ANGLE))
        cos_angle = np.cos(np.deg2rad(self.ANGLE))
        longitude_len = sin_angle * self.RADIUS * 2 + cos_angle * self.CONNECT_PART_LEN + self.RAMP_LEN

        self.set_part_idx(0)
        # part 0 road 0
        dec_lane_len = self.PARAMETER_SPACE.sample()[Parameter.length]
        dec_lane = ExtendStraightLane(self.positive_basic_lane,
                                      dec_lane_len + self.lane_width,
                                      [LineType.STRIPED, LineType.CONTINUOUS])
        dec_road = Road(self._pre_block_socket.positive_road.end_node,
                        self.add_road_node())
        no_cross = CreateRoadFrom(dec_lane, self.positive_lane_num, dec_road,
                                  self.block_network,
                                  self._global_network) and no_cross
        no_cross = CreateAdverseRoad(dec_road, self.block_network,
                                     self._global_network) and no_cross
        dec_right_lane = dec_road.get_lanes(self.block_network)[-1]
        dec_right_lane.line_types = [LineType.STRIPED, LineType.STRIPED]

        # part 0 road 1
        extend_lane = ExtendStraightLane(
            dec_right_lane, longitude_len,
            [LineType.STRIPED, LineType.CONTINUOUS])
        extend_road = Road(dec_road.end_node, self.add_road_node())
        no_cross = CreateRoadFrom(extend_lane, self.positive_lane_num,
                                  extend_road, self.block_network,
                                  self._global_network) and no_cross
        no_cross = CreateAdverseRoad(extend_road, self.block_network,
                                     self._global_network) and no_cross
        self.add_sockets(self.create_socket_from_positive_road(extend_road))

        # part 1 road 0
        self.set_part_idx(1)
        dec_side_right_lane = self._get_deacc_lane(dec_right_lane)
        self.block_network.add_lane(dec_road.start_node, self.add_road_node(),
                                    dec_side_right_lane)
        no_cross = (not check_lane_on_road(
            self._global_network, dec_side_right_lane, 0.95)) and no_cross

        bend_1, connect_part = sharpbend(dec_side_right_lane,
                                         self.CONNECT_PART_LEN,
                                         self.RADIUS,
                                         np.deg2rad(self.ANGLE),
                                         True,
                                         self.lane_width,
                                         self.LANE_TYPE,
                                         speed_limit=self.SPEED_LIMIT)
        bend_1_road = Road(self.road_node(1, 0), self.add_road_node())
        connect_road = Road(bend_1_road.end_node, self.add_road_node())
        self.block_network.add_lane(bend_1_road.start_node,
                                    bend_1_road.end_node, bend_1)
        self.block_network.add_lane(connect_road.start_node,
                                    connect_road.end_node, connect_part)
        no_cross = (not check_lane_on_road(self._global_network, bend_1,
                                           0.95)) and no_cross
        no_cross = (not check_lane_on_road(self._global_network, connect_part,
                                           0.95)) and no_cross

        bend_2, straight_part = sharpbend(connect_part,
                                          self.RAMP_LEN,
                                          self.RADIUS,
                                          np.deg2rad(self.ANGLE),
                                          False,
                                          self.lane_width,
                                          self.LANE_TYPE,
                                          speed_limit=self.SPEED_LIMIT)
        bend_2_road = Road(
            connect_road.end_node,
            self.add_road_node())  # end at part1 road 0, extend road
        straight_road = Road(bend_2_road.end_node, self.add_road_node())
        self.block_network.add_lane(bend_2_road.start_node,
                                    bend_2_road.end_node, bend_2)
        self.block_network.add_lane(straight_road.start_node,
                                    straight_road.end_node, straight_part)
        no_cross = (not check_lane_on_road(self._global_network, bend_2,
                                           0.95)) and no_cross
        no_cross = (not check_lane_on_road(self._global_network, straight_part,
                                           0.95)) and no_cross

        decoration_part = self._get_merge_part(dec_side_right_lane)
        self.block_network.add_lane(Decoration.start, Decoration.end,
                                    decoration_part)
        return no_cross
Ejemplo n.º 9
0
    def _try_plug_into_previous_block(self) -> bool:
        no_cross = True
        parameters = self.get_config()
        lane_num_changed = parameters[Parameter.lane_num]

        start_ndoe = self.pre_block_socket.positive_road.end_node
        straight_lane_num = int(self.positive_lane_num - lane_num_changed)
        straight_lane_num = max(1, straight_lane_num)

        circular_lane_num = self.positive_lane_num - straight_lane_num

        # part 1, straight road 0
        basic_lane = self.positive_lanes[straight_lane_num - 1]
        ref_lane = ExtendStraightLane(basic_lane, self.BOTTLENECK_LEN,
                                      [LineType.NONE, LineType.NONE])
        straight_road = Road(start_ndoe, self.road_node(0, 0))
        no_cross = CreateRoadFrom(
            ref_lane,
            straight_lane_num,
            straight_road,
            self.block_network,
            self._global_network,
            center_line_type=LineType.CONTINUOUS,
            side_lane_line_type=LineType.NONE,
            inner_lane_line_type=LineType.NONE) and no_cross
        no_cross = CreateAdverseRoad(
            straight_road,
            self.block_network,
            self._global_network,
            inner_lane_line_type=LineType.NONE,
            side_lane_line_type=LineType.NONE,
            center_line_type=LineType.CONTINUOUS) and no_cross

        # extend for socket ,part 1 road 1
        ref_lane = ExtendStraightLane(ref_lane, parameters[Parameter.length],
                                      [LineType.NONE, LineType.NONE])
        socket_road = Road(self.road_node(0, 0), self.road_node(0, 1))
        no_cross = CreateRoadFrom(
            ref_lane,
            straight_lane_num,
            socket_road,
            self.block_network,
            self._global_network,
            center_line_type=LineType.CONTINUOUS,
            side_lane_line_type=LineType.SIDE,
            inner_lane_line_type=LineType.BROKEN) and no_cross
        no_cross = CreateAdverseRoad(
            socket_road,
            self.block_network,
            self._global_network,
            inner_lane_line_type=LineType.BROKEN,
            side_lane_line_type=LineType.SIDE,
            center_line_type=LineType.CONTINUOUS) and no_cross

        negative_sockect_road = -socket_road
        self.add_sockets(BlockSocket(socket_road, negative_sockect_road))

        # part 2, circular part
        for index, lane in enumerate(self.positive_lanes[straight_lane_num:],
                                     1):
            lateral_dist = index * self.lane_width / 2
            inner_node = self.road_node(1, index)
            side_line_type = LineType.SIDE if index == self.positive_lane_num - straight_lane_num else LineType.NONE

            # positive part
            circular_1, circular_2, _ = create_wave_lanes(
                lane, lateral_dist, self.BOTTLENECK_LEN, 5, self.lane_width)
            road_1 = Road(start_ndoe, inner_node)
            no_cross = CreateRoadFrom(
                circular_1,
                1,
                road_1,
                self.block_network,
                self._global_network,
                center_line_type=LineType.NONE,
                side_lane_line_type=side_line_type,
                inner_lane_line_type=LineType.NONE) and no_cross
            road_2 = Road(inner_node, self.road_node(0, 0))
            no_cross = CreateRoadFrom(
                circular_2,
                1,
                road_2,
                self.block_network,
                self._global_network,
                center_line_type=LineType.NONE,
                side_lane_line_type=side_line_type,
                inner_lane_line_type=LineType.NONE) and no_cross

            # adverse part
            lane = negative_sockect_road.get_lanes(self.block_network)[-1]
            circular_2, circular_1, _ = create_wave_lanes(
                lane, lateral_dist, self.BOTTLENECK_LEN, 5, self.lane_width,
                False)
            road_2 = -road_2
            no_cross = CreateRoadFrom(
                circular_2,
                1,
                road_2,
                self.block_network,
                self._global_network,
                center_line_type=LineType.NONE,
                side_lane_line_type=side_line_type,
                inner_lane_line_type=LineType.NONE) and no_cross

            road_1 = -road_1
            no_cross = CreateRoadFrom(
                circular_1,
                1,
                road_1,
                self.block_network,
                self._global_network,
                center_line_type=LineType.NONE,
                side_lane_line_type=side_line_type,
                inner_lane_line_type=LineType.NONE) and no_cross

        return no_cross
Ejemplo n.º 10
0
    def _try_plug_into_previous_block(self) -> bool:
        self.spawn_roads = []
        self.dest_roads = []

        no_cross = True
        para = self.get_config()
        assert self.positive_lane_num == 1, "Lane number of previous block must be 1 in each direction"

        self.parking_space_length = para[Parameter.length]
        self.parking_space_width = self.lane_width
        parking_space_num = para[Parameter.one_side_vehicle_num]
        # parking_space_num = 10
        radius = para[Parameter.radius]

        main_straight_road_length = 2 * radius + (parking_space_num - 1) * self.parking_space_width
        main_lane = ExtendStraightLane(
            self.positive_lanes[0], main_straight_road_length, [LineType.BROKEN, LineType.NONE]
        )
        road = Road(self.pre_block_socket.positive_road.end_node, self.road_node(0, 0))

        # main straight part
        no_cross = CreateRoadFrom(
            main_lane,
            self.positive_lane_num,
            road,
            self.block_network,
            self._global_network,
            center_line_type=LineType.BROKEN,
            inner_lane_line_type=LineType.BROKEN,
            side_lane_line_type=LineType.NONE,
            center_line_color=LineColor.GREY
        ) and no_cross
        no_cross = CreateAdverseRoad(
            road,
            self.block_network,
            self._global_network,
            center_line_type=LineType.BROKEN,
            inner_lane_line_type=LineType.BROKEN,
            side_lane_line_type=LineType.NONE,
            center_line_color=LineColor.GREY
        ) and no_cross

        # socket part
        parking_lot_out_lane = ExtendStraightLane(main_lane, self.SOCKET_LENGTH, [LineType.BROKEN, LineType.NONE])
        parking_lot_out_road = Road(self.road_node(0, 0), self.road_node(0, 1))

        # out socket part
        no_cross = CreateRoadFrom(
            parking_lot_out_lane,
            self.positive_lane_num,
            parking_lot_out_road,
            self.block_network,
            self._global_network,
            center_line_type=LineType.BROKEN,
            inner_lane_line_type=LineType.BROKEN,
            side_lane_line_type=LineType.SIDE
        ) and no_cross

        no_cross = CreateAdverseRoad(
            parking_lot_out_road,
            self.block_network,
            self._global_network,
            center_line_type=LineType.BROKEN,
            inner_lane_line_type=LineType.BROKEN,
            side_lane_line_type=LineType.SIDE
        ) and no_cross

        socket = self.create_socket_from_positive_road(parking_lot_out_road)
        self.add_sockets(socket)

        # add parking space
        for i in range(int(parking_space_num)):
            no_cross = self._add_one_parking_space(
                copy.copy(self.get_socket_list()[0]).get_socket_in_reverse(),
                self.pre_block_socket.get_socket_in_reverse(), i + 1, radius, i * self.parking_space_width,
                (parking_space_num - i - 1) * self.parking_space_width
            ) and no_cross

        for i in range(parking_space_num, 2 * parking_space_num):
            index = i + 1
            i -= parking_space_num
            no_cross = self._add_one_parking_space(
                self.pre_block_socket, copy.copy(self.get_socket_list()[0]), index, radius,
                i * self.parking_space_width, (parking_space_num - i - 1) * self.parking_space_width
            ) and no_cross

        return no_cross