SUMO - Simulation of Urban MObility
NBAlgorithms.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Algorithms for network computation
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2012-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <sstream>
33 #include <iostream>
34 #include <cassert>
35 #include <algorithm>
37 #include <utils/common/ToString.h>
38 #include "NBEdge.h"
39 #include "NBNodeCont.h"
40 #include "NBTypeCont.h"
41 #include "NBNode.h"
42 #include "NBAlgorithms.h"
43 
44 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
48 // ---------------------------------------------------------------------------
49 // NBTurningDirectionsComputer
50 // ---------------------------------------------------------------------------
51 void
53  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
54  computeTurnDirectionsForNode(i->second, warn);
55  }
56 }
57 
58 void
60  const std::vector<NBEdge*>& incoming = node->getIncomingEdges();
61  const std::vector<NBEdge*>& outgoing = node->getOutgoingEdges();
62  // reset turning directions since this may be called multiple times
63  for (std::vector<NBEdge*>::const_iterator k = incoming.begin(); k != incoming.end(); ++k) {
64  (*k)->setTurningDestination(0);
65  }
66  std::vector<Combination> combinations;
67  for (std::vector<NBEdge*>::const_iterator j = outgoing.begin(); j != outgoing.end(); ++j) {
68  NBEdge* outedge = *j;
69  for (std::vector<NBEdge*>::const_iterator k = incoming.begin(); k != incoming.end(); ++k) {
70  NBEdge* e = *k;
71  // @todo: check whether NBHelpers::relAngle is properly defined and whether it should really be used, here
72  const double signedAngle = NBHelpers::normRelAngle(e->getAngleAtNode(node), outedge->getAngleAtNode(node));
73  if (signedAngle > 0 && signedAngle < 177 && e->getGeometry().back().distanceTo2D(outedge->getGeometry().front()) < POSITION_EPS) {
74  // backwards curving edges can only be turnaround when there are
75  // non-default endpoints
76  continue;
77  }
78  double angle = fabs(signedAngle);
79  // std::cout << "incoming=" << e->getID() << " outgoing=" << outedge->getID() << " relAngle=" << NBHelpers::relAngle(e->getAngleAtNode(node), outedge->getAngleAtNode(node)) << "\n";
80  if (e->getFromNode() == outedge->getToNode() && angle > 120) {
81  // they connect the same nodes; should be the turnaround direction
82  // we'll assign a maximum number
83  //
84  // @todo: indeed, we have observed some pathological intersections
85  // see "294831560" in OSM/adlershof. Here, several edges are connecting
86  // same nodes. We have to do the angle check before...
87  //
88  // @todo: and well, there are some other as well, see plain import
89  // of delphi_muenchen (elmar), intersection "59534191". Not that it would
90  // be realistic in any means; we will warn, here.
91  angle += 360;
92  }
93  if (angle < 160) {
94  continue;
95  }
96  Combination c;
97  c.from = e;
98  c.to = outedge;
99  c.angle = angle;
100  combinations.push_back(c);
101  }
102  }
103  // sort combinations so that the ones with the highest angle are at the begin
104  std::sort(combinations.begin(), combinations.end(), combination_by_angle_sorter());
105  std::set<NBEdge*> seen;
106  for (std::vector<Combination>::const_iterator j = combinations.begin(); j != combinations.end(); ++j) {
107  if (seen.find((*j).from) != seen.end() || seen.find((*j).to) != seen.end()) {
108  // do not regard already set edges
109  if ((*j).angle > 360 && warn) {
110  WRITE_WARNING("Ambiguity in turnarounds computation at junction '" + node->getID() + "'.");
111  warn = false;
112  }
113  continue;
114  }
115  // mark as seen
116  seen.insert((*j).from);
117  seen.insert((*j).to);
118  // set turnaround information
119  bool onlyPossible = (*j).from->getConnections().size() != 0 && !(*j).from->isConnectedTo((*j).to);
120  //std::cout << " setTurningDestination from=" << (*j).from->getID() << " to=" << (*j).to->getID() << " onlyPossible=" << onlyPossible << "\n";
121  (*j).from->setTurningDestination((*j).to, onlyPossible);
122  }
123 }
124 
125 
126 // ---------------------------------------------------------------------------
127 // NBNodesEdgesSorter
128 // ---------------------------------------------------------------------------
129 void
131  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
132  i->second->sortEdges(useNodeShape);
133  }
134 }
135 
136 
137 void
139  const std::vector<NBEdge*>::iterator& i1,
140  const std::vector<NBEdge*>::iterator& i2) {
141  NBEdge* e1 = *i1;
142  NBEdge* e2 = *i2;
143  // @todo: The difference between "isTurningDirectionAt" and "isTurnaround"
144  // is not nice. Maybe we could get rid of it if we would always mark edges
145  // as turnarounds, even if they do not have to be added, as mentioned in
146  // notes on NBTurningDirectionsComputer::computeTurnDirectionsForNode
147  if (e2->getToNode() == n && e2->isTurningDirectionAt(e1)) {
148  std::swap(*i1, *i2);
149  }
150 }
151 
152 
153 // ---------------------------------------------------------------------------
154 // NBNodeTypeComputer
155 // ---------------------------------------------------------------------------
156 void
158  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
159  NBNode* n = (*i).second;
160  // the type may already be set from the data
161  if (n->myType != NODETYPE_UNKNOWN && n->myType != NODETYPE_DEAD_END) {
162  continue;
163  }
164  // check whether the node is a waterway node. Set to unregulated by default
165  bool waterway = true;
166  for (EdgeVector::const_iterator i = n->getEdges().begin(); i != n->getEdges().end(); ++i) {
167  if (!isWaterway((*i)->getPermissions())) {
168  waterway = false;
169  break;
170  }
171  }
172  if (waterway && (n->myType == NODETYPE_UNKNOWN || n->myType == NODETYPE_DEAD_END)) {
174  continue;
175  }
176 
177  // check whether the junction is not a real junction
178  if (n->myIncomingEdges.size() == 1) {
180  continue;
181  }
182  // @todo "isSimpleContinuation" should be revalidated
183  if (n->isSimpleContinuation()) {
185  continue;
186  }
187  // determine the type
189  for (EdgeVector::const_iterator i = n->myIncomingEdges.begin(); i != n->myIncomingEdges.end(); i++) {
190  for (EdgeVector::const_iterator j = i + 1; j != n->myIncomingEdges.end(); j++) {
191  // @todo "getOppositeIncoming" should probably be refactored into something the edge knows
192  if (n->getOppositeIncoming(*j) == *i && n->myIncomingEdges.size() > 2) {
193  continue;
194  }
195  // @todo check against a legal document
196  // @todo figure out when NODETYPE_PRIORITY_STOP is appropriate
197  const double s1 = (*i)->getSpeed() * (double) 3.6;
198  const double s2 = (*j)->getSpeed() * (double) 3.6;
199  const int p1 = (*i)->getPriority();
200  const int p2 = (*j)->getPriority();
201  if (fabs(s1 - s2) > (double) 9.5 || MAX2(s1, s2) >= (double) 49. || p1 != p2) {
202  type = NODETYPE_PRIORITY;
203  break;
204  }
205  }
206  }
207  // save type
208  n->myType = type;
209  }
210 }
211 
212 
213 void
215  // the type may already be set from the data
216  if (node->myType != NODETYPE_UNKNOWN && node->myType != NODETYPE_DEAD_END) {
217  }
218  // check whether the node is a waterway node. Set to unregulated by default
219  bool waterway = true;
220  for (EdgeVector::const_iterator i = node->getEdges().begin(); i != node->getEdges().end(); ++i) {
221  if (!isWaterway((*i)->getPermissions())) {
222  waterway = false;
223  break;
224  }
225  }
226  if (waterway && (node->myType == NODETYPE_UNKNOWN || node->myType == NODETYPE_DEAD_END)) {
227  node->myType = NODETYPE_NOJUNCTION;
228  }
229 
230  // check whether the junction is not a real junction
231  if (node->myIncomingEdges.size() == 1) {
232  node->myType = NODETYPE_PRIORITY;
233  }
234  // @todo "isSimpleContinuation" should be revalidated
235  if (node->isSimpleContinuation()) {
236  node->myType = NODETYPE_PRIORITY;
237  }
238  // determine the type
240  for (EdgeVector::const_iterator i = node->myIncomingEdges.begin(); i != node->myIncomingEdges.end(); i++) {
241  for (EdgeVector::const_iterator j = i + 1; j != node->myIncomingEdges.end(); j++) {
242  // @todo "getOppositeIncoming" should probably be refactored into something the edge knows
243  if (node->getOppositeIncoming(*j) == *i && node->myIncomingEdges.size() > 2) {
244  continue;
245  }
246  // @todo check against a legal document
247  // @todo figure out when NODETYPE_PRIORITY_STOP is appropriate
248  const double s1 = (*i)->getSpeed() * (double) 3.6;
249  const double s2 = (*j)->getSpeed() * (double) 3.6;
250  const int p1 = (*i)->getPriority();
251  const int p2 = (*j)->getPriority();
252  if (fabs(s1 - s2) > (double) 9.5 || MAX2(s1, s2) >= (double) 49. || p1 != p2) {
253  type = NODETYPE_PRIORITY;
254  break;
255  }
256  }
257  }
258  // save type
259  node->myType = type;
260 }
261 
262 // ---------------------------------------------------------------------------
263 // NBEdgePriorityComputer
264 // ---------------------------------------------------------------------------
265 void
267  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
268  NBNode* n = (*i).second;
269  // preset all junction's edge priorities to zero
270  for (EdgeVector::iterator j = n->myAllEdges.begin(); j != n->myAllEdges.end(); ++j) {
271  (*j)->setJunctionPriority(n, NBEdge::MINOR_ROAD);
272  }
273  // check if the junction is not a real junction
274  if (n->myIncomingEdges.size() == 1 && n->myOutgoingEdges.size() == 1) {
275  continue;
276  }
277  // compute the priorities on junction when needed
279  setPriorityJunctionPriorities(*n);
280  }
281  }
282 }
283 
284 
285 void
287  // preset all junction's edge priorities to zero
288  for (EdgeVector::iterator j = node->myAllEdges.begin(); j != node->myAllEdges.end(); ++j) {
289  (*j)->setJunctionPriority(node, NBEdge::MINOR_ROAD);
290  }
291  // check if the junction is not a real junction
292  if (node->myIncomingEdges.size() == 1 && node->myOutgoingEdges.size() == 1) {
293  }
294  // compute the priorities on junction when needed
295  if (node->getType() != NODETYPE_RIGHT_BEFORE_LEFT) {
296  setPriorityJunctionPriorities(*node);
297  }
298 }
299 
300 
301 void
303  if (n.myIncomingEdges.size() == 0 || n.myOutgoingEdges.size() == 0) {
304  return;
305  }
306  EdgeVector incoming = n.myIncomingEdges;
307  EdgeVector outgoing = n.myOutgoingEdges;
308  // what we do want to have is to extract the pair of roads that are
309  // the major roads for this junction
310  // let's get the list of incoming edges with the highest priority
311  std::sort(incoming.begin(), incoming.end(), NBContHelper::edge_by_priority_sorter());
312  EdgeVector bestIncoming;
313  NBEdge* best = incoming[0];
314  while (incoming.size() > 0 && samePriority(best, incoming[0])) {
315  bestIncoming.push_back(*incoming.begin());
316  incoming.erase(incoming.begin());
317  }
318  // now, let's get the list of best outgoing
319  assert(outgoing.size() != 0);
320  sort(outgoing.begin(), outgoing.end(), NBContHelper::edge_by_priority_sorter());
321  EdgeVector bestOutgoing;
322  best = outgoing[0];
323  while (outgoing.size() > 0 && samePriority(best, outgoing[0])) { //->getPriority()==best->getPriority()) {
324  bestOutgoing.push_back(*outgoing.begin());
325  outgoing.erase(outgoing.begin());
326  }
327  // special case: user input makes mainDirection unambiguous
328  const bool mainDirectionExplicit = (
329  bestIncoming.size() == 1 && n.myIncomingEdges.size() <= 2
330  && (incoming.size() == 0 || bestIncoming[0]->getPriority() > incoming[0]->getPriority())
331  && bestOutgoing.size() == 1 && n.myOutgoingEdges.size() <= 2
332  && (outgoing.size() == 0 || bestOutgoing[0]->getPriority() > outgoing[0]->getPriority())
333  && !bestIncoming[0]->isTurningDirectionAt(bestOutgoing[0]));
334  // now, let's compute for each of the best incoming edges
335  // the incoming which is most opposite
336  // the outgoing which is most opposite
337  EdgeVector::iterator i;
338  std::map<NBEdge*, NBEdge*> counterIncomingEdges;
339  std::map<NBEdge*, NBEdge*> counterOutgoingEdges;
340  incoming = n.myIncomingEdges;
341  outgoing = n.myOutgoingEdges;
342  for (i = bestIncoming.begin(); i != bestIncoming.end(); ++i) {
343  std::sort(incoming.begin(), incoming.end(), NBContHelper::edge_opposite_direction_sorter(*i, &n));
344  counterIncomingEdges[*i] = *incoming.begin();
345  std::sort(outgoing.begin(), outgoing.end(), NBContHelper::edge_opposite_direction_sorter(*i, &n));
346  counterOutgoingEdges[*i] = *outgoing.begin();
347  }
348  // ok, let's try
349  // 1) there is one best incoming road
350  if (bestIncoming.size() == 1) {
351  // let's mark this road as the best
352  NBEdge* best1 = extractAndMarkFirst(n, bestIncoming);
353  if (!mainDirectionExplicit && counterIncomingEdges.find(best1) != counterIncomingEdges.end()) {
354  // ok, look, what we want is the opposit of the straight continuation edge
355  // but, what if such an edge does not exist? By now, we'll determine it
356  // geometrically
357  NBEdge* s = counterIncomingEdges.find(best1)->second;
358  if (GeomHelper::getMinAngleDiff(best1->getAngleAtNode(&n), s->getAngleAtNode(&n)) > 180 - 45) {
360  }
361  }
362  assert(bestOutgoing.size() != 0);
363  // mark the best outgoing as the continuation
364  sort(bestOutgoing.begin(), bestOutgoing.end(), NBContHelper::edge_similar_direction_sorter(best1));
365  // assign extra priority if the priorities are unambiguous (regardless of geometry)
366  best1 = extractAndMarkFirst(n, bestOutgoing);
367  if (!mainDirectionExplicit && counterOutgoingEdges.find(best1) != counterOutgoingEdges.end()) {
368  NBEdge* s = counterOutgoingEdges.find(best1)->second;
369  if (GeomHelper::getMinAngleDiff(best1->getAngleAtNode(&n), s->getAngleAtNode(&n)) > 180 - 45) {
370  s->setJunctionPriority(&n, 1);
371  }
372  }
373  return;
374  }
375 
376  // ok, what we want to do in this case is to determine which incoming
377  // has the best continuation...
378  // This means, when several incoming roads have the same priority,
379  // we want a (any) straight connection to be more priorised than a turning
380  double bestAngle = 0;
381  NBEdge* bestFirst = 0;
382  NBEdge* bestSecond = 0;
383  bool hadBest = false;
384  for (i = bestIncoming.begin(); i != bestIncoming.end(); ++i) {
385  EdgeVector::iterator j;
386  NBEdge* t1 = *i;
387  double angle1 = t1->getAngleAtNode(&n) + 180;
388  if (angle1 >= 360) {
389  angle1 -= 360;
390  }
391  for (j = i + 1; j != bestIncoming.end(); ++j) {
392  NBEdge* t2 = *j;
393  double angle2 = t2->getAngleAtNode(&n) + 180;
394  if (angle2 >= 360) {
395  angle2 -= 360;
396  }
397  double angle = GeomHelper::getMinAngleDiff(angle1, angle2);
398  if (!hadBest || angle > bestAngle) {
399  bestAngle = angle;
400  bestFirst = *i;
401  bestSecond = *j;
402  hadBest = true;
403  }
404  }
405  }
406  bestFirst->setJunctionPriority(&n, 1);
407  sort(bestOutgoing.begin(), bestOutgoing.end(), NBContHelper::edge_similar_direction_sorter(bestFirst));
408  if (bestOutgoing.size() != 0) {
409  extractAndMarkFirst(n, bestOutgoing);
410  }
411  bestSecond->setJunctionPriority(&n, 1);
412  sort(bestOutgoing.begin(), bestOutgoing.end(), NBContHelper::edge_similar_direction_sorter(bestSecond));
413  if (bestOutgoing.size() != 0) {
414  extractAndMarkFirst(n, bestOutgoing);
415  }
416 }
417 
418 
419 NBEdge*
420 NBEdgePriorityComputer::extractAndMarkFirst(NBNode& n, std::vector<NBEdge*>& s, int prio) {
421  if (s.size() == 0) {
422  return 0;
423  }
424  NBEdge* ret = s.front();
425  s.erase(s.begin());
426  ret->setJunctionPriority(&n, prio);
427  return ret;
428 }
429 
430 
431 bool
432 NBEdgePriorityComputer::samePriority(const NBEdge* const e1, const NBEdge* const e2) {
433  if (e1 == e2) {
434  return true;
435  }
436  if (e1->getPriority() != e2->getPriority()) {
437  return false;
438  }
439  if ((int) e1->getSpeed() != (int) e2->getSpeed()) {
440  return false;
441  }
442  return (int) e1->getNumLanes() == (int) e2->getNumLanes();
443 }
444 
445 
447  // reorder based on getAngleAtNodeToCenter
448  myOrdering = ordering;
449  sort(myOrdering.begin(), myOrdering.end(), NBContHelper::edge_by_angle_to_nodeShapeCentroid_sorter(node));
450  // let the first edge remain the first
451  rotate(myOrdering.begin(), std::find(myOrdering.begin(), myOrdering.end(), ordering.front()), myOrdering.end());
452 }
453 
454 
455 /****************************************************************************/
456 
static double getMinAngleDiff(double angle1, double angle2)
Returns the minimum distance (clockwise/counter-clockwise) between both angles.
Definition: GeomHelper.cpp:168
std::map< std::string, NBNode * >::const_iterator begin() const
Returns the pointer to the begin of the stored nodes.
Definition: NBNodeCont.h:114
std::map< std::string, NBNode * >::const_iterator end() const
Returns the pointer to the end of the stored nodes.
Definition: NBNodeCont.h:119
static double normRelAngle(double angle1, double angle2)
ensure that reverse relAngles (>=179.999) always count as turnarounds (-180)
Definition: NBHelpers.cpp:65
SumoXMLNodeType myType
The type of the junction.
Definition: NBNode.h:741
int getPriority() const
Returns the priority of the edge.
Definition: NBEdge.h:420
The representation of a single edge during network building.
Definition: NBEdge.h:71
Class to sort edges by their angle in relation to the given edge.
Definition: NBContHelper.h:178
T MAX2(T a, T b)
Definition: StdDefs.h:70
static void computeSingleNodeType(NBNode *node)
Computes a single node type.
const std::string & getID() const
Returns the id.
Definition: Named.h:66
static void sortNodesEdges(NBNodeCont &nc, bool useNodeShape=false)
Sorts a node&#39;s edges clockwise regarding driving direction.
Stores the information about the angle between an incoming ("from") and an outgoing ("to") edge...
Definition: NBAlgorithms.h:75
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
const EdgeVector & getOutgoingEdges() const
Returns this node&#39;s outgoing edges (The edges which start at this node)
Definition: NBNode.h:245
EdgeVector myAllEdges
Vector of incoming and outgoing edges.
Definition: NBNode.h:732
static void swapWhenReversed(const NBNode *const n, const std::vector< NBEdge *>::iterator &i1, const std::vector< NBEdge *>::iterator &i2)
Assures correct order for same-angle opposite-direction edges.
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:413
static void computeEdgePriorities(NBNodeCont &nc)
Computes edge priorities within a node.
bool isWaterway(SVCPermissions permissions)
Returns whether an edge with the given permission is a waterway edge.
const EdgeVector & getEdges() const
Returns all edges which participate in this node (Edges that start or end at this node) ...
Definition: NBNode.h:250
#define POSITION_EPS
Definition: config.h:175
double getAngleAtNode(const NBNode *const node) const
Returns the angle of the edge&#39;s geometry at the given node.
Definition: NBEdge.cpp:1576
crossing_by_junction_angle_sorter(const NBNode *node, const EdgeVector &ordering)
EdgeVector myIncomingEdges
Vector of incoming edges.
Definition: NBNode.h:726
static void computeTurnDirectionsForNode(NBNode *node, bool warn)
Computes turnaround destinations for all incoming edges of the given nodes (if any) ...
static bool samePriority(const NBEdge *const e1, const NBEdge *const e2)
Returns whether both edges have the same priority.
double getSpeed() const
Returns the speed allowed on this edge.
Definition: NBEdge.h:507
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:595
EdgeVector myOutgoingEdges
Vector of outgoing edges.
Definition: NBNode.h:729
static void computeEdgePrioritiesSingleNode(NBNode *node)
Computes edge priorities within a single node.
const EdgeVector & getIncomingEdges() const
Returns this node&#39;s incoming edges (The edges which yield in this node)
Definition: NBNode.h:240
static void computeNodeTypes(NBNodeCont &nc)
Computes node types.
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:41
void setJunctionPriority(const NBNode *const node, int prio)
Sets the junction priority of the edge.
Definition: NBEdge.cpp:1566
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:257
bool isTurningDirectionAt(const NBEdge *const edge) const
Returns whether the given edge is the opposite direction to this edge.
Definition: NBEdge.cpp:2367
Represents a single node (junction) during network building.
Definition: NBNode.h:75
static void computeTurnDirections(NBNodeCont &nc, bool warn=true)
Computes turnaround destinations for all edges (if exist)
static NBEdge * extractAndMarkFirst(NBNode &n, std::vector< NBEdge *> &s, int prio=1)
Sets the priorites in case of a priority junction.
NBEdge * getOppositeIncoming(NBEdge *e) const
returns the opposite incoming edge of certain edge
Definition: NBNode.cpp:1230
Sorts "Combination"s by decreasing angle.
Definition: NBAlgorithms.h:85
static void setPriorityJunctionPriorities(NBNode &n)
Sets the priorites in case of a priority junction.
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:427
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:63
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:434
bool isSimpleContinuation(bool checkLaneNumbers=true) const
check if node is a simple continuation
Definition: NBNode.cpp:432