MSRoute.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A vehicle route
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 <cassert>
00031 #include <algorithm>
00032 #include <limits>
00033 #include "MSRoute.h"
00034 #include "MSEdge.h"
00035 #include "MSLane.h"
00036 #include <utils/common/FileHelpers.h>
00037 #include <utils/common/RGBColor.h>
00038 #include <utils/iodevices/BinaryInputDevice.h>
00039 #include <utils/iodevices/OutputDevice.h>
00040 
00041 #ifdef CHECK_MEMORY_LEAKS
00042 #include <foreign/nvwa/debug_new.h>
00043 #endif // CHECK_MEMORY_LEAKS
00044 
00045 
00046 // ===========================================================================
00047 // static member variables
00048 // ===========================================================================
00049 MSRoute::RouteDict MSRoute::myDict;
00050 MSRoute::RouteDistDict MSRoute::myDistDict;
00051 
00052 
00053 // ===========================================================================
00054 // member method definitions
00055 // ===========================================================================
00056 MSRoute::MSRoute(const std::string &id,
00057                  const MSEdgeVector &edges,
00058                  bool multipleReferenced, const RGBColor &c,
00059                  const std::vector<SUMOVehicleParameter::Stop> &stops) throw()
00060         : Named(id), myEdges(edges),
00061         myMultipleReferenced(multipleReferenced),
00062         myColor(c), myStops(stops) {}
00063 
00064 
00065 MSRoute::~MSRoute() throw() {}
00066 
00067 MSRouteIterator
00068 MSRoute::begin() const {
00069     return myEdges.begin();
00070 }
00071 
00072 MSRouteIterator
00073 MSRoute::end() const {
00074     return myEdges.end();
00075 }
00076 
00077 unsigned
00078 MSRoute::size() const {
00079     return (unsigned) myEdges.size();
00080 }
00081 
00082 
00083 const MSEdge *
00084 MSRoute::getLastEdge() const {
00085     assert(myEdges.size()>0);
00086     return myEdges[myEdges.size()-1];
00087 }
00088 
00089 bool
00090 MSRoute::dictionary(const std::string &id, const MSRoute* route) {
00091     if (myDict.find(id) == myDict.end() && myDistDict.find(id) == myDistDict.end()) {
00092         myDict[id] = route;
00093         return true;
00094     }
00095     return false;
00096 }
00097 
00098 
00099 bool
00100 MSRoute::dictionary(const std::string &id, RandomDistributor<const MSRoute*>* routeDist) {
00101     if (myDict.find(id) == myDict.end() && myDistDict.find(id) == myDistDict.end()) {
00102         myDistDict[id] = routeDist;
00103         return true;
00104     }
00105     return false;
00106 }
00107 
00108 
00109 const MSRoute*
00110 MSRoute::dictionary(const std::string &id) {
00111     RouteDict::iterator it = myDict.find(id);
00112     if (it == myDict.end()) {
00113         RouteDistDict::iterator it2 = myDistDict.find(id);
00114         if (it2 == myDistDict.end() || it2->second->getOverallProb() == 0) {
00115             return 0;
00116         }
00117         return it2->second->get();
00118     }
00119     return it->second;
00120 }
00121 
00122 
00123 RandomDistributor<const MSRoute*> *
00124 MSRoute::distDictionary(const std::string &id) {
00125     RouteDistDict::iterator it2 = myDistDict.find(id);
00126     if (it2 == myDistDict.end()) {
00127         return 0;
00128     }
00129     return it2->second;
00130 }
00131 
00132 
00133 void
00134 MSRoute::clear() {
00135     for (RouteDict::iterator i=myDict.begin(); i!=myDict.end(); ++i) {
00136         delete(*i).second;
00137     }
00138     myDict.clear();
00139 }
00140 
00141 
00142 void
00143 MSRoute::erase(std::string id) {
00144     RouteDict::iterator i=myDict.find(id);
00145     assert(i!=myDict.end());
00146     delete(*i).second;
00147     myDict.erase(id);
00148 }
00149 
00150 
00151 bool
00152 MSRoute::inFurtherUse() const {
00153     return myMultipleReferenced;
00154 }
00155 
00156 
00157 void
00158 MSRoute::insertIDs(std::vector<std::string> &into) {
00159     into.reserve(myDict.size()+myDistDict.size()+into.size());
00160     for (RouteDict::const_iterator i=myDict.begin(); i!=myDict.end(); ++i) {
00161         into.push_back((*i).first);
00162     }
00163     for (RouteDistDict::const_iterator i=myDistDict.begin(); i!=myDistDict.end(); ++i) {
00164         into.push_back((*i).first);
00165     }
00166 }
00167 
00168 
00169 MSRouteIterator
00170 MSRoute::find(const MSEdge *e) const {
00171     return std::find(myEdges.begin(), myEdges.end(), e);
00172 }
00173 
00174 
00175 MSRouteIterator
00176 MSRoute::find(const MSEdge *e, const MSRouteIterator &startingAt) const {
00177     return std::find(startingAt, myEdges.end(), e);
00178 }
00179 
00180 
00181 void
00182 MSRoute::writeEdgeIDs(OutputDevice &os, const MSEdge *upTo) const {
00183     MSEdgeVector::const_iterator i = myEdges.begin();
00184     for (; i!=myEdges.end(); ++i) {
00185         if (i!=myEdges.begin()) {
00186             os << ' ';
00187         }
00188         if ((*i) == upTo) {
00189             return;
00190         }
00191         os << (*i)->getID();
00192     }
00193 }
00194 
00195 
00196 bool
00197 MSRoute::containsAnyOf(const std::vector<MSEdge*> &edgelist) const {
00198     std::vector<MSEdge*>::const_iterator i = edgelist.begin();
00199     for (; i!=edgelist.end(); ++i) {
00200         if (contains(*i)) {
00201             return true;
00202         }
00203     }
00204     return false;
00205 }
00206 
00207 
00208 const MSEdge *
00209 MSRoute::operator[](unsigned index) const {
00210     return myEdges[index];
00211 }
00212 
00213 
00214 #ifdef HAVE_MESOSIM
00215 void
00216 MSRoute::dict_saveState(std::ostream &os) throw() {
00217     FileHelpers::writeUInt(os, (unsigned int) myDict.size());
00218     for (RouteDict::iterator it = myDict.begin(); it!=myDict.end(); ++it) {
00219         FileHelpers::writeString(os, (*it).second->getID());
00220         FileHelpers::writeUInt(os, (unsigned int)(*it).second->myEdges.size());
00221         FileHelpers::writeByte(os, (*it).second->myMultipleReferenced);
00222         for (MSEdgeVector::const_iterator i = (*it).second->myEdges.begin(); i!=(*it).second->myEdges.end(); ++i) {
00223             FileHelpers::writeUInt(os, (*i)->getNumericalID());
00224         }
00225     }
00226     FileHelpers::writeUInt(os, (unsigned int) myDistDict.size());
00227     for (RouteDistDict::iterator it = myDistDict.begin(); it!=myDistDict.end(); ++it) {
00228         FileHelpers::writeString(os, (*it).first);
00229         const unsigned int size = (unsigned int)(*it).second->getVals().size();
00230         FileHelpers::writeUInt(os, size);
00231         for (unsigned int i = 0; i < size; ++i) {
00232             FileHelpers::writeString(os, (*it).second->getVals()[i]->getID());
00233             FileHelpers::writeFloat(os, (*it).second->getProbs()[i]);
00234         }
00235     }
00236 }
00237 
00238 
00239 void
00240 MSRoute::dict_loadState(BinaryInputDevice &bis) throw() {
00241     unsigned int numRoutes;
00242     bis >> numRoutes;
00243     for (; numRoutes>0; numRoutes--) {
00244         std::string id;
00245         bis >> id;
00246         unsigned int no;
00247         bis >> no;
00248         bool multipleReferenced;
00249         bis >> multipleReferenced;
00250         if (dictionary(id)==0) {
00251             MSEdgeVector edges;
00252             edges.reserve(no);
00253             for (; no>0; no--) {
00254                 unsigned int edgeID;
00255                 bis >> edgeID;
00256                 MSEdge *e = MSEdge::dictionary(edgeID);
00257                 assert(e!=0);
00258                 edges.push_back(e);
00259             }
00260             MSRoute *r = new MSRoute(id, edges, multipleReferenced,
00261                                      RGBColor::DEFAULT_COLOR, std::vector<SUMOVehicleParameter::Stop>());
00262             dictionary(id, r);
00263         } else {
00264             for (; no>0; no--) {
00265                 unsigned int edgeID;
00266                 bis >> edgeID;
00267             }
00268         }
00269     }
00270     unsigned int numRouteDists;
00271     bis >> numRouteDists;
00272     for (; numRouteDists>0; numRouteDists--) {
00273         std::string id;
00274         bis >> id;
00275         unsigned int no;
00276         bis >> no;
00277         if (dictionary(id)==0) {
00278             RandomDistributor<const MSRoute*> *dist = new RandomDistributor<const MSRoute*>();
00279             for (; no>0; no--) {
00280                 std::string routeID;
00281                 bis >> routeID;
00282                 const MSRoute *r = dictionary(routeID);
00283                 assert(r!=0);
00284                 SUMOReal prob;
00285                 bis >> prob;
00286                 dist->add(prob, r, false);
00287             }
00288             dictionary(id, dist);
00289         } else {
00290             for (; no>0; no--) {
00291                 std::string routeID;
00292                 bis >> routeID;
00293                 SUMOReal prob;
00294                 bis >> prob;
00295             }
00296         }
00297     }
00298 }
00299 #endif
00300 
00301 
00302 unsigned
00303 MSRoute::posInRoute(const MSRouteIterator &currentEdge) const {
00304     return (unsigned int) distance(myEdges.begin(), currentEdge);
00305 }
00306 
00307 
00308 SUMOReal
00309 MSRoute::getLength() const {
00310     SUMOReal ret = 0;
00311     for (MSEdgeVector::const_iterator i=myEdges.begin(); i!=myEdges.end(); ++i) {
00312         ret += (*i)->getLanes()[0]->getLength();
00313     }
00314     return ret;
00315 }
00316 
00317 
00318 SUMOReal
00319 MSRoute::getDistanceBetween(SUMOReal fromPos, SUMOReal toPos, const MSEdge* fromEdge, const MSEdge* toEdge) const {
00320     bool isFirstIteration = true;
00321     SUMOReal distance = -fromPos;
00322 
00323     if ((find(fromEdge) == end()) || (find(toEdge) == end())) {
00324         // start or destination not contained in route
00325         return std::numeric_limits<SUMOReal>::max();
00326     }
00327     if (fromEdge == toEdge) {
00328         if (fromPos <= toPos) {
00329             // destination position is on start edge
00330             return (toPos - fromPos);
00331         } else {
00332             // start and destination edge are equal: ensure that this edge is contained at least twice in the route
00333             if (std::find(find(fromEdge)+1, end(), fromEdge) == end()) {
00334                 return std::numeric_limits<SUMOReal>::max();
00335             }
00336         }
00337     }
00338     for (MSRouteIterator it = find(fromEdge); it!=end(); ++it) {
00339         if ((*it) == toEdge && !isFirstIteration) {
00340             distance += toPos;
00341             break;
00342         } else {
00343             const std::vector<MSLane*>& lanes = (*it)->getLanes();
00344             distance += lanes[0]->getLength();
00345 #ifdef HAVE_INTERNAL_LANES
00346             // add length of internal lanes to the result
00347             for (std::vector<MSLane*>::const_iterator laneIt = lanes.begin(); laneIt != lanes.end(); laneIt++) {
00348                 const MSLinkCont& links = (*laneIt)->getLinkCont();
00349                 for (MSLinkCont::const_iterator linkIt = links.begin(); linkIt != links.end(); linkIt++) {
00350                     if ((*linkIt)==0||(*linkIt)->getLane()==0) {
00351                         continue;
00352                     }
00353                     std::string succLaneId = (*(it+1))->getLanes()[0]->getID();
00354                     if ((*linkIt)->getLane()->getID().compare(succLaneId) == 0) {
00355                         distance += (*linkIt)->getLength();
00356                     }
00357                 }
00358             }
00359 #endif
00360         }
00361         isFirstIteration = false;
00362     }
00363     return distance;
00364 }
00365 
00366 
00367 const RGBColor &
00368 MSRoute::getColor() const {
00369     return myColor;
00370 }
00371 
00372 
00373 const std::vector<SUMOVehicleParameter::Stop> &
00374 MSRoute::getStops() const {
00375     return myStops;
00376 }
00377 
00378 
00379 /****************************************************************************/
00380 

Generated on Wed May 5 00:06:32 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6