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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Some methods for traversing lists of edges
18 /****************************************************************************/
19 #ifndef NBContHelper_h
20 #define NBContHelper_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <vector>
29 #include <iostream>
30 #include <cmath>
31 #include <algorithm>
32 #include <cassert>
33 #include "NBHelpers.h"
34 #include "NBCont.h"
35 #include "NBEdge.h"
36 #include "NBNode.h"
37 #include <utils/common/StdDefs.h>
38 #include <utils/geom/GeomHelper.h>
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
49 class NBContHelper {
50 public:
53  static void nextCW(const EdgeVector& edges,
54  EdgeVector::const_iterator& from);
55 
58  static void nextCCW(const EdgeVector& edges,
59  EdgeVector::const_iterator& from);
60 
61  static double getMaxSpeed(const EdgeVector& edges);
62 
63  static double getMinSpeed(const EdgeVector& edges);
64 
66  static std::ostream& out(std::ostream& os, const std::vector<bool>& v);
67 
68 
78  public:
81 
82  public:
84  int operator()(NBEdge* e1, NBEdge* e2) const;
85 
86  private:
89  };
90 
91 
98  public:
100  explicit straightness_sorter(const NBNode* n, const NBEdge* e):
101  myRefIncoming(e->getToNode() == n) {
102  if (myRefIncoming) {
103  myReferencePos = e->getLaneShape(0).back();
104  myReferenceAngle = e->getShapeEndAngle();
105  } else {
106  myReferencePos = e->getLaneShape(0).front();
107  myReferenceAngle = e->getShapeStartAngle();
108  }
109  }
110 
111  public:
113  int operator()(NBEdge* e1, NBEdge* e2) const;
114 
115  private:
119  };
120 
121 
130  public:
133 
134  public:
136  int operator()(NBEdge* e1, NBEdge* e2) const;
137 
138  private:
141  };
142 
143 
149  public:
151  int operator()(NBEdge* e1, NBEdge* e2) const {
152  if (e1->getPriority() != e2->getPriority()) {
153  return e1->getPriority() > e2->getPriority();
154  }
155  if (e1->getSpeed() != e2->getSpeed()) {
156  return e1->getSpeed() > e2->getSpeed();
157  }
158  return e1->getNumLanes() > e2->getNumLanes();
159  }
160  };
161 
162  // ---------------------------
163 
172  public:
177  explicit edge_opposite_direction_sorter(const NBEdge* const e, const NBNode* const n, bool regardPriority) :
178  myNode(n),
179  myEdge(e),
180  myRegardPriority(regardPriority) {
181  myAngle = getEdgeAngleAt(e, n);
182  }
183 
189  int operator()(NBEdge* e1, NBEdge* e2) const {
190  if (!myRegardPriority || e1->getPriority() == e2->getPriority() || e1 == myEdge || e2 == myEdge) {
191  return getDiff(e1) > getDiff(e2);
192  } else {
193  return e1->getPriority() > e2->getPriority();
194  }
195  }
196 
197  protected:
202  double getDiff(const NBEdge* const e) const {
203  return fabs(GeomHelper::angleDiff(getEdgeAngleAt(e, myNode), myAngle));
204  }
205 
212  double getEdgeAngleAt(const NBEdge* const e, const NBNode* const n) const {
213  if (e->getFromNode() == n) {
214  return e->getGeometry().angleAt2D(0);
215  } else {
216  return e->getGeometry()[-1].angleTo2D(e->getGeometry()[-2]);
217  }
218  }
219 
220  private:
221 
223  const NBNode* const myNode;
224 
226  const NBEdge* const myEdge;
227 
229  double myAngle;
230 
233 
234  private:
237 
238  };
239 
240  // ---------------------------
241 
249  public:
251  explicit edge_similar_direction_sorter(const NBEdge* const e)
252  : myAngle(e->getTotalAngle()) {}
253 
255  int operator()(NBEdge* e1, NBEdge* e2) const {
256  double d1 = GeomHelper::getMinAngleDiff(e1->getTotalAngle(), myAngle);
257  double d2 = GeomHelper::getMinAngleDiff(e2->getTotalAngle(), myAngle);
258  return d1 < d2;
259  }
260 
261  private:
263  double myAngle;
264  };
265 
266 
271  public:
273  node_with_incoming_finder(const NBEdge* const e);
274 
275  bool operator()(const NBNode* const n) const;
276 
277  private:
278  const NBEdge* const myEdge;
279 
280  private:
283 
284  };
285 
286 
291  public:
293  node_with_outgoing_finder(const NBEdge* const e);
294 
295  bool operator()(const NBNode* const n) const;
296 
297  private:
298  const NBEdge* const myEdge;
299 
300  private:
303 
304  };
305 
306 
307 
308 
310  public:
313 
314  bool operator()(NBEdge* e) const;
315 
316  private:
318 
319  private:
322 
323  };
324 
325 
328  static NBEdge* findConnectingEdge(const EdgeVector& edges,
329  NBNode* from, NBNode* to);
330 
331 
333  static double maxSpeed(const EdgeVector& ev);
334 
342  public:
345 
347  int operator()(NBEdge* e1, NBEdge* e2) const {
348  std::pair<double, double> mm1 = getMinMaxRelAngles(e1);
349  std::pair<double, double> mm2 = getMinMaxRelAngles(e2);
350  if (mm1.first == mm2.first && mm1.second == mm2.second) {
351  // ok, let's simply sort them arbitrarily
352  return e1->getID() < e2->getID();
353  }
354 
355  assert(
356  (mm1.first <= mm2.first && mm1.second <= mm2.second)
357  ||
358  (mm1.first >= mm2.first && mm1.second >= mm2.second));
359  return (mm1.first >= mm2.first && mm1.second >= mm2.second);
360  }
361 
365  std::pair<double, double> getMinMaxRelAngles(NBEdge* e) const {
366  double min = 360;
367  double max = 360;
368  const EdgeVector& ev = e->getConnectedEdges();
369  for (EdgeVector::const_iterator i = ev.begin(); i != ev.end(); ++i) {
370  double angle = NBHelpers::normRelAngle(
371  e->getTotalAngle(), (*i)->getTotalAngle());
372  if (min == 360 || min > angle) {
373  min = angle;
374  }
375  if (max == 360 || max < angle) {
376  max = angle;
377  }
378  }
379  return std::pair<double, double>(min, max);
380  }
381  };
382 
383 
384  friend std::ostream& operator<<(std::ostream& os, const EdgeVector& ev);
385 
387  public:
390  : myReferenceEdge(edge) { }
391 
392  bool operator()(NBEdge* e) const {
393  return e->isTurningDirectionAt(myReferenceEdge) ||
394  myReferenceEdge->isTurningDirectionAt(e);
395  }
396 
397  private:
399 
400  };
401 
407  public:
409  explicit edge_by_angle_to_nodeShapeCentroid_sorter(const NBNode* n) : myNode(n) {}
410 
411  public:
413  int operator()(const NBEdge* e1, const NBEdge* e2) const;
414 
415  private:
417  const NBNode* myNode;
418  };
419 
420 };
421 
422 
423 #endif
424 
425 /****************************************************************************/
426 
static double getMinAngleDiff(double angle1, double angle2)
Returns the minimum distance (clockwise/counter-clockwise) between both angles.
Definition: GeomHelper.cpp:161
double myAngle
the angle to find the edge with the opposite direction
Definition: NBContHelper.h:263
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:88
static double normRelAngle(double angle1, double angle2)
ensure that reverse relAngles (>=179.999) always count as turnarounds (-180)
Definition: NBHelpers.cpp:60
bool myRegardPriority
Whether edge priority may override closer angles.
Definition: NBContHelper.h:232
const NBNode *const myNode
The related node.
Definition: NBContHelper.h:223
int getPriority() const
Returns the priority of the edge.
Definition: NBEdge.h:427
friend std::ostream & operator<<(std::ostream &os, const EdgeVector &ev)
The representation of a single edge during network building.
Definition: NBEdge.h:65
Class to sort edges by their angle in relation to the given edge.
Definition: NBContHelper.h:171
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:212
std::pair< double, double > getMinMaxRelAngles(NBEdge *e) const
Definition: NBContHelper.h:365
opposite_finder(NBEdge *edge)
constructor
Definition: NBContHelper.h:389
int operator()(NBEdge *e1, NBEdge *e2) const
Comparing operation.
Definition: NBContHelper.h:189
double getDiff(const NBEdge *const e) const
Computes the angle difference between the related and the given edge.
Definition: NBContHelper.h:202
static void nextCW(const EdgeVector &edges, EdgeVector::const_iterator &from)
const std::string & getID() const
Returns the id.
Definition: Named.h:78
relative_outgoing_edge_sorter(NBEdge *e)
constructor
Definition: NBContHelper.h:80
double getShapeStartAngle() const
Returns the angle at the start of the edge.
Definition: NBEdge.cpp:1951
bool operator()(NBEdge *e) const
Definition: NBContHelper.h:392
straightness_sorter(const NBNode *n, const NBEdge *e)
constructor
Definition: NBContHelper.h:100
edge_similar_direction_sorter(const NBEdge *const e)
constructor
Definition: NBContHelper.h:251
static std::ostream & out(std::ostream &os, const std::vector< bool > &v)
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:420
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
Definition: NBContHelper.h:347
NBEdge * myEdge
the edge to compute the relative angle of
Definition: NBContHelper.h:140
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
edge_by_angle_to_nodeShapeCentroid_sorter(const NBNode *n)
constructor
Definition: NBContHelper.h:409
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
Definition: NBContHelper.h:255
double myAngle
The angle of the related edge at the given node.
Definition: NBContHelper.h:229
double getSpeed() const
Returns the speed allowed on this edge.
Definition: NBEdge.h:514
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:622
double getTotalAngle() const
Returns the angle at the start of the edge.
Definition: NBEdge.h:481
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operator
Definition: NBContHelper.h:151
const PositionVector & getLaneShape(int i) const
Returns the shape of the nth lane.
Definition: NBEdge.cpp:855
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:34
const NBNode * myNode
the edge to compute the relative angle of
Definition: NBContHelper.h:417
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:177
bool isTurningDirectionAt(const NBEdge *const edge) const
Returns whether the given edge is the opposite direction to this edge.
Definition: NBEdge.cpp:2684
Represents a single node (junction) during network building.
Definition: NBNode.h:68
EdgeVector getConnectedEdges() const
Returns the list of outgoing edges unsorted.
Definition: NBEdge.cpp:1214
static double getMaxSpeed(const EdgeVector &edges)
const NBEdge *const myEdge
the reference edge
Definition: NBContHelper.h:226
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:434
relative_incoming_edge_sorter(NBEdge *e)
constructor
Definition: NBContHelper.h:132
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:167
double getShapeEndAngle() const
Returns the angle at the end of the edge.
Definition: NBEdge.cpp:1959
static void nextCCW(const EdgeVector &edges, EdgeVector::const_iterator &from)
static NBEdge * findConnectingEdge(const EdgeVector &edges, NBNode *from, NBNode *to)