Eclipse 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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Some methods for traversing lists of edges
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
25 #include <vector>
26 #include <iostream>
27 #include <cmath>
28 #include <algorithm>
29 #include <cassert>
30 #include "NBHelpers.h"
31 #include "NBCont.h"
32 #include "NBEdge.h"
33 #include "NBNode.h"
34 #include <utils/common/StdDefs.h>
35 #include <utils/geom/GeomHelper.h>
36 
37 
38 // ===========================================================================
39 // class definitions
40 // ===========================================================================
46 class NBContHelper {
47 public:
50  static void nextCW(const EdgeVector& edges,
51  EdgeVector::const_iterator& from);
52 
55  static void nextCCW(const EdgeVector& edges,
56  EdgeVector::const_iterator& from);
57 
58  static double getMaxSpeed(const EdgeVector& edges);
59 
60  static double getMinSpeed(const EdgeVector& edges);
61 
63  static std::ostream& out(std::ostream& os, const std::vector<bool>& v);
64 
65 
75  public:
77  explicit relative_outgoing_edge_sorter(NBEdge* e) : myAngle(e->getEndAngle()) {}
79  explicit relative_outgoing_edge_sorter(double angle) : myAngle(angle) {}
80 
81  public:
83  bool operator()(const NBEdge* e1, const NBEdge* e2) const;
84 
85  private:
87  double myAngle;
88  };
89 
90 
99  public:
101  explicit relative_incoming_edge_sorter(NBEdge* e) : myAngle(e->getStartAngle()) {}
103  explicit relative_incoming_edge_sorter(double angle) : myAngle(angle) {}
104 
105  public:
107  bool operator()(const NBEdge* e1, const NBEdge* e2) const;
108 
109  private:
111  double myAngle;
112  };
113 
114 
120  public:
122  int operator()(NBEdge* e1, NBEdge* e2) const {
123  if (e1->getPriority() != e2->getPriority()) {
124  return e1->getPriority() > e2->getPriority();
125  }
126  if (e1->getSpeed() != e2->getSpeed()) {
127  return e1->getSpeed() > e2->getSpeed();
128  }
129  return e1->getNumLanes() > e2->getNumLanes();
130  }
131  };
132 
133  // ---------------------------
134 
143  public:
148  explicit edge_opposite_direction_sorter(const NBEdge* const e, const NBNode* const n, bool regardPriority) :
149  myNode(n),
150  myEdge(e),
151  myRegardPriority(regardPriority) {
152  myAngle = getEdgeAngleAt(e, n);
153  }
154 
160  int operator()(NBEdge* e1, NBEdge* e2) const {
161  if (!myRegardPriority || e1->getPriority() == e2->getPriority() || e1 == myEdge || e2 == myEdge) {
162  return getDiff(e1) > getDiff(e2);
163  } else {
164  return e1->getPriority() > e2->getPriority();
165  }
166  }
167 
168  protected:
173  double getDiff(const NBEdge* const e) const {
175  }
176 
183  double getEdgeAngleAt(const NBEdge* const e, const NBNode* const n) const {
184  if (e->getFromNode() == n) {
185  return e->getGeometry().angleAt2D(0);
186  } else {
187  return e->getGeometry()[-1].angleTo2D(e->getGeometry()[-2]);
188  }
189  }
190 
191  private:
192 
194  const NBNode* const myNode;
195 
197  const NBEdge* const myEdge;
198 
200  double myAngle;
201 
204 
205  private:
208 
209  };
210 
211  // ---------------------------
212 
220  public:
222  explicit edge_similar_direction_sorter(const NBEdge* const e, bool outgoing = true) :
223  myCompareOutgoing(outgoing),
224  myAngle(outgoing ? e->getShapeEndAngle() : e->getShapeStartAngle())
225  {}
226 
228  int operator()(const NBEdge* e1, const NBEdge* e2) const {
229  const double d1 = angleDiff(myCompareOutgoing ? e1->getShapeStartAngle() : e1->getShapeEndAngle(), myAngle);
230  const double d2 = angleDiff(myCompareOutgoing ? e2->getShapeStartAngle() : e2->getShapeEndAngle(), myAngle);
231  if (fabs(fabs(d1) - fabs(d2)) < NUMERICAL_EPS) {
232  if (fabs(d1 - d2) > NUMERICAL_EPS) {
233  return d1 < d2;
234  } else {
235  return e1->getNumericalID() < e2->getNumericalID();
236  }
237  }
238  return fabs(d1) < fabs(d2);
239  }
240 
241  private:
242  double angleDiff(const double angle1, const double angle2) const {
243  double d = angle2 - angle1;
244  while (d >= 180.) {
245  d -= 360.;
246  }
247  while (d < -180.) {
248  d += 360.;
249  }
250  return d;
251  }
252 
253 
254  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 {
389  }
390 
391  private:
393 
394  };
395 
401  public:
404 
405  public:
407  bool operator()(const NBEdge* e1, const NBEdge* e2) const;
408 
409  private:
411  const NBNode* myNode;
412  };
413 
414 };
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:34
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:179
bool operator()(const NBEdge *e1, const NBEdge *e2) const
comparing operation
const NBNode * myNode
the edge to compute the relative angle of
Definition: NBContHelper.h:411
edge_by_angle_to_nodeShapeCentroid_sorter(const NBNode *n)
constructor
Definition: NBContHelper.h:403
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operator
Definition: NBContHelper.h:122
Class to sort edges by their angle in relation to the given edge.
Definition: NBContHelper.h:142
double getEdgeAngleAt(const NBEdge *const e, const NBNode *const n) const
Returns the given edge's angle at the given node.
Definition: NBContHelper.h:183
bool myRegardPriority
Whether edge priority may override closer angles.
Definition: NBContHelper.h:203
double getDiff(const NBEdge *const e) const
Computes the angle difference between the related and the given edge.
Definition: NBContHelper.h:173
const NBNode *const myNode
The related node.
Definition: NBContHelper.h:194
const NBEdge *const myEdge
the reference edge
Definition: NBContHelper.h:197
edge_opposite_direction_sorter & operator=(const edge_opposite_direction_sorter &s)
Invalidated assignment operator.
double myAngle
The angle of the related edge at the given node.
Definition: NBContHelper.h:200
edge_opposite_direction_sorter(const NBEdge *const e, const NBNode *const n, bool regardPriority)
Constructor.
Definition: NBContHelper.h:148
int operator()(NBEdge *e1, NBEdge *e2) const
Comparing operation.
Definition: NBContHelper.h:160
double angleDiff(const double angle1, const double angle2) const
Definition: NBContHelper.h:242
bool myCompareOutgoing
the angle to find the edge with the opposite direction
Definition: NBContHelper.h:256
int operator()(const NBEdge *e1, const NBEdge *e2) const
comparing operation
Definition: NBContHelper.h:228
edge_similar_direction_sorter(const NBEdge *const e, bool outgoing=true)
constructor
Definition: NBContHelper.h:222
edge_with_destination_finder(NBNode *dest)
constructor
edge_with_destination_finder & operator=(const edge_with_destination_finder &s)
invalidated assignment operator
bool operator()(const NBNode *const n) const
node_with_incoming_finder & operator=(const node_with_incoming_finder &s)
invalidated assignment operator
node_with_incoming_finder(const NBEdge *const e)
constructor
bool operator()(const NBNode *const n) const
node_with_outgoing_finder(const NBEdge *const e)
constructor
node_with_outgoing_finder & operator=(const node_with_outgoing_finder &s)
invalidated assignment operator
opposite_finder(NBEdge *edge)
constructor
Definition: NBContHelper.h:383
bool operator()(NBEdge *e) const
Definition: NBContHelper.h:386
relative_incoming_edge_sorter(NBEdge *e)
constructor
Definition: NBContHelper.h:101
bool operator()(const NBEdge *e1, const NBEdge *e2) const
comparing operation
double myAngle
the reference angle to compare edges agains
Definition: NBContHelper.h:111
relative_incoming_edge_sorter(double angle)
constructor
Definition: NBContHelper.h:103
bool operator()(const NBEdge *e1, const NBEdge *e2) const
comparing operation
relative_outgoing_edge_sorter(double angle)
constructor
Definition: NBContHelper.h:79
double myAngle
the reference angle to compare edges agains
Definition: NBContHelper.h:87
relative_outgoing_edge_sorter(NBEdge *e)
constructor
Definition: NBContHelper.h:77
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
Definition: NBContHelper.h:341
std::pair< double, double > getMinMaxRelAngles(NBEdge *e) const
Definition: NBContHelper.h:359
static NBEdge * findConnectingEdge(const EdgeVector &edges, NBNode *from, NBNode *to)
static double getMaxSpeed(const EdgeVector &edges)
static void nextCCW(const EdgeVector &edges, EdgeVector::const_iterator &from)
static void nextCW(const EdgeVector &edges, EdgeVector::const_iterator &from)
static double getMinSpeed(const EdgeVector &edges)
static double maxSpeed(const EdgeVector &ev)
static std::ostream & out(std::ostream &os, const std::vector< bool > &v)
friend std::ostream & operator<<(std::ostream &os, const EdgeVector &ev)
The representation of a single edge during network building.
Definition: NBEdge.h:91
double getShapeStartAngle() const
Returns the angle at the start of the edge.
Definition: NBEdge.cpp:2155
int getNumericalID() const
Returns the index (numeric id) of the edge.
Definition: NBEdge.h:1405
const std::string & getID() const
Definition: NBEdge.h:1423
double getSpeed() const
Returns the speed allowed on this edge.
Definition: NBEdge.h:589
bool isTurningDirectionAt(const NBEdge *const edge) const
Returns whether the given edge is the opposite direction to this edge.
Definition: NBEdge.cpp:3006
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:490
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:716
EdgeVector getConnectedEdges() const
Returns the list of outgoing edges unsorted.
Definition: NBEdge.cpp:1251
double getTotalAngle() const
Returns the angle at the start of the edge.
Definition: NBEdge.h:556
int getPriority() const
Returns the priority of the edge.
Definition: NBEdge.h:497
double getShapeEndAngle() const
Returns the angle at the end of the edge.
Definition: NBEdge.cpp:2163
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:509
static double normRelAngle(double angle1, double angle2)
ensure that reverse relAngles (>=179.999) always count as turnarounds (-180)
Definition: NBHelpers.cpp:58
Represents a single node (junction) during network building.
Definition: NBNode.h:66
double angleAt2D(int pos) const
get angle in certain position of position vector