Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2017 Fraunhofer ESK
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Karsten Roscher <karsten.roscher@esk.fraunhofer.de>
*/
#include "traci-roi-manager.h"
#include <algorithm>
#include <vector>
#include <ns3/assert.h>
#include <ns3/log.h>
#include <ns3/simulator.h>
NS_LOG_COMPONENT_DEFINE("TraCiRoiManager");
namespace ns3 {
namespace traci {
RoiManager::RoiManager () : m_roi (0, 0, 0, 0)
{
}
void
RoiManager::SetRegionOfInterest (const Region& roi)
{
m_roi = roi;
}
bool
RoiManager::HasRegionOfInterest () const
{
return IsValidRegion (m_roi);
}
void
RoiManager::AddVehicle (Ptr<Vehicle> vehicle)
{
NS_ASSERT (vehicle);
m_outsideRoi.emplace(vehicle->GetId (), ObservedVehicle (vehicle));
}
void
RoiManager::RemoveVehicle (std::string vehicleId)
{
NS_LOG_FUNCTION (this << vehicleId);
// remove from outside map
m_outsideRoi.erase (vehicleId);
// check if in inside map, remove and notify
auto it = m_insideRoi.find (vehicleId);
if (it != m_insideRoi.end ())
{
auto v = it->second.vehicle;
m_insideRoi.erase (it);
if (m_onLeave)
{
NS_LOG_LOGIC ("Vehicle leaving ROI (removal): " << vehicleId);
m_onLeave (v);
}
else
{
NS_LOG_DEBUG ("'leave' function not set");
}
}
}
void
RoiManager::RemoveVehicle (Ptr<Vehicle> vehicle)
{
NS_ASSERT (vehicle);
RemoveVehicle (vehicle->GetId ());
}
void
RoiManager::CheckForUpdates ()
{
if (!IsValidRegion (m_roi))
{
UpdateWithoutRegion ();
}
else
{
UpdateWithRegion ();
}
}
void
RoiManager::SetRoiEnterSignal (StateUpdateSignal onEnter)
{
m_onEnter = onEnter;
}
void
RoiManager::SetRoiLeaveSignal (StateUpdateSignal onLeave)
{
m_onLeave = onLeave;
}
void
RoiManager::UpdateWithoutRegion ()
{
// simply add everything from the outside map
if (m_onEnter)
{
for (auto const& v : m_outsideRoi)
{
m_onEnter (v.second.vehicle);
}
}
else
{
NS_LOG_DEBUG ("'enter' function not set");
}
// copy contents
m_insideRoi.insert (m_outsideRoi.begin (), m_outsideRoi.end ());
// clear map
m_outsideRoi.clear ();
}
void
RoiManager::UpdateWithRegion ()
{
NS_ASSERT (IsValidRegion (m_roi));
auto now = Simulator::Now ();
std::vector<Ptr<Vehicle>> vehiclesEntered;
std::vector<Ptr<Vehicle>> vehiclesLeft;
NS_LOG_LOGIC ("Checking vehicles previously outside ROI");
for (auto it = m_outsideRoi.begin (); it != m_outsideRoi.end (); )
{
if (now >= it->second.nextCheck)
{
auto const& vehicle = it->second.vehicle;
auto pos = vehicle->GetPosition ();
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
if (m_roi.IsInside(pos))
{
NS_LOG_LOGIC ("Vehicle entered ROI: " << it->first);
vehiclesEntered.push_back (vehicle);
it = m_outsideRoi.erase (it);
}
else
{
NS_LOG_LOGIC ("Vehicle is still not in ROI: " << it->first);
// calculate distance to ROI
auto distanceToRoi = DistanceToRegion (pos, m_roi);
auto vMax = vehicle->GetMaxSpeed ();
double minTimeToRoi = distanceToRoi / vMax;
Time timeToCheck = now + Seconds (minTimeToRoi);
NS_LOG_LOGIC ("distanceToRoi=" << distanceToRoi
<< ", vMax=" << vMax
<< ", minTimeToRoi=" << minTimeToRoi
<< ", timeToCheck=" << timeToCheck);
it->second.nextCheck = timeToCheck;
++it;
}
}
else
{
NS_LOG_LOGIC ("Skipping (not due): " << it->first);
++it;
}
}
NS_LOG_LOGIC ("Checking vehicles previously inside ROI");
for (auto it = m_insideRoi.begin (); it != m_insideRoi.end (); )
{
auto const& vehicle = it->second.vehicle;
auto pos = vehicle->GetPosition ();
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
if (!m_roi.IsInside(pos))
{
NS_LOG_LOGIC ("Vehicle left ROI: " << it->first);
vehiclesLeft.push_back (vehicle);
it = m_insideRoi.erase (it);
}
else
{
NS_LOG_LOGIC ("Vehicle is still in ROI: " << it->first);
++it;
}
}
NS_LOG_DEBUG ("entered=" << vehiclesEntered.size () <<
", left=" << vehiclesLeft.size ());
// call signals and reenter the vehicles in their new destination maps
for (auto const& v : vehiclesEntered)
{
if (m_onEnter)
{
m_onEnter (v);
}
m_insideRoi.emplace (v->GetId (), ObservedVehicle (v));
}
for (auto const& v : vehiclesLeft)
{
if (m_onLeave)
{
m_onLeave (v);
}
m_outsideRoi.emplace (v->GetId (), ObservedVehicle (v));
}
}
RoiManager::ObservedVehicle::ObservedVehicle (Ptr<Vehicle> v)
: vehicle (v), nextCheck (Simulator::Now ())
{
}
bool
IsValidRegion (const RoiManager::Region& region)
{
return (region.xMin < region.xMax) && (region.yMin < region.yMax);
}
double
DistanceToRegion (const Vector& position, const RoiManager::Region& region)
{
double dx = std::max(
std::max<double>(region.xMin - position.x, 0),
position.x - region.xMax);
double dy = std::max(
std::max<double>(region.yMin - position.y, 0),
position.y - region.yMax);
return std::sqrt(dx * dx + dy * dy);
}
} // namespace traci
} // namespace ns3