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.dev/sumo
3 // Copyright (C) 2001-2024 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  };
206 
207  // ---------------------------
208 
216  public:
218  explicit edge_similar_direction_sorter(const NBEdge* const e, bool outgoing = true) :
219  myCompareOutgoing(outgoing),
220  myAngle(outgoing ? e->getShapeEndAngle() : e->getShapeStartAngle())
221  {}
222 
224  int operator()(const NBEdge* e1, const NBEdge* e2) const {
225  const double d1 = angleDiff(myCompareOutgoing ? e1->getShapeStartAngle() : e1->getShapeEndAngle(), myAngle);
226  const double d2 = angleDiff(myCompareOutgoing ? e2->getShapeStartAngle() : e2->getShapeEndAngle(), myAngle);
227  if (fabs(fabs(d1) - fabs(d2)) < NUMERICAL_EPS) {
228  if (fabs(d1 - d2) > NUMERICAL_EPS) {
229  return d1 < d2;
230  } else {
231  return e1->getNumericalID() < e2->getNumericalID();
232  }
233  }
234  return fabs(d1) < fabs(d2);
235  }
236 
237  private:
238  double angleDiff(const double angle1, const double angle2) const {
239  double d = angle2 - angle1;
240  while (d >= 180.) {
241  d -= 360.;
242  }
243  while (d < -180.) {
244  d += 360.;
245  }
246  return d;
247  }
248 
249 
250  private:
253  double myAngle;
254  };
255 
256 
261  public:
263  node_with_incoming_finder(const NBEdge* const e);
264 
265  bool operator()(const NBNode* const n) const;
266 
267  private:
268  const NBEdge* const myEdge;
269 
270  };
271 
272 
277  public:
279  node_with_outgoing_finder(const NBEdge* const e);
280 
281  bool operator()(const NBNode* const n) const;
282 
283  private:
284  const NBEdge* const myEdge;
285 
286  };
287 
288 
289 
290 
292  public:
295 
296  bool operator()(NBEdge* e) const;
297 
298  private:
300 
301  private:
304 
305  };
306 
307 
310  static NBEdge* findConnectingEdge(const EdgeVector& edges,
311  NBNode* from, NBNode* to);
312 
313 
315  static double maxSpeed(const EdgeVector& ev);
316 
324  public:
327 
329  int operator()(NBEdge* e1, NBEdge* e2) const {
330  std::pair<double, double> mm1 = getMinMaxRelAngles(e1);
331  std::pair<double, double> mm2 = getMinMaxRelAngles(e2);
332  if (mm1.first == mm2.first && mm1.second == mm2.second) {
333  // ok, let's simply sort them arbitrarily
334  return e1->getID() < e2->getID();
335  }
336 
337  assert(
338  (mm1.first <= mm2.first && mm1.second <= mm2.second)
339  ||
340  (mm1.first >= mm2.first && mm1.second >= mm2.second));
341  return (mm1.first >= mm2.first && mm1.second >= mm2.second);
342  }
343 
347  std::pair<double, double> getMinMaxRelAngles(NBEdge* e) const {
348  double min = 360;
349  double max = 360;
350  const EdgeVector& ev = e->getConnectedEdges();
351  for (EdgeVector::const_iterator i = ev.begin(); i != ev.end(); ++i) {
352  double angle = NBHelpers::normRelAngle(
353  e->getTotalAngle(), (*i)->getTotalAngle());
354  if (min == 360 || min > angle) {
355  min = angle;
356  }
357  if (max == 360 || max < angle) {
358  max = angle;
359  }
360  }
361  return std::pair<double, double>(min, max);
362  }
363  };
364 
365 
366  friend std::ostream& operator<<(std::ostream& os, const EdgeVector& ev);
367 
369  public:
372  : myReferenceEdge(edge) { }
373 
374  bool operator()(NBEdge* e) const {
377  }
378 
379  private:
381 
382  };
383 
389  public:
392 
393  public:
395  bool operator()(const NBEdge* e1, const NBEdge* e2) const;
396 
397  private:
399  const NBNode* myNode;
400  };
401 
402 };
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:35
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:178
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:399
edge_by_angle_to_nodeShapeCentroid_sorter(const NBNode *n)
constructor
Definition: NBContHelper.h:391
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
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:238
bool myCompareOutgoing
the angle to find the edge with the opposite direction
Definition: NBContHelper.h:252
int operator()(const NBEdge *e1, const NBEdge *e2) const
comparing operation
Definition: NBContHelper.h:224
edge_similar_direction_sorter(const NBEdge *const e, bool outgoing=true)
constructor
Definition: NBContHelper.h:218
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(const NBEdge *const e)
constructor
bool operator()(const NBNode *const n) const
node_with_outgoing_finder(const NBEdge *const e)
constructor
opposite_finder(NBEdge *edge)
constructor
Definition: NBContHelper.h:371
bool operator()(NBEdge *e) const
Definition: NBContHelper.h:374
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:329
std::pair< double, double > getMinMaxRelAngles(NBEdge *e) const
Definition: NBContHelper.h:347
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:92
double getShapeStartAngle() const
Returns the angle at the start of the edge.
Definition: NBEdge.cpp:2359
int getNumericalID() const
Returns the index (numeric id) of the edge.
Definition: NBEdge.h:1504
const std::string & getID() const
Definition: NBEdge.h:1522
double getSpeed() const
Returns the speed allowed on this edge.
Definition: NBEdge.h:615
bool isTurningDirectionAt(const NBEdge *const edge) const
Returns whether the given edge is the opposite direction to this edge.
Definition: NBEdge.cpp:3592
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:516
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:779
EdgeVector getConnectedEdges() const
Returns the list of outgoing edges unsorted.
Definition: NBEdge.cpp:1346
double getTotalAngle() const
Returns the angle at the start of the edge.
Definition: NBEdge.h:582
int getPriority() const
Returns the priority of the edge.
Definition: NBEdge.h:523
double getShapeEndAngle() const
Returns the angle at the end of the edge.
Definition: NBEdge.cpp:2367
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:535
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 (in radians between -M_PI and M_PI)