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