SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NIVissimTL.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // -------------------
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2014 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 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 
55 
56 // ===========================================================================
57 // static member variables
58 // ===========================================================================
60 
61 
62 // ===========================================================================
63 // method definitions
64 // ===========================================================================
66  const std::string& name,
67  const std::vector<int>& groupids,
68  int edgeid,
69  int laneno,
70  SUMOReal position,
71  const std::vector<int>& vehicleTypes)
72  : myLSA(lsaid), myID(id), myName(name), myGroupIDs(groupids),
73  myEdgeID(edgeid), myLane(laneno), myPosition(position),
74  myVehicleTypes(vehicleTypes) {}
75 
76 
78 
79 bool
81  return poly.around(getPosition());
82 }
83 
84 
87  return NIVissimAbstractEdge::dictionary(myEdgeID)->getGeomPosition(myPosition);
88 }
89 
90 
91 bool
94  SignalDictType::iterator i = myDict.find(lsaid);
95  if (i == myDict.end()) {
96  myDict[lsaid] = SSignalDictType();
97  i = myDict.find(lsaid);
98  }
99  SSignalDictType::iterator j = (*i).second.find(id);
100  if (j == (*i).second.end()) {
101  myDict[lsaid][id] = o;
102  return true;
103  }
104  return false;
105 }
106 
107 
110  SignalDictType::iterator i = myDict.find(lsaid);
111  if (i == myDict.end()) {
112  return 0;
113  }
114  SSignalDictType::iterator j = (*i).second.find(id);
115  if (j == (*i).second.end()) {
116  return 0;
117  }
118  return (*j).second;
119 }
120 
121 
122 void
124  for (SignalDictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
125  for (SSignalDictType::iterator j = (*i).second.begin(); j != (*i).second.end(); j++) {
126  delete(*j).second;
127  }
128  }
129  myDict.clear();
130 }
131 
132 
135  SignalDictType::iterator i = myDict.find(tlid);
136  if (i == myDict.end()) {
137  return SSignalDictType();
138  }
139  return (*i).second;
140 }
141 
142 
143 bool
146  NBConnectionVector assignedConnections;
147  if (c == 0) {
148  // What to do if on an edge? -> close all outgoing connections
149  NBEdge* edge = ec.retrievePossiblySplit(toString<int>(myEdgeID), myPosition);
150  if (edge == 0) {
151  WRITE_WARNING("Could not set tls signal at edge '" + toString(myEdgeID) + "' - the edge was not built.");
152  return false;
153  }
154  // Check whether it is already known, which edges are approached
155  // by which lanes
156  // check whether to use the original lanes only
157  if (edge->lanesWereAssigned()) {
158  std::vector<NBEdge::Connection> connections = edge->getConnectionsFromLane(myLane - 1);
159  for (std::vector<NBEdge::Connection>::iterator i = connections.begin(); i != connections.end(); i++) {
160  const NBEdge::Connection& conn = *i;
161  assert(myLane - 1 < (int)edge->getNumLanes());
162  assignedConnections.push_back(NBConnection(edge, myLane - 1, conn.toEdge, conn.toLane));
163  }
164  } else {
165  WRITE_WARNING("Edge : Lanes were not assigned(!)");
166  for (unsigned int j = 0; j < edge->getNumLanes(); j++) {
167  std::vector<NBEdge::Connection> connections = edge->getConnectionsFromLane(j);
168  for (std::vector<NBEdge::Connection>::iterator i = connections.begin(); i != connections.end(); i++) {
169  const NBEdge::Connection& conn = *i;
170  assignedConnections.push_back(NBConnection(edge, j, conn.toEdge, conn.toLane));
171  }
172  }
173  }
174  } else {
175  // get the edges
176  NBEdge* tmpFrom = ec.retrievePossiblySplit(toString<int>(c->getFromEdgeID()), toString<int>(c->getToEdgeID()), true);
177  NBEdge* tmpTo = ec.retrievePossiblySplit(toString<int>(c->getToEdgeID()), toString<int>(c->getFromEdgeID()), false);
178  // check whether the edges are known
179  if (tmpFrom != 0 && tmpTo != 0) {
180  // add connections this signal is responsible for
181  assignedConnections.push_back(NBConnection(tmpFrom, -1, tmpTo, -1));
182  } else {
183  return false;
184  // !!! one of the edges could not be build
185  }
186  }
187  // add to the group
188  assert(myGroupIDs.size() != 0);
189  // @todo just another hack?!
190  /*
191  if (myGroupIDs.size() == 1) {
192  return tl->addToSignalGroup(toString<int>(*(myGroupIDs.begin())),
193  assignedConnections);
194  } else {
195  // !!!
196  return tl->addToSignalGroup(toString<int>(*(myGroupIDs.begin())),
197  assignedConnections);
198  }
199  */
200  return tl->addToSignalGroup(toString<int>(myGroupIDs.front()), assignedConnections);
201 }
202 
203 
204 
205 
206 
207 
208 
209 
211 
213  int lsaid, int id,
214  const std::string& name,
215  bool isGreenBegin, const std::vector<SUMOReal>& times,
216  SUMOTime tredyellow, SUMOTime tyellow)
217  : myLSA(lsaid), myID(id), myName(name), myTimes(times),
218  myFirstIsRed(!isGreenBegin), myTRedYellow(tredyellow),
219  myTYellow(tyellow) {}
220 
221 
223 
224 
225 bool
228  GroupDictType::iterator i = myDict.find(lsaid);
229  if (i == myDict.end()) {
230  myDict[lsaid] = SGroupDictType();
231  i = myDict.find(lsaid);
232  }
233  SGroupDictType::iterator j = (*i).second.find(id);
234  if (j == (*i).second.end()) {
235  myDict[lsaid][id] = o;
236  return true;
237  }
238  return false;
239  /*
240  GroupDictType::iterator i=myDict.find(id);
241  if(i==myDict.end()) {
242  myDict[id] = o;
243  return true;
244  }
245  return false;
246  */
247 }
248 
249 
252  GroupDictType::iterator i = myDict.find(lsaid);
253  if (i == myDict.end()) {
254  return 0;
255  }
256  SGroupDictType::iterator j = (*i).second.find(id);
257  if (j == (*i).second.end()) {
258  return 0;
259  }
260  return (*j).second;
261 }
262 
263 void
265  for (GroupDictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
266  for (SGroupDictType::iterator j = (*i).second.begin(); j != (*i).second.end(); j++) {
267  delete(*j).second;
268  }
269  }
270  myDict.clear();
271 }
272 
273 
276  GroupDictType::iterator i = myDict.find(tlid);
277  if (i == myDict.end()) {
278  return SGroupDictType();
279  }
280  return (*i).second;
281 }
282 
283 
284 bool
286  // get the color at the begin
287  NBTrafficLightDefinition::TLColor color = myFirstIsRed
289  std::string id = toString<int>(myID);
290  tl->addSignalGroup(id); // !!! myTimes als SUMOTime
291  for (std::vector<SUMOReal>::const_iterator i = myTimes.begin(); i != myTimes.end(); i++) {
292  tl->addSignalGroupPhaseBegin(id, (SUMOTime) *i, color);
295  }
296  if (myTimes.size() == 0) {
297  if (myFirstIsRed) {
299  } else {
301  }
302  }
303  tl->setSignalYellowTimes(id, myTRedYellow, myTYellow);
304  return true;
305 }
306 
307 
308 
309 
310 
311 
312 
313 
315 
316 NIVissimTL::NIVissimTL(int id, const std::string& type,
317  const std::string& name, SUMOTime absdur,
318  SUMOTime offset)
319  : myID(id), myName(name), myAbsDuration(absdur), myOffset(offset),
320  myCurrentGroup(0), myType(type)
321 
322 {}
323 
324 
326 
327 
328 
329 
330 
331 bool
332 NIVissimTL::dictionary(int id, const std::string& type,
333  const std::string& name, SUMOTime absdur,
334  SUMOTime offset) {
335  NIVissimTL* o = new NIVissimTL(id, type, name, absdur, offset);
336  if (!dictionary(id, o)) {
337  delete o;
338  return false;
339  }
340  return true;
341 }
342 
343 bool
345  DictType::iterator i = myDict.find(id);
346  if (i == myDict.end()) {
347  myDict[id] = o;
348  return true;
349  }
350  return false;
351 }
352 
353 
354 NIVissimTL*
356  DictType::iterator i = myDict.find(id);
357  if (i == myDict.end()) {
358  return 0;
359  }
360  return (*i).second;
361 }
362 
363 
364 void
366  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
367  delete(*i).second;
368  }
369  myDict.clear();
370 }
371 
372 
373 
374 
375 
376 bool
378  NBEdgeCont& ec) {
379  size_t ref = 0;
380  size_t ref_groups = 0;
381  size_t ref_signals = 0;
382  size_t no_signals = 0;
383  size_t no_groups = 0;
384  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
385  NIVissimTL* tl = (*i).second;
386  /* if(tl->myType!="festzeit") {
387  cout << " Warning: The traffic light '" << tl->myID
388  << "' could not be assigned to a node." << endl;
389  ref++;
390  continue;
391  }*/
392  std::string id = toString<int>(tl->myID);
393  TrafficLightType type = ((tl->getType() == "festzeit" || tl->getType() == "festzeit_fake") ?
395  NBLoadedTLDef* def = new NBLoadedTLDef(id, 0, type);
396  if (!tlc.insert(def)) {
397  WRITE_ERROR("Error on adding a traffic light\n Must be a multiple id ('" + id + "')");
398  continue;
399  }
400  def->setCycleDuration((unsigned int) tl->myAbsDuration);
401  // add each group to the node's container
403  for (SGroupDictType::const_iterator j = sgs.begin(); j != sgs.end(); j++) {
404  if (!(*j).second->addTo(def)) {
405  WRITE_WARNING("The signal group '" + toString<int>((*j).first) + "' could not be assigned to tl '" + toString<int>(tl->myID) + "'.");
406  ref_groups++;
407  }
408  no_groups++;
409  }
410  // add the signal group signals to the node
412  for (SSignalDictType::const_iterator k = signals.begin(); k != signals.end(); k++) {
413  if (!(*k).second->addTo(ec, def)) {
414  WRITE_WARNING("The signal '" + toString<int>((*k).first) + "' could not be assigned to tl '" + toString<int>(tl->myID) + "'.");
415  ref_signals++;
416  }
417  no_signals++;
418  }
419  }
420  if (ref != 0) {
421  WRITE_WARNING("Could not set " + toString<size_t>(ref) + " of " + toString<size_t>(myDict.size()) + " traffic lights.");
422  }
423  if (ref_groups != 0) {
424  WRITE_WARNING("Could not set " + toString<size_t>(ref_groups) + " of " + toString<size_t>(no_groups) + " groups.");
425  }
426  if (ref_signals != 0) {
427  WRITE_WARNING("Could not set " + toString<size_t>(ref_signals) + " of " + toString<size_t>(no_signals) + " signals.");
428  }
429  return true;
430 
431 }
432 
433 
434 std::string
436  return myType;
437 }
438 
439 
440 int
442  return myID;
443 }
444 
445 
446 
447 /****************************************************************************/
448 
A structure which describes a connection between edges or lanes.
Definition: NBEdge.h:148
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:166
NBEdge * retrievePossiblySplit(const std::string &id, bool downstream) const
Tries to retrieve an edge, even if it is splitted.
Definition: NBEdgeCont.cpp:262
NIVissimTLSignalGroup(int lsaid, int id, const std::string &name, bool isGreenBegin, const std::vector< SUMOReal > &times, SUMOTime tredyellow, SUMOTime tyellow)
Definition: NIVissimTL.cpp:212
static bool dictionary(int id, NIVissimAbstractEdge *e)
NIVissimTLSignal(int lsaid, int id, const std::string &name, const std::vector< int > &groupids, int edgeid, int laneno, SUMOReal position, const std::vector< int > &assignedVehicleTypes)
Definition: NIVissimTL.cpp:65
NBEdge * toEdge
The edge the connections yields in.
Definition: NBEdge.h:164
NIVissimTL(int id, const std::string &type, const std::string &name, SUMOTime absdur, SUMOTime offset)
Definition: NIVissimTL.cpp:316
std::string getType() const
Definition: NIVissimTL.cpp:435
std::map< int, NIVissimTL * > DictType
Definition: NIVissimTL.h:142
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:134
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
SUMOTime myOffset
Definition: NIVissimTL.h:138
NIVissimTLSignalGroup * myCurrentGroup
Definition: NIVissimTL.h:139
int getID() const
Definition: NIVissimTL.cpp:441
std::map< int, SSignalDictType > SignalDictType
Definition: NIVissimTL.h:77
TLColor
An enumeration of possible tl-signal states.
bool around(const Position &p, SUMOReal offset=0) const
Returns the information whether the position vector describes a polygon lying around the given point ...
std::vector< Connection > getConnectionsFromLane(unsigned int lane) const
Returns connections from a given lane.
Definition: NBEdge.cpp:726
static bool dictionary(int lsaid, int id, NIVissimTLSignal *o)
Definition: NIVissimTL.cpp:92
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
bool addTo(NBLoadedTLDef *node) const
Definition: NIVissimTL.cpp:285
std::map< int, SGroupDictType > GroupDictType
Definition: NIVissimTL.h:78
std::string myName
Definition: NIVissimTL.h:136
std::map< int, NIVissimTLSignal * > SSignalDictType
Definition: NIVissimTL.h:74
SUMOTime myAbsDuration
Definition: NIVissimTL.h:137
bool addToSignalGroup(const std::string &groupid, const NBConnection &connection)
Adds a connection to a signal group.
static DictType myDict
Definition: NIVissimTL.h:143
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:332
bool lanesWereAssigned() const
Definition: NBEdge.cpp:1713
static bool dictionary(int lsaid, int id, NIVissimTLSignalGroup *o)
Definition: NIVissimTL.cpp:226
unsigned int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:344
static bool dict_SetSignals(NBTrafficLightLogicCont &tlc, NBEdgeCont &ec)
Definition: NIVissimTL.cpp:377
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
std::map< int, NIVissimTLSignalGroup * > SGroupDictType
Definition: NIVissimTL.h:76
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
bool isWithin(const PositionVector &poly) const
Definition: NIVissimTL.cpp:80
std::vector< NBConnection > NBConnectionVector
Definition of a connection vector.
static GroupDictType myDict
Definition: NIVissimTL.h:131
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
static SignalDictType myDict
Definition: NIVissimTL.h:108
void setCycleDuration(unsigned int cycleDur)
Sets the duration of a cycle.
void addSignalGroup(const std::string &id)
Adds a signal group.
std::string myType
Definition: NIVissimTL.h:140
static void clearDict()
Definition: NIVissimTL.cpp:365
static bool dictionary(int id, const std::string &name, const NIVissimExtendedEdgePoint &from_def, const NIVissimExtendedEdgePoint &to_def, const PositionVector &geom, Direction direction, SUMOReal dxnothalt, SUMOReal dxeinordnen, SUMOReal zuschlag1, SUMOReal zuschlag2, SUMOReal seglength, const std::vector< int > &assignedVehicles, const NIVissimClosedLanesVector &clv)
Position getPosition() const
Definition: NIVissimTL.cpp:86
bool insert(NBTrafficLightDefinition *logic, bool forceInsert=false)
Adds a logic definition to the dictionary.
static SGroupDictType getGroupsFor(int tlid)
Definition: NIVissimTL.cpp:275
#define SUMOReal
Definition: config.h:215
bool addTo(NBEdgeCont &ec, NBLoadedTLDef *node) const
Definition: NIVissimTL.cpp:144
TrafficLightType