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