SUMO - Simulation of Urban MObility
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-2017 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 
41 // ===========================================================================
42 // static members
43 // ===========================================================================
44 const int NBConnection::InvalidTlIndex = -1;
45 const NBConnection NBConnection::InvalidConnection("invalidFrom", 0, "invalidTo", 0);
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
51  myFrom(from), myTo(to),
52  myFromID(from->getID()), myToID(to->getID()),
53  myFromLane(-1), myToLane(-1),
54  myTlIndex(InvalidTlIndex) {
55 }
56 
57 
58 NBConnection::NBConnection(const std::string& fromID, NBEdge* from,
59  const std::string& toID, NBEdge* to) :
60  myFrom(from), myTo(to),
61  myFromID(fromID), myToID(toID),
62  myFromLane(-1), myToLane(-1),
64 }
65 
66 
67 NBConnection::NBConnection(NBEdge* from, int fromLane,
68  NBEdge* to, int toLane, int tlIndex) :
69  myFrom(from), myTo(to),
70  myFromLane(fromLane), myToLane(toLane),
71  myTlIndex(tlIndex) {
72  /* @todo what should we assert here?
73  assert(myFromLane<0||from->getNumLanes()>(int) myFromLane);
74  assert(myToLane<0||to->getNumLanes()>(int) myToLane);
75  */
76  myFromID = from->getID();
77  myToID = to != 0 ? to->getID() : "";
78 }
79 
80 
82 
83 
85  myFrom(c.myFrom), myTo(c.myTo),
88  myTlIndex(c.myTlIndex) {
89 }
90 
91 
92 NBEdge*
94  return myFrom;
95 }
96 
97 
98 NBEdge*
100  return myTo;
101 }
102 
103 
104 bool
106  if (myFrom == which) {
107  myFrom = by;
108  if (myFrom != 0) {
109  myFromID = myFrom->getID();
110  } else {
111  myFromID = "invalidFrom";
112  }
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 == whichLane || myFromLane < 0 || whichLane < 0)) {
123  myFrom = by;
124  if (myFrom != 0) {
125  myFromID = myFrom->getID();
126  } else {
127  myFromID = "invalidFrom";
128  }
129  if (byLane >= 0) {
130  myFromLane = byLane;
131  }
132  return true;
133  }
134  return false;
135 }
136 
137 
138 bool
140  if (myTo == which) {
141  myTo = by;
142  if (myTo != 0) {
143  myToID = myTo->getID();
144  } else {
145  myToID = "invalidTo";
146  }
147  return true;
148  }
149  return false;
150 }
151 
152 
153 bool
154 NBConnection::replaceTo(NBEdge* which, int whichLane,
155  NBEdge* by, int byLane) {
156  if (myTo == which && (myToLane == whichLane || myFromLane < 0 || whichLane < 0)) {
157  myTo = by;
158  if (myTo != 0) {
159  myToID = myTo->getID();
160  } else {
161  myToID = "invalidTo";
162  }
163  if (byLane >= 0) {
164  myToLane = byLane;
165  }
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
std::string getID() const
returns the id of the connection (!!! not really pretty)
bool check(const NBEdgeCont &ec)
checks whether the edges are still valid
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
friend bool operator<(const NBConnection &c1, const NBConnection &c2)
Compares both connections in order to allow sorting.
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)
const std::string & getID() const
Returns the id.
Definition: Named.h:66
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
static const int InvalidTlIndex
Definition: NBConnection.h:126
bool replaceFrom(NBEdge *which, NBEdge *by)
replaces the from-edge by the one given
NBEdge * retrievePossiblySplit(const std::string &id, bool downstream) const
Tries to retrieve an edge, even if it is splitted.
Definition: NBEdgeCont.cpp:287
NBEdge * checkTo(const NBEdgeCont &ec)
Checks whether the to-edge is still valid.
friend std::ostream & operator<<(std::ostream &os, const NBConnection &c)
Output operator.
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
bool operator==(const NBConnection &c) const
Comparison operator.
int getToLane() const
returns the to-lane
NBEdge * getTo() const
returns the to-edge (end of the connection)
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:251
int getFromLane() const
returns the from-lane
NBEdge * myFrom
The from- and the to-edges.
Definition: NBConnection.h:138
int getTLIndex() const
returns the index within the controlling tls or InvalidTLIndex if this link is unontrolled ...
Definition: NBConnection.h:100
NBEdge * checkFrom(const NBEdgeCont &ec)
Checks whether the from-edge is still valid.