SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-2014 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) {
59  }
60 }
61 
62 void
64  const std::vector<NBEdge*>& incoming = node->getIncomingEdges();
65  const std::vector<NBEdge*>& outgoing = node->getOutgoingEdges();
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  if (e->getConnections().size() != 0 && !e->isConnectedTo(outedge)) {
72  // has connections, but not to outedge; outedge will not be the turn direction
73  //
74  // @todo: this seems to be needed due to legacy issues; actually, we could regard
75  // such pairs, too, and it probably would increase the accuracy. But there is
76  // no mechanism implemented, yet, which would avoid adding them as turnarounds though
77  // no connection is specified.
78  continue;
79  }
80 
81  // @todo: check whether NBHelpers::relAngle is properly defined and whether it should really be used, here
82  SUMOReal angle = fabs(NBHelpers::relAngle(e->getAngleAtNode(node), outedge->getAngleAtNode(node)));
83  if (angle < 160) {
84  continue;
85  }
86  if (e->getFromNode() == outedge->getToNode()) {
87  // they connect the same nodes; should be the turnaround direction
88  // we'll assign a maximum number
89  //
90  // @todo: indeed, we have observed some pathological intersections
91  // see "294831560" in OSM/adlershof. Here, several edges are connecting
92  // same nodes. We have to do the angle check before...
93  //
94  // @todo: and well, there are some other as well, see plain import
95  // of delphi_muenchen (elmar), intersection "59534191". Not that it would
96  // be realistic in any means; we will warn, here.
97  angle += 360;
98  }
99  Combination c;
100  c.from = e;
101  c.to = outedge;
102  c.angle = angle;
103  combinations.push_back(c);
104  }
105  }
106  // sort combinations so that the ones with the highest angle are at the begin
107  std::sort(combinations.begin(), combinations.end(), combination_by_angle_sorter());
108  std::set<NBEdge*> seen;
109  bool haveWarned = false;
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 && !haveWarned) {
114  WRITE_WARNING("Ambiguity in turnarounds computation at node '" + node->getID() + "'.");
115  haveWarned = true;
116  }
117  continue;
118  }
119  // mark as seen
120  seen.insert((*j).from);
121  seen.insert((*j).to);
122  // set turnaround information
123  (*j).from->setTurningDestination((*j).to);
124  }
125 }
126 
127 
128 // ---------------------------------------------------------------------------
129 // NBNodesEdgesSorter
130 // ---------------------------------------------------------------------------
131 void
133  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
134  NBNode* n = (*i).second;
135  if (n->myAllEdges.size() == 0) {
136  continue;
137  }
138  EdgeVector& allEdges = (*i).second->myAllEdges;
139  EdgeVector& incoming = (*i).second->myIncomingEdges;
140  EdgeVector& outgoing = (*i).second->myOutgoingEdges;
141  std::vector<NBNode::Crossing>& crossings = (*i).second->myCrossings;
142  // sort the edges
143  std::sort(allEdges.begin(), allEdges.end(), edge_by_junction_angle_sorter(n));
144  std::sort(incoming.begin(), incoming.end(), edge_by_junction_angle_sorter(n));
145  std::sort(outgoing.begin(), outgoing.end(), edge_by_junction_angle_sorter(n));
146  std::vector<NBEdge*>::iterator j;
147  for (j = allEdges.begin(); j != allEdges.end() - 1 && j != allEdges.end(); ++j) {
148  swapWhenReversed(n, leftHand, j, j + 1);
149  }
150  if (allEdges.size() > 1 && j != allEdges.end()) {
151  swapWhenReversed(n, leftHand, allEdges.end() - 1, allEdges.begin());
152  }
153  // sort the crossings
154  std::sort(crossings.begin(), crossings.end(), crossing_by_junction_angle_sorter(allEdges));
155  // DEBUG
156  //if (n->getID() == "cluster_492462300_671564296") {
157  // if (crossings.size() > 0) {
158  // std::cout << " crossings at " << n->getID() << "\n";
159  // for (std::vector<NBNode::Crossing>::iterator it = crossings.begin(); it != crossings.end(); ++it) {
160  // std::cout << " " << toString((*it).edges) << "\n";
161  // }
162  // }
163  //}
164  }
165 }
166 
167 
168 void
169 NBNodesEdgesSorter::swapWhenReversed(const NBNode* const n, bool leftHand,
170  const std::vector<NBEdge*>::iterator& i1,
171  const std::vector<NBEdge*>::iterator& i2) {
172  NBEdge* e1 = *i1;
173  NBEdge* e2 = *i2;
174  if (leftHand) {
175  // @todo: check this; shouldn't it be "swap(*e1, *e2)"?
176  std::swap(e1, e2);
177  }
178  // @todo: The difference between "isTurningDirectionAt" and "isTurnaround"
179  // is not nice. Maybe we could get rid of it if we would always mark edges
180  // as turnarounds, even if they do not have to be added, as mentioned in
181  // notes on NBTurningDirectionsComputer::computeTurnDirectionsForNode
182  if (e2->getToNode() == n && e2->isTurningDirectionAt(n, e1)) {
183  std::swap(*i1, *i2);
184  }
185 }
186 
187 
188 // ---------------------------------------------------------------------------
189 // NBNodeTypeComputer
190 // ---------------------------------------------------------------------------
191 void
193  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
194  NBNode* n = (*i).second;
195  // the type may already be set from the data
196  if (n->myType != NODETYPE_UNKNOWN) {
197  continue;
198  }
199  // check whether the junction is not a real junction
200  if (n->myIncomingEdges.size() == 1) {
202  continue;
203  }
204  // @todo "isSimpleContinuation" should be revalidated
205  if (n->isSimpleContinuation()) {
207  continue;
208  }
209  // determine the type
211  for (EdgeVector::const_iterator i = n->myIncomingEdges.begin(); i != n->myIncomingEdges.end(); i++) {
212  for (EdgeVector::const_iterator j = i + 1; j != n->myIncomingEdges.end(); j++) {
213  // @todo "getOppositeIncoming" should probably be refactored into something the edge knows
214  if (n->getOppositeIncoming(*j) == *i && n->myIncomingEdges.size() > 2) {
215  continue;
216  }
217  // @todo check against a legal document
218  // @todo figure out when NODETYPE_PRIORITY_STOP is appropriate
219  const SUMOReal s1 = (*i)->getSpeed() * (SUMOReal) 3.6;
220  const SUMOReal s2 = (*j)->getSpeed() * (SUMOReal) 3.6;
221  const int p1 = (*i)->getPriority();
222  const int p2 = (*j)->getPriority();
223  if (fabs(s1 - s2) > (SUMOReal) 9.5 || MAX2(s1, s2) >= (SUMOReal) 49. || p1 != p2) {
224  type = NODETYPE_PRIORITY;
225  break;
226  }
227  }
228  }
229  // save type
230  n->myType = type;
231  }
232 }
233 
234 
235 // ---------------------------------------------------------------------------
236 // NBEdgePriorityComputer
237 // ---------------------------------------------------------------------------
238 void
240  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
241  NBNode* n = (*i).second;
242  // preset all junction's edge priorities to zero
243  for (EdgeVector::iterator j = n->myAllEdges.begin(); j != n->myAllEdges.end(); ++j) {
244  (*j)->setJunctionPriority(n, 0);
245  }
246  // check if the junction is not a real junction
247  if (n->myIncomingEdges.size() == 1 && n->myOutgoingEdges.size() == 1) {
248  continue;
249  }
250  // compute the priorities on junction when needed
253  }
254  }
255 }
256 
257 
258 void
260  if (n.myIncomingEdges.size() == 0 || n.myOutgoingEdges.size() == 0) {
261  return;
262  }
263  EdgeVector incoming = n.myIncomingEdges;
264  EdgeVector outgoing = n.myOutgoingEdges;
265  // what we do want to have is to extract the pair of roads that are
266  // the major roads for this junction
267  // let's get the list of incoming edges with the highest priority
268  std::sort(incoming.begin(), incoming.end(), NBContHelper::edge_by_priority_sorter());
269  EdgeVector bestIncoming;
270  NBEdge* best = incoming[0];
271  while (incoming.size() > 0 && samePriority(best, incoming[0])) {
272  bestIncoming.push_back(*incoming.begin());
273  incoming.erase(incoming.begin());
274  }
275  // now, let's get the list of best outgoing
276  assert(outgoing.size() != 0);
277  sort(outgoing.begin(), outgoing.end(), NBContHelper::edge_by_priority_sorter());
278  EdgeVector bestOutgoing;
279  best = outgoing[0];
280  while (outgoing.size() > 0 && samePriority(best, outgoing[0])) { //->getPriority()==best->getPriority()) {
281  bestOutgoing.push_back(*outgoing.begin());
282  outgoing.erase(outgoing.begin());
283  }
284  // special case: user input makes mainDirection unambiguous
285  const bool mainDirectionExplicit = (
286  bestIncoming.size() == 1 && n.myIncomingEdges.size() <= 2
287  && (incoming.size() == 0 || bestIncoming[0]->getPriority() > incoming[0]->getPriority())
288  && bestOutgoing.size() == 1 && n.myOutgoingEdges.size() <= 2
289  && (outgoing.size() == 0 || bestOutgoing[0]->getPriority() > outgoing[0]->getPriority())
290  && !bestIncoming[0]->isTurningDirectionAt(&n, bestOutgoing[0]));
291  // now, let's compute for each of the best incoming edges
292  // the incoming which is most opposite
293  // the outgoing which is most opposite
294  EdgeVector::iterator i;
295  std::map<NBEdge*, NBEdge*> counterIncomingEdges;
296  std::map<NBEdge*, NBEdge*> counterOutgoingEdges;
297  incoming = n.myIncomingEdges;
298  outgoing = n.myOutgoingEdges;
299  for (i = bestIncoming.begin(); i != bestIncoming.end(); ++i) {
300  std::sort(incoming.begin(), incoming.end(), NBContHelper::edge_opposite_direction_sorter(*i, &n));
301  counterIncomingEdges[*i] = *incoming.begin();
302  std::sort(outgoing.begin(), outgoing.end(), NBContHelper::edge_opposite_direction_sorter(*i, &n));
303  counterOutgoingEdges[*i] = *outgoing.begin();
304  }
305  // ok, let's try
306  // 1) there is one best incoming road
307  if (bestIncoming.size() == 1) {
308  // let's mark this road as the best
309  NBEdge* best1 = extractAndMarkFirst(n, bestIncoming);
310  if (!mainDirectionExplicit && counterIncomingEdges.find(best1) != counterIncomingEdges.end()) {
311  // ok, look, what we want is the opposit of the straight continuation edge
312  // but, what if such an edge does not exist? By now, we'll determine it
313  // geometrically
314  NBEdge* s = counterIncomingEdges.find(best1)->second;
315  if (GeomHelper::getMinAngleDiff(best1->getAngleAtNode(&n), s->getAngleAtNode(&n)) > 180 - 45) {
316  s->setJunctionPriority(&n, 1);
317  }
318  }
319  assert(bestOutgoing.size() != 0);
320  // mark the best outgoing as the continuation
321  sort(bestOutgoing.begin(), bestOutgoing.end(), NBContHelper::edge_similar_direction_sorter(best1));
322  // assign extra priority if the priorities are unambiguous (regardless of geometry)
323  best1 = extractAndMarkFirst(n, bestOutgoing);
324  if (!mainDirectionExplicit && counterOutgoingEdges.find(best1) != counterOutgoingEdges.end()) {
325  NBEdge* s = counterOutgoingEdges.find(best1)->second;
326  if (GeomHelper::getMinAngleDiff(best1->getAngleAtNode(&n), s->getAngleAtNode(&n)) > 180 - 45) {
327  s->setJunctionPriority(&n, 1);
328  }
329  }
330  return;
331  }
332 
333  // ok, what we want to do in this case is to determine which incoming
334  // has the best continuation...
335  // This means, when several incoming roads have the same priority,
336  // we want a (any) straight connection to be more priorised than a turning
337  SUMOReal bestAngle = 0;
338  NBEdge* bestFirst = 0;
339  NBEdge* bestSecond = 0;
340  bool hadBest = false;
341  for (i = bestIncoming.begin(); i != bestIncoming.end(); ++i) {
342  EdgeVector::iterator j;
343  NBEdge* t1 = *i;
344  SUMOReal angle1 = t1->getTotalAngle() + 180;
345  if (angle1 >= 360) {
346  angle1 -= 360;
347  }
348  for (j = i + 1; j != bestIncoming.end(); ++j) {
349  NBEdge* t2 = *j;
350  SUMOReal angle2 = t2->getTotalAngle() + 180;
351  if (angle2 >= 360) {
352  angle2 -= 360;
353  }
354  SUMOReal angle = GeomHelper::getMinAngleDiff(angle1, angle2);
355  if (!hadBest || angle > bestAngle) {
356  bestAngle = angle;
357  bestFirst = *i;
358  bestSecond = *j;
359  hadBest = true;
360  }
361  }
362  }
363  bestFirst->setJunctionPriority(&n, 1);
364  sort(bestOutgoing.begin(), bestOutgoing.end(), NBContHelper::edge_similar_direction_sorter(bestFirst));
365  if (bestOutgoing.size() != 0) {
366  extractAndMarkFirst(n, bestOutgoing);
367  }
368  bestSecond->setJunctionPriority(&n, 1);
369  sort(bestOutgoing.begin(), bestOutgoing.end(), NBContHelper::edge_similar_direction_sorter(bestSecond));
370  if (bestOutgoing.size() != 0) {
371  extractAndMarkFirst(n, bestOutgoing);
372  }
373 }
374 
375 
376 NBEdge*
377 NBEdgePriorityComputer::extractAndMarkFirst(NBNode& n, std::vector<NBEdge*>& s, int prio) {
378  if (s.size() == 0) {
379  return 0;
380  }
381  NBEdge* ret = s.front();
382  s.erase(s.begin());
383  ret->setJunctionPriority(&n, prio);
384  return ret;
385 }
386 
387 
388 bool
389 NBEdgePriorityComputer::samePriority(const NBEdge* const e1, const NBEdge* const e2) {
390  if (e1 == e2) {
391  return true;
392  }
393  if (e1->getPriority() != e2->getPriority()) {
394  return false;
395  }
396  if ((int) e1->getSpeed() != (int) e2->getSpeed()) {
397  return false;
398  }
399  return (int) e1->getNumLanes() == (int) e2->getNumLanes();
400 }
401 
402 
403 /****************************************************************************/
404 
const EdgeVector & getIncomingEdges() const
Returns this node&#39;s incoming edges.
Definition: NBNode.h:244
Sorts incoming and outgoing edges clockwise around the given node.
Definition: NBAlgorithms.h:159
Sorts crossings by minimum clockwise clockwise edge angle. Use the ordering found in myAllEdges of th...
Definition: NBAlgorithms.h:118
NBEdge * getOppositeIncoming(NBEdge *e) const
Definition: NBNode.cpp:970
SumoXMLNodeType myType
The type of the junction.
Definition: NBNode.h:669
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:148
static void swapWhenReversed(const NBNode *const n, bool leftHand, const std::vector< NBEdge * >::iterator &i1, const std::vector< NBEdge * >::iterator &i2)
Assures correct order for same-angle opposite-direction edges.
static void computeTurnDirectionsForNode(NBNode *node)
Computes turnaround destinations for all incoming edges of the given nodes (if any) ...
T MAX2(T a, T b)
Definition: StdDefs.h:74
bool isTurningDirectionAt(const NBNode *n, const NBEdge *const edge) const
Returns whether the given edge is the opposite direction to this edge.
Definition: NBEdge.cpp:1655
Stores the information about the angle between an incoming (&quot;from&quot;) and an outgoing (&quot;to&quot;) edge...
Definition: NBAlgorithms.h:73
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
const EdgeVector & getOutgoingEdges() const
Returns this node&#39;s outgoing edges.
Definition: NBNode.h:252
const std::string & getID() const
Returns the id.
Definition: Named.h:60
EdgeVector myAllEdges
Vector of incoming and outgoing edges.
Definition: NBNode.h:660
bool isConnectedTo(NBEdge *e)
Returns the information whethe a connection to the given edge has been added (or computed) ...
Definition: NBEdge.cpp:744
int getPriority() const
Returns the priority of the edge.
Definition: NBEdge.h:352
bool isSimpleContinuation() const
Definition: NBNode.cpp:420
static void computeEdgePriorities(NBNodeCont &nc)
Computes edge priorities within a node.
unsigned int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:344
static NBEdge * extractAndMarkFirst(NBNode &n, std::vector< NBEdge * > &s, int prio=1)
Sets the priorites in case of a priority junction.
static void computeTurnDirections(NBNodeCont &nc)
Computes turnaround destinations for all edges (if exist)
std::map< std::string, NBNode * >::const_iterator end() const
Returns the pointer to the end of the stored nodes.
Definition: NBNodeCont.h:135
EdgeVector myIncomingEdges
Vector of incoming edges.
Definition: NBNode.h:654
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:657
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:368
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
Definition: NBCont.h:41
static SUMOReal getMinAngleDiff(SUMOReal angle1, SUMOReal angle2)
Returns the minimum distance (clockwise/counter-clockwise) between both angles.
Definition: GeomHelper.cpp:400
void setJunctionPriority(const NBNode *const node, int prio)
Sets the junction priority of the edge.
Definition: NBEdge.cpp:1152
SUMOReal getTotalAngle() const
get the angle as measure from the start to the end of this edge
Definition: NBEdge.h:396
Represents a single node (junction) during network building.
Definition: NBNode.h:75
#define SUMOReal
Definition: config.h:218
Sorts &quot;Combination&quot;s by decreasing angle.
Definition: NBAlgorithms.h:83
static SUMOReal relAngle(SUMOReal angle1, SUMOReal angle2)
Definition: NBHelpers.cpp:63
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:428
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:64
static void sortNodesEdges(NBNodeCont &nc, bool leftHand)
Sorts a node&#39;s edges clockwise regarding driving direction.
const std::vector< Connection > & getConnections() const
Returns the connections.
Definition: NBEdge.h:747
std::map< std::string, NBNode * >::const_iterator begin() const
Returns the pointer to the begin of the stored nodes.
Definition: NBNodeCont.h:127
SUMOReal getAngleAtNode(const NBNode *const node) const
Returns the angle of the edge&#39;s geometry at the given node.
Definition: NBEdge.cpp:1162
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:360