SUMO - Simulation of Urban MObility
NBContHelper.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Some methods for traversing lists of edges
20 /****************************************************************************/
21 #ifndef NBContHelper_h
22 #define NBContHelper_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <vector>
35 #include <iostream>
36 #include <cmath>
37 #include <algorithm>
38 #include <cassert>
39 #include "NBHelpers.h"
40 #include "NBCont.h"
41 #include "NBEdge.h"
42 #include "NBNode.h"
43 #include <utils/common/StdDefs.h>
44 #include <utils/geom/GeomHelper.h>
45 
46 
47 // ===========================================================================
48 // class definitions
49 // ===========================================================================
55 class NBContHelper {
56 public:
59  static void nextCW(const EdgeVector& edges,
60  EdgeVector::const_iterator& from);
61 
64  static void nextCCW(const EdgeVector& edges,
65  EdgeVector::const_iterator& from);
66 
67  static double getMaxSpeed(const EdgeVector& edges);
68 
69  static double getMinSpeed(const EdgeVector& edges);
70 
72  static std::ostream& out(std::ostream& os, const std::vector<bool>& v);
73 
74 
84  public:
87 
88  public:
90  int operator()(NBEdge* e1, NBEdge* e2) const;
91 
92  private:
95  };
96 
97 
104  public:
106  explicit straightness_sorter(const NBNode* n, const NBEdge* e):
107  myRefIncoming(e->getToNode() == n) {
108  if (myRefIncoming) {
109  myReferencePos = e->getLaneShape(0).back();
110  myReferenceAngle = e->getShapeEndAngle();
111  } else {
112  myReferencePos = e->getLaneShape(0).front();
113  myReferenceAngle = e->getShapeStartAngle();
114  }
115  }
116 
117  public:
119  int operator()(NBEdge* e1, NBEdge* e2) const;
120 
121  private:
125  };
126 
127 
136  public:
139 
140  public:
142  int operator()(NBEdge* e1, NBEdge* e2) const;
143 
144  private:
147  };
148 
149 
155  public:
157  int operator()(NBEdge* e1, NBEdge* e2) const {
158  if (e1->getPriority() != e2->getPriority()) {
159  return e1->getPriority() > e2->getPriority();
160  }
161  if (e1->getSpeed() != e2->getSpeed()) {
162  return e1->getSpeed() > e2->getSpeed();
163  }
164  return e1->getNumLanes() > e2->getNumLanes();
165  }
166  };
167 
168  // ---------------------------
169 
178  public:
183  explicit edge_opposite_direction_sorter(const NBEdge* const e, const NBNode* const n, bool regardPriority) :
184  myNode(n),
185  myEdge(e),
186  myRegardPriority(regardPriority) {
187  myAngle = getEdgeAngleAt(e, n);
188  }
189 
195  int operator()(NBEdge* e1, NBEdge* e2) const {
196  if (!myRegardPriority || e1->getPriority() == e2->getPriority() || e1 == myEdge || e2 == myEdge) {
197  return getDiff(e1) > getDiff(e2);
198  } else {
199  return e1->getPriority() > e2->getPriority();
200  }
201  }
202 
203  protected:
208  double getDiff(const NBEdge* const e) const {
209  return fabs(GeomHelper::angleDiff(getEdgeAngleAt(e, myNode), myAngle));
210  }
211 
218  double getEdgeAngleAt(const NBEdge* const e, const NBNode* const n) const {
219  if (e->getFromNode() == n) {
220  return e->getGeometry().angleAt2D(0);
221  } else {
222  return e->getGeometry()[-1].angleTo2D(e->getGeometry()[-2]);
223  }
224  }
225 
226  private:
227 
229  const NBNode* const myNode;
230 
232  const NBEdge* const myEdge;
233 
235  double myAngle;
236 
239 
240  private:
243 
244  };
245 
246  // ---------------------------
247 
255  public:
257  explicit edge_similar_direction_sorter(const NBEdge* const e)
258  : myAngle(e->getTotalAngle()) {}
259 
261  int operator()(NBEdge* e1, NBEdge* e2) const {
262  double d1 = GeomHelper::getMinAngleDiff(e1->getTotalAngle(), myAngle);
263  double d2 = GeomHelper::getMinAngleDiff(e2->getTotalAngle(), myAngle);
264  return d1 < d2;
265  }
266 
267  private:
269  double myAngle;
270  };
271 
272 
277  public:
279  node_with_incoming_finder(const NBEdge* const e);
280 
281  bool operator()(const NBNode* const n) const;
282 
283  private:
284  const NBEdge* const myEdge;
285 
286  private:
289 
290  };
291 
292 
297  public:
299  node_with_outgoing_finder(const NBEdge* const e);
300 
301  bool operator()(const NBNode* const n) const;
302 
303  private:
304  const NBEdge* const myEdge;
305 
306  private:
309 
310  };
311 
312 
313 
314 
316  public:
319 
320  bool operator()(NBEdge* e) const;
321 
322  private:
324 
325  private:
328 
329  };
330 
331 
334  static NBEdge* findConnectingEdge(const EdgeVector& edges,
335  NBNode* from, NBNode* to);
336 
337 
339  static double maxSpeed(const EdgeVector& ev);
340 
348  public:
351 
353  int operator()(NBEdge* e1, NBEdge* e2) const {
354  std::pair<double, double> mm1 = getMinMaxRelAngles(e1);
355  std::pair<double, double> mm2 = getMinMaxRelAngles(e2);
356  if (mm1.first == mm2.first && mm1.second == mm2.second) {
357  // ok, let's simply sort them arbitrarily
358  return e1->getID() < e2->getID();
359  }
360 
361  assert(
362  (mm1.first <= mm2.first && mm1.second <= mm2.second)
363  ||
364  (mm1.first >= mm2.first && mm1.second >= mm2.second));
365  return (mm1.first >= mm2.first && mm1.second >= mm2.second);
366  }
367 
371  std::pair<double, double> getMinMaxRelAngles(NBEdge* e) const {
372  double min = 360;
373  double max = 360;
374  const EdgeVector& ev = e->getConnectedEdges();
375  for (EdgeVector::const_iterator i = ev.begin(); i != ev.end(); ++i) {
376  double angle = NBHelpers::normRelAngle(
377  e->getTotalAngle(), (*i)->getTotalAngle());
378  if (min == 360 || min > angle) {
379  min = angle;
380  }
381  if (max == 360 || max < angle) {
382  max = angle;
383  }
384  }
385  return std::pair<double, double>(min, max);
386  }
387  };
388 
389 
390  friend std::ostream& operator<<(std::ostream& os, const EdgeVector& ev);
391 
393  public:
396  : myReferenceEdge(edge) { }
397 
398  bool operator()(NBEdge* e) const {
399  return e->isTurningDirectionAt(myReferenceEdge) ||
400  myReferenceEdge->isTurningDirectionAt(e);
401  }
402 
403  private:
405 
406  };
407 
413  public:
415  explicit edge_by_angle_to_nodeShapeCentroid_sorter(const NBNode* n) : myNode(n) {}
416 
417  public:
419  int operator()(const NBEdge* e1, const NBEdge* e2) const;
420 
421  private:
423  const NBNode* myNode;
424  };
425 
426 };
427 
428 
429 #endif
430 
431 /****************************************************************************/
432 
static double getMinAngleDiff(double angle1, double angle2)
Returns the minimum distance (clockwise/counter-clockwise) between both angles.
Definition: GeomHelper.cpp:167
double myAngle
the angle to find the edge with the opposite direction
Definition: NBContHelper.h:269
static double maxSpeed(const EdgeVector &ev)
static double getMinSpeed(const EdgeVector &edges)
NBEdge * myEdge
the edge to compute the relative angle of
Definition: NBContHelper.h:94
static double normRelAngle(double angle1, double angle2)
ensure that reverse relAngles (>=179.999) always count as turnarounds (-180)
Definition: NBHelpers.cpp:66
bool myRegardPriority
Whether edge priority may override closer angles.
Definition: NBContHelper.h:238
const NBNode *const myNode
The related node.
Definition: NBContHelper.h:229
int getPriority() const
Returns the priority of the edge.
Definition: NBEdge.h:419
friend std::ostream & operator<<(std::ostream &os, const EdgeVector &ev)
The representation of a single edge during network building.
Definition: NBEdge.h:70
Class to sort edges by their angle in relation to the given edge.
Definition: NBContHelper.h:177
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:218
std::pair< double, double > getMinMaxRelAngles(NBEdge *e) const
Definition: NBContHelper.h:371
opposite_finder(NBEdge *edge)
constructor
Definition: NBContHelper.h:395
int operator()(NBEdge *e1, NBEdge *e2) const
Comparing operation.
Definition: NBContHelper.h:195
double getDiff(const NBEdge *const e) const
Computes the angle difference between the related and the given edge.
Definition: NBContHelper.h:208
static void nextCW(const EdgeVector &edges, EdgeVector::const_iterator &from)
const std::string & getID() const
Returns the id.
Definition: Named.h:74
relative_outgoing_edge_sorter(NBEdge *e)
constructor
Definition: NBContHelper.h:86
double getShapeStartAngle() const
Returns the angle at the start of the edge.
Definition: NBEdge.cpp:1754
bool operator()(NBEdge *e) const
Definition: NBContHelper.h:398
straightness_sorter(const NBNode *n, const NBEdge *e)
constructor
Definition: NBContHelper.h:106
edge_similar_direction_sorter(const NBEdge *const e)
constructor
Definition: NBContHelper.h:257
static std::ostream & out(std::ostream &os, const std::vector< bool > &v)
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:412
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
Definition: NBContHelper.h:353
NBEdge * myEdge
the edge to compute the relative angle of
Definition: NBContHelper.h:146
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
edge_by_angle_to_nodeShapeCentroid_sorter(const NBNode *n)
constructor
Definition: NBContHelper.h:415
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
Definition: NBContHelper.h:261
double myAngle
The angle of the related edge at the given node.
Definition: NBContHelper.h:235
double getSpeed() const
Returns the speed allowed on this edge.
Definition: NBEdge.h:506
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:602
double getTotalAngle() const
Returns the angle at the start of the edge.
Definition: NBEdge.h:473
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operator
Definition: NBContHelper.h:157
const PositionVector & getLaneShape(int i) const
Returns the shape of the nth lane.
Definition: NBEdge.cpp:778
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:40
const NBNode * myNode
the edge to compute the relative angle of
Definition: NBContHelper.h:423
double angleAt2D(int pos) const
get angle in certain position of position vector
edge_opposite_direction_sorter(const NBEdge *const e, const NBNode *const n, bool regardPriority)
Constructor.
Definition: NBContHelper.h:183
bool isTurningDirectionAt(const NBEdge *const edge) const
Returns whether the given edge is the opposite direction to this edge.
Definition: NBEdge.cpp:2450
Represents a single node (junction) during network building.
Definition: NBNode.h:74
EdgeVector getConnectedEdges() const
Returns the list of outgoing edges unsorted.
Definition: NBEdge.cpp:1134
static double getMaxSpeed(const EdgeVector &edges)
const NBEdge *const myEdge
the reference edge
Definition: NBContHelper.h:232
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:426
relative_incoming_edge_sorter(NBEdge *e)
constructor
Definition: NBContHelper.h:138
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:173
double getShapeEndAngle() const
Returns the angle at the end of the edge.
Definition: NBEdge.cpp:1762
static void nextCCW(const EdgeVector &edges, EdgeVector::const_iterator &from)
static NBEdge * findConnectingEdge(const EdgeVector &edges, NBNode *from, NBNode *to)