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 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 // ---------------------------------------------------------------------------
53 // NBTurningDirectionsComputer
54 // ---------------------------------------------------------------------------
55 void
57  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
58  computeTurnDirectionsForNode(i->second, warn);
59  }
60 }
61 
62 void
64  const std::vector<NBEdge*>& incoming = node->getIncomingEdges();
65  const std::vector<NBEdge*>& outgoing = node->getOutgoingEdges();
66  // reset turning directions since this may be called multiple times
67  for (std::vector<NBEdge*>::const_iterator k = incoming.begin(); k != incoming.end(); ++k) {
68  (*k)->setTurningDestination(0);
69  }
70  std::vector<Combination> combinations;
71  for (std::vector<NBEdge*>::const_iterator j = outgoing.begin(); j != outgoing.end(); ++j) {
72  NBEdge* outedge = *j;
73  for (std::vector<NBEdge*>::const_iterator k = incoming.begin(); k != incoming.end(); ++k) {
74  NBEdge* e = *k;
75  // @todo: check whether NBHelpers::relAngle is properly defined and whether it should really be used, here
76  const SUMOReal signedAngle = NBHelpers::normRelAngle(e->getAngleAtNode(node), outedge->getAngleAtNode(node));
77  if (signedAngle > 0 && signedAngle < 177 && e->getGeometry().back().distanceTo2D(outedge->getGeometry().front()) < POSITION_EPS) {
78  // backwards curving edges can only be turnaround when there are
79  // non-default endpoints
80  continue;
81  }
82  SUMOReal angle = fabs(signedAngle);
83  // std::cout << "incoming=" << e->getID() << " outgoing=" << outedge->getID() << " relAngle=" << NBHelpers::relAngle(e->getAngleAtNode(node), outedge->getAngleAtNode(node)) << "\n";
84  if (e->getFromNode() == outedge->getToNode() && angle > 120) {
85  // they connect the same nodes; should be the turnaround direction
86  // we'll assign a maximum number
87  //
88  // @todo: indeed, we have observed some pathological intersections
89  // see "294831560" in OSM/adlershof. Here, several edges are connecting
90  // same nodes. We have to do the angle check before...
91  //
92  // @todo: and well, there are some other as well, see plain import
93  // of delphi_muenchen (elmar), intersection "59534191". Not that it would
94  // be realistic in any means; we will warn, here.
95  angle += 360;
96  }
97  if (angle < 160) {
98  continue;
99  }
100  Combination c;
101  c.from = e;
102  c.to = outedge;
103  c.angle = angle;
104  combinations.push_back(c);
105  }
106  }
107  // sort combinations so that the ones with the highest angle are at the begin
108  std::sort(combinations.begin(), combinations.end(), combination_by_angle_sorter());
109  std::set<NBEdge*> seen;
110  for (std::vector<Combination>::const_iterator j = combinations.begin(); j != combinations.end(); ++j) {
111  if (seen.find((*j).from) != seen.end() || seen.find((*j).to) != seen.end()) {
112  // do not regard already set edges
113  if ((*j).angle > 360 && warn) {
114  WRITE_WARNING("Ambiguity in turnarounds computation at junction '" + node->getID() + "'.");
115  warn = false;
116  }
117  continue;
118  }
119  // mark as seen
120  seen.insert((*j).from);
121  seen.insert((*j).to);
122  // set turnaround information
123  bool onlyPossible = (*j).from->getConnections().size() != 0 && !(*j).from->isConnectedTo((*j).to);
124  //std::cout << " setTurningDestination from=" << (*j).from->getID() << " to=" << (*j).to->getID() << " onlyPossible=" << onlyPossible << "\n";
125  (*j).from->setTurningDestination((*j).to, onlyPossible);
126  }
127 }
128 
129 
130 // ---------------------------------------------------------------------------
131 // NBNodesEdgesSorter
132 // ---------------------------------------------------------------------------
133 void
135  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
136  i->second->sortEdges(useNodeShape);
137  }
138 }
139 
140 
141 void
143  const std::vector<NBEdge*>::iterator& i1,
144  const std::vector<NBEdge*>::iterator& i2) {
145  NBEdge* e1 = *i1;
146  NBEdge* e2 = *i2;
147  // @todo: The difference between "isTurningDirectionAt" and "isTurnaround"
148  // is not nice. Maybe we could get rid of it if we would always mark edges
149  // as turnarounds, even if they do not have to be added, as mentioned in
150  // notes on NBTurningDirectionsComputer::computeTurnDirectionsForNode
151  if (e2->getToNode() == n && e2->isTurningDirectionAt(e1)) {
152  std::swap(*i1, *i2);
153  }
154 }
155 
156 
157 // ---------------------------------------------------------------------------
158 // NBNodeTypeComputer
159 // ---------------------------------------------------------------------------
160 void
162  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
163  NBNode* n = (*i).second;
164  // the type may already be set from the data
165  if (n->myType != NODETYPE_UNKNOWN) {
166  continue;
167  }
168  // check whether the node is a waterway node. Set to unregulated by default
169  bool waterway = true;
170  for (EdgeVector::const_iterator i = n->getEdges().begin(); i != n->getEdges().end(); ++i) {
171  if (!isWaterway((*i)->getPermissions())) {
172  waterway = false;
173  break;
174  }
175  }
176  if (waterway && n->myType == NODETYPE_UNKNOWN) {
178  continue;
179  }
180 
181  // check whether the junction is not a real junction
182  if (n->myIncomingEdges.size() == 1) {
184  continue;
185  }
186  // @todo "isSimpleContinuation" should be revalidated
187  if (n->isSimpleContinuation()) {
189  continue;
190  }
191  // determine the type
193  for (EdgeVector::const_iterator i = n->myIncomingEdges.begin(); i != n->myIncomingEdges.end(); i++) {
194  for (EdgeVector::const_iterator j = i + 1; j != n->myIncomingEdges.end(); j++) {
195  // @todo "getOppositeIncoming" should probably be refactored into something the edge knows
196  if (n->getOppositeIncoming(*j) == *i && n->myIncomingEdges.size() > 2) {
197  continue;
198  }
199  // @todo check against a legal document
200  // @todo figure out when NODETYPE_PRIORITY_STOP is appropriate
201  const SUMOReal s1 = (*i)->getSpeed() * (SUMOReal) 3.6;
202  const SUMOReal s2 = (*j)->getSpeed() * (SUMOReal) 3.6;
203  const int p1 = (*i)->getPriority();
204  const int p2 = (*j)->getPriority();
205  if (fabs(s1 - s2) > (SUMOReal) 9.5 || MAX2(s1, s2) >= (SUMOReal) 49. || p1 != p2) {
206  type = NODETYPE_PRIORITY;
207  break;
208  }
209  }
210  }
211  // save type
212  n->myType = type;
213  }
214 }
215 
216 
217 // ---------------------------------------------------------------------------
218 // NBEdgePriorityComputer
219 // ---------------------------------------------------------------------------
220 void
222  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
223  NBNode* n = (*i).second;
224  // preset all junction's edge priorities to zero
225  for (EdgeVector::iterator j = n->myAllEdges.begin(); j != n->myAllEdges.end(); ++j) {
226  (*j)->setJunctionPriority(n, NBEdge::MINOR_ROAD);
227  }
228  // check if the junction is not a real junction
229  if (n->myIncomingEdges.size() == 1 && n->myOutgoingEdges.size() == 1) {
230  continue;
231  }
232  // compute the priorities on junction when needed
234  setPriorityJunctionPriorities(*n);
235  }
236  }
237 }
238 
239 
240 void
242  if (n.myIncomingEdges.size() == 0 || n.myOutgoingEdges.size() == 0) {
243  return;
244  }
245  EdgeVector incoming = n.myIncomingEdges;
246  EdgeVector outgoing = n.myOutgoingEdges;
247  // what we do want to have is to extract the pair of roads that are
248  // the major roads for this junction
249  // let's get the list of incoming edges with the highest priority
250  std::sort(incoming.begin(), incoming.end(), NBContHelper::edge_by_priority_sorter());
251  EdgeVector bestIncoming;
252  NBEdge* best = incoming[0];
253  while (incoming.size() > 0 && samePriority(best, incoming[0])) {
254  bestIncoming.push_back(*incoming.begin());
255  incoming.erase(incoming.begin());
256  }
257  // now, let's get the list of best outgoing
258  assert(outgoing.size() != 0);
259  sort(outgoing.begin(), outgoing.end(), NBContHelper::edge_by_priority_sorter());
260  EdgeVector bestOutgoing;
261  best = outgoing[0];
262  while (outgoing.size() > 0 && samePriority(best, outgoing[0])) { //->getPriority()==best->getPriority()) {
263  bestOutgoing.push_back(*outgoing.begin());
264  outgoing.erase(outgoing.begin());
265  }
266  // special case: user input makes mainDirection unambiguous
267  const bool mainDirectionExplicit = (
268  bestIncoming.size() == 1 && n.myIncomingEdges.size() <= 2
269  && (incoming.size() == 0 || bestIncoming[0]->getPriority() > incoming[0]->getPriority())
270  && bestOutgoing.size() == 1 && n.myOutgoingEdges.size() <= 2
271  && (outgoing.size() == 0 || bestOutgoing[0]->getPriority() > outgoing[0]->getPriority())
272  && !bestIncoming[0]->isTurningDirectionAt(bestOutgoing[0]));
273  // now, let's compute for each of the best incoming edges
274  // the incoming which is most opposite
275  // the outgoing which is most opposite
276  EdgeVector::iterator i;
277  std::map<NBEdge*, NBEdge*> counterIncomingEdges;
278  std::map<NBEdge*, NBEdge*> counterOutgoingEdges;
279  incoming = n.myIncomingEdges;
280  outgoing = n.myOutgoingEdges;
281  for (i = bestIncoming.begin(); i != bestIncoming.end(); ++i) {
282  std::sort(incoming.begin(), incoming.end(), NBContHelper::edge_opposite_direction_sorter(*i, &n));
283  counterIncomingEdges[*i] = *incoming.begin();
284  std::sort(outgoing.begin(), outgoing.end(), NBContHelper::edge_opposite_direction_sorter(*i, &n));
285  counterOutgoingEdges[*i] = *outgoing.begin();
286  }
287  // ok, let's try
288  // 1) there is one best incoming road
289  if (bestIncoming.size() == 1) {
290  // let's mark this road as the best
291  NBEdge* best1 = extractAndMarkFirst(n, bestIncoming);
292  if (!mainDirectionExplicit && counterIncomingEdges.find(best1) != counterIncomingEdges.end()) {
293  // ok, look, what we want is the opposit of the straight continuation edge
294  // but, what if such an edge does not exist? By now, we'll determine it
295  // geometrically
296  NBEdge* s = counterIncomingEdges.find(best1)->second;
297  if (GeomHelper::getMinAngleDiff(best1->getAngleAtNode(&n), s->getAngleAtNode(&n)) > 180 - 45) {
299  }
300  }
301  assert(bestOutgoing.size() != 0);
302  // mark the best outgoing as the continuation
303  sort(bestOutgoing.begin(), bestOutgoing.end(), NBContHelper::edge_similar_direction_sorter(best1));
304  // assign extra priority if the priorities are unambiguous (regardless of geometry)
305  best1 = extractAndMarkFirst(n, bestOutgoing);
306  if (!mainDirectionExplicit && counterOutgoingEdges.find(best1) != counterOutgoingEdges.end()) {
307  NBEdge* s = counterOutgoingEdges.find(best1)->second;
308  if (GeomHelper::getMinAngleDiff(best1->getAngleAtNode(&n), s->getAngleAtNode(&n)) > 180 - 45) {
309  s->setJunctionPriority(&n, 1);
310  }
311  }
312  return;
313  }
314 
315  // ok, what we want to do in this case is to determine which incoming
316  // has the best continuation...
317  // This means, when several incoming roads have the same priority,
318  // we want a (any) straight connection to be more priorised than a turning
319  SUMOReal bestAngle = 0;
320  NBEdge* bestFirst = 0;
321  NBEdge* bestSecond = 0;
322  bool hadBest = false;
323  for (i = bestIncoming.begin(); i != bestIncoming.end(); ++i) {
324  EdgeVector::iterator j;
325  NBEdge* t1 = *i;
326  SUMOReal angle1 = t1->getAngleAtNode(&n) + 180;
327  if (angle1 >= 360) {
328  angle1 -= 360;
329  }
330  for (j = i + 1; j != bestIncoming.end(); ++j) {
331  NBEdge* t2 = *j;
332  SUMOReal angle2 = t2->getAngleAtNode(&n) + 180;
333  if (angle2 >= 360) {
334  angle2 -= 360;
335  }
336  SUMOReal angle = GeomHelper::getMinAngleDiff(angle1, angle2);
337  if (!hadBest || angle > bestAngle) {
338  bestAngle = angle;
339  bestFirst = *i;
340  bestSecond = *j;
341  hadBest = true;
342  }
343  }
344  }
345  bestFirst->setJunctionPriority(&n, 1);
346  sort(bestOutgoing.begin(), bestOutgoing.end(), NBContHelper::edge_similar_direction_sorter(bestFirst));
347  if (bestOutgoing.size() != 0) {
348  extractAndMarkFirst(n, bestOutgoing);
349  }
350  bestSecond->setJunctionPriority(&n, 1);
351  sort(bestOutgoing.begin(), bestOutgoing.end(), NBContHelper::edge_similar_direction_sorter(bestSecond));
352  if (bestOutgoing.size() != 0) {
353  extractAndMarkFirst(n, bestOutgoing);
354  }
355 }
356 
357 
358 NBEdge*
359 NBEdgePriorityComputer::extractAndMarkFirst(NBNode& n, std::vector<NBEdge*>& s, int prio) {
360  if (s.size() == 0) {
361  return 0;
362  }
363  NBEdge* ret = s.front();
364  s.erase(s.begin());
365  ret->setJunctionPriority(&n, prio);
366  return ret;
367 }
368 
369 
370 bool
371 NBEdgePriorityComputer::samePriority(const NBEdge* const e1, const NBEdge* const e2) {
372  if (e1 == e2) {
373  return true;
374  }
375  if (e1->getPriority() != e2->getPriority()) {
376  return false;
377  }
378  if ((int) e1->getSpeed() != (int) e2->getSpeed()) {
379  return false;
380  }
381  return (int) e1->getNumLanes() == (int) e2->getNumLanes();
382 }
383 
384 
386  // reorder based on getAngleAtNodeToCenter
387  myOrdering = ordering;
388  sort(myOrdering.begin(), myOrdering.end(), NBContHelper::edge_by_angle_to_nodeShapeCentroid_sorter(node));
389  // let the first edge remain the first
390  rotate(myOrdering.begin(), std::find(myOrdering.begin(), myOrdering.end(), ordering.front()), myOrdering.end());
391 }
392 
393 
394 /****************************************************************************/
395 
const EdgeVector & getIncomingEdges() const
Returns this node&#39;s incoming edges (The edges which yield in this node)
Definition: NBNode.h:240
NBEdge * getOppositeIncoming(NBEdge *e) const
returns the opposite incoming edge of certain edge
Definition: NBNode.cpp:1234
SumoXMLNodeType myType
The type of the junction.
Definition: NBNode.h:738
static SUMOReal normRelAngle(SUMOReal angle1, SUMOReal angle2)
ensure that reverse relAngles (>=179.999) always count as turnarounds (-180)
Definition: NBHelpers.cpp:69
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
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.
T MAX2(T a, T b)
Definition: StdDefs.h:70
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
const std::string & getID() const
Returns the id.
Definition: Named.h:66
EdgeVector myAllEdges
Vector of incoming and outgoing edges.
Definition: NBNode.h:729
int getPriority() const
Returns the priority of the edge.
Definition: NBEdge.h:409
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.
static NBEdge * extractAndMarkFirst(NBNode &n, std::vector< NBEdge * > &s, int prio=1)
Sets the priorites in case of a priority junction.
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:402
bool isTurningDirectionAt(const NBEdge *const edge) const
Returns whether the given edge is the opposite direction to this edge.
Definition: NBEdge.cpp:2226
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:182
std::map< std::string, NBNode * >::const_iterator end() const
Returns the pointer to the end of the stored nodes.
Definition: NBNodeCont.h:119
crossing_by_junction_angle_sorter(const NBNode *node, const EdgeVector &ordering)
EdgeVector myIncomingEdges
Vector of incoming edges.
Definition: NBNode.h:723
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.
EdgeVector myOutgoingEdges
Vector of outgoing edges.
Definition: NBNode.h:726
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:423
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
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:584
static SUMOReal getMinAngleDiff(SUMOReal angle1, SUMOReal angle2)
Returns the minimum distance (clockwise/counter-clockwise) between both angles.
Definition: GeomHelper.cpp:172
void setJunctionPriority(const NBNode *const node, int prio)
Sets the junction priority of the edge.
Definition: NBEdge.cpp:1476
Represents a single node (junction) during network building.
Definition: NBNode.h:75
bool isSimpleContinuation(bool checkLaneNumbers=true) const
check if node is a simple continuation
Definition: NBNode.cpp:436
static void computeTurnDirections(NBNodeCont &nc, bool warn=true)
Computes turnaround destinations for all edges (if exist)
#define SUMOReal
Definition: config.h:208
Sorts "Combination"s by decreasing angle.
Definition: NBAlgorithms.h:85
static void setPriorityJunctionPriorities(NBNode &n)
Sets the priorites in case of a priority junction.
SUMOReal getSpeed() const
Returns the speed allowed on this edge.
Definition: NBEdge.h:496
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:63
std::map< std::string, NBNode * >::const_iterator begin() const
Returns the pointer to the begin of the stored nodes.
Definition: NBNodeCont.h:114
SUMOReal getAngleAtNode(const NBNode *const node) const
Returns the angle of the edge&#39;s geometry at the given node.
Definition: NBEdge.cpp:1486
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:416