00001 /****************************************************************************/ 00007 // Stores edges and lanes, performs moving of vehicle 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright 2001-2010 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 // assign lane usage definitions to lanes 00072 for (size_t j=0; j<myLanes.size(); j++) { 00073 myLanes[j].lane->init(*this, myLanes[j].firstNeigh, myLanes[j].lastNeigh); 00074 } 00075 } 00076 00077 00078 MSEdgeControl::~MSEdgeControl() throw() { 00079 } 00080 00081 00082 void 00083 MSEdgeControl::patchActiveLanes() throw() { 00084 for (std::set<MSLane*>::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) throw() { 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) throw() { 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) throw() { 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) throw() { 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 throw() { 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) throw() { 00191 myChangedStateLanes.insert(l); 00192 } 00193 00194 00195 /****************************************************************************/ 00196
1.5.6