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-2013 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 // used namespaces
56 // ===========================================================================
57 
58 using namespace std;
59 
60 
62 
64  const std::string& name,
65  const std::vector<int>& groupids,
66  int edgeid,
67  int laneno,
68  SUMOReal position,
69  const std::vector<int>& vehicleTypes)
70  : myLSA(lsaid), myID(id), myName(name), myGroupIDs(groupids),
71  myEdgeID(edgeid), myLane(laneno), myPosition(position),
72  myVehicleTypes(vehicleTypes) {}
73 
74 
76 
77 bool
79  return poly.around(getPosition());
80 }
81 
82 
85  return NIVissimAbstractEdge::dictionary(myEdgeID)->getGeomPosition(myPosition);
86 }
87 
88 
89 bool
92  SignalDictType::iterator i = myDict.find(lsaid);
93  if (i == myDict.end()) {
94  myDict[lsaid] = SSignalDictType();
95  i = myDict.find(lsaid);
96  }
97  SSignalDictType::iterator j = (*i).second.find(id);
98  if (j == (*i).second.end()) {
99  myDict[lsaid][id] = o;
100  return true;
101  }
102  return false;
103 }
104 
105 
108  SignalDictType::iterator i = myDict.find(lsaid);
109  if (i == myDict.end()) {
110  return 0;
111  }
112  SSignalDictType::iterator j = (*i).second.find(id);
113  if (j == (*i).second.end()) {
114  return 0;
115  }
116  return (*j).second;
117 }
118 
119 
120 void
122  for (SignalDictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
123  for (SSignalDictType::iterator j = (*i).second.begin(); j != (*i).second.end(); j++) {
124  delete(*j).second;
125  }
126  }
127  myDict.clear();
128 }
129 
130 
133  SignalDictType::iterator i = myDict.find(tlid);
134  if (i == myDict.end()) {
135  return SSignalDictType();
136  }
137  return (*i).second;
138 }
139 
140 
141 bool
144  NBConnectionVector assignedConnections;
145  if (c == 0) {
146  // What to do if on an edge? -> close all outgoing connections
147  NBEdge* edge = ec.retrievePossiblySplit(toString<int>(myEdgeID), myPosition);
148  if (edge == 0) {
149  WRITE_WARNING("Could not set tls signal at edge '" + toString(myEdgeID) + "' - the edge was not built.");
150  return false;
151  }
152  // Check whether it is already known, which edges are approached
153  // by which lanes
154  // check whether to use the original lanes only
155  if (edge->lanesWereAssigned()) {
156  std::vector<NBEdge::Connection> connections = edge->getConnectionsFromLane(myLane - 1);
157  for (std::vector<NBEdge::Connection>::iterator i = connections.begin(); i != connections.end(); i++) {
158  const NBEdge::Connection& conn = *i;
159  assert(myLane - 1 < (int)edge->getNumLanes());
160  assignedConnections.push_back(NBConnection(edge, myLane - 1, conn.toEdge, conn.toLane));
161  }
162  } else {
163  WRITE_WARNING("Edge : Lanes were not assigned(!)");
164  for (unsigned int j = 0; j < edge->getNumLanes(); j++) {
165  std::vector<NBEdge::Connection> connections = edge->getConnectionsFromLane(j);
166  for (std::vector<NBEdge::Connection>::iterator i = connections.begin(); i != connections.end(); i++) {
167  const NBEdge::Connection& conn = *i;
168  assignedConnections.push_back(NBConnection(edge, j, conn.toEdge, conn.toLane));
169  }
170  }
171  }
172  } else {
173  // get the edges
174  NBEdge* tmpFrom = ec.retrievePossiblySplit(toString<int>(c->getFromEdgeID()), toString<int>(c->getToEdgeID()), true);
175  NBEdge* tmpTo = ec.retrievePossiblySplit(toString<int>(c->getToEdgeID()), toString<int>(c->getFromEdgeID()), false);
176  // check whether the edges are known
177  if (tmpFrom != 0 && tmpTo != 0) {
178  // add connections this signal is responsible for
179  assignedConnections.push_back(NBConnection(tmpFrom, -1, tmpTo, -1));
180  } else {
181  return false;
182  // !!! one of the edges could not be build
183  }
184  }
185  // add to the group
186  assert(myGroupIDs.size() != 0);
187  // @todo just another hack?!
188  /*
189  if (myGroupIDs.size() == 1) {
190  return tl->addToSignalGroup(toString<int>(*(myGroupIDs.begin())),
191  assignedConnections);
192  } else {
193  // !!!
194  return tl->addToSignalGroup(toString<int>(*(myGroupIDs.begin())),
195  assignedConnections);
196  }
197  */
198  return tl->addToSignalGroup(toString<int>(myGroupIDs.front()), assignedConnections);
199 }
200 
201 
202 
203 
204 
205 
206 
207 
209 
211  int lsaid, int id,
212  const std::string& name,
213  bool isGreenBegin, const std::vector<SUMOReal>& times,
214  SUMOTime tredyellow, SUMOTime tyellow)
215  : myLSA(lsaid), myID(id), myName(name), myTimes(times),
216  myFirstIsRed(!isGreenBegin), myTRedYellow(tredyellow),
217  myTYellow(tyellow) {}
218 
219 
221 
222 
223 bool
226  GroupDictType::iterator i = myDict.find(lsaid);
227  if (i == myDict.end()) {
228  myDict[lsaid] = SGroupDictType();
229  i = myDict.find(lsaid);
230  }
231  SGroupDictType::iterator j = (*i).second.find(id);
232  if (j == (*i).second.end()) {
233  myDict[lsaid][id] = o;
234  return true;
235  }
236  return false;
237  /*
238  GroupDictType::iterator i=myDict.find(id);
239  if(i==myDict.end()) {
240  myDict[id] = o;
241  return true;
242  }
243  return false;
244  */
245 }
246 
247 
250  GroupDictType::iterator i = myDict.find(lsaid);
251  if (i == myDict.end()) {
252  return 0;
253  }
254  SGroupDictType::iterator j = (*i).second.find(id);
255  if (j == (*i).second.end()) {
256  return 0;
257  }
258  return (*j).second;
259 }
260 
261 void
263  for (GroupDictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
264  for (SGroupDictType::iterator j = (*i).second.begin(); j != (*i).second.end(); j++) {
265  delete(*j).second;
266  }
267  }
268  myDict.clear();
269 }
270 
271 
274  GroupDictType::iterator i = myDict.find(tlid);
275  if (i == myDict.end()) {
276  return SGroupDictType();
277  }
278  return (*i).second;
279 }
280 
281 
282 bool
284  // get the color at the begin
285  NBTrafficLightDefinition::TLColor color = myFirstIsRed
287  std::string id = toString<int>(myID);
288  tl->addSignalGroup(id); // !!! myTimes als SUMOTime
289  for (std::vector<SUMOReal>::const_iterator i = myTimes.begin(); i != myTimes.end(); i++) {
290  tl->addSignalGroupPhaseBegin(id, (SUMOTime) *i, color);
293  }
294  if (myTimes.size() == 0) {
295  if (myFirstIsRed) {
297  } else {
299  }
300  }
301  tl->setSignalYellowTimes(id, myTRedYellow, myTYellow);
302  return true;
303 }
304 
305 
306 
307 
308 
309 
310 
311 
313 
314 NIVissimTL::NIVissimTL(int id, const std::string& type,
315  const std::string& name, SUMOTime absdur,
316  SUMOTime offset)
317  : myID(id), myName(name), myAbsDuration(absdur), myOffset(offset),
318  myCurrentGroup(0), myType(type)
319 
320 {}
321 
322 
324 
325 
326 
327 
328 
329 bool
330 NIVissimTL::dictionary(int id, const std::string& type,
331  const std::string& name, SUMOTime absdur,
332  SUMOTime offset) {
333  NIVissimTL* o = new NIVissimTL(id, type, name, absdur, offset);
334  if (!dictionary(id, o)) {
335  delete o;
336  return false;
337  }
338  return true;
339 }
340 
341 bool
343  DictType::iterator i = myDict.find(id);
344  if (i == myDict.end()) {
345  myDict[id] = o;
346  return true;
347  }
348  return false;
349 }
350 
351 
352 NIVissimTL*
354  DictType::iterator i = myDict.find(id);
355  if (i == myDict.end()) {
356  return 0;
357  }
358  return (*i).second;
359 }
360 
361 
362 void
364  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
365  delete(*i).second;
366  }
367  myDict.clear();
368 }
369 
370 
371 
372 
373 
374 bool
376  NBEdgeCont& ec) {
377  size_t ref = 0;
378  size_t ref_groups = 0;
379  size_t ref_signals = 0;
380  size_t no_signals = 0;
381  size_t no_groups = 0;
382  for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
383  NIVissimTL* tl = (*i).second;
384  /* if(tl->myType!="festzeit") {
385  cout << " Warning: The traffic light '" << tl->myID
386  << "' could not be assigned to a node." << endl;
387  ref++;
388  continue;
389  }*/
390  std::string id = toString<int>(tl->myID);
391  TrafficLightType type = ((tl->getType() == "festzeit" || tl->getType() == "festzeit_fake") ?
393  NBLoadedTLDef* def = new NBLoadedTLDef(id, 0, type);
394  if (!tlc.insert(def)) {
395  WRITE_ERROR("Error on adding a traffic light\n Must be a multiple id ('" + id + "')");
396  continue;
397  }
398  def->setCycleDuration((unsigned int) tl->myAbsDuration);
399  // add each group to the node's container
401  for (SGroupDictType::const_iterator j = sgs.begin(); j != sgs.end(); j++) {
402  if (!(*j).second->addTo(def)) {
403  WRITE_WARNING("The signal group '" + toString<int>((*j).first) + "' could not be assigned to tl '" + toString<int>(tl->myID) + "'.");
404  ref_groups++;
405  }
406  no_groups++;
407  }
408  // add the signal group signals to the node
410  for (SSignalDictType::const_iterator k = signals.begin(); k != signals.end(); k++) {
411  if (!(*k).second->addTo(ec, def)) {
412  WRITE_WARNING("The signal '" + toString<int>((*k).first) + "' could not be assigned to tl '" + toString<int>(tl->myID) + "'.");
413  ref_signals++;
414  }
415  no_signals++;
416  }
417  }
418  if (ref != 0) {
419  WRITE_WARNING("Could not set " + toString<size_t>(ref) + " of " + toString<size_t>(myDict.size()) + " traffic lights.");
420  }
421  if (ref_groups != 0) {
422  WRITE_WARNING("Could not set " + toString<size_t>(ref_groups) + " of " + toString<size_t>(no_groups) + " groups.");
423  }
424  if (ref_signals != 0) {
425  WRITE_WARNING("Could not set " + toString<size_t>(ref_signals) + " of " + toString<size_t>(no_signals) + " signals.");
426  }
427  return true;
428 
429 }
430 
431 
432 std::string
434  return myType;
435 }
436 
437 
438 int
440  return myID;
441 }
442 
443 
444 
445 /****************************************************************************/
446