VIPRA Documentation
Loading...
Searching...
No Matches
timed_latch.hpp
1#pragma once
2
3#include <utility>
4
5#include "vipra/types/time.hpp"
6
7#include "vipra/vipra_behaviors/values/numeric_value.hpp"
8
9namespace VIPRA::Behaviors {
10
15class TimedLatchCollection {
16 public:
17 explicit TimedLatchCollection(Behaviors::NumericValue value)
18 : _duration(std::move(value))
19 {
20 }
21
27 void resize(VIPRA::size latchCnt) { _startTimes.resize(latchCnt, -1); }
28
36 void latch(VIPRA::time_s startTime, VIPRA::idx pedIdx)
37 {
38 if ( _startTimes[pedIdx] == -1 ) {
39 _startTimes[pedIdx] = startTime;
40 }
41 }
42
43 [[nodiscard]] auto size() const -> VIPRA::size { return _startTimes.size(); }
44
53 auto check(VIPRA::time_s currTime, VIPRA::idx pedIdx) -> bool
54 {
55 if ( _startTimes[pedIdx] == -1 ) return false;
56
57 VIPRA::f_pnt val = _duration.value(pedIdx);
58 if ( currTime - _startTimes.at(pedIdx) >= val ) {
59 _startTimes.at(pedIdx) = -1;
60 return false;
61 }
62
63 return true;
64 }
65
72 auto get_duration(VIPRA::idx pedIdx) -> VIPRA::f_pnt { return _duration.value(pedIdx); }
73
74 private:
76 std::vector<VIPRA::time_s> _startTimes;
77
87 static inline constexpr auto in_time_step(VIPRA::time_s currTime,
88 VIPRA::time_s checkTime,
89 VIPRA::delta_t deltaT) -> bool
90 {
91 const VIPRA::delta_t left = checkTime - deltaT;
92 const VIPRA::delta_t right = checkTime + deltaT;
93 return (currTime > left && currTime < right);
94 }
95};
96} // namespace VIPRA::Behaviors
Numeric Values hold runtime VIPRA::f_pnt values taken from Behaviors.
Definition numeric_value.hpp:21
auto check(VIPRA::time_s currTime, VIPRA::idx pedIdx) -> bool
Checks if the latches duration time has passed since it was latched.
Definition timed_latch.hpp:53
void latch(VIPRA::time_s startTime, VIPRA::idx pedIdx)
Sets the latch for pedestrian at pedIdx.
Definition timed_latch.hpp:36
auto get_duration(VIPRA::idx pedIdx) -> VIPRA::f_pnt
Returns the duration for a pedestrians latch.
Definition timed_latch.hpp:72
void resize(VIPRA::size latchCnt)
Sets the size of the container.
Definition timed_latch.hpp:27