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.sourceforge.net/
12 // Copyright (C) 2001-2012 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/NBLoadedTLDef.h>
43 #include <netbuild/NBEdge.h>
44 #include <netbuild/NBEdgeCont.h>
46 #include <netbuild/NBLoadedTLDef.h>
47 #include "NIVissimConnection.h"
48 #include "NIVissimDisturbance.h"
49 #include "NIVissimNodeDef.h"
50 #include "NIVissimEdge.h"
51 #include "NIVissimTL.h"
52 
53 #ifdef CHECK_MEMORY_LEAKS
54 #include <foreign/nvwa/debug_new.h>
55 #endif // CHECK_MEMORY_LEAKS
56 // ===========================================================================
57 // used namespaces
58 // ===========================================================================
59 
60 using namespace std;
61 
62 
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.retrievePossiblySplitted(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.retrievePossiblySplitted(
177  toString<int>(c->getFromEdgeID()),
178  toString<int>(c->getToEdgeID()),
179  true);
180  NBEdge* tmpTo = ec.retrievePossiblySplitted(
181  toString<int>(c->getToEdgeID()),
182  toString<int>(c->getFromEdgeID()),
183  false);
184  // check whether the edges are known
185  if (tmpFrom != 0 && tmpTo != 0) {
186  // add connections this signal is responsible for
187  assignedConnections.push_back(NBConnection(tmpFrom, -1, tmpTo, -1));
188  } else {
189  return false;
190  // !!! one of the edges could not be build
191  }
192  }
193  // add to the group
194  assert(myGroupIDs.size() != 0);
195  if (myGroupIDs.size() == 1) {
196  return tl->addToSignalGroup(toString<int>(*(myGroupIDs.begin())),
197  assignedConnections);
198  } else {
199  // !!!
200  return tl->addToSignalGroup(toString<int>(*(myGroupIDs.begin())),
201  assignedConnections);
202  }
203  return true;
204 }
205 
206 
207 
208 
209 
210 
211 
212 
214 
216  int lsaid, int id,
217  const std::string& name,
218  bool isGreenBegin, const std::vector<SUMOReal>& times,
219  SUMOTime tredyellow, SUMOTime tyellow)
220  : myLSA(lsaid), myID(id), myName(name), myTimes(times),
221  myFirstIsRed(!isGreenBegin), myTRedYellow(tredyellow),
222  myTYellow(tyellow) {}
223 
224 
226 
227 
228 bool
231  GroupDictType::iterator i = myDict.find(lsaid);
232  if (i == myDict.end()) {
233  myDict[lsaid] = SGroupDictType();
234  i = myDict.find(lsaid);
235  }
236  SGroupDictType::iterator j = (*i).second.find(id);
237  if (j == (*i).second.end()) {
238  myDict[lsaid][id] = o;
239  return true;
240  }
241  return false;
242  /*
243  GroupDictType::iterator i=myDict.find(id);
244  if(i==myDict.end()) {
245  myDict[id] = o;
246  return true;
247  }
248  return false;
249  */
250 }
251 
252 
255  GroupDictType::iterator i = myDict.find(lsaid);
256  if (i == myDict.end()) {
257  return 0;
258  }
259  SGroupDictType::iterator j = (*i).second.find(id);
260  if (j == (*i).second.end()) {
261  return 0;
262  }
263  return (*j).second;
264 }
265 
266 void
268  for (GroupDictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
269  for (SGroupDictType::iterator j = (*i).second.begin(); j != (*i).second.end(); j++) {
270  delete(*j).second;
271  }
272  }
273  myDict.clear();
274 }
275 
276 
279  GroupDictType::iterator i = myDict.find(tlid);
280  if (i == myDict.end()) {
281  return SGroupDictType();
282  }
283  return (*i).second;
284 }
285 
286 
287 bool
289  // get the color at the begin
290  NBTrafficLightDefinition::TLColor color = myFirstIsRed
292  std::string id = toString<int>(myID);
293  tl->addSignalGroup(id); // !!! myTimes als SUMOTime
294  for (std::vector<SUMOReal>::const_iterator i = myTimes.begin(); i != myTimes.end(); i++) {
295  tl->addSignalGroupPhaseBegin(id, (SUMOTime) *i, color);
298  }
299  if (myTimes.size() == 0) {
300  if (myFirstIsRed) {
302  } else {
304  }
305  }
306  tl->setSignalYellowTimes(id, myTRedYellow, myTYellow);
307  return true;
308 }
309 
310 
311 
312 
313 
314 
315 
316 
318 
319 NIVissimTL::NIVissimTL(int id, const std::string& type,
320  const std::string& name, SUMOTime absdur,
321  SUMOTime offset)
322  : myID(id), myName(name), myAbsDuration(absdur), myOffset(offset),
323  myCurrentGroup(0), myType(type)
324 
325 {}
326 
327 
329 
330 
331 
332 
333 
334 bool
335 NIVissimTL::dictionary(int id, const std::string& type,
336  const std::string& name, SUMOTime absdur,
337  SUMOTime offset) {
338  NIVissimTL* o = new NIVissimTL(id, type, name, absdur, offset);
339  if (!dictionary(id, o)) {
340  delete o;
341  return false;
342  }
343  return true;
344 }
345 
346 bool
348  DictType::iterator i = myDict.find(id);
349  if (i == myDict.end()) {
350  myDict[id] = o;
351  return true;
352  }
353  return false;
354 }
355 
356 
357 NIVissimTL*
359  DictType::iterator i = myDict.find(id);
360  if (i == myDict.end()) {
361  return 0;
362  }
363  return (*i).second;
364 }
365 
366 
367 void
369  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
370  delete(*i).second;
371  }
372  myDict.clear();
373 }
374 
375 
376 
377 
378 
379 bool
381  NBEdgeCont& ec) {
382  size_t ref = 0;
383  size_t ref_groups = 0;
384  size_t ref_signals = 0;
385  size_t no_signals = 0;
386  size_t no_groups = 0;
387  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
388  NIVissimTL* tl = (*i).second;
389  /* if(tl->myType!="festzeit") {
390  cout << " Warning: The traffic light '" << tl->myID
391  << "' could not be assigned to a node." << endl;
392  ref++;
393  continue;
394  }*/
395  std::string id = toString<int>(tl->myID);
396  NBLoadedTLDef* def = new NBLoadedTLDef(id);
397  if (!tlc.insert(def)) {
398  WRITE_ERROR("Error on adding a traffic light\n Must be a multiple id ('" + id + "')");
399  continue;
400  }
401  def->setCycleDuration((unsigned int) tl->myAbsDuration);
402  // add each group to the node's container
404  for (SGroupDictType::const_iterator j = sgs.begin(); j != sgs.end(); j++) {
405  if (!(*j).second->addTo(def)) {
406  WRITE_WARNING("The signal group '" + toString<int>((*j).first) + "' could not be assigned to tl '" + toString<int>(tl->myID) + "'.");
407  ref_groups++;
408  }
409  no_groups++;
410  }
411  // add the signal group signals to the node
413  for (SSignalDictType::const_iterator k = signals.begin(); k != signals.end(); k++) {
414  if (!(*k).second->addTo(ec, def)) {
415  WRITE_WARNING("The signal '" + toString<int>((*k).first) + "' could not be assigned to tl '" + toString<int>(tl->myID) + "'.");
416  ref_signals++;
417  }
418  no_signals++;
419  }
420  }
421  if (ref != 0) {
422  WRITE_WARNING("Could not set " + toString<size_t>(ref) + " of " + toString<size_t>(myDict.size()) + " traffic lights.");
423  }
424  if (ref_groups != 0) {
425  WRITE_WARNING("Could not set " + toString<size_t>(ref_groups) + " of " + toString<size_t>(no_groups) + " groups.");
426  }
427  if (ref_signals != 0) {
428  WRITE_WARNING("Could not set " + toString<size_t>(ref_signals) + " of " + toString<size_t>(no_signals) + " signals.");
429  }
430  return true;
431 
432 }
433 
434 
435 std::string
437  return myType;
438 }
439 
440 
441 int
443  return myID;
444 }
445 
446 
447 
448 /****************************************************************************/
449