def __init__(self, scene, counter, position=Point([0, 0]), index=-1, color=None): """ Initializes the pedestrian :param scene: Scene instance for the pedestrian to walk in :param counter: Number of pedestrians spawned before. :param position: Current position of the pedestrian. Position [0,0] will create a randomized position :param index: index in the arrays (index <= counter) :return: Pedestrian instance """ self.scene = scene self.counter = counter if index < 0: self.index = counter else: self.index = index self.position = position self.color = color if not self.color: self.color = self._get_random_color() while self.position.is_zero() and type(self) == Pedestrian: new_position = scene.size.random_internal_point() if scene.is_accessible(new_position, at_start=True): self.position = new_position self.origin = self.position
def remove_pedestrian(self, pedestrian): """ Removing a pedestrian now puts him in a new position in the scene :param pedestrian: Unremoved pedestrian :return: """ new_point = Point([pedestrian.position.x, self.size.height - 1]) pedestrian.position = new_point
def test_initial_potential_respects_exits(self): mesh = self.dyn_plan.potential_field.mesh_grid for i, j in np.ndindex(self.dyn_plan.grid_dimension): interface_val = self.dyn_plan.initial_interface[(i, j)] is_in_exit = any([ Point([mesh[0][(i, j)], mesh[1][(i, j)]]) in exit for exit in self.scene.exit_list ]) assert (interface_val == 0) == is_in_exit
def __init__(self): self.config = SimulationManager.get_default_config() self.config['general']['obstacle_file'] = empty_scene_file self.scene = Scene(config=self.config, initial_pedestrian_number=1) self.dyn_plan = DynamicPlanner(self.scene, self.config) self.pedestrian = self.scene.pedestrian_list[0] self.ped_x = np.random.randint(3, self.scene.size.width - 2) self.ped_y = np.random.randint(3, self.scene.size.height - 2) self.pedestrian.position = Point([self.ped_x, self.ped_y]) self.pedestrian.velocity = Velocity([1, -1])
def _init_pedestrians(self, initial_pedestrian_number): self.pedestrian_list = [] for counter in range(initial_pedestrian_number): ped_loc = None while not ped_loc: ped_loc = Point(self.size.array * [ np.random.rand(), 1 - np.random.rand() * (1 - self.barrier) ]) if not self.is_accessible(ped_loc, True): ped_loc = None self.pedestrian_list.append( Pedestrian(self, counter, self.exit_list, position=ped_loc))
def _provide_information(self, event): """ When assigned to a button by tkinter, prints a live summary of the present pedestrians to the screen. :param event: Event instance passed by tkinter :return: None """ x, y = (event.x / self.size[0], 1 - event.y / self.size[1]) scene_point = Point([x * self.scene.size[0], y * self.scene.size[1]]) ft.log("Mouse location: %s" % scene_point) for pedestrian in self.scene.pedestrian_list: print(pedestrian) for obstacle in self.scene.obstacle_list: print(obstacle)
def _init_pedestrians(self, initial_pedestrian_number): center = self.size.array * self.impulse_location self.pedestrian_list = [] for counter in range(initial_pedestrian_number): ped_loc = None while not ped_loc: x, y = (np.random.rand(2) * 2 - 1) * self.impulse_size ped_loc = Point(center + np.array([x, y])) if x**2 + y**2 > self.impulse_size**2 or not self.is_accessible( ped_loc): ped_loc = None self.pedestrian_list.append( Pedestrian(self, counter, self.exit_list, position=ped_loc))
def _load_waypoints(self, file_name="None"): """ Not yet used, not yet implemented. Future research :return: """ with open(file_name, 'r') as wp_file: data = json.loads(wp_file.read()) if not 'waypoints' in data: return for entry in data['waypoints']: position = Point(self.scene.size * entry['position']) direction = Velocity([self.potential_x.ev(*position.array), self.potential_y.ev(*position.array)]) * -1 waypoint = Waypoint(self.scene, position, direction) self.waypoints.append(waypoint) self.scene.drawables.append(waypoint) self.waypoint_positions = np.zeros([len(self.waypoints), 2]) self.waypoint_velocities = np.zeros([len(self.waypoints), 2]) for i, waypoint in enumerate(self.waypoints): self.waypoint_positions[i, :] = waypoint.position.array # Waypoints are immutable, so no copy worries self.waypoint_velocities[i, :] = waypoint.direction.array * 20
def test_line_starting_in_corner(self): line = LineSegment([Point([7, 10]), Point([7, 15])]) assert line.begin in self.obs assert line.crosses_obstacle(self.obs) assert not line.crosses_obstacle(self.obs, open_sets=True)
def __init__(self): self.rec1 = (Point([0, 0]), Point([5, 5])) self.rec2 = (Point([4, 4]), Point([6, 6])) self.rec3 = (Point([5, 0]), Point([6, 1])) self.rec4 = (Point([2, 2]), Point([3, 3])) self.rec5 = (Point([2, -1]), Point([4, 6])) self.rec6 = (Point([2, -1]), Point([4, 4]))
def test_boundary_processed_correctly(self): upper_left_corner = Point( [self.dyn_plan.dx / 2, self.scene.size[1] - self.dyn_plan.dy / 2]) self.pedestrian.position = upper_left_corner self.dyn_plan.compute_density_and_velocity_field() assert self.dyn_plan.density_field is not None
def position(self): """ Position getter. Similar to velocity :return: Current position """ return Point(self.scene.position_array[self.index])
def test_line_starting_close_to_corner(self): line = LineSegment([Point([7.01, 10.01]), Point([7, 15])]) assert line.begin not in self.obs assert not line.crosses_obstacle(self.obs)
def test_decreasing_line(self): line = LineSegment([Point([3, 5]), Point([6, 2])]) assert line.begin not in self.obs assert not line.crosses_obstacle(self.obs)
def test_line_far_from_obstacle(self): line = LineSegment([Point([4, 5]), Point([2, 3])]) assert line.begin not in self.obs and line.end not in self.obs assert not line.crosses_obstacle(self.obs)
def test_line_close_to_border(self): line = LineSegment([Point([4, 5]), Point([5, 6.94])]) assert line.begin not in self.obs and line.end not in self.obs assert not line.crosses_obstacle(self.obs)
def test_line_ending_in_corner(self): line = LineSegment([Point([3, 4]), Point([5, 7])]) assert line.end in self.obs assert line.crosses_obstacle(self.obs, open_sets=False) assert not line.crosses_obstacle(self.obs, open_sets=True)
def __init__(self): self.obs = Obstacle(Point([5, 7]), Size([2, 3]), 'test obs')
def test_line_starting_in_obstacle(self): line = LineSegment([Point([6, 8]), Point([7, 15])]) assert line.begin in self.obs assert line.crosses_obstacle(self.obs)
def test_line_passing_through_obstacle(self): line = LineSegment([Point([6, 6]), Point([6, 11])]) assert line.begin not in self.obs and line.end not in self.obs assert line.crosses_obstacle(self.obs)
def test_line_boundaries(self): line = LineSegment([Point([5, 5]), Point([6, 11])]) assert all(line.get_point(0) == line.begin) assert all(line.get_point(1) == line.end)