SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSLink.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A connnection between lanes
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
13 // Copyright (C) 2001-2012 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 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <iostream>
34 #include <cassert>
35 #include "MSLink.h"
36 #include "MSLane.h"
37 #include "MSGlobals.h"
38 #include "MSVehicle.h"
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // static member variables
47 // ===========================================================================
49 
50 
51 // ===========================================================================
52 // member method definitions
53 // ===========================================================================
54 #ifndef HAVE_INTERNAL_LANES
56  LinkDirection dir, LinkState state,
57  SUMOReal length)
58  :
59  myLane(succLane),
60  myRequestIdx(0), myRespondIdx(0),
61  myState(state), myDirection(dir), myLength(length) {}
62 #else
63 MSLink::MSLink(MSLane* succLane, MSLane* via,
64  LinkDirection dir, LinkState state, SUMOReal length)
65  :
66  myLane(succLane),
67  myRequestIdx(0), myRespondIdx(0),
68  myState(state), myDirection(dir), myLength(length),
69  myJunctionInlane(via) {}
70 #endif
71 
72 
74 
75 
76 void
77 MSLink::setRequestInformation(unsigned int requestIdx, unsigned int respondIdx, bool isCrossing, bool isCont,
78  const std::vector<MSLink*> &foeLinks,
79  const std::vector<MSLane*> &foeLanes) {
80  myRequestIdx = requestIdx;
81  myRespondIdx = respondIdx;
83  myAmCont = isCont;
84  myFoeLinks = foeLinks;
85  myFoeLanes = foeLanes;
86 }
87 
88 
89 void
90 MSLink::setApproaching(SUMOVehicle* approaching, SUMOTime arrivalTime, SUMOReal speed, bool setRequest) {
91  LinkApproachingVehicles::iterator i = find_if(myApproachingVehicles.begin(), myApproachingVehicles.end(), vehicle_in_request_finder(approaching));
92  if (i != myApproachingVehicles.end()) {
93  myApproachingVehicles.erase(i);
94  }
95  const SUMOTime leaveTime = arrivalTime + TIME2STEPS((approaching->getVehicleType().getLengthWithGap() + getLength()) / speed);
96  ApproachingVehicleInformation approachInfo(arrivalTime, leaveTime, approaching, setRequest);
97  myApproachingVehicles.push_back(approachInfo);
98 }
99 
100 
101 void
103  myBlockedFoeLinks.insert(link);
104 }
105 
106 
107 
108 bool
110  for (std::set<MSLink*>::const_iterator i = myBlockedFoeLinks.begin(); i != myBlockedFoeLinks.end(); ++i) {
111  if ((*i)->isBlockingAnyone()) {
112  return true;
113  }
114  }
115  return false;
116 }
117 
118 
119 void
121  LinkApproachingVehicles::iterator i = find_if(myApproachingVehicles.begin(), myApproachingVehicles.end(), vehicle_in_request_finder(veh));
122  if (i != myApproachingVehicles.end()) {
123  myApproachingVehicles.erase(i);
124  }
125 }
126 
127 
128 bool
129 MSLink::opened(SUMOTime arrivalTime, SUMOReal arrivalSpeed, SUMOReal vehicleLength) const {
130  if (myState == LINKSTATE_TL_RED) {
131  return false;
132  }
133  if (myAmCont) {
134  return true;
135  }
136 #ifdef HAVE_INTERNAL_LANES
137  const SUMOReal length = myJunctionInlane == 0 ? getLength() : myJunctionInlane->getLength();
138 #else
139  const SUMOReal length = getLength();
140 #endif
141  const SUMOTime leaveTime = arrivalTime + TIME2STEPS((length + vehicleLength) / arrivalSpeed);
142  for (std::vector<MSLink*>::const_iterator i = myFoeLinks.begin(); i != myFoeLinks.end(); ++i) {
143 #ifdef HAVE_MESOSIM
145  if ((*i)->getState() == LINKSTATE_TL_RED) {
146  continue;
147  }
148  }
149 #endif
150  if ((*i)->blockedAtTime(arrivalTime, leaveTime)) {
151  return false;
152  }
153  }
154  for (std::vector<MSLane*>::const_iterator i = myFoeLanes.begin(); i != myFoeLanes.end(); ++i) {
155  if ((*i)->getVehicleNumber() > 0 || (*i)->getPartialOccupator() != 0) {
156  return false;
157  }
158  }
159  return true;
160 }
161 
162 
163 bool
164 MSLink::blockedAtTime(SUMOTime arrivalTime, SUMOTime leaveTime) const {
165  for (LinkApproachingVehicles::const_iterator i = myApproachingVehicles.begin(); i != myApproachingVehicles.end(); ++i) {
166  if (!(*i).willPass) {
167  continue;
168  }
169  if (!(((*i).leavingTime + myLookaheadTime < arrivalTime) || ((*i).arrivalTime - myLookaheadTime > leaveTime))) {
170  return true;
171  }
172  }
173  return false;
174 }
175 
176 
177 bool
178 MSLink::hasApproachingFoe(SUMOTime arrivalTime, SUMOTime leaveTime) const {
179  for (std::vector<MSLink*>::const_iterator i = myFoeLinks.begin(); i != myFoeLinks.end(); ++i) {
180  if ((*i)->blockedAtTime(arrivalTime, leaveTime)) {
181  return true;
182  }
183  }
184  for (std::vector<MSLane*>::const_iterator i = myFoeLanes.begin(); i != myFoeLanes.end(); ++i) {
185  if ((*i)->getVehicleNumber() > 0 || (*i)->getPartialOccupator() != 0) {
186  return true;
187  }
188  }
189  return false;
190 }
191 
192 
195  return myDirection;
196 }
197 
198 
199 void
201  myState = state;
202 }
203 
204 
205 MSLane*
207  return myLane;
208 }
209 
210 
211 #ifdef HAVE_INTERNAL_LANES
212 MSLane*
213 MSLink::getViaLane() const {
214  return myJunctionInlane;
215 }
216 #endif
217 
218 
219 unsigned int
221  return myRespondIdx;
222 }
223 
224 
225 
226 /****************************************************************************/
227