SUMO - Simulation of Urban MObility
NBContHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Some methods for traversing lists of 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 <vector>
34 #include <map>
35 #include <cassert>
36 #include "NBContHelper.h"
37 #include <utils/geom/GeomHelper.h>
38 
39 #ifdef CHECK_MEMORY_LEAKS
40 #include <foreign/nvwa/debug_new.h>
41 #endif // CHECK_MEMORY_LEAKS
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
47 /* -------------------------------------------------------------------------
48  * utility methods
49  * ----------------------------------------------------------------------- */
50 void
51 NBContHelper::nextCW(const EdgeVector& edges, EdgeVector::const_iterator& from) {
52  from++;
53  if (from == edges.end()) {
54  from = edges.begin();
55  }
56 }
57 
58 
59 void
60 NBContHelper::nextCCW(const EdgeVector& edges, EdgeVector::const_iterator& from) {
61  if (from == edges.begin()) {
62  from = edges.end() - 1;
63  } else {
64  --from;
65  }
66 }
67 
68 
69 std::ostream&
70 NBContHelper::out(std::ostream& os, const std::vector<bool>& v) {
71  for (std::vector<bool>::const_iterator i = v.begin(); i != v.end(); i++) {
72  os << *i;
73  }
74  return os;
75 }
76 
77 
78 NBEdge*
80  NBNode* from, NBNode* to) {
81  for (EdgeVector::const_iterator i = edges.begin(); i != edges.end(); i++) {
82  if ((*i)->getToNode() == to && (*i)->getFromNode() == from) {
83  return *i;
84  }
85  }
86  return 0;
87 }
88 
89 
90 
93  assert(ev.size() > 0);
94  SUMOReal max = (*(ev.begin()))->getSpeed();
95  for (EdgeVector::const_iterator i = ev.begin() + 1; i != ev.end(); i++) {
96  max =
97  max > (*i)->getSpeed()
98  ? max : (*i)->getSpeed();
99  }
100  return max;
101 }
102 
103 
104 
105 /* -------------------------------------------------------------------------
106  * methods from node_with_incoming_finder
107  * ----------------------------------------------------------------------- */
109  : myEdge(e) {}
110 
111 
112 bool
114  const EdgeVector& incoming = n->getIncomingEdges();
115  return std::find(incoming.begin(), incoming.end(), myEdge) != incoming.end();
116 }
117 
118 
119 
120 /* -------------------------------------------------------------------------
121  * methods from node_with_outgoing_finder
122  * ----------------------------------------------------------------------- */
124  : myEdge(e) {}
125 
126 
127 bool
129  const EdgeVector& outgoing = n->getOutgoingEdges();
130  return std::find(outgoing.begin(), outgoing.end(), myEdge) != outgoing.end();
131 }
132 
133 
134 
135 /* -------------------------------------------------------------------------
136  * methods from edge_with_destination_finder
137  * ----------------------------------------------------------------------- */
139  : myDestinationNode(dest) {}
140 
141 
142 bool
144  return e->getToNode() == myDestinationNode;
145 }
146 
147 /* -------------------------------------------------------------------------
148  * methods from relative_outgoing_edge_sorter
149  * ----------------------------------------------------------------------- */
150 int
152  if (e1 == 0 || e2 == 0) {
153  return -1;
154  }
155  SUMOReal relAngle1 = NBHelpers::normRelAngle(
156  myEdge->getEndAngle(), e1->getStartAngle());
157  SUMOReal relAngle2 = NBHelpers::normRelAngle(
158  myEdge->getEndAngle(), e2->getStartAngle());
159 
160  SUMOReal lookAhead = 2 * NBEdge::ANGLE_LOOKAHEAD;
161  while (fabs(relAngle1 - relAngle2) < 3.0) {
162  // look at further geometry segments to resolve ambiguity
163  const Position referencePos1 = e1->getGeometry().positionAtOffset2D(lookAhead);
164  const Position referencePos2 = e2->getGeometry().positionAtOffset2D(lookAhead);
165  relAngle1 = NBHelpers::normRelAngle(myEdge->getEndAngle(), GeomHelper::legacyDegree(
166  e1->getFromNode()->getPosition().angleTo2D(referencePos1), true));
167  relAngle2 = NBHelpers::normRelAngle(myEdge->getEndAngle(), GeomHelper::legacyDegree(
168  e2->getFromNode()->getPosition().angleTo2D(referencePos2), true));
169  if (lookAhead > MAX2(e1->getLength(), e2->getLength())) {
170  break;
171  }
172  lookAhead *= 2;
173  }
174  return relAngle1 > relAngle2;
175 }
176 
177 
178 /* -------------------------------------------------------------------------
179  * methods from relative_incoming_edge_sorter
180  * ----------------------------------------------------------------------- */
181 int
183  if (e1 == 0 || e2 == 0) {
184  return -1;
185  }
186  SUMOReal relAngle1 = NBHelpers::normRelAngle(
187  myEdge->getStartAngle(), e1->getEndAngle());
188  SUMOReal relAngle2 = NBHelpers::normRelAngle(
189  myEdge->getStartAngle(), e2->getEndAngle());
190 
191  SUMOReal lookAhead = 2 * NBEdge::ANGLE_LOOKAHEAD;
192  while (fabs(relAngle1 - relAngle2) < 3.0) {
193  // look at further geometry segments to resolve ambiguity
194  const Position referencePos1 = e1->getGeometry().positionAtOffset2D(e1->getGeometry().length() - lookAhead);
195  const Position referencePos2 = e2->getGeometry().positionAtOffset2D(e2->getGeometry().length() - lookAhead);
196  relAngle1 = NBHelpers::normRelAngle(myEdge->getStartAngle(), GeomHelper::legacyDegree(
197  referencePos1.angleTo2D(e1->getToNode()->getPosition()), true));
198  relAngle2 = NBHelpers::normRelAngle(myEdge->getStartAngle(), GeomHelper::legacyDegree(
199  referencePos2.angleTo2D(e2->getToNode()->getPosition()), true));
200  if (lookAhead > MAX2(e1->getLength(), e2->getLength())) {
201  break;
202  }
203  lookAhead *= 2;
204  }
205  return relAngle1 > relAngle2;
206 }
207 
208 
209 std::ostream&
210 operator<<(std::ostream& os, const EdgeVector& ev) {
211  for (EdgeVector::const_iterator i = ev.begin(); i != ev.end(); i++) {
212  if (i != ev.begin()) {
213  os << ", ";
214  }
215  os << (*i)->getID();
216  }
217  return os;
218 }
219 
220 
221 
222 
223 SUMOReal
225  if (edges.size() == 0) {
226  return -1;
227  }
228  SUMOReal ret = (*(edges.begin()))->getSpeed();
229  for (EdgeVector::const_iterator i = edges.begin() + 1; i != edges.end(); i++) {
230  if ((*i)->getSpeed() > ret) {
231  ret = (*i)->getSpeed();
232  }
233  }
234  return ret;
235 }
236 
237 
238 SUMOReal
240  if (edges.size() == 0) {
241  return -1;
242  }
243  SUMOReal ret = (*(edges.begin()))->getSpeed();
244  for (EdgeVector::const_iterator i = edges.begin() + 1; i != edges.end(); i++) {
245  if ((*i)->getSpeed() < ret) {
246  ret = (*i)->getSpeed();
247  }
248  }
249  return ret;
250 }
251 
252 
253 int
255  assert(e1->getFromNode() == myNode || e1->getToNode() == myNode);
256  assert(e2->getFromNode() == myNode || e2->getToNode() == myNode);
257  const SUMOReal angle1 = e1->getAngleAtNodeToCenter(myNode);
258  const SUMOReal angle2 = e2->getAngleAtNodeToCenter(myNode);
259  const SUMOReal absDiff = fabs(angle1 - angle2);
260 
261  // cannot trust the angle difference hence a heuristic:
262  if (absDiff < 2 || absDiff > (360 - 2)) {
263  const bool sameDir = ((e1->getFromNode() == myNode && e2->getFromNode() == myNode)
264  || (e1->getToNode() == myNode && e2->getToNode() == myNode));
265  if (sameDir) {
266  // put edges that allow pedestrians on the 'outside', but be aware if both allow / disallow
267  if (e1->getToNode() == myNode) {
268  if ((e1->getPermissions() & SVC_PEDESTRIAN) != 0) {
269  if ((e2->getPermissions() & SVC_PEDESTRIAN) == 0) {
270  return true;
271  }
272  }
273  } else {
274  if ((e1->getPermissions() & SVC_PEDESTRIAN) == 0) {
275  if ((e2->getPermissions() & SVC_PEDESTRIAN) != 0) {
276  return true;
277  }
278  }
279  }
280  // break ties to ensure strictly weak ordering
281  return e1->getID() < e2->getID();
282  } else {
283  // sort incoming before outgoing, no need to break ties here
284  return e1->getToNode() == myNode;
285  }
286  }
287  return angle1 < angle2;
288 }
289 
290 /****************************************************************************/
291 
const EdgeVector & getIncomingEdges() const
Returns this node&#39;s incoming edges.
Definition: NBNode.h:240
node_with_outgoing_finder(const NBEdge *const e)
constructor
is a pedestrian
static SUMOReal normRelAngle(SUMOReal angle1, SUMOReal angle2)
ensure that reverse relAngles (>=179.999) always count as turnarounds (-180)
Definition: NBHelpers.cpp:69
SUMOReal getEndAngle() const
Returns the angle at the end of the edge The angle is computed in computeAngle()
Definition: NBEdge.h:387
friend std::ostream & operator<<(std::ostream &os, const EdgeVector &ev)
The representation of a single edge during network building.
Definition: NBEdge.h:70
T MAX2(T a, T b)
Definition: StdDefs.h:75
bool operator()(const NBNode *const n) const
static void nextCW(const EdgeVector &edges, EdgeVector::const_iterator &from)
int operator()(const NBEdge *e1, const NBEdge *e2) const
comparing operation
Position positionAtOffset2D(SUMOReal pos, SUMOReal lateralOffset=0) const
Returns the position at the given length.
const EdgeVector & getOutgoingEdges() const
Returns this node&#39;s outgoing edges.
Definition: NBNode.h:248
const std::string & getID() const
Returns the id.
Definition: Named.h:65
const Position & getPosition() const
Returns the position of this node.
Definition: NBNode.h:228
#define max(a, b)
Definition: polyfonts.c:65
static std::ostream & out(std::ostream &os, const std::vector< bool > &v)
static SUMOReal legacyDegree(const SUMOReal angle, const bool positive=false)
Definition: GeomHelper.cpp:204
bool operator()(const NBNode *const n) const
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
static SUMOReal maxSpeed(const EdgeVector &ev)
SUMOReal getAngleAtNodeToCenter(const NBNode *const node) const
Returns the angle of from the node shape center to where the edge meets the node shape.
Definition: NBEdge.cpp:1299
edge_with_destination_finder(NBNode *dest)
constructor
SVCPermissions getPermissions(int lane=-1) const
get the union of allowed classes over all lanes or for a specific lane
Definition: NBEdge.cpp:2393
static SUMOReal getMaxSpeed(const EdgeVector &edges)
SUMOReal length() const
Returns the length.
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:369
std::vector< NBEdge * > EdgeVector
Definition: NBCont.h:41
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:523
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
node_with_incoming_finder(const NBEdge *const e)
constructor
Represents a single node (junction) during network building.
Definition: NBNode.h:74
#define SUMOReal
Definition: config.h:213
SUMOReal getStartAngle() const
Returns the angle at the start of the edge The angle is computed in computeAngle() ...
Definition: NBEdge.h:378
static SUMOReal getMinSpeed(const EdgeVector &edges)
static void nextCCW(const EdgeVector &edges, EdgeVector::const_iterator &from)
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
SUMOReal angleTo2D(const Position &other) const
returns the angle in the plane of the vector pointing from here to the other position ...
Definition: Position.h:243
static const SUMOReal ANGLE_LOOKAHEAD
the distance at which to take the default angle
Definition: NBEdge.h:214
static NBEdge * findConnectingEdge(const EdgeVector &edges, NBNode *from, NBNode *to)
SUMOReal getLength() const
Returns the computed length of the edge.
Definition: NBEdge.h:404
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:361