SUMO - Simulation of Urban MObility

MSLink.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A connnection between lanes
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 // ===========================================================================
00021 // included modules
00022 // ===========================================================================
00023 #ifdef _MSC_VER
00024 #include <windows_config.h>
00025 #else
00026 #include <config.h>
00027 #endif
00028 
00029 #include "MSLink.h"
00030 #include "MSLane.h"
00031 #include <iostream>
00032 #include <cassert>
00033 
00034 #ifdef CHECK_MEMORY_LEAKS
00035 #include <foreign/nvwa/debug_new.h>
00036 #endif // CHECK_MEMORY_LEAKS
00037 
00038 
00039 // ===========================================================================
00040 // static member variables
00041 // ===========================================================================
00042 SUMOTime MSLink::myLookaheadTime = TIME2STEPS(3);
00043 
00044 
00045 // ===========================================================================
00046 // member method definitions
00047 // ===========================================================================
00048 #ifndef HAVE_INTERNAL_LANES
00049 MSLink::MSLink(MSLane* succLane,
00050                LinkDirection dir, LinkState state,
00051                SUMOReal length) throw()
00052     :
00053     myLane(succLane),
00054     myRequestIdx(0), myRespondIdx(0),
00055     myState(state), myDirection(dir),  myLength(length) {}
00056 #else
00057 MSLink::MSLink(MSLane* succLane, MSLane* via,
00058                LinkDirection dir, LinkState state, SUMOReal length) throw()
00059     :
00060     myLane(succLane),
00061     myRequestIdx(0), myRespondIdx(0),
00062     myState(state), myDirection(dir), myLength(length),
00063     myJunctionInlane(via) {}
00064 #endif
00065 
00066 
00067 MSLink::~MSLink() throw() {}
00068 
00069 
00070 void
00071 MSLink::setRequestInformation(unsigned int requestIdx, unsigned int respondIdx, bool isCrossing, bool isCont,
00072                               const std::vector<MSLink*> &foeLinks,
00073                               const std::vector<MSLane*> &foeLanes) throw() {
00074     myRequestIdx = requestIdx;
00075     myRespondIdx = respondIdx;
00076     myIsCrossing = isCrossing;
00077     myAmCont = isCont;
00078     myFoeLinks = foeLinks;
00079     myFoeLanes = foeLanes;
00080 }
00081 
00082 
00083 void
00084 MSLink::setApproaching(SUMOVehicle* approaching, SUMOTime arrivalTime, SUMOReal speed, bool setRequest) throw() {
00085     LinkApproachingVehicles::iterator i = find_if(myApproachingVehicles.begin(), myApproachingVehicles.end(), vehicle_in_request_finder(approaching));
00086     if (i!=myApproachingVehicles.end()) {
00087         myApproachingVehicles.erase(i);
00088     }
00089     const SUMOTime leaveTime = arrivalTime + TIME2STEPS((approaching->getVehicleType().getLengthWithGap() + getLength()) / speed);
00090     ApproachingVehicleInformation approachInfo(arrivalTime, leaveTime, approaching, setRequest);
00091     myApproachingVehicles.push_back(approachInfo);
00092 }
00093 
00094 
00095 void
00096 MSLink::addBlockedLink(MSLink* link) throw() {
00097     myBlockedFoeLinks.insert(link);
00098 }
00099 
00100 
00101 
00102 bool
00103 MSLink::willHaveBlockedFoe() const throw() {
00104     for (std::set<MSLink*>::const_iterator i=myBlockedFoeLinks.begin(); i!=myBlockedFoeLinks.end(); ++i) {
00105         if ((*i)->isBlockingAnyone()) {
00106             return true;
00107         }
00108     }
00109     return false;
00110 }
00111 
00112 
00113 void
00114 MSLink::removeApproaching(SUMOVehicle* veh) {
00115     LinkApproachingVehicles::iterator i = find_if(myApproachingVehicles.begin(), myApproachingVehicles.end(), vehicle_in_request_finder(veh));
00116     if (i!=myApproachingVehicles.end()) {
00117         myApproachingVehicles.erase(i);
00118     }
00119 }
00120 
00121 
00122 bool
00123 MSLink::opened(SUMOTime arrivalTime, SUMOReal arrivalSpeed, SUMOReal vehicleLength) const throw() {
00124     if (myState==LINKSTATE_TL_RED) {
00125         return false;
00126     }
00127     if (myAmCont) {
00128         return true;
00129     }
00130 #ifdef HAVE_INTERNAL_LANES
00131     const SUMOReal length = myJunctionInlane==0 ? getLength() : myJunctionInlane->getLength();
00132 #else
00133     const SUMOReal length = getLength();
00134 #endif
00135     const SUMOTime leaveTime = arrivalTime + TIME2STEPS((length + vehicleLength) / arrivalSpeed);
00136     for (std::vector<MSLink*>::const_iterator i=myFoeLinks.begin(); i!=myFoeLinks.end(); ++i) {
00137 #ifdef HAVE_MESOSIM
00138         if (MSGlobals::gUseMesoSim) {
00139             if ((*i)->getState() == LINKSTATE_TL_RED) {
00140                 continue;
00141             }
00142         }
00143 #endif
00144         if ((*i)->blockedAtTime(arrivalTime, leaveTime)) {
00145             return false;
00146         }
00147     }
00148     for (std::vector<MSLane*>::const_iterator i=myFoeLanes.begin(); i!=myFoeLanes.end(); ++i) {
00149         if ((*i)->getVehicleNumber()>0||(*i)->getPartialOccupator()!=0) {
00150             return false;
00151         }
00152     }
00153     return true;
00154 }
00155 
00156 
00157 bool
00158 MSLink::blockedAtTime(SUMOTime arrivalTime, SUMOTime leaveTime) const throw() {
00159     for (LinkApproachingVehicles::const_iterator i=myApproachingVehicles.begin(); i!=myApproachingVehicles.end(); ++i) {
00160         if (!(*i).willPass) {
00161             continue;
00162         }
00163         if (!(((*i).leavingTime+myLookaheadTime < arrivalTime) || ((*i).arrivalTime-myLookaheadTime > leaveTime))) {
00164             return true;
00165         }
00166     }
00167     return false;
00168 }
00169 
00170 
00171 bool
00172 MSLink::hasApproachingFoe(SUMOTime arrivalTime, SUMOTime leaveTime) const throw() {
00173     for (std::vector<MSLink*>::const_iterator i=myFoeLinks.begin(); i!=myFoeLinks.end(); ++i) {
00174         if ((*i)->blockedAtTime(arrivalTime, leaveTime)) {
00175             return true;
00176         }
00177     }
00178     for (std::vector<MSLane*>::const_iterator i=myFoeLanes.begin(); i!=myFoeLanes.end(); ++i) {
00179         if ((*i)->getVehicleNumber()>0||(*i)->getPartialOccupator()!=0) {
00180             return true;
00181         }
00182     }
00183     return false;
00184 }
00185 
00186 
00187 LinkDirection
00188 MSLink::getDirection() const throw() {
00189     return myDirection;
00190 }
00191 
00192 
00193 void
00194 MSLink::setTLState(LinkState state, SUMOTime /*t*/) throw() {
00195     myState = state;
00196 }
00197 
00198 
00199 MSLane*
00200 MSLink::getLane() const throw() {
00201     return myLane;
00202 }
00203 
00204 
00205 #ifdef HAVE_INTERNAL_LANES
00206 MSLane*
00207 MSLink::getViaLane() const throw() {
00208     return myJunctionInlane;
00209 }
00210 #endif
00211 
00212 
00213 unsigned int
00214 MSLink::getRespondIndex() const throw() {
00215     return myRespondIdx;
00216 }
00217 
00218 
00219 
00220 /****************************************************************************/
00221 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines