SUMO - Simulation of Urban MObility

ROHelper.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Some helping methods for router
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 // included modules
00022 // ===========================================================================
00023 #ifdef _MSC_VER
00024 #include <windows_config.h>
00025 #else
00026 #include <config.h>
00027 #endif
00028 
00029 #include <functional>
00030 #include <vector>
00031 #include "ROEdge.h"
00032 #include "ROVehicle.h"
00033 
00034 
00035 // ===========================================================================
00036 // class definitions
00037 // ===========================================================================
00038 
00039 
00040 namespace ROHelper {
00041 /*
00042 SUMOReal
00043 recomputeCosts(SUMOAbstractRouter<ROEdge,ROVehicle> &router,
00044                const std::vector<const ROEdge*> &edges,
00045                const ROVehicle * const v, SUMOTime time) throw() {
00046     SUMOReal costs = 0;
00047     for (std::vector<const ROEdge*>::const_iterator i=edges.begin(); i!=edges.end(); i++) {
00048         costs += router.getEffort(v, time + costs, *i);
00049         if ((*i)->prohibits(v)) {
00050             return -1;
00051         }
00052     }
00053     return costs;
00054 }
00055 */
00056 
00057 void
00058 recheckForLoops(std::vector<const ROEdge*> &edges) throw() {
00059     // remove loops at the route's begin
00060     //  (vehicle makes a turnaround to get into the right direction at an already passed node)
00061     RONode* start = edges[0]->getFromNode();
00062     unsigned lastStart = 0;
00063     for (unsigned i=1; i<edges.size(); i++) {
00064         if (edges[i]->getFromNode() == start) {
00065             lastStart = i;
00066         }
00067     }
00068     if (lastStart > 0) {
00069         edges.erase(edges.begin(), edges.begin() + lastStart - 1);
00070     }
00071     // remove loops at the route's end
00072     //  (vehicle makes a turnaround to get into the right direction at an already passed node)
00073     RONode* end = edges.back()->getToNode();
00074     size_t firstEnd = edges.size()-1;
00075     for (unsigned i=0; i<firstEnd; i++) {
00076         if (edges[i]->getToNode() == end) {
00077             firstEnd = i;
00078         }
00079     }
00080     if (firstEnd < edges.size()-1) {
00081         edges.erase(edges.begin() + firstEnd + 2, edges.end());
00082     }
00083     // remove loops within the route
00084     std::vector<RONode*> nodes;
00085     for (std::vector<const ROEdge*>::iterator i=edges.begin(); i!=edges.end(); ++i) {
00086         nodes.push_back((*i)->getFromNode());
00087     }
00088     nodes.push_back(edges.back()->getToNode());
00089     bool changed = false;
00090     do {
00091         changed = false;
00092         for (unsigned int b=0; b<nodes.size()&&!changed; ++b) {
00093             RONode* bn = nodes[b];
00094             for (unsigned int e=b+1; e<nodes.size()&&!changed; ++e) {
00095                 if (bn==nodes[e]) {
00096                     changed = true;
00097                     nodes.erase(nodes.begin()+b, nodes.begin()+e);
00098                     edges.erase(edges.begin()+b, edges.begin()+e);
00099                 }
00100             }
00101         }
00102     } while (changed);
00103     /*
00104     */
00105 }
00106 
00107 
00108 }
00109 
00110 std::ostream& operator<<(std::ostream& os, const std::vector<const ROEdge*> &ev) {
00111     bool hadFirst = false;
00112     for (std::vector<const ROEdge*>::const_iterator j=ev.begin(); j!=ev.end(); j++) {
00113         if ((*j)->getType() != ROEdge::ET_DISTRICT) {
00114             if (hadFirst) {
00115                 os << ' ';
00116             }
00117             os << (*j)->getID();
00118             hadFirst = true;
00119         }
00120     }
00121     return os;
00122 }
00123 
00124 
00125 /****************************************************************************/
00126 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines