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