Exemple #1
0
    def generate_control_nodes(self, attempts=NUM_UNDO_ATTEMPTS) -> List[Tuple4F]:
        condition = True
        while condition:
            nodes = [self._get_initial_control_node(), self.initial_node]

            # i is the number of valid generated control nodes.
            i = 0

            # When attempt >= attempts and the skeleton of the road is still invalid,
            # the construction of the skeleton starts again from the beginning.
            # attempt is incremented every time the skeleton is invalid.
            attempt = 0

            while i < self.num_control_nodes and attempt <= attempts:
                nodes.append(self._get_next_node(nodes[-2], nodes[-1], self._get_next_max_angle(i)))
                road_polygon = RoadPolygon.from_nodes(nodes)

                # budget is the number of iterations used to attempt to add a valid next control node
                # before also removing the previous control node.
                budget = self.num_control_nodes - i
                assert budget >= 1

                intersect_boundary = self.road_bbox.intersects_boundary(road_polygon.polygons[-1])
                is_valid = road_polygon.is_valid() and (((i==0) and intersect_boundary) or ((i>0) and not intersect_boundary))
                while not is_valid and budget > 0:
                    nodes.pop()
                    budget -= 1
                    attempt += 1

                    nodes.append(self._get_next_node(nodes[-2], nodes[-1], self._get_next_max_angle(i)))
                    road_polygon = RoadPolygon.from_nodes(nodes)

                    intersect_boundary = self.road_bbox.intersects_boundary(road_polygon.polygons[-1])
                    is_valid = road_polygon.is_valid() and (
                                ((i == 0) and intersect_boundary) or ((i > 0) and not intersect_boundary))

                if is_valid:
                    i += 1
                else:
                    assert budget == 0
                    nodes.pop()
                    if len(nodes) > 2:
                        nodes.pop()
                        i -= 1

                assert RoadPolygon.from_nodes(nodes).is_valid()
                assert 0 <= i <= self.num_control_nodes

            # The road generation ends when there are the control nodes plus the two extra nodes needed by the current Catmull-Rom model
            if len(nodes) - 2 == self.num_control_nodes:
                condition = False
        return nodes
 def __init__(self, vehicle: Vehicle, beamng: BeamNGpy, road: DecalRoad):
     cameras = BeamNGCarCameras()
     self.vehicle_state_reader = VehicleStateReader(vehicle, beamng, additional_sensors=cameras.cameras_array)
     self.oob_monitor = OutOfBoundsMonitor(RoadPolygon.from_nodes(road.nodes), self.vehicle_state_reader)
     self.beamng = beamng
     self.road = road
     self.log_folder = 'training_recordings/' + datetime.today().strftime('%Y-%m-%d_%H-%M-%S')
     self.sequence_index = 0
     os.makedirs(self.log_folder, exist_ok=True)
     self.append_line(CSV_header)
        def __init__(self, road_points):
            assert type(road_points) is list, "You must provide a list of road points to create a RoadTest"
            assert all(len(i) == 2 for i in road_points), "Malformed list of road points"
            # The original input
            self.road_points = road_points[:]
            # The interpolated input
            self.interpolated_points = _interpolate(self.road_points)
            # The rendered road
            self.road_polygon = RoadPolygon.from_nodes(self.interpolated_points)

            # At the beginning we do not know whether the test is valid or not
            self.is_valid = None
            self.validation_message = None
Exemple #4
0
 def __init__(self, vehicle: Vehicle, beamng: BeamNGpy, road: DecalRoad,
              params: SimulationParams,
              vehicle_state_reader: VehicleStateReader = None,
              simulation_name: str = None):
     self.vehicle_state_reader = vehicle_state_reader if vehicle_state_reader \
         else VehicleStateReader(vehicle, beamng)
     self.oob_monitor = OutOfBoundsMonitor(RoadPolygon.from_nodes(road.nodes), self.vehicle_state_reader)
     self.beamng: BeamNGpy = beamng
     self.road: DecalRoad = road
     self.params: SimulationParams = params
     self.name = simulation_name
     self.states: SimulationDataRecords = []
     self.simulation_data: SimulationData = SimulationData(simulation_name)
     self.simulation_data.set(self.params, self.road, self.states)
     self.simulation_data.clean()
Exemple #5
0
def plot_road(beam_ng_member: BeamNGMember,
              title="Road",
              show=True,
              save=False,
              folder_path="../data/images",
              file_name="road.jpg",
              format="jpg"):
    fig = plot_road_bbox(beam_ng_member.road_bbox,
                         show=False,
                         fill_color="#76BA1B",
                         border_color="black")
    road_polygon = RoadPolygon.from_nodes(beam_ng_member.sample_nodes)
    fig = plot_road_polygon(road_polygon,
                            fig=fig,
                            title=title,
                            show=show,
                            is_final_road=True,
                            save=save,
                            folder_path=folder_path,
                            file_name=file_name,
                            format=format)
    return fig
 def is_valid(self):
     return (RoadPolygon.from_nodes(self.sample_nodes).is_valid()
             and self.road_bbox.contains(
                 RoadPolygon.from_nodes(self.control_nodes[1:-1])))
Exemple #7
0
 def is_valid(self, control_nodes, sample_nodes, num_spline_nodes):
     return (RoadPolygon.from_nodes(sample_nodes).is_valid()
             and self.road_bbox.contains(
                 RoadPolygon.from_nodes(control_nodes[1:-1])))
Exemple #8
0
    def generate_control_nodes(self,
                               visualise=False,
                               attempts=NUM_UNDO_ATTEMPTS) -> List[Tuple4F]:
        while True:
            nodes = [self._get_initial_control_node(), self.initial_node]

            # i is the number of valid generated control nodes.
            i = 0

            # When attempt >= attempts and the skeleton of the road is still invalid,
            # the construction of the skeleton starts again from the beginning.
            # attempt is incremented every time the skeleton is invalid.
            attempt = 0

            while i < self.num_control_nodes:
                nodes.append(
                    self._get_next_node(nodes[-1],
                                        self._get_next_max_angle(i)))
                road_polygon = RoadPolygon.from_nodes(nodes)

                if visualise:
                    fig = plot_road_bbox(self.road_bbox)
                    plot_road_polygon(road_polygon,
                                      title="RoadPolygon i=%s" % i,
                                      fig=fig)

                # budget is the number of iterations used to attempt to add a valid next control node
                # before also removing the previous control node.
                budget = self.num_control_nodes - i
                assert budget >= 1

                is_valid = road_polygon.is_valid()
                while not is_valid and budget > 0:
                    nodes.pop()
                    budget = budget - 1
                    attempt += 1

                    nodes.append(
                        self._get_next_node(nodes[-1],
                                            self._get_next_max_angle(i)))
                    road_polygon = RoadPolygon.from_nodes(nodes)
                    is_valid = road_polygon.is_valid()

                    if visualise:
                        fig = plot_road_bbox(self.road_bbox)
                        plot_road_polygon(road_polygon,
                                          title="RoadPolygon i=%s" % i,
                                          fig=fig)

                if not is_valid:
                    assert budget == 0
                    nodes.pop()
                    if len(nodes) > 2:
                        nodes.pop()
                        i -= 1
                else:
                    i += 1

                assert RoadPolygon.from_nodes(nodes).is_valid()
                assert 0 <= i <= self.num_control_nodes

                if i >= 2 and self.road_bbox.intersects_boundary(
                        road_polygon.polygons[-1]):
                    break

                if attempt >= attempts:
                    break

            if attempt < attempts:
                return nodes