SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00009 // The class holds a description of a connection between two edges 00010 /****************************************************************************/ 00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00013 /****************************************************************************/ 00014 // 00015 // This file is part of SUMO. 00016 // SUMO is free software: you can redistribute it and/or modify 00017 // it under the terms of the GNU General Public License as published by 00018 // the Free Software Foundation, either version 3 of the License, or 00019 // (at your option) any later version. 00020 // 00021 /****************************************************************************/ 00022 00023 00024 // =========================================================================== 00025 // included modules 00026 // =========================================================================== 00027 #ifdef _MSC_VER 00028 #include <windows_config.h> 00029 #else 00030 #include <config.h> 00031 #endif 00032 00033 #include <sstream> 00034 #include <iostream> 00035 #include <cassert> 00036 #include "NBEdgeCont.h" 00037 #include "NBEdge.h" 00038 #include "NBConnection.h" 00039 00040 #ifdef CHECK_MEMORY_LEAKS 00041 #include <foreign/nvwa/debug_new.h> 00042 #endif // CHECK_MEMORY_LEAKS 00043 00044 00045 // =========================================================================== 00046 // static members 00047 // =========================================================================== 00048 const int NBConnection::InvalidTlIndex = -1; 00049 const NBConnection NBConnection::InvalidConnection("invalidFrom", 0, "invalidTo", 0); 00050 00051 // =========================================================================== 00052 // method definitions 00053 // =========================================================================== 00054 NBConnection::NBConnection(NBEdge* from, NBEdge* to) : 00055 myFrom(from), myTo(to), 00056 myFromID(from->getID()), myToID(to->getID()), 00057 myFromLane(-1), myToLane(-1), 00058 myTlIndex(InvalidTlIndex) 00059 {} 00060 00061 00062 NBConnection::NBConnection(const std::string& fromID, NBEdge* from, 00063 const std::string& toID, NBEdge* to) : 00064 myFrom(from), myTo(to), 00065 myFromID(fromID), myToID(toID), 00066 myFromLane(-1), myToLane(-1), 00067 myTlIndex(InvalidTlIndex) 00068 {} 00069 00070 00071 NBConnection::NBConnection(NBEdge* from, int fromLane, 00072 NBEdge* to, int toLane, int tlIndex) : 00073 myFrom(from), myTo(to), 00074 myFromLane(fromLane), myToLane(toLane), 00075 myTlIndex(tlIndex) { 00076 /* @todo what should we assert here? 00077 assert(myFromLane<0||from->getNumLanes()>(size_t) myFromLane); 00078 assert(myToLane<0||to->getNumLanes()>(size_t) myToLane); 00079 */ 00080 myFromID = from->getID(); 00081 myToID = to != 0 ? to->getID() : ""; 00082 } 00083 00084 00085 NBConnection::~NBConnection() {} 00086 00087 00088 NBConnection::NBConnection(const NBConnection& c) : 00089 myFrom(c.myFrom), myTo(c.myTo), 00090 myFromID(c.myFromID), myToID(c.myToID), 00091 myFromLane(c.myFromLane), myToLane(c.myToLane), 00092 myTlIndex(c.myTlIndex) 00093 {} 00094 00095 00096 NBEdge* 00097 NBConnection::getFrom() const { 00098 return myFrom; 00099 } 00100 00101 00102 NBEdge* 00103 NBConnection::getTo() const { 00104 return myTo; 00105 } 00106 00107 00108 bool 00109 NBConnection::replaceFrom(NBEdge* which, NBEdge* by) { 00110 if (myFrom == which) { 00111 myFrom = by; 00112 myFromID = myFrom->getID(); 00113 return true; 00114 } 00115 return false; 00116 } 00117 00118 00119 bool 00120 NBConnection::replaceFrom(NBEdge* which, int whichLane, 00121 NBEdge* by, int byLane) { 00122 if (myFrom == which && (myFromLane == (int) whichLane || myFromLane < 0)) { 00123 myFrom = by; 00124 myFromID = myFrom->getID(); 00125 myFromLane = byLane; 00126 return true; 00127 } 00128 return false; 00129 } 00130 00131 00132 bool 00133 NBConnection::replaceTo(NBEdge* which, NBEdge* by) { 00134 if (myTo == which) { 00135 myTo = by; 00136 myToID = myTo->getID(); 00137 return true; 00138 } 00139 return false; 00140 } 00141 00142 00143 bool 00144 NBConnection::replaceTo(NBEdge* which, int whichLane, 00145 NBEdge* by, int byLane) { 00146 if (myTo == which && (myToLane == (int) whichLane || myFromLane < 0)) { 00147 myTo = by; 00148 myToID = myTo->getID(); 00149 myToLane = byLane; 00150 return true; 00151 } 00152 return false; 00153 } 00154 00155 00156 bool 00157 operator<(const NBConnection& c1, const NBConnection& c2) { 00158 if (c1.myFromID != c2.myFromID) { 00159 return c1.myFromID < c2.myFromID; 00160 } 00161 if (c1.myToID != c2.myToID) { 00162 return c1.myToID < c2.myToID; 00163 } 00164 if (c1.myFromLane != c2.myFromLane) { 00165 return c1.myFromLane < c2.myFromLane; 00166 } 00167 return c1.myToLane < c2.myToLane; 00168 } 00169 00170 00171 bool 00172 NBConnection::operator==(const NBConnection& c) const { 00173 return (myFrom == c.myFrom && myTo == c.myTo && 00174 myFromID == c.myFromID && myToID == c.myToID && 00175 myFromLane == c.myFromLane && myToLane == c.myToLane && 00176 myTlIndex == c.myTlIndex); 00177 } 00178 00179 00180 bool 00181 NBConnection::check(const NBEdgeCont& ec) { 00182 myFrom = checkFrom(ec); 00183 myTo = checkTo(ec); 00184 return myFrom != 0 && myTo != 0; 00185 } 00186 00187 00188 NBEdge* 00189 NBConnection::checkFrom(const NBEdgeCont& ec) { 00190 NBEdge* e = ec.retrieve(myFromID); 00191 // ok, the edge was not changed 00192 if (e == myFrom) { 00193 return myFrom; 00194 } 00195 // try to get the edge 00196 return ec.retrievePossiblySplitted(myFromID, myToID, true); 00197 } 00198 00199 00200 NBEdge* 00201 NBConnection::checkTo(const NBEdgeCont& ec) { 00202 NBEdge* e = ec.retrieve(myToID); 00203 // ok, the edge was not changed 00204 if (e == myTo) { 00205 return myTo; 00206 } 00207 // try to get the edge 00208 return ec.retrievePossiblySplitted(myToID, myFromID, false); 00209 } 00210 00211 00212 std::string 00213 NBConnection::getID() const { 00214 std::stringstream str; 00215 str << myFromID << "_" << myFromLane << "->" << myToID << "_" << myToLane; 00216 return str.str(); 00217 } 00218 00219 00220 int 00221 NBConnection::getFromLane() const { 00222 return myFromLane; 00223 } 00224 00225 00226 int 00227 NBConnection::getToLane() const { 00228 return myToLane; 00229 } 00230 00231 00232 00233 /****************************************************************************/ 00234