VIPRA Documentation
Loading...
Searching...
No Matches
goals.hpp
1#pragma once
2
3#include <algorithm>
4
5#include "vipra/geometry/f3d.hpp"
6
7#include "vipra/logging/logging.hpp"
8
9#include "vipra/macros/goals.hpp"
10#include "vipra/macros/performance.hpp"
11
12#include "vipra/modules/map.hpp"
13#include "vipra/modules/pedestrians.hpp"
14#include "vipra/random/random.hpp"
15
16#include "vipra/types/float.hpp"
17#include "vipra/types/idx.hpp"
18#include "vipra/types/size.hpp"
19#include "vipra/types/time.hpp"
20
21namespace VIPRA::Modules {
26class Goals {
27 public:
28 // FORWARD_REGISTER_PARAMS;
29
30 virtual VIPRA_GOALS_INIT_STEP = 0;
31 virtual VIPRA_GOALS_UPDATE_STEP = 0;
32 virtual VIPRA_GOALS_NEXT_GOAL = 0;
33 virtual VIPRA_GOALS_CHANGE_GOAL = 0;
34 virtual VIPRA_GOALS_RESET = 0;
35
36 void initialize(VIPRA::Modules::Pedestrians const& pedset,
38 {
39 assert(pedset.num_pedestrians() > 0);
40
41 VIPRA::Log::debug("Initializing Goals");
42
43 VIPRA::size const pedCnt = pedset.num_pedestrians();
44
45 _currentGoals.resize(pedCnt);
46 _endGoals.resize(pedCnt);
47 _timeSinceLastGoal = std::vector<VIPRA::f_pnt>(pedCnt, 0);
48 _met = std::vector<bool>(pedCnt, false);
49
50 init_step(pedset, map, engine);
51
52 assert(_timeSinceLastGoal.size() == pedCnt);
53 assert(_currentGoals.size() == pedCnt);
54 assert(_endGoals.size() == pedCnt);
55
56 VIPRA::Log::debug("Goals Initialized");
57 }
58
59 void update(Modules::Pedestrians const& pedset, Modules::Map const& map,
60 VIPRA::delta_t deltaT)
61 {
62 assert(pedset.num_pedestrians() > 0);
63
64 for ( VIPRA::idx pedIdx = 0; pedIdx < pedset.num_pedestrians(); ++pedIdx ) {
65 if ( _met[pedIdx] ) continue;
66
67 auto const pos = pedset.ped_coords(pedIdx);
68 _timeSinceLastGoal[pedIdx] += deltaT;
69
70 if ( pos.distance_to(end_goal(pedIdx)) < MIN_GOAL_DIST ) {
71 _met[pedIdx] = true;
72 }
73 else {
74 if ( pos.distance_to(current_goal(pedIdx)) < MIN_GOAL_DIST ) {
75 if ( next_goal(pedIdx, pedset, map, deltaT) ) _met[pedIdx] = true;
76 _timeSinceLastGoal[pedIdx] = 0.0F;
77 }
78 }
79 }
80
81 update_step(pedset, map, deltaT);
82 }
83
84 void change_end_goal(VIPRA::idx pedIdx, VIPRA::f3d currPos, VIPRA::f3d goalPos,
86 {
87 assert(pedIdx < _endGoals.size());
88 set_end_goal(pedIdx, goalPos);
89 set_goal_met(pedIdx, false);
90 return change_end_goal_impl(pedIdx, currPos, goalPos, engine);
91 }
92
93 [[nodiscard]] VIPRA_INLINE auto current_goals() const -> const VIPRA::f3dVec&
94 {
95 return _currentGoals;
96 }
97
98 [[nodiscard]] VIPRA_INLINE auto end_goals() const -> const VIPRA::f3dVec&
99 {
100 return _endGoals;
101 }
102
103 [[nodiscard]] VIPRA_INLINE auto current_goal(VIPRA::idx pedIdx) const
104 -> VIPRA::f3d const&
105 {
106 return _currentGoals[pedIdx];
107 }
108
109 [[nodiscard]] VIPRA_INLINE auto end_goal(VIPRA::idx pedIdx) const -> VIPRA::f3d const&
110 {
111 return _endGoals[pedIdx];
112 }
113
114 [[nodiscard]] VIPRA_INLINE auto is_goal_met(VIPRA::idx pedIdx) const -> bool
115 {
116 return _met[pedIdx];
117 }
118
119 [[nodiscard]] VIPRA_INLINE auto is_sim_goal_met() const -> bool
120 {
121 return std::all_of(_met.begin(), _met.end(), [](bool met) { return met; });
122 }
123
124 [[nodiscard]] VIPRA_INLINE auto time_since_last_goal(VIPRA::idx pedIdx) const
125 -> VIPRA::f_pnt
126 {
127 assert(_timeSinceLastGoal.size() > pedIdx);
128 return _timeSinceLastGoal[pedIdx];
129 }
130
131 private:
132 VIPRA::f3dVec _currentGoals;
133 VIPRA::f3dVec _endGoals;
134 std::vector<VIPRA::f_pnt> _timeSinceLastGoal;
135
136 std::vector<bool> _met;
137
138 static constexpr VIPRA::f_pnt MIN_GOAL_DIST = 0.05;
139
140 protected:
141 VIPRA_INLINE void set_goal_met(VIPRA::idx pedIdx, bool met) { _met[pedIdx] = met; }
142 VIPRA_INLINE void set_end_goal(VIPRA::idx pedIdx, VIPRA::f3d pos)
143 {
144 _endGoals[pedIdx] = pos;
145 }
146 VIPRA_INLINE void set_current_goal(VIPRA::idx pedIdx, VIPRA::f3d pos)
147 {
148 _currentGoals[pedIdx] = pos;
149 }
150
151 public:
152 Goals() = default;
153 Goals(const Goals&) = default;
154 Goals(Goals&&) noexcept = default;
155 auto operator=(const Goals&) -> Goals& = default;
156 auto operator=(Goals&&) noexcept -> Goals& = default;
157 virtual ~Goals() = default;
158};
159} // namespace VIPRA::Modules
static VIPRA_INLINE void debug(fmt::format_string< param_ts... > message, param_ts &&... params)
Calls the provided Logger with Level DEBUG.
Definition logging.hpp:85
Base Map Module Class.
Definition map.hpp:23
Base Pedestrians module.
Definition pedestrians.hpp:35
Psuedo Random number engine.
Definition random.hpp:22
Definition f3d.hpp:27