SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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.dlr.de/
12 // Copyright (C) 2001-2016 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()>(int) myFromLane);
78  assert(myToLane<0||to->getNumLanes()>(int) 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  if (myFrom != 0) {
113  myFromID = myFrom->getID();
114  } else {
115  myFromID = "invalidFrom";
116  }
117  return true;
118  }
119  return false;
120 }
121 
122 
123 bool
124 NBConnection::replaceFrom(NBEdge* which, int whichLane,
125  NBEdge* by, int byLane) {
126  if (myFrom == which && (myFromLane == whichLane || myFromLane < 0 || whichLane < 0)) {
127  myFrom = by;
128  if (myFrom != 0) {
129  myFromID = myFrom->getID();
130  } else {
131  myFromID = "invalidFrom";
132  }
133  myFromLane = byLane;
134  return true;
135  }
136  return false;
137 }
138 
139 
140 bool
142  if (myTo == which) {
143  myTo = by;
144  if (myTo != 0) {
145  myToID = myTo->getID();
146  } else {
147  myToID = "invalidTo";
148  }
149  return true;
150  }
151  return false;
152 }
153 
154 
155 bool
156 NBConnection::replaceTo(NBEdge* which, int whichLane,
157  NBEdge* by, int byLane) {
158  if (myTo == which && (myToLane == whichLane || myFromLane < 0 || whichLane < 0)) {
159  myTo = by;
160  if (myTo != 0) {
161  myToID = myTo->getID();
162  } else {
163  myToID = "invalidTo";
164  }
165  myToLane = byLane;
166  return true;
167  }
168  return false;
169 }
170 
171 
172 bool
173 operator<(const NBConnection& c1, const NBConnection& c2) {
174  if (c1.myFromID != c2.myFromID) {
175  return c1.myFromID < c2.myFromID;
176  }
177  if (c1.myToID != c2.myToID) {
178  return c1.myToID < c2.myToID;
179  }
180  if (c1.myFromLane != c2.myFromLane) {
181  return c1.myFromLane < c2.myFromLane;
182  }
183  return c1.myToLane < c2.myToLane;
184 }
185 
186 
187 bool
189  return (myFrom == c.myFrom && myTo == c.myTo &&
190  myFromID == c.myFromID && myToID == c.myToID &&
191  myFromLane == c.myFromLane && myToLane == c.myToLane &&
192  myTlIndex == c.myTlIndex);
193 }
194 
195 
196 bool
198  myFrom = checkFrom(ec);
199  myTo = checkTo(ec);
200  return myFrom != 0 && myTo != 0;
201 }
202 
203 
204 NBEdge*
206  NBEdge* e = ec.retrieve(myFromID);
207  // ok, the edge was not changed
208  if (e == myFrom) {
209  return myFrom;
210  }
211  // try to get the edge
212  return ec.retrievePossiblySplit(myFromID, myToID, true);
213 }
214 
215 
216 NBEdge*
218  NBEdge* e = ec.retrieve(myToID);
219  // ok, the edge was not changed
220  if (e == myTo) {
221  return myTo;
222  }
223  // try to get the edge
224  return ec.retrievePossiblySplit(myToID, myFromID, false);
225 }
226 
227 
228 std::string
230  std::stringstream str;
231  str << myFromID << "_" << myFromLane << "->" << myToID << "_" << myToLane;
232  return str.str();
233 }
234 
235 
236 int
238  return myFromLane;
239 }
240 
241 
242 int
244  return myToLane;
245 }
246 
247 
248 void
250  if (myFrom == edge) {
251  myFromLane += offset;
252  } else if (myTo == edge) {
253  myToLane += offset;
254  }
255 }
256 
257 
258 std::ostream&
259 operator<<(std::ostream& os, const NBConnection& c) {
260  os
261  << "Con(from=" << Named::getIDSecure(c.getFrom())
262  << " fromLane=" << c.getFromLane()
263  << " to=" << Named::getIDSecure(c.getTo())
264  << " toLane=" << c.getToLane()
265  << " tlIndex=" << c.getTLIndex()
266  << ")";
267  return os;
268 }
269 
270 
271 
272 /****************************************************************************/
273 
std::string myFromID
The names of both edges, needed for verification of validity.
Definition: NBConnection.h:141
bool check(const NBEdgeCont &ec)
checks whether the edges are still valid
NBEdge * retrievePossiblySplit(const std::string &id, bool downstream) const
Tries to retrieve an edge, even if it is splitted.
Definition: NBEdgeCont.cpp:283
int getTLIndex() const
returns the index within the controlling tls or InvalidTLIndex if this link is unontrolled ...
Definition: NBConnection.h:100
virtual ~NBConnection()
Destructor.
static const NBConnection InvalidConnection
Definition: NBConnection.h:127
The representation of a single edge during network building.
Definition: NBEdge.h:71
bool replaceTo(NBEdge *which, NBEdge *by)
replaces the to-edge by the one given
int getFromLane() const
returns the from-lane
static std::string getIDSecure(const T *obj, const std::string &fallBack="NULL")
get an identifier for Named-like object which may be Null
Definition: Named.h:59
NBEdge * getFrom() const
returns the from-edge (start of the connection)
void shiftLaneIndex(NBEdge *edge, int offset)
patches lane indices refering to the given edge
int myFromLane
The lanes; may be -1 if no certain lane was specified.
Definition: NBConnection.h:144
std::string getID() const
returns the id of the connection (!!! not really pretty)
const std::string & getID() const
Returns the id.
Definition: Named.h:66
bool operator==(const NBConnection &c) const
Comparison operator.
static const int InvalidTlIndex
Definition: NBConnection.h:126
bool replaceFrom(NBEdge *which, NBEdge *by)
replaces the from-edge by the one given
bool operator<(const NBConnection &c1, const NBConnection &c2)
NBEdge * checkTo(const NBEdgeCont &ec)
Checks whether the to-edge is still valid.
NBEdge * myTo
Definition: NBConnection.h:138
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
NBConnection(NBEdge *from, NBEdge *to)
Constructor.
std::string myToID
Definition: NBConnection.h:141
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:247
int getToLane() const
returns the to-lane
NBEdge * getTo() const
returns the to-edge (end of the connection)
std::ostream & operator<<(std::ostream &os, const NBConnection &c)
NBEdge * myFrom
The from- and the to-edges.
Definition: NBConnection.h:138
NBEdge * checkFrom(const NBEdgeCont &ec)
Checks whether the from-edge is still valid.