Eclipse SUMO - Simulation of Urban MObility
NIVissimTL.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // -------------------
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 
26 #include <map>
27 #include <string>
28 #include <cassert>
29 #include <utils/geom/GeomHelper.h>
30 #include <utils/geom/Boundary.h>
32 #include <utils/common/ToString.h>
33 #include "NIVissimConnection.h"
34 #include <netbuild/NBEdge.h>
35 #include <netbuild/NBEdgeCont.h>
37 #include <netbuild/NBLoadedTLDef.h>
38 #include "NIVissimDisturbance.h"
39 #include "NIVissimNodeDef.h"
40 #include "NIVissimEdge.h"
41 #include "NIVissimTL.h"
42 
43 
44 // ===========================================================================
45 // static member variables
46 // ===========================================================================
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
54  const std::string& name,
55  const std::vector<int>& groupids,
56  int edgeid,
57  int laneno,
58  double position,
59  const std::vector<int>& vehicleTypes)
60  : myID(id), myName(name), myGroupIDs(groupids),
61  myEdgeID(edgeid), myLane(laneno), myPosition(position),
62  myVehicleTypes(vehicleTypes) {}
63 
64 
66 
67 bool
69  return poly.around(getPosition());
70 }
71 
72 
75  return NIVissimAbstractEdge::dictionary(myEdgeID)->getGeomPosition(myPosition);
76 }
77 
78 
79 bool
82  SignalDictType::iterator i = myDict.find(lsaid);
83  if (i == myDict.end()) {
84  myDict[lsaid] = SSignalDictType();
85  i = myDict.find(lsaid);
86  }
87  SSignalDictType::iterator j = (*i).second.find(id);
88  if (j == (*i).second.end()) {
89  myDict[lsaid][id] = o;
90  return true;
91  }
92  return false;
93 }
94 
95 
98  SignalDictType::iterator i = myDict.find(lsaid);
99  if (i == myDict.end()) {
100  return nullptr;
101  }
102  SSignalDictType::iterator j = (*i).second.find(id);
103  if (j == (*i).second.end()) {
104  return nullptr;
105  }
106  return (*j).second;
107 }
108 
109 
110 void
112  for (SignalDictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
113  for (SSignalDictType::iterator j = (*i).second.begin(); j != (*i).second.end(); j++) {
114  delete (*j).second;
115  }
116  }
117  myDict.clear();
118 }
119 
120 
123  SignalDictType::iterator i = myDict.find(tlid);
124  if (i == myDict.end()) {
125  return SSignalDictType();
126  }
127  return (*i).second;
128 }
129 
130 
131 bool
134  NBConnectionVector assignedConnections;
135  if (c == nullptr) {
136  // What to do if on an edge? -> close all outgoing connections
137  NBEdge* edge = ec.retrievePossiblySplit(toString<int>(myEdgeID), myPosition);
138  if (edge == nullptr) {
139  WRITE_WARNING("Could not set tls signal at edge '" + toString(myEdgeID) + "' - the edge was not built.");
140  return false;
141  }
142  // Check whether it is already known, which edges are approached
143  // by which lanes
144  // check whether to use the original lanes only
145  if (edge->lanesWereAssigned()) {
146  std::vector<NBEdge::Connection> connections = edge->getConnectionsFromLane(myLane - 1);
147  for (std::vector<NBEdge::Connection>::iterator i = connections.begin(); i != connections.end(); i++) {
148  const NBEdge::Connection& conn = *i;
149  assert(myLane - 1 < (int)edge->getNumLanes());
150  assignedConnections.push_back(NBConnection(edge, myLane - 1, conn.toEdge, conn.toLane));
151  }
152  } else {
153  WRITE_WARNING("Edge : Lanes were not assigned(!)");
154  for (int j = 0; j < edge->getNumLanes(); j++) {
155  std::vector<NBEdge::Connection> connections = edge->getConnectionsFromLane(j);
156  for (std::vector<NBEdge::Connection>::iterator i = connections.begin(); i != connections.end(); i++) {
157  const NBEdge::Connection& conn = *i;
158  assignedConnections.push_back(NBConnection(edge, j, conn.toEdge, conn.toLane));
159  }
160  }
161  }
162  } else {
163  // get the edges
164  NBEdge* tmpFrom = ec.retrievePossiblySplit(toString<int>(c->getFromEdgeID()), toString<int>(c->getToEdgeID()), true);
165  NBEdge* tmpTo = ec.retrievePossiblySplit(toString<int>(c->getToEdgeID()), toString<int>(c->getFromEdgeID()), false);
166  // check whether the edges are known
167  if (tmpFrom != nullptr && tmpTo != nullptr) {
168  // add connections this signal is responsible for
169  assignedConnections.push_back(NBConnection(tmpFrom, -1, tmpTo, -1));
170  } else {
171  return false;
172  // !!! one of the edges could not be build
173  }
174  }
175  // add to the group
176  assert(myGroupIDs.size() != 0);
177  // @todo just another hack?!
178  /*
179  if (myGroupIDs.size() == 1) {
180  return tl->addToSignalGroup(toString<int>(*(myGroupIDs.begin())),
181  assignedConnections);
182  } else {
183  // !!!
184  return tl->addToSignalGroup(toString<int>(*(myGroupIDs.begin())),
185  assignedConnections);
186  }
187  */
188  return tl->addToSignalGroup(toString<int>(myGroupIDs.front()), assignedConnections);
189 }
190 
191 
192 
193 
194 
195 
196 
197 
199 
201  int id,
202  const std::string& name,
203  bool isGreenBegin, const std::vector<double>& times,
204  SUMOTime tredyellow, SUMOTime tyellow)
205  : myID(id), myName(name), myTimes(times),
206  myFirstIsRed(!isGreenBegin), myTRedYellow(tredyellow),
207  myTYellow(tyellow) {}
208 
209 
211 
212 
213 bool
216  GroupDictType::iterator i = myDict.find(lsaid);
217  if (i == myDict.end()) {
218  myDict[lsaid] = SGroupDictType();
219  i = myDict.find(lsaid);
220  }
221  SGroupDictType::iterator j = (*i).second.find(id);
222  if (j == (*i).second.end()) {
223  myDict[lsaid][id] = o;
224  return true;
225  }
226  return false;
227  /*
228  GroupDictType::iterator i=myDict.find(id);
229  if(i==myDict.end()) {
230  myDict[id] = o;
231  return true;
232  }
233  return false;
234  */
235 }
236 
237 
240  GroupDictType::iterator i = myDict.find(lsaid);
241  if (i == myDict.end()) {
242  return nullptr;
243  }
244  SGroupDictType::iterator j = (*i).second.find(id);
245  if (j == (*i).second.end()) {
246  return nullptr;
247  }
248  return (*j).second;
249 }
250 
251 void
253  for (GroupDictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
254  for (SGroupDictType::iterator j = (*i).second.begin(); j != (*i).second.end(); j++) {
255  delete (*j).second;
256  }
257  }
258  myDict.clear();
259 }
260 
261 
264  GroupDictType::iterator i = myDict.find(tlid);
265  if (i == myDict.end()) {
266  return SGroupDictType();
267  }
268  return (*i).second;
269 }
270 
271 
272 bool
274  // get the color at the begin
275  NBTrafficLightDefinition::TLColor color = myFirstIsRed
277  std::string id = toString<int>(myID);
278  tl->addSignalGroup(id); // !!! myTimes als SUMOTime
279  for (std::vector<double>::const_iterator i = myTimes.begin(); i != myTimes.end(); i++) {
280  tl->addSignalGroupPhaseBegin(id, (SUMOTime) *i, color);
283  }
284  if (myTimes.size() == 0) {
285  if (myFirstIsRed) {
287  } else {
289  }
290  }
291  tl->setSignalYellowTimes(id, myTRedYellow, myTYellow);
292  return true;
293 }
294 
295 
296 
297 
298 
299 
300 
301 
303 
304 NIVissimTL::NIVissimTL(int id, const std::string& type,
305  const std::string& name, SUMOTime absdur,
306  SUMOTime offset)
307  : myID(id), myName(name), myAbsDuration(absdur), myOffset(offset),
308  myCurrentGroup(nullptr), myType(type)
309 
310 {}
311 
312 
314 
315 
316 
317 
318 
319 bool
320 NIVissimTL::dictionary(int id, const std::string& type,
321  const std::string& name, SUMOTime absdur,
322  SUMOTime offset) {
323  NIVissimTL* o = new NIVissimTL(id, type, name, absdur, offset);
324  if (!dictionary(id, o)) {
325  delete o;
326  return false;
327  }
328  return true;
329 }
330 
331 bool
333  DictType::iterator i = myDict.find(id);
334  if (i == myDict.end()) {
335  myDict[id] = o;
336  return true;
337  }
338  return false;
339 }
340 
341 
342 NIVissimTL*
344  DictType::iterator i = myDict.find(id);
345  if (i == myDict.end()) {
346  return nullptr;
347  }
348  return (*i).second;
349 }
350 
351 
352 void
354  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
355  delete (*i).second;
356  }
357  myDict.clear();
358 }
359 
360 
361 
362 
363 
364 bool
366  NBEdgeCont& ec) {
367  int ref = 0;
368  int ref_groups = 0;
369  int ref_signals = 0;
370  int no_signals = 0;
371  int no_groups = 0;
372  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
373  NIVissimTL* tl = (*i).second;
374  /* if(tl->myType!="festzeit") {
375  cout << " Warning: The traffic light '" << tl->myID
376  << "' could not be assigned to a node." << endl;
377  ref++;
378  continue;
379  }*/
380  std::string id = toString<int>(tl->myID);
381  TrafficLightType type = ((tl->getType() == "festzeit" || tl->getType() == "festzeit_fake") ?
383  NBLoadedTLDef* def = new NBLoadedTLDef(ec, id, 0, type);
384  if (!tlc.insert(def)) {
385  WRITE_ERROR("Error on adding a traffic light\n Must be a multiple id ('" + id + "')");
386  continue;
387  }
388  def->setCycleDuration((int) tl->myAbsDuration);
389  // add each group to the node's container
391  for (SGroupDictType::const_iterator j = sgs.begin(); j != sgs.end(); j++) {
392  if (!(*j).second->addTo(def)) {
393  WRITE_WARNING("The signal group '" + toString<int>((*j).first) + "' could not be assigned to tl '" + toString<int>(tl->myID) + "'.");
394  ref_groups++;
395  }
396  no_groups++;
397  }
398  // add the signal group signals to the node
400  for (SSignalDictType::const_iterator k = signals.begin(); k != signals.end(); k++) {
401  if (!(*k).second->addTo(ec, def)) {
402  WRITE_WARNING("The signal '" + toString<int>((*k).first) + "' could not be assigned to tl '" + toString<int>(tl->myID) + "'.");
403  ref_signals++;
404  }
405  no_signals++;
406  }
407  }
408  if (ref != 0) {
409  WRITE_WARNING("Could not set " + toString<int>(ref) + " of " + toString<int>((int)myDict.size()) + " traffic lights.");
410  }
411  if (ref_groups != 0) {
412  WRITE_WARNING("Could not set " + toString<int>(ref_groups) + " of " + toString<int>(no_groups) + " groups.");
413  }
414  if (ref_signals != 0) {
415  WRITE_WARNING("Could not set " + toString<int>(ref_signals) + " of " + toString<int>(no_signals) + " signals.");
416  }
417  return true;
418 
419 }
420 
421 
422 std::string
424  return myType;
425 }
426 
427 
428 int
430  return myID;
431 }
432 
433 
434 
435 /****************************************************************************/
436 
NIVissimTL::myOffset
SUMOTime myOffset
Definition: NIVissimTL.h:132
Boundary.h
NIVissimTL::myName
std::string myName
Definition: NIVissimTL.h:130
ToString.h
NIVissimTL::myAbsDuration
SUMOTime myAbsDuration
Definition: NIVissimTL.h:131
NBEdge::Connection::toEdge
NBEdge * toEdge
The edge the connections yields in.
Definition: NBEdge.h:212
NIVissimTL::NIVissimTLSignalGroup::NIVissimTLSignalGroup
NIVissimTLSignalGroup(int id, const std::string &name, bool isGreenBegin, const std::vector< double > &times, SUMOTime tredyellow, SUMOTime tyellow)
Definition: NIVissimTL.cpp:200
NIVissimTL::NIVissimTLSignal::getPosition
Position getPosition() const
Definition: NIVissimTL.cpp:74
NIVissimTL::myID
int myID
Definition: NIVissimTL.h:129
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
NIVissimEdge.h
NBEdgeCont
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:60
NBTrafficLightDefinition::TLCOLOR_RED
@ TLCOLOR_RED
Signal shows red.
Definition: NBTrafficLightDefinition.h:79
NIVissimTL::NIVissimTLSignal
Definition: NIVissimTL.h:79
NBTrafficLightLogicCont
A container for traffic light definitions and built programs.
Definition: NBTrafficLightLogicCont.h:57
NIVissimTL::NIVissimTL
NIVissimTL(int id, const std::string &type, const std::string &name, SUMOTime absdur, SUMOTime offset)
Definition: NIVissimTL.cpp:304
NBLoadedTLDef::addSignalGroup
void addSignalGroup(const std::string &id)
Adds a signal group.
Definition: NBLoadedTLDef.cpp:594
TLTYPE_STATIC
@ TLTYPE_STATIC
Definition: SUMOXMLDefinitions.h:1198
MsgHandler.h
NIVissimTL::NIVissimTLSignal::~NIVissimTLSignal
~NIVissimTLSignal()
Definition: NIVissimTL.cpp:65
NIVissimTL::NIVissimTLSignalGroup
Definition: NIVissimTL.h:106
NIVissimTL::NIVissimTLSignal::getSignalsFor
static SSignalDictType getSignalsFor(int tlid)
Definition: NIVissimTL.cpp:122
NBEdge::getConnectionsFromLane
std::vector< Connection > getConnectionsFromLane(int lane, NBEdge *to=nullptr, int toLane=-1) const
Returns connections from a given lane.
Definition: NBEdge.cpp:1100
TrafficLightType
TrafficLightType
Definition: SUMOXMLDefinitions.h:1197
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
NBEdgeCont.h
NIVissimTL::clearDict
static void clearDict()
Definition: NIVissimTL.cpp:353
NBLoadedTLDef::addSignalGroupPhaseBegin
void addSignalGroupPhaseBegin(const std::string &groupid, SUMOTime time, TLColor color)
Sets the information about the begin of a phase.
Definition: NBLoadedTLDef.cpp:601
NIVissimTL::NIVissimTLSignal::dictionary
static bool dictionary(int lsaid, int id, NIVissimTLSignal *o)
Definition: NIVissimTL.cpp:80
PositionVector
A list of positions.
Definition: PositionVector.h:45
NIVissimConnection::getToEdgeID
int getToEdgeID() const
Definition: NIVissimConnection.cpp:162
NBLoadedTLDef::setCycleDuration
void setCycleDuration(int cycleDur)
Sets the duration of a cycle.
Definition: NBLoadedTLDef.cpp:616
NIVissimTL::dictionary
static bool dictionary(int id, const std::string &type, const std::string &name, SUMOTime absdur, SUMOTime offset)
Definition: NIVissimTL.cpp:320
NBEdge
The representation of a single edge during network building.
Definition: NBEdge.h:91
NIVissimTL::dict_SetSignals
static bool dict_SetSignals(NBTrafficLightLogicCont &tlc, NBEdgeCont &ec)
Definition: NIVissimTL.cpp:365
NBEdge::Connection::toLane
int toLane
The lane the connections yields in.
Definition: NBEdge.h:215
NIVissimTL::myCurrentGroup
NIVissimTLSignalGroup * myCurrentGroup
Definition: NIVissimTL.h:133
NBEdge::lanesWereAssigned
bool lanesWereAssigned() const
Check if lanes were assigned.
Definition: NBEdge.cpp:2797
NIVissimTL::getID
int getID() const
Definition: NIVissimTL.cpp:429
NIVissimDisturbance.h
NIVissimNodeDef.h
NIVissimTL::NIVissimTLSignal::addTo
bool addTo(NBEdgeCont &ec, NBLoadedTLDef *node) const
Definition: NIVissimTL.cpp:132
NBTrafficLightDefinition::TLColor
TLColor
An enumeration of possible tl-signal states.
Definition: NBTrafficLightDefinition.h:77
PositionVector::around
bool around(const Position &p, double offset=0) const
Returns the information whether the position vector describes a polygon lying around the given point.
Definition: PositionVector.cpp:74
NBLoadedTLDef.h
NBLoadedTLDef::setSignalYellowTimes
void setSignalYellowTimes(const std::string &groupid, SUMOTime tRedYellow, SUMOTime tYellow)
Sets the times the light is yellow or red/yellow.
Definition: NBLoadedTLDef.cpp:608
NIVissimConnection
Definition: NIVissimConnection.h:47
NBEdge::getNumLanes
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:477
NBEdgeCont::retrievePossiblySplit
NBEdge * retrievePossiblySplit(const std::string &id, bool downstream) const
Tries to retrieve an edge, even if it is splitted.
Definition: NBEdgeCont.cpp:282
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
NIVissimTL::~NIVissimTL
~NIVissimTL()
Definition: NIVissimTL.cpp:313
NIVissimTL::NIVissimTLSignalGroup::dictionary
static bool dictionary(int lsaid, int id, NIVissimTLSignalGroup *o)
Definition: NIVissimTL.cpp:214
NIVissimAbstractEdge::dictionary
static bool dictionary(int id, NIVissimAbstractEdge *e)
Definition: NIVissimAbstractEdge.cpp:58
NBConnection
Definition: NBConnection.h:43
NIVissimTL::NIVissimTLSignalGroup::myDict
static GroupDictType myDict
Definition: NIVissimTL.h:125
NIVissimTL::NIVissimTLSignalGroup::addTo
bool addTo(NBLoadedTLDef *node) const
Definition: NIVissimTL.cpp:273
NIVissimTL::NIVissimTLSignal::NIVissimTLSignal
NIVissimTLSignal(int id, const std::string &name, const std::vector< int > &groupids, int edgeid, int laneno, double position, const std::vector< int > &assignedVehicleTypes)
Definition: NIVissimTL.cpp:53
NIVissimTL
Definition: NIVissimTL.h:49
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
NIVissimTL::NIVissimTLSignal::clearDict
static void clearDict()
Definition: NIVissimTL.cpp:111
NIVissimTL::GroupDictType
std::map< int, SGroupDictType > GroupDictType
Definition: NIVissimTL.h:74
NIVissimConnection.h
NIVissimTL::SignalDictType
std::map< int, SSignalDictType > SignalDictType
Definition: NIVissimTL.h:73
NIVissimTL::DictType
std::map< int, NIVissimTL * > DictType
Definition: NIVissimTL.h:136
NIVissimTL.h
NIVissimTL::NIVissimTLSignal::myDict
static SignalDictType myDict
Definition: NIVissimTL.h:103
NBLoadedTLDef
A loaded (complete) traffic light logic.
Definition: NBLoadedTLDef.h:41
NIVissimTL::SGroupDictType
std::map< int, NIVissimTLSignalGroup * > SGroupDictType
Definition: NIVissimTL.h:72
NBConnectionVector
std::vector< NBConnection > NBConnectionVector
Definition of a connection vector.
Definition: NBConnectionDefs.h:34
NIVissimTL::NIVissimTLSignalGroup::clearDict
static void clearDict()
Definition: NIVissimTL.cpp:252
NIVissimTL::getType
std::string getType() const
Definition: NIVissimTL.cpp:423
NIVissimTL::myDict
static DictType myDict
Definition: NIVissimTL.h:137
config.h
NIVissimConnection::dictionary
static bool dictionary(int id, NIVissimConnection *o)
Definition: NIVissimConnection.cpp:77
GeomHelper.h
NIVissimTL::NIVissimTLSignal::isWithin
bool isWithin(const PositionVector &poly) const
Definition: NIVissimTL.cpp:68
NIVissimConnection::getFromEdgeID
int getFromEdgeID() const
Definition: NIVissimConnection.cpp:156
NIVissimTL::SSignalDictType
std::map< int, NIVissimTLSignal * > SSignalDictType
Definition: NIVissimTL.h:70
NBTrafficLightLogicCont.h
NBEdge::Connection
A structure which describes a connection between edges or lanes.
Definition: NBEdge.h:189
NIVissimTL::NIVissimTLSignalGroup::getGroupsFor
static SGroupDictType getGroupsFor(int tlid)
Definition: NIVissimTL.cpp:263
NBTrafficLightLogicCont::insert
bool insert(NBTrafficLightDefinition *logic, bool forceInsert=false)
Adds a logic definition to the dictionary.
Definition: NBTrafficLightLogicCont.cpp:73
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
NIVissimTL::myType
std::string myType
Definition: NIVissimTL.h:134
NBLoadedTLDef::addToSignalGroup
bool addToSignalGroup(const std::string &groupid, const NBConnection &connection)
Adds a connection to a signal group.
Definition: NBLoadedTLDef.cpp:562
NBEdge.h
TLTYPE_ACTUATED
@ TLTYPE_ACTUATED
Definition: SUMOXMLDefinitions.h:1201
NIVissimTL::NIVissimTLSignalGroup::~NIVissimTLSignalGroup
~NIVissimTLSignalGroup()
Definition: NIVissimTL.cpp:210
NBTrafficLightDefinition::TLCOLOR_GREEN
@ TLCOLOR_GREEN
Signal shows green.
Definition: NBTrafficLightDefinition.h:85