|
SUMO - Simulation of Urban MObility
|
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 (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 // =========================================================================== 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 // static members 00044 // =========================================================================== 00045 const int NBConnection::InvalidTlIndex = -1; 00046 const NBConnection NBConnection::InvalidConnection("invalidFrom", 0, "invalidTo", 0); 00047 00048 // =========================================================================== 00049 // method definitions 00050 // =========================================================================== 00051 NBConnection::NBConnection(NBEdge* from, NBEdge* to) : 00052 myFrom(from), myTo(to), 00053 myFromID(from->getID()), myToID(to->getID()), 00054 myFromLane(-1), myToLane(-1), 00055 myTlIndex(InvalidTlIndex) 00056 {} 00057 00058 00059 NBConnection::NBConnection(const std::string& fromID, NBEdge* from, 00060 const std::string& toID, NBEdge* to) : 00061 myFrom(from), myTo(to), 00062 myFromID(fromID), myToID(toID), 00063 myFromLane(-1), myToLane(-1), 00064 myTlIndex(InvalidTlIndex) 00065 {} 00066 00067 00068 NBConnection::NBConnection(NBEdge* from, int fromLane, 00069 NBEdge* to, int toLane, int tlIndex) : 00070 myFrom(from), myTo(to), 00071 myFromLane(fromLane), myToLane(toLane), 00072 myTlIndex(tlIndex) { 00073 /* @todo what should we assert here? 00074 assert(myFromLane<0||from->getNumLanes()>(size_t) myFromLane); 00075 assert(myToLane<0||to->getNumLanes()>(size_t) myToLane); 00076 */ 00077 myFromID = from->getID(); 00078 myToID = to!=0 ? to->getID() : ""; 00079 } 00080 00081 00082 NBConnection::~NBConnection() {} 00083 00084 00085 NBConnection::NBConnection(const NBConnection& c) : 00086 myFrom(c.myFrom), myTo(c.myTo), 00087 myFromID(c.myFromID), myToID(c.myToID), 00088 myFromLane(c.myFromLane), myToLane(c.myToLane), 00089 myTlIndex(c.myTlIndex) 00090 {} 00091 00092 00093 NBEdge* 00094 NBConnection::getFrom() const { 00095 return myFrom; 00096 } 00097 00098 00099 NBEdge* 00100 NBConnection::getTo() const { 00101 return myTo; 00102 } 00103 00104 00105 bool 00106 NBConnection::replaceFrom(NBEdge* which, NBEdge* by) { 00107 if (myFrom==which) { 00108 myFrom = by; 00109 myFromID = myFrom->getID(); 00110 return true; 00111 } 00112 return false; 00113 } 00114 00115 00116 bool 00117 NBConnection::replaceFrom(NBEdge* which, int whichLane, 00118 NBEdge* by, int byLane) { 00119 if (myFrom==which&&(myFromLane==(int) whichLane||myFromLane<0)) { 00120 myFrom = by; 00121 myFromID = myFrom->getID(); 00122 myFromLane = byLane; 00123 return true; 00124 } 00125 return false; 00126 } 00127 00128 00129 bool 00130 NBConnection::replaceTo(NBEdge* which, NBEdge* by) { 00131 if (myTo==which) { 00132 myTo = by; 00133 myToID = myTo->getID(); 00134 return true; 00135 } 00136 return false; 00137 } 00138 00139 00140 bool 00141 NBConnection::replaceTo(NBEdge* which, int whichLane, 00142 NBEdge* by, int byLane) { 00143 if (myTo==which&&(myToLane==(int) whichLane||myFromLane<0)) { 00144 myTo = by; 00145 myToID = myTo->getID(); 00146 myToLane = byLane; 00147 return true; 00148 } 00149 return false; 00150 } 00151 00152 00153 bool 00154 operator<(const NBConnection& c1, const NBConnection& c2) { 00155 if (c1.myFromID != c2.myFromID) { 00156 return c1.myFromID < c2.myFromID; 00157 } 00158 if (c1.myToID != c2.myToID) { 00159 return c1.myToID < c2.myToID; 00160 } 00161 if (c1.myFromLane != c2.myFromLane) { 00162 return c1.myFromLane < c2.myFromLane; 00163 } 00164 return c1.myToLane < c2.myToLane; 00165 } 00166 00167 00168 bool 00169 NBConnection::operator==(const NBConnection& c) const { 00170 return (myFrom ==c.myFrom && myTo ==c.myTo && 00171 myFromID ==c.myFromID && myToID ==c.myToID && 00172 myFromLane==c.myFromLane && myToLane==c.myToLane && 00173 myTlIndex ==c.myTlIndex); 00174 } 00175 00176 00177 bool 00178 NBConnection::check(const NBEdgeCont& ec) { 00179 myFrom = checkFrom(ec); 00180 myTo = checkTo(ec); 00181 return myFrom!=0 && myTo!=0; 00182 } 00183 00184 00185 NBEdge* 00186 NBConnection::checkFrom(const NBEdgeCont& ec) { 00187 NBEdge* e = ec.retrieve(myFromID); 00188 // ok, the edge was not changed 00189 if (e==myFrom) { 00190 return myFrom; 00191 } 00192 // try to get the edge 00193 return ec.retrievePossiblySplitted(myFromID, myToID, true); 00194 } 00195 00196 00197 NBEdge* 00198 NBConnection::checkTo(const NBEdgeCont& ec) { 00199 NBEdge* e = ec.retrieve(myToID); 00200 // ok, the edge was not changed 00201 if (e==myTo) { 00202 return myTo; 00203 } 00204 // try to get the edge 00205 return ec.retrievePossiblySplitted(myToID, myFromID, false); 00206 } 00207 00208 00209 std::string 00210 NBConnection::getID() const { 00211 std::stringstream str; 00212 str << myFromID << "_" << myFromLane << "->" << myToID << "_" << myToLane; 00213 return str.str(); 00214 } 00215 00216 00217 int 00218 NBConnection::getFromLane() const { 00219 return myFromLane; 00220 } 00221 00222 00223 int 00224 NBConnection::getToLane() const { 00225 return myToLane; 00226 } 00227 00228 00229 00230 /****************************************************************************/ 00231