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