SimDriver  0.1
StopHorizon.h
1 // Copyright (c) 2020 Institute for Automotive Engineering (ika), RWTH Aachen University. All rights reserved.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 //
21 // Created by Jens Klimke on 2020-04-06.
22 // Contributors:
23 //
24 
25 
26 #ifndef SIMDRIVER_STOPHORIZON_H
27 #define SIMDRIVER_STOPHORIZON_H
28 
29 #include <map>
30 #include <cmath>
31 #include <limits>
32 
33 #ifndef EPS_TIME
34 #define EPS_TIME 1e-6
35 #endif
36 
37 #ifndef EPS_DISTANCE
38 #define EPS_DISTANCE 1e-9
39 #endif
40 
41 namespace agent_model {
42 
43 
44  class StopHorizon {
45 
46  protected:
47 
48  constexpr static const double DELETE_AFTER_DISTANCE = 10.0;
49 
50  struct _StopPoint {
51  double s = INFINITY;
52  double sStart = INFINITY;
53  double timeStartStanding = INFINITY;
54  double standingTime = INFINITY;
55  bool passed = false;
56  };
57 
58  double _sActual = 0.0;
59  std::map<unsigned long, _StopPoint> _elements{};
60 
61 
62  public:
63 
65  struct StopPoint {
66  unsigned long id;
67  double ds;
68  double interval;
69  };
70 
71 
76  void init(double s) {
77 
78  _sActual = s;
79  if (_elements.size() > 0) _elements.clear();
80 
81  }
82 
83 
91  bool addStopPoint(unsigned long id, double sStop, double standingTime) {
92 
93  if(_elements.find(id) != _elements.end() && standingTime == 0) {
94  _elements[id].passed = true;
95  return true;
96  }
97  // only add if not already added
98  if(_elements.find(id) != _elements.end())
99  if (fabs(_elements[id].s - sStop) < 0.5)
100  return false;
101 
102  // only add when distance is large enough
103  if(_sActual - sStop >= DELETE_AFTER_DISTANCE - EPS_DISTANCE)
104  return false;
105 
106  // add to list
107  _elements[id] = {sStop, _sActual, INFINITY, standingTime, false};
108 
109  return true;
110 
111  }
112 
113 
114 
121  bool stopped(unsigned long id, double actualTime) {
122 
123  // only set start time if not set before
124  if(std::isinf(_elements.at(id).timeStartStanding)) {
125 
126  // set start time to actual time
127  _elements.at(id).timeStartStanding = actualTime;
128 
129  // return success
130  return true;
131 
132  }
133 
134  // not set
135  return false;
136 
137  }
138 
139 
145  void update(double actualPosition, double actualTime) {
146 
147  _sActual = actualPosition;
148 
149  // iterate over elements
150  for(auto &ke : _elements) {
151 
152  // get element
153  auto &e = ke.second;
154 
155  // ignore passed stops
156  if(e.passed)
157  continue;
158 
159  // set passed
160  if(actualTime - e.timeStartStanding >= e.standingTime - EPS_TIME)
161  e.passed = true;
162 
163  }
164 
165  // clean up
166  auto it = _elements.cbegin();
167  while(it != _elements.cend()) {
168 
169  // delete elements after passed and distance large
170  if(it->second.passed && _sActual - it->second.s >= DELETE_AFTER_DISTANCE - EPS_DISTANCE)
171  it = _elements.erase(it);
172  else
173  it++;
174 
175  }
176 
177 
178  }
179 
180 
186 
187  // init
188  double dsMin = INFINITY;
189  double interval = INFINITY;
190  unsigned long id = (std::numeric_limits<unsigned long>::max)();
191 
192  // iterate over elements
193  for(auto &ke : _elements) {
194 
195  // get element
196  auto &e = ke.second;
197 
198  // save distance
199  double ds = e.s - _sActual;
200 
201  // ignore
202  if(e.passed || ds > dsMin)
203  continue;
204 
205  // save data
206  dsMin = ds;
207  id = ke.first;
208  interval = e.s - e.sStart;
209 
210  }
211 
212  // return;
213  return StopPoint{id, dsMin, interval};
214 
215  }
216 
217 
218  };
219 
220 
221 }
222 
223 #endif // SIMDRIVER_STOPHORIZON_H
StopPoint getNextStop()
Definition: StopHorizon.h:185
Definition: StopHorizon.h:44
bool stopped(unsigned long id, double actualTime)
Definition: StopHorizon.h:121
Definition: DistanceTimeInterval.h:33
static constexpr const double DELETE_AFTER_DISTANCE
Distance after which the stop point is deleted from the list.
Definition: StopHorizon.h:48
void init(double s)
Definition: StopHorizon.h:76
Definition: StopHorizon.h:50
bool addStopPoint(unsigned long id, double sStop, double standingTime)
Definition: StopHorizon.h:91
void update(double actualPosition, double actualTime)
Definition: StopHorizon.h:145
a struct to store a stop point
Definition: StopHorizon.h:65