SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NBConnection.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // The class holds a description of a connection between two edges
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <sstream>
34 #include <iostream>
35 #include <cassert>
36 #include "NBEdgeCont.h"
37 #include "NBEdge.h"
38 #include "NBConnection.h"
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // static members
47 // ===========================================================================
48 const int NBConnection::InvalidTlIndex = -1;
49 const NBConnection NBConnection::InvalidConnection("invalidFrom", 0, "invalidTo", 0);
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
55  myFrom(from), myTo(to),
56  myFromID(from->getID()), myToID(to->getID()),
57  myFromLane(-1), myToLane(-1),
58  myTlIndex(InvalidTlIndex)
59 {}
60 
61 
62 NBConnection::NBConnection(const std::string& fromID, NBEdge* from,
63  const std::string& toID, NBEdge* to) :
64  myFrom(from), myTo(to),
65  myFromID(fromID), myToID(toID),
66  myFromLane(-1), myToLane(-1),
67  myTlIndex(InvalidTlIndex)
68 {}
69 
70 
71 NBConnection::NBConnection(NBEdge* from, int fromLane,
72  NBEdge* to, int toLane, int tlIndex) :
73  myFrom(from), myTo(to),
74  myFromLane(fromLane), myToLane(toLane),
75  myTlIndex(tlIndex) {
76  /* @todo what should we assert here?
77  assert(myFromLane<0||from->getNumLanes()>(size_t) myFromLane);
78  assert(myToLane<0||to->getNumLanes()>(size_t) myToLane);
79  */
80  myFromID = from->getID();
81  myToID = to != 0 ? to->getID() : "";
82 }
83 
84 
86 
87 
89  myFrom(c.myFrom), myTo(c.myTo),
90  myFromID(c.myFromID), myToID(c.myToID),
91  myFromLane(c.myFromLane), myToLane(c.myToLane),
92  myTlIndex(c.myTlIndex)
93 {}
94 
95 
96 NBEdge*
98  return myFrom;
99 }
100 
101 
102 NBEdge*
104  return myTo;
105 }
106 
107 
108 bool
110  if (myFrom == which) {
111  myFrom = by;
112  myFromID = myFrom->getID();
113  return true;
114  }
115  return false;
116 }
117 
118 
119 bool
120 NBConnection::replaceFrom(NBEdge* which, int whichLane,
121  NBEdge* by, int byLane) {
122  if (myFrom == which && (myFromLane == (int) whichLane || myFromLane < 0)) {
123  myFrom = by;
124  myFromID = myFrom->getID();
125  myFromLane = byLane;
126  return true;
127  }
128  return false;
129 }
130 
131 
132 bool
134  if (myTo == which) {
135  myTo = by;
136  myToID = myTo->getID();
137  return true;
138  }
139  return false;
140 }
141 
142 
143 bool
144 NBConnection::replaceTo(NBEdge* which, int whichLane,
145  NBEdge* by, int byLane) {
146  if (myTo == which && (myToLane == (int) whichLane || myFromLane < 0)) {
147  myTo = by;
148  myToID = myTo->getID();
149  myToLane = byLane;
150  return true;
151  }
152  return false;
153 }
154 
155 
156 bool
157 operator<(const NBConnection& c1, const NBConnection& c2) {
158  if (c1.myFromID != c2.myFromID) {
159  return c1.myFromID < c2.myFromID;
160  }
161  if (c1.myToID != c2.myToID) {
162  return c1.myToID < c2.myToID;
163  }
164  if (c1.myFromLane != c2.myFromLane) {
165  return c1.myFromLane < c2.myFromLane;
166  }
167  return c1.myToLane < c2.myToLane;
168 }
169 
170 
171 bool
173  return (myFrom == c.myFrom && myTo == c.myTo &&
174  myFromID == c.myFromID && myToID == c.myToID &&
175  myFromLane == c.myFromLane && myToLane == c.myToLane &&
176  myTlIndex == c.myTlIndex);
177 }
178 
179 
180 bool
182  myFrom = checkFrom(ec);
183  myTo = checkTo(ec);
184  return myFrom != 0 && myTo != 0;
185 }
186 
187 
188 NBEdge*
190  NBEdge* e = ec.retrieve(myFromID);
191  // ok, the edge was not changed
192  if (e == myFrom) {
193  return myFrom;
194  }
195  // try to get the edge
196  return ec.retrievePossiblySplit(myFromID, myToID, true);
197 }
198 
199 
200 NBEdge*
202  NBEdge* e = ec.retrieve(myToID);
203  // ok, the edge was not changed
204  if (e == myTo) {
205  return myTo;
206  }
207  // try to get the edge
208  return ec.retrievePossiblySplit(myToID, myFromID, false);
209 }
210 
211 
212 std::string
214  std::stringstream str;
215  str << myFromID << "_" << myFromLane << "->" << myToID << "_" << myToLane;
216  return str.str();
217 }
218 
219 
220 int
222  return myFromLane;
223 }
224 
225 
226 int
228  return myToLane;
229 }
230 
231 
232 void
234  if (myFrom == edge) {
235  myFromLane += offset;
236  } else if (myTo == edge) {
237  myToLane += offset;
238  }
239 }
240 
241 /****************************************************************************/
242