SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSEdgeControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Stores edges and lanes, performs moving of vehicle
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include "MSEdgeControl.h"
35 #include "MSEdge.h"
36 #include "MSLane.h"
37 #include <iostream>
38 #include <vector>
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // member method definitions
47 // ===========================================================================
48 MSEdgeControl::MSEdgeControl(const std::vector< MSEdge* >& edges)
49  : myEdges(edges),
50  myLanes(MSLane::dictSize()),
51  myLastLaneChange(MSEdge::dictSize()) {
52  // build the usage definitions for lanes
53  for (std::vector< MSEdge* >::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
54  const std::vector<MSLane*>& lanes = (*i)->getLanes();
55  if (lanes.size() == 1) {
56  size_t pos = (*lanes.begin())->getNumericalID();
57  myLanes[pos].lane = *(lanes.begin());
58  myLanes[pos].firstNeigh = lanes.end();
59  myLanes[pos].lastNeigh = lanes.end();
60  myLanes[pos].amActive = false;
61  myLanes[pos].haveNeighbors = false;
62  } else {
63  for (std::vector<MSLane*>::const_iterator j = lanes.begin(); j != lanes.end(); ++j) {
64  size_t pos = (*j)->getNumericalID();
65  myLanes[pos].lane = *j;
66  myLanes[pos].firstNeigh = (j + 1);
67  myLanes[pos].lastNeigh = lanes.end();
68  myLanes[pos].amActive = false;
69  myLanes[pos].haveNeighbors = true;
70  }
71  myLastLaneChange[(*i)->getNumericalID()] = -1;
72  }
73  }
74 }
75 
76 
78 }
79 
80 
81 void
83  for (std::set<MSLane*, Named::ComparatorIdLess>::iterator i = myChangedStateLanes.begin(); i != myChangedStateLanes.end(); ++i) {
84  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
85  // if the lane was inactive but is now...
86  if (!lu.amActive && (*i)->getVehicleNumber() > 0) {
87  // ... add to active lanes and mark as such
88  if (lu.haveNeighbors) {
89  myActiveLanes.push_front(*i);
90  } else {
91  myActiveLanes.push_back(*i);
92  }
93  lu.amActive = true;
94  }
95  }
96  myChangedStateLanes.clear();
97 }
98 
99 void
101  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
102  if ((*i)->getVehicleNumber() == 0) {
103  myLanes[(*i)->getNumericalID()].amActive = false;
104  i = myActiveLanes.erase(i);
105  } else {
106  (*i)->planMovements(t);
107  ++i;
108  }
109  }
110 }
111 
112 
113 void
115  myWithVehicles2Integrate.clear();
116  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
117  if ((*i)->getVehicleNumber() == 0 || (*i)->executeMovements(t, myWithVehicles2Integrate)) {
118  myLanes[(*i)->getNumericalID()].amActive = false;
119  i = myActiveLanes.erase(i);
120  } else {
121  ++i;
122  }
123  }
124  for (std::vector<MSLane*>::iterator i = myWithVehicles2Integrate.begin(); i != myWithVehicles2Integrate.end(); ++i) {
125  if ((*i)->integrateNewVehicle(t)) {
126  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
127  if (!lu.amActive) {
128  if (lu.haveNeighbors) {
129  myActiveLanes.push_front(*i);
130  } else {
131  myActiveLanes.push_back(*i);
132  }
133  lu.amActive = true;
134  }
135  }
136  }
137 }
138 
139 
140 void
142  std::vector<MSLane*> toAdd;
143  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
144  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
145  if (lu.haveNeighbors) {
146  MSEdge& edge = (*i)->getEdge();
147  if (myLastLaneChange[edge.getNumericalID()] != t) {
148  myLastLaneChange[edge.getNumericalID()] = t;
149  edge.changeLanes(t);
150  const std::vector<MSLane*>& lanes = edge.getLanes();
151  for (std::vector<MSLane*>::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
152  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
153  if ((*i)->getVehicleNumber() > 0 && !lu.amActive) {
154  toAdd.push_back(*i);
155  lu.amActive = true;
156  }
157  }
158  }
159  ++i;
160  } else {
161  i = myActiveLanes.end();
162  }
163  }
164  for (std::vector<MSLane*>::iterator i = toAdd.begin(); i != toAdd.end(); ++i) {
165  myActiveLanes.push_front(*i);
166  }
167 }
168 
169 
170 void
171 MSEdgeControl::detectCollisions(SUMOTime timestep, const std::string& stage) {
172  // Detections is made by the edge's lanes, therefore hand over.
173  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end(); ++i) {
174  (*i)->detectCollisions(timestep, stage);
175  }
176 }
177 
178 
179 std::vector<std::string>
181  std::vector<std::string> ret;
182  for (std::vector<MSEdge*>::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
183  ret.push_back((*i)->getID());
184  }
185  return ret;
186 }
187 
188 
189 void
191  myChangedStateLanes.insert(l);
192 }
193 
194 
195 /****************************************************************************/
196