37 virtual VIPRA_PEDS_INIT_STEP = 0;
38 virtual VIPRA_PEDS_UPDATE_STEP = 0;
39 virtual VIPRA_PEDS_RESET = 0;
41 [[nodiscard]]
virtual auto all_neighbors_within(
42 VIPRA::idx pedIdx, VIPRA::f_pnt radius)
const -> std::vector<VIPRA::idx> = 0;
44 [[nodiscard]]
virtual auto closest_ped(VIPRA::idx pedIdx)
const -> VIPRA::idx = 0;
46 [[nodiscard]]
virtual auto conditional_closest_ped(
48 std::function<
bool(VIPRA::idx)>
const& condition)
const -> VIPRA::idx = 0;
50 VIPRA_MODULE_TYPE(Pedestrians);
52 VIPRA_REGISTER_BASE_PARAMS(VIPRA_PARAM(
"random_count", _randomPedCnt),
53 VIPRA_PARAM(
"spawn_random", _randomSpawn),
54 VIPRA_PARAM(
"use_file", _useFile))
60 if ( ! (_randomSpawn || _useFile) )
61 VIPRA_BASE_MODULE_ERROR(
63 "Random spawning AND Using the pedestrian file input is turned off "
64 "in parameters (Cannot Add any "
66 "one needs to be true)");
68 init_specific_peds(input);
70 if ( _randomSpawn && _randomPedCnt > 0 ) {
71 init_random_peds(map, engine);
74 if ( _coords.size() == 0 )
75 VIPRA_BASE_MODULE_ERROR(
"Pedestrians",
"No Pedestrians added to simulation");
77 assert(_coords.size() > 0);
78 assert(_coords.size() == _velocities.size());
80 init_step(map, engine);
94 for ( VIPRA::idx pedIdx = 0; pedIdx < state.size(); ++pedIdx ) {
95 _coords[pedIdx] = state.positions[pedIdx];
96 _velocities[pedIdx] = state.velocities[pedIdx];
100 [[nodiscard]]
auto distance(VIPRA::idx firstPed,
101 VIPRA::idx secondPed)
const -> VIPRA::f_pnt
103 return _coords[firstPed].distance_to(_coords[secondPed]);
106 [[nodiscard]]
auto num_pedestrians()
const -> VIPRA::size {
return _coords.size(); }
108 [[nodiscard]]
auto ped_coords(VIPRA::idx pedIdx)
const ->
VIPRA::f3d const&
110 assert(pedIdx < _coords.size());
112 return _coords[pedIdx];
115 [[nodiscard]]
auto all_coords()
const -> std::vector<VIPRA::f3d>
const&
120 [[nodiscard]]
auto ped_velocity(VIPRA::idx pedIdx)
const ->
VIPRA::f3d const&
122 assert(pedIdx < _velocities.size());
123 return _velocities[pedIdx];
126 [[nodiscard]]
auto all_velocities()
const -> std::vector<VIPRA::f3d>
const&
132 VIPRA::f3dVec _coords;
133 VIPRA::f3dVec _velocities;
135 size_t _randomPedCnt{0};
136 bool _randomSpawn{
false};
140 void set_velocities(VIPRA::f3dVec
const& velocities) { _velocities = velocities; }
141 void set_velocities(VIPRA::f3dVec&& velocities)
noexcept { _velocities = velocities; }
143 void set_coordinates(VIPRA::f3dVec
const& coordinates) { _coords = coordinates; }
144 void set_coordinates(VIPRA::f3dVec&& coordinates)
noexcept { _coords = coordinates; }
146 [[nodiscard]]
auto get_velocities()
const -> VIPRA::f3dVec
const&
150 [[nodiscard]]
auto get_coordinates()
const -> VIPRA::f3dVec
const& {
return _coords; }
155 assert(_coords.size() == _velocities.size());
157 auto const& spawnAreas = map.get_spawns();
158 if ( spawnAreas.size() == 0 ) {
160 "{} Pedestrians were set to spawn randomly, but no spawn areas were "
161 "provided in the map",
166 size_t startSize = _coords.size();
167 _coords.resize(_coords.size() + _randomPedCnt);
168 _velocities.resize(_velocities.size() + _randomPedCnt);
170 std::uniform_int_distribution<size_t> polyDist{0, spawnAreas.size() - 1};
173 for (
size_t i = startSize; i < _coords.size(); ++i ) {
174 VIPRA::idx spawnIdx = polyDist(engine);
176 _coords[i] = find_random_point(spawnAreas[spawnIdx], map, engine);
179 VIPRA::Log::debug(
"{} pedestrians added randomly to {} spawn areas", _randomPedCnt,
185 if ( ! _useFile )
return;
187 auto coords = input.get_pedestrians();
188 if ( ! (coords || _randomSpawn) )
189 VIPRA_BASE_MODULE_ERROR(
191 "Could not find pedestrian coordinates in input file and random "
192 "spawning is turned off (No "
193 "Pedestrians Added)");
197 "Pedestrians were set to be loaded from a file, but no coordinates "
202 set_coordinates(std::move(*coords));
203 set_velocities(std::vector<VIPRA::f3d>(_coords.size()));
210 constexpr size_t MAX_RETRIES = 100;
211 constexpr VIPRA::f_pnt CLOSEST_DIST = 0.3;
214 f3d point = polygon.random_point(engine);
216 point = polygon.random_point(engine);
218 if ( retries > MAX_RETRIES ) {
219 VIPRA_BASE_MODULE_ERROR(
221 "Unable to obtain a point in the spawn area centered at ({}, {}). "
222 "Make sure all spawn areas are outside of obstacles.",
223 polygon.center().x, polygon.center().y);
231 virtual ~Pedestrians() =
default;
232 Pedestrians() =
default;
233 Pedestrians(Pedestrians
const&) =
default;
234 Pedestrians(Pedestrians&&)
noexcept =
default;
235 auto operator=(Pedestrians
const&) -> Pedestrians& =
default;
236 auto operator=(Pedestrians&&)
noexcept -> Pedestrians& =
default;