SUMO - Simulation of Urban MObility

MSEdgeControl.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Stores edges and lanes, performs moving of vehicle
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 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #include "MSEdgeControl.h"
00031 #include "MSEdge.h"
00032 #include "MSLane.h"
00033 #include <iostream>
00034 #include <vector>
00035 
00036 #ifdef CHECK_MEMORY_LEAKS
00037 #include <foreign/nvwa/debug_new.h>
00038 #endif // CHECK_MEMORY_LEAKS
00039 
00040 
00041 // ===========================================================================
00042 // member method definitions
00043 // ===========================================================================
00044 MSEdgeControl::MSEdgeControl(const std::vector< MSEdge* > &edges) throw()
00045     : myEdges(edges),
00046       myLanes(MSLane::dictSize()),
00047       myLastLaneChange(MSEdge::dictSize()) {
00048     // build the usage definitions for lanes
00049     for (std::vector< MSEdge* >::const_iterator i=myEdges.begin(); i!=myEdges.end(); ++i) {
00050         const std::vector<MSLane*> &lanes = (*i)->getLanes();
00051         if (lanes.size()==1) {
00052             size_t pos = (*lanes.begin())->getNumericalID();
00053             myLanes[pos].lane = *(lanes.begin());
00054             myLanes[pos].firstNeigh = lanes.end();
00055             myLanes[pos].lastNeigh = lanes.end();
00056             myLanes[pos].amActive = false;
00057             myLanes[pos].haveNeighbors = false;
00058         } else {
00059             for (std::vector<MSLane*>::const_iterator j=lanes.begin(); j!=lanes.end(); ++j) {
00060                 size_t pos = (*j)->getNumericalID();
00061                 myLanes[pos].lane = *j;
00062                 myLanes[pos].firstNeigh = (j+1);
00063                 myLanes[pos].lastNeigh = lanes.end();
00064                 myLanes[pos].amActive = false;
00065                 myLanes[pos].haveNeighbors = true;
00066             }
00067         }
00068         size_t pos = (*i)->getNumericalID();
00069         myLastLaneChange[pos] = -1;
00070     }
00071 }
00072 
00073 
00074 MSEdgeControl::~MSEdgeControl() throw() {
00075 }
00076 
00077 
00078 void
00079 MSEdgeControl::patchActiveLanes() throw() {
00080     for (std::set<MSLane*, Named::ComparatorIdLess>::iterator i=myChangedStateLanes.begin(); i!=myChangedStateLanes.end(); ++i) {
00081         LaneUsage& lu = myLanes[(*i)->getNumericalID()];
00082         // if the lane was inactive but is now...
00083         if (!lu.amActive && (*i)->getVehicleNumber()>0) {
00084             // ... add to active lanes and mark as such
00085             if (lu.haveNeighbors) {
00086                 myActiveLanes.push_front(*i);
00087             } else {
00088                 myActiveLanes.push_back(*i);
00089             }
00090             lu.amActive = true;
00091         }
00092     }
00093     myChangedStateLanes.clear();
00094 }
00095 
00096 void
00097 MSEdgeControl::moveCritical(SUMOTime t) throw() {
00098     for (std::list<MSLane*>::iterator i=myActiveLanes.begin(); i!=myActiveLanes.end();) {
00099         if ((*i)->getVehicleNumber()==0 || (*i)->moveCritical(t)) {
00100             myLanes[(*i)->getNumericalID()].amActive = false;
00101             i = myActiveLanes.erase(i);
00102         } else {
00103             ++i;
00104         }
00105     }
00106 }
00107 
00108 
00109 void
00110 MSEdgeControl::moveFirst(SUMOTime t) throw() {
00111     myWithVehicles2Integrate.clear();
00112     for (std::list<MSLane*>::iterator i=myActiveLanes.begin(); i!=myActiveLanes.end();) {
00113         if ((*i)->getVehicleNumber()==0 || (*i)->setCritical(t, myWithVehicles2Integrate)) {
00114             myLanes[(*i)->getNumericalID()].amActive = false;
00115             i = myActiveLanes.erase(i);
00116         } else {
00117             ++i;
00118         }
00119     }
00120     for (std::vector<MSLane*>::iterator i=myWithVehicles2Integrate.begin(); i!=myWithVehicles2Integrate.end(); ++i) {
00121         if ((*i)->integrateNewVehicle(t)) {
00122             LaneUsage& lu = myLanes[(*i)->getNumericalID()];
00123             if (!lu.amActive) {
00124                 if (lu.haveNeighbors) {
00125                     myActiveLanes.push_front(*i);
00126                 } else {
00127                     myActiveLanes.push_back(*i);
00128                 }
00129                 lu.amActive = true;
00130             }
00131         }
00132     }
00133 }
00134 
00135 
00136 void
00137 MSEdgeControl::changeLanes(SUMOTime t) throw() {
00138     std::vector<MSLane*> toAdd;
00139     for (std::list<MSLane*>::iterator i=myActiveLanes.begin(); i!=myActiveLanes.end();) {
00140         LaneUsage& lu = myLanes[(*i)->getNumericalID()];
00141         if (lu.haveNeighbors) {
00142             MSEdge& edge = (*i)->getEdge();
00143             if (myLastLaneChange[edge.getNumericalID()]!=t) {
00144                 myLastLaneChange[edge.getNumericalID()] = t;
00145                 edge.changeLanes(t);
00146                 const std::vector<MSLane*> &lanes = edge.getLanes();
00147                 for (std::vector<MSLane*>::const_iterator i=lanes.begin(); i!=lanes.end(); ++i) {
00148                     LaneUsage& lu = myLanes[(*i)->getNumericalID()];
00149                     if ((*i)->getVehicleNumber()>0 && !lu.amActive) {
00150                         toAdd.push_back(*i);
00151                         lu.amActive = true;
00152                     }
00153                 }
00154             }
00155             ++i;
00156         } else {
00157             i = myActiveLanes.end();
00158         }
00159     }
00160     for (std::vector<MSLane*>::iterator i=toAdd.begin(); i!=toAdd.end(); ++i) {
00161         myActiveLanes.push_front(*i);
00162     }
00163 }
00164 
00165 
00166 void
00167 MSEdgeControl::detectCollisions(SUMOTime timestep) throw() {
00168     // Detections is made by the edge's lanes, therefore hand over.
00169     for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end(); ++i) {
00170         (*i)->detectCollisions(timestep);
00171     }
00172 }
00173 
00174 
00175 std::vector<std::string>
00176 MSEdgeControl::getEdgeNames() const throw() {
00177     std::vector<std::string> ret;
00178     for (std::vector<MSEdge*>::const_iterator i=myEdges.begin(); i!=myEdges.end(); ++i) {
00179         ret.push_back((*i)->getID());
00180     }
00181     return ret;
00182 }
00183 
00184 
00185 void
00186 MSEdgeControl::gotActive(MSLane* l) throw() {
00187     myChangedStateLanes.insert(l);
00188 }
00189 
00190 
00191 /****************************************************************************/
00192 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines