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, bool regardPriority) :
185  myNode(n),
186  myEdge(e),
187  myRegardPriority(regardPriority) {
188  myAngle = getEdgeAngleAt(e, n);
189  }
190 
196  int operator()(NBEdge* e1, NBEdge* e2) const {
197  if (!myRegardPriority || e1->getPriority() == e2->getPriority() || e1 == myEdge || e2 == myEdge) {
198  return getDiff(e1) > getDiff(e2);
199  } else {
200  return e1->getPriority() > e2->getPriority();
201  }
202  }
203 
204  protected:
209  double getDiff(const NBEdge* const e) const {
210  return fabs(GeomHelper::angleDiff(getEdgeAngleAt(e, myNode), myAngle));
211  }
212 
219  double getEdgeAngleAt(const NBEdge* const e, const NBNode* const n) const {
220  if (e->getFromNode() == n) {
221  return e->getGeometry().angleAt2D(0);
222  } else {
223  return e->getGeometry()[-1].angleTo2D(e->getGeometry()[-2]);
224  }
225  }
226 
227  private:
228 
230  const NBNode* const myNode;
231 
233  const NBEdge* const myEdge;
234 
236  double myAngle;
237 
240 
241  private:
244 
245  };
246 
247  // ---------------------------
248 
256  public:
258  explicit edge_similar_direction_sorter(const NBEdge* const e)
259  : myAngle(e->getTotalAngle()) {}
260 
262  int operator()(NBEdge* e1, NBEdge* e2) const {
263  double d1 = GeomHelper::getMinAngleDiff(e1->getTotalAngle(), myAngle);
264  double d2 = GeomHelper::getMinAngleDiff(e2->getTotalAngle(), myAngle);
265  return d1 < d2;
266  }
267 
268  private:
270  double myAngle;
271  };
272 
273 
278  public:
280  node_with_incoming_finder(const NBEdge* const e);
281 
282  bool operator()(const NBNode* const n) const;
283 
284  private:
285  const NBEdge* const myEdge;
286 
287  private:
290 
291  };
292 
293 
298  public:
300  node_with_outgoing_finder(const NBEdge* const e);
301 
302  bool operator()(const NBNode* const n) const;
303 
304  private:
305  const NBEdge* const myEdge;
306 
307  private:
310 
311  };
312 
313 
314 
315 
317  public:
320 
321  bool operator()(NBEdge* e) const;
322 
323  private:
325 
326  private:
329 
330  };
331 
332 
335  static NBEdge* findConnectingEdge(const EdgeVector& edges,
336  NBNode* from, NBNode* to);
337 
338 
340  static double maxSpeed(const EdgeVector& ev);
341 
349  public:
352 
354  int operator()(NBEdge* e1, NBEdge* e2) const {
355  std::pair<double, double> mm1 = getMinMaxRelAngles(e1);
356  std::pair<double, double> mm2 = getMinMaxRelAngles(e2);
357  if (mm1.first == mm2.first && mm1.second == mm2.second) {
358  // ok, let's simply sort them arbitrarily
359  return e1->getID() < e2->getID();
360  }
361 
362  assert(
363  (mm1.first <= mm2.first && mm1.second <= mm2.second)
364  ||
365  (mm1.first >= mm2.first && mm1.second >= mm2.second));
366  return (mm1.first >= mm2.first && mm1.second >= mm2.second);
367  }
368 
372  std::pair<double, double> getMinMaxRelAngles(NBEdge* e) const {
373  double min = 360;
374  double max = 360;
375  const EdgeVector& ev = e->getConnectedEdges();
376  for (EdgeVector::const_iterator i = ev.begin(); i != ev.end(); ++i) {
377  double angle = NBHelpers::normRelAngle(
378  e->getTotalAngle(), (*i)->getTotalAngle());
379  if (min == 360 || min > angle) {
380  min = angle;
381  }
382  if (max == 360 || max < angle) {
383  max = angle;
384  }
385  }
386  return std::pair<double, double>(min, max);
387  }
388  };
389 
390 
391  friend std::ostream& operator<<(std::ostream& os, const EdgeVector& ev);
392 
394  public:
397  : myReferenceEdge(edge) { }
398 
399  bool operator()(NBEdge* e) const {
400  return e->isTurningDirectionAt(myReferenceEdge) ||
401  myReferenceEdge->isTurningDirectionAt(e);
402  }
403 
404  private:
406 
407  };
408 
414  public:
416  explicit edge_by_angle_to_nodeShapeCentroid_sorter(const NBNode* n) : myNode(n) {}
417 
418  public:
420  int operator()(const NBEdge* e1, const NBEdge* e2) const;
421 
422  private:
424  const NBNode* myNode;
425  };
426 
427 };
428 
429 
430 #endif
431 
432 /****************************************************************************/
433 
static double getMinAngleDiff(double angle1, double angle2)
Returns the minimum distance (clockwise/counter-clockwise) between both angles.
Definition: GeomHelper.cpp:168
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:219
double getDiff(const NBEdge *const e) const
Computes the angle difference between the related and the given edge.
Definition: NBContHelper.h:209
double myAngle
the angle to find the edge with the opposite direction
Definition: NBContHelper.h:270
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
bool myRegardPriority
Whether edge priority may override closer angles.
Definition: NBContHelper.h:239
const NBNode *const myNode
The related node.
Definition: NBContHelper.h:230
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 getSpeed() const
Returns the speed allowed on this edge.
Definition: NBEdge.h:508
opposite_finder(NBEdge *edge)
constructor
Definition: NBContHelper.h:396
static void nextCW(const EdgeVector &edges, EdgeVector::const_iterator &from)
relative_outgoing_edge_sorter(NBEdge *e)
constructor
Definition: NBContHelper.h:87
straightness_sorter(const NBNode *n, const NBEdge *e)
constructor
Definition: NBContHelper.h:107
EdgeVector getConnectedEdges() const
Returns the list of outgoing edges unsorted.
Definition: NBEdge.cpp:1120
edge_similar_direction_sorter(const NBEdge *const e)
constructor
Definition: NBContHelper.h:258
double getTotalAngle() const
Returns the angle at the start of the edge.
Definition: NBEdge.h:475
const std::string & getID() const
Returns the id.
Definition: Named.h:66
double getShapeEndAngle() const
Returns the angle at the end of the edge.
Definition: NBEdge.cpp:1746
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
Definition: NBContHelper.h:262
#define max(a, b)
Definition: polyfonts.c:65
int getPriority() const
Returns the priority of the edge.
Definition: NBEdge.h:421
static std::ostream & out(std::ostream &os, const std::vector< bool > &v)
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
Definition: NBContHelper.h:354
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
NBEdge * myEdge
the edge to compute the relative angle of
Definition: NBContHelper.h:147
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:414
bool isTurningDirectionAt(const NBEdge *const edge) const
Returns whether the given edge is the opposite direction to this edge.
Definition: NBEdge.cpp:2422
std::pair< double, double > getMinMaxRelAngles(NBEdge *e) const
Definition: NBContHelper.h:372
int operator()(NBEdge *e1, NBEdge *e2) const
Comparing operation.
Definition: NBContHelper.h:196
edge_by_angle_to_nodeShapeCentroid_sorter(const NBNode *n)
constructor
Definition: NBContHelper.h:416
double myAngle
The angle of the related edge at the given node.
Definition: NBContHelper.h:236
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operator
Definition: NBContHelper.h:158
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:596
const NBNode * myNode
the edge to compute the relative angle of
Definition: NBContHelper.h:424
edge_opposite_direction_sorter(const NBEdge *const e, const NBNode *const n, bool regardPriority)
Constructor.
Definition: NBContHelper.h:184
Represents a single node (junction) during network building.
Definition: NBNode.h:75
double getShapeStartAngle() const
Returns the angle at the start of the edge.
Definition: NBEdge.cpp:1738
static double getMaxSpeed(const EdgeVector &edges)
const NBEdge *const myEdge
the reference edge
Definition: NBContHelper.h:233
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
bool operator()(NBEdge *e) const
Definition: NBContHelper.h:399
const PositionVector & getLaneShape(int i) const
Returns the shape of the nth lane.
Definition: NBEdge.cpp:770
double angleAt2D(int pos) const
get angle in certain position of position vector
static void nextCCW(const EdgeVector &edges, EdgeVector::const_iterator &from)
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
static NBEdge * findConnectingEdge(const EdgeVector &edges, NBNode *from, NBNode *to)
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:428