NBConnection.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // The class holds a description of a connection between two edges
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 <sstream>
00031 #include <iostream>
00032 #include <cassert>
00033 #include "NBEdgeCont.h"
00034 #include "NBEdge.h"
00035 #include "NBConnection.h"
00036 
00037 #ifdef CHECK_MEMORY_LEAKS
00038 #include <foreign/nvwa/debug_new.h>
00039 #endif // CHECK_MEMORY_LEAKS
00040 
00041 
00042 // ===========================================================================
00043 // method definitions
00044 // ===========================================================================
00045 NBConnection::NBConnection(NBEdge *from, NBEdge *to)
00046         : myFrom(from), myTo(to), myFromLane(-1), myToLane(-1) {
00047     myFromID = from->getID();
00048     myToID = to->getID();
00049 }
00050 
00051 
00052 NBConnection::NBConnection(const std::string &fromID, NBEdge *from,
00053                            const std::string &toID, NBEdge *to)
00054         : myFrom(from), myTo(to), myFromID(fromID), myToID(toID),
00055         myFromLane(-1), myToLane(-1) {}
00056 
00057 
00058 NBConnection::NBConnection(NBEdge *from, int fromLane,
00059                            NBEdge *to, int toLane)
00060         : myFrom(from), myTo(to), myFromLane(fromLane), myToLane(toLane) {
00061     assert(myFromLane<0||from->getNoLanes()>(size_t) myFromLane);
00062     assert(myToLane<0||to->getNoLanes()>(size_t) myToLane);
00063     myFromID = from->getID();
00064     myToID = to!=0 ? to->getID() : "";
00065 }
00066 
00067 
00068 NBConnection::~NBConnection() {}
00069 
00070 
00071 NBConnection::NBConnection(const NBConnection &c)
00072         : myFrom(c.myFrom), myTo(c.myTo),
00073         myFromID(c.myFromID), myToID(c.myToID),
00074         myFromLane(c.myFromLane), myToLane(c.myToLane) {}
00075 
00076 
00077 NBEdge *
00078 NBConnection::getFrom() const {
00079     return myFrom;
00080 }
00081 
00082 
00083 NBEdge *
00084 NBConnection::getTo() const {
00085     return myTo;
00086 }
00087 
00088 
00089 bool
00090 NBConnection::replaceFrom(NBEdge *which, NBEdge *by) {
00091     if (myFrom==which) {
00092         myFrom = by;
00093         myFromID = myFrom->getID();
00094         return true;
00095     }
00096     return false;
00097 }
00098 
00099 
00100 bool
00101 NBConnection::replaceFrom(NBEdge *which, int whichLane,
00102                           NBEdge *by, int byLane) {
00103     if (myFrom==which&&(myFromLane==(int) whichLane||myFromLane<0)) {
00104         myFrom = by;
00105         myFromID = myFrom->getID();
00106         myFromLane = byLane;
00107         return true;
00108     }
00109     return false;
00110 }
00111 
00112 
00113 bool
00114 NBConnection::replaceTo(NBEdge *which, NBEdge *by) {
00115     if (myTo==which) {
00116         myTo = by;
00117         myToID = myTo->getID();
00118         return true;
00119     }
00120     return false;
00121 }
00122 
00123 
00124 bool
00125 NBConnection::replaceTo(NBEdge *which, int whichLane,
00126                         NBEdge *by, int byLane) {
00127     if (myTo==which&&(myToLane==(int) whichLane||myFromLane<0)) {
00128         myTo = by;
00129         myToID = myTo->getID();
00130         myToLane = byLane;
00131         return true;
00132     }
00133     return false;
00134 }
00135 
00136 
00137 bool
00138 operator<(const NBConnection &c1, const NBConnection &c2) {
00139     return
00140         std::pair<NBEdge*, NBEdge*>(c1.getFrom(), c1.getTo())
00141         <
00142         std::pair<NBEdge*, NBEdge*>(c2.getFrom(), c2.getTo());
00143 }
00144 
00145 
00146 
00147 bool
00148 NBConnection::check(const NBEdgeCont &ec) {
00149     myFrom = checkFrom(ec);
00150     myTo = checkTo(ec);
00151     return myFrom!=0 && myTo!=0;
00152 }
00153 
00154 
00155 NBEdge *
00156 NBConnection::checkFrom(const NBEdgeCont &ec) {
00157     NBEdge *e = ec.retrieve(myFromID);
00158     // ok, the edge was not changed
00159     if (e==myFrom) {
00160         return myFrom;
00161     }
00162     // try to get the edge
00163     return ec.retrievePossiblySplitted(myFromID, myToID, true);
00164 }
00165 
00166 
00167 NBEdge *
00168 NBConnection::checkTo(const NBEdgeCont &ec) {
00169     NBEdge *e = ec.retrieve(myToID);
00170     // ok, the edge was not changed
00171     if (e==myTo) {
00172         return myTo;
00173     }
00174     // try to get the edge
00175     return ec.retrievePossiblySplitted(myToID, myFromID, false);
00176 }
00177 
00178 
00179 std::string
00180 NBConnection::getID() const {
00181     std::stringstream str;
00182     str << myFromID << "_" << myFromLane << "->" << myToID << "_" << myToLane;
00183     return str.str();
00184 }
00185 
00186 
00187 int
00188 NBConnection::getFromLane() const {
00189     return myFromLane;
00190 }
00191 
00192 
00193 int
00194 NBConnection::getToLane() const {
00195     return myToLane;
00196 }
00197 
00198 
00199 
00200 /****************************************************************************/
00201 

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