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-sim.org/
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  // now, let's compute for each of the best incoming edges
285  // the incoming which is most opposite
286  // the outgoing which is most opposite
287  EdgeVector::iterator i;
288  std::map<NBEdge*, NBEdge*> counterIncomingEdges;
289  std::map<NBEdge*, NBEdge*> counterOutgoingEdges;
290  incoming = n.myIncomingEdges;
291  outgoing = n.myOutgoingEdges;
292  for (i = bestIncoming.begin(); i != bestIncoming.end(); ++i) {
293  std::sort(incoming.begin(), incoming.end(), NBContHelper::edge_opposite_direction_sorter(*i, &n));
294  counterIncomingEdges[*i] = *incoming.begin();
295  std::sort(outgoing.begin(), outgoing.end(), NBContHelper::edge_opposite_direction_sorter(*i, &n));
296  counterOutgoingEdges[*i] = *outgoing.begin();
297  }
298  // ok, let's try
299  // 1) there is one best incoming road
300  if (bestIncoming.size() == 1) {
301  // let's mark this road as the best
302  NBEdge* best1 = extractAndMarkFirst(n, bestIncoming);
303  if (counterIncomingEdges.find(best1) != counterIncomingEdges.end()) {
304  // ok, look, what we want is the opposit of the straight continuation edge
305  // but, what if such an edge does not exist? By now, we'll determine it
306  // geometrically
307  NBEdge* s = counterIncomingEdges.find(best1)->second;
308  if (GeomHelper::getMinAngleDiff(best1->getAngleAtNode(&n), s->getAngleAtNode(&n)) > 180 - 45) {
309  s->setJunctionPriority(&n, 1);
310  }
311  }
312  if (bestOutgoing.size() != 0) {
313  // mark the best outgoing as the continuation
314  sort(bestOutgoing.begin(), bestOutgoing.end(), NBContHelper::edge_similar_direction_sorter(best1));
315  best1 = extractAndMarkFirst(n, bestOutgoing);
316  if (counterOutgoingEdges.find(best1) != counterOutgoingEdges.end()) {
317  NBEdge* s = counterOutgoingEdges.find(best1)->second;
318  if (GeomHelper::getMinAngleDiff(best1->getAngleAtNode(&n), s->getAngleAtNode(&n)) > 180 - 45) {
319  s->setJunctionPriority(&n, 1);
320  }
321  }
322  }
323  return;
324  }
325 
326  // ok, what we want to do in this case is to determine which incoming
327  // has the best continuation...
328  // This means, when several incoming roads have the same priority,
329  // we want a (any) straight connection to be more priorised than a turning
330  SUMOReal bestAngle = 0;
331  NBEdge* bestFirst = 0;
332  NBEdge* bestSecond = 0;
333  bool hadBest = false;
334  for (i = bestIncoming.begin(); i != bestIncoming.end(); ++i) {
335  EdgeVector::iterator j;
336  NBEdge* t1 = *i;
337  SUMOReal angle1 = t1->getTotalAngle() + 180;
338  if (angle1 >= 360) {
339  angle1 -= 360;
340  }
341  for (j = i + 1; j != bestIncoming.end(); ++j) {
342  NBEdge* t2 = *j;
343  SUMOReal angle2 = t2->getTotalAngle() + 180;
344  if (angle2 >= 360) {
345  angle2 -= 360;
346  }
347  SUMOReal angle = GeomHelper::getMinAngleDiff(angle1, angle2);
348  if (!hadBest || angle > bestAngle) {
349  bestAngle = angle;
350  bestFirst = *i;
351  bestSecond = *j;
352  hadBest = true;
353  }
354  }
355  }
356  bestFirst->setJunctionPriority(&n, 1);
357  sort(bestOutgoing.begin(), bestOutgoing.end(), NBContHelper::edge_similar_direction_sorter(bestFirst));
358  if (bestOutgoing.size() != 0) {
359  extractAndMarkFirst(n, bestOutgoing);
360  }
361  bestSecond->setJunctionPriority(&n, 1);
362  sort(bestOutgoing.begin(), bestOutgoing.end(), NBContHelper::edge_similar_direction_sorter(bestSecond));
363  if (bestOutgoing.size() != 0) {
364  extractAndMarkFirst(n, bestOutgoing);
365  }
366 }
367 
368 
369 NBEdge*
371  if (s.size() == 0) {
372  return 0;
373  }
374  NBEdge* ret = s.front();
375  s.erase(s.begin());
376  ret->setJunctionPriority(&n, 1);
377  return ret;
378 }
379 
380 
381 bool
382 NBEdgePriorityComputer::samePriority(const NBEdge* const e1, const NBEdge* const e2) {
383  if (e1 == e2) {
384  return true;
385  }
386  if (e1->getPriority() != e2->getPriority()) {
387  return false;
388  }
389  if ((int) e1->getSpeed() != (int) e2->getSpeed()) {
390  return false;
391  }
392  return (int) e1->getNumLanes() == (int) e2->getNumLanes();
393 }
394 
395 
396 /****************************************************************************/
397 
const EdgeVector & getIncomingEdges() const
Returns this node'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:666
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:72
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 ("from") and an outgoing ("to") edge...
Definition: NBAlgorithms.h:73
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
const EdgeVector & getOutgoingEdges() const
Returns this node'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:657
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 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:142
EdgeVector myIncomingEdges
Vector of incoming edges.
Definition: NBNode.h:651
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:654
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:38
static SUMOReal getMinAngleDiff(SUMOReal angle1, SUMOReal angle2)
Returns the minimum distance (clockwise/counter-clockwise) between both angles.
Definition: GeomHelper.cpp:395
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
static NBEdge * extractAndMarkFirst(NBNode &n, std::vector< NBEdge * > &s)
Sets the priorites in case of a priority junction.
#define SUMOReal
Definition: config.h:215
Sorts "Combination"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'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:134
SUMOReal getAngleAtNode(const NBNode *const node) const
Returns the angle of the edge's geometry at the given node.
Definition: NBEdge.cpp:1162
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:360