SUMO - Simulation of Urban MObility
NBContHelper.h
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-2017 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 #ifndef NBContHelper_h
23 #define NBContHelper_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <vector>
36 #include <iostream>
37 #include <cmath>
38 #include <algorithm>
39 #include <cassert>
40 #include "NBHelpers.h"
41 #include "NBCont.h"
42 #include "NBEdge.h"
43 #include "NBNode.h"
44 #include <utils/common/StdDefs.h>
45 #include <utils/geom/GeomHelper.h>
46 
47 
48 // ===========================================================================
49 // class definitions
50 // ===========================================================================
56 class NBContHelper {
57 public:
60  static void nextCW(const EdgeVector& edges,
61  EdgeVector::const_iterator& from);
62 
65  static void nextCCW(const EdgeVector& edges,
66  EdgeVector::const_iterator& from);
67 
68  static double getMaxSpeed(const EdgeVector& edges);
69 
70  static double getMinSpeed(const EdgeVector& edges);
71 
73  static std::ostream& out(std::ostream& os, const std::vector<bool>& v);
74 
75 
85  public:
88 
89  public:
91  int operator()(NBEdge* e1, NBEdge* e2) const;
92 
93  private:
96  };
97 
98 
105  public:
107  explicit straightness_sorter(const NBNode* n, const NBEdge* e):
108  myRefIncoming(e->getToNode() == n) {
109  if (myRefIncoming) {
110  myReferencePos = e->getLaneShape(0).back();
111  myReferenceAngle = e->getShapeEndAngle();
112  } else {
113  myReferencePos = e->getLaneShape(0).front();
114  myReferenceAngle = e->getShapeStartAngle();
115  }
116  }
117 
118  public:
120  int operator()(NBEdge* e1, NBEdge* e2) const;
121 
122  private:
126  };
127 
128 
137  public:
140 
141  public:
143  int operator()(NBEdge* e1, NBEdge* e2) const;
144 
145  private:
148  };
149 
150 
156  public:
158  int operator()(NBEdge* e1, NBEdge* e2) const {
159  if (e1->getPriority() != e2->getPriority()) {
160  return e1->getPriority() > e2->getPriority();
161  }
162  if (e1->getSpeed() != e2->getSpeed()) {
163  return e1->getSpeed() > e2->getSpeed();
164  }
165  return e1->getNumLanes() > e2->getNumLanes();
166  }
167  };
168 
169  // ---------------------------
170 
179  public:
184  explicit edge_opposite_direction_sorter(const NBEdge* const e, const NBNode* const n)
185  : myNode(n) {
186  myAngle = getEdgeAngleAt(e, n);
187  }
188 
194  int operator()(NBEdge* e1, NBEdge* e2) const {
195  return getDiff(e1) > getDiff(e2);
196  }
197 
198  protected:
203  double getDiff(const NBEdge* const e) const {
204  return fabs(GeomHelper::angleDiff(getEdgeAngleAt(e, myNode), myAngle));
205  }
206 
213  double getEdgeAngleAt(const NBEdge* const e, const NBNode* const n) const {
214  if (e->getFromNode() == n) {
215  return e->getGeometry().angleAt2D(0);
216  } else {
217  return e->getGeometry()[-1].angleTo2D(e->getGeometry()[-2]);
218  }
219  }
220 
221  private:
223  double myAngle;
224 
226  const NBNode* const myNode;
227 
228  private:
231 
232  };
233 
234  // ---------------------------
235 
243  public:
245  explicit edge_similar_direction_sorter(const NBEdge* const e)
246  : myAngle(e->getTotalAngle()) {}
247 
249  int operator()(NBEdge* e1, NBEdge* e2) const {
250  double d1 = GeomHelper::getMinAngleDiff(e1->getTotalAngle(), myAngle);
251  double d2 = GeomHelper::getMinAngleDiff(e2->getTotalAngle(), myAngle);
252  return d1 < d2;
253  }
254 
255  private:
257  double myAngle;
258  };
259 
260 
265  public:
267  node_with_incoming_finder(const NBEdge* const e);
268 
269  bool operator()(const NBNode* const n) const;
270 
271  private:
272  const NBEdge* const myEdge;
273 
274  private:
277 
278  };
279 
280 
285  public:
287  node_with_outgoing_finder(const NBEdge* const e);
288 
289  bool operator()(const NBNode* const n) const;
290 
291  private:
292  const NBEdge* const myEdge;
293 
294  private:
297 
298  };
299 
300 
301 
302 
304  public:
307 
308  bool operator()(NBEdge* e) const;
309 
310  private:
312 
313  private:
316 
317  };
318 
319 
322  static NBEdge* findConnectingEdge(const EdgeVector& edges,
323  NBNode* from, NBNode* to);
324 
325 
327  static double maxSpeed(const EdgeVector& ev);
328 
336  public:
339 
341  int operator()(NBEdge* e1, NBEdge* e2) const {
342  std::pair<double, double> mm1 = getMinMaxRelAngles(e1);
343  std::pair<double, double> mm2 = getMinMaxRelAngles(e2);
344  if (mm1.first == mm2.first && mm1.second == mm2.second) {
345  // ok, let's simply sort them arbitrarily
346  return e1->getID() < e2->getID();
347  }
348 
349  assert(
350  (mm1.first <= mm2.first && mm1.second <= mm2.second)
351  ||
352  (mm1.first >= mm2.first && mm1.second >= mm2.second));
353  return (mm1.first >= mm2.first && mm1.second >= mm2.second);
354  }
355 
359  std::pair<double, double> getMinMaxRelAngles(NBEdge* e) const {
360  double min = 360;
361  double max = 360;
362  const EdgeVector& ev = e->getConnectedEdges();
363  for (EdgeVector::const_iterator i = ev.begin(); i != ev.end(); ++i) {
364  double angle = NBHelpers::normRelAngle(
365  e->getTotalAngle(), (*i)->getTotalAngle());
366  if (min == 360 || min > angle) {
367  min = angle;
368  }
369  if (max == 360 || max < angle) {
370  max = angle;
371  }
372  }
373  return std::pair<double, double>(min, max);
374  }
375  };
376 
377 
378  friend std::ostream& operator<<(std::ostream& os, const EdgeVector& ev);
379 
381  public:
384  : myReferenceEdge(edge) { }
385 
386  bool operator()(NBEdge* e) const {
387  return e->isTurningDirectionAt(myReferenceEdge) ||
388  myReferenceEdge->isTurningDirectionAt(e);
389  }
390 
391  private:
393 
394  };
395 
401  public:
403  explicit edge_by_angle_to_nodeShapeCentroid_sorter(const NBNode* n) : myNode(n) {}
404 
405  public:
407  int operator()(const NBEdge* e1, const NBEdge* e2) const;
408 
409  private:
411  const NBNode* myNode;
412  };
413 
414 };
415 
416 
417 #endif
418 
419 /****************************************************************************/
420 
static double getMinAngleDiff(double angle1, double angle2)
Returns the minimum distance (clockwise/counter-clockwise) between both angles.
Definition: GeomHelper.cpp:168
double myAngle
the angle to find the edge with the opposite direction
Definition: NBContHelper.h:257
static double maxSpeed(const EdgeVector &ev)
#define min(a, b)
Definition: polyfonts.c:66
static double getMinSpeed(const EdgeVector &edges)
NBEdge * myEdge
the edge to compute the relative angle of
Definition: NBContHelper.h:95
static double normRelAngle(double angle1, double angle2)
ensure that reverse relAngles (>=179.999) always count as turnarounds (-180)
Definition: NBHelpers.cpp:65
const NBNode *const myNode
The related node.
Definition: NBContHelper.h:226
int getPriority() const
Returns the priority of the edge.
Definition: NBEdge.h:420
friend std::ostream & operator<<(std::ostream &os, const EdgeVector &ev)
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
double getEdgeAngleAt(const NBEdge *const e, const NBNode *const n) const
Returns the given edge&#39;s angle at the given node.
Definition: NBContHelper.h:213
std::pair< double, double > getMinMaxRelAngles(NBEdge *e) const
Definition: NBContHelper.h:359
opposite_finder(NBEdge *edge)
constructor
Definition: NBContHelper.h:383
int operator()(NBEdge *e1, NBEdge *e2) const
Comparing operation.
Definition: NBContHelper.h:194
double getDiff(const NBEdge *const e) const
Computes the angle difference between the related and the given edge.
Definition: NBContHelper.h:203
static void nextCW(const EdgeVector &edges, EdgeVector::const_iterator &from)
const std::string & getID() const
Returns the id.
Definition: Named.h:66
relative_outgoing_edge_sorter(NBEdge *e)
constructor
Definition: NBContHelper.h:87
double getShapeStartAngle() const
Returns the angle at the start of the edge.
Definition: NBEdge.cpp:1715
bool operator()(NBEdge *e) const
Definition: NBContHelper.h:386
straightness_sorter(const NBNode *n, const NBEdge *e)
constructor
Definition: NBContHelper.h:107
edge_similar_direction_sorter(const NBEdge *const e)
constructor
Definition: NBContHelper.h:245
edge_opposite_direction_sorter(const NBEdge *const e, const NBNode *const n)
Constructor.
Definition: NBContHelper.h:184
#define max(a, b)
Definition: polyfonts.c:65
static std::ostream & out(std::ostream &os, const std::vector< bool > &v)
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:413
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
Definition: NBContHelper.h:341
NBEdge * myEdge
the edge to compute the relative angle of
Definition: NBContHelper.h:147
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
edge_by_angle_to_nodeShapeCentroid_sorter(const NBNode *n)
constructor
Definition: NBContHelper.h:403
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
Definition: NBContHelper.h:249
double myAngle
The angle of the related edge at the given node.
Definition: NBContHelper.h:223
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
double getTotalAngle() const
Returns the angle at the start of the edge.
Definition: NBEdge.h:474
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operator
Definition: NBContHelper.h:158
const PositionVector & getLaneShape(int i) const
Returns the shape of the nth lane.
Definition: NBEdge.cpp:757
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:41
const NBNode * myNode
the edge to compute the relative angle of
Definition: NBContHelper.h:411
double angleAt2D(int pos) const
get angle in certain position of position vector
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
EdgeVector getConnectedEdges() const
Returns the list of outgoing edges unsorted.
Definition: NBEdge.cpp:1107
static double getMaxSpeed(const EdgeVector &edges)
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:427
relative_incoming_edge_sorter(NBEdge *e)
constructor
Definition: NBContHelper.h:139
static double angleDiff(const double angle1, const double angle2)
Returns the difference of the second angle to the first angle in radiants.
Definition: GeomHelper.cpp:174
double getShapeEndAngle() const
Returns the angle at the end of the edge.
Definition: NBEdge.cpp:1723
static void nextCCW(const EdgeVector &edges, EdgeVector::const_iterator &from)
static NBEdge * findConnectingEdge(const EdgeVector &edges, NBNode *from, NBNode *to)