Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2025 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"
36
37
38// ===========================================================================
39// class definitions
40// ===========================================================================
47public:
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:
123 int operator()(NBEdge* e1, NBEdge* e2) const {
124 if (e1->getPriority() != e2->getPriority()) {
125 return e1->getPriority() > e2->getPriority();
126 }
127 if (e1->getSpeed() != e2->getSpeed()) {
128 return e1->getSpeed() > e2->getSpeed();
129 }
131 }
132
133 private:
135 };
136
137 // ---------------------------
138
147 public:
152 explicit edge_opposite_direction_sorter(const NBEdge* const e, const NBNode* const n, bool regardPriority) :
153 myNode(n),
154 myEdge(e),
155 myRegardPriority(regardPriority) {
156 myAngle = getEdgeAngleAt(e, n);
157 }
158
164 int operator()(NBEdge* e1, NBEdge* e2) const {
165 if (!myRegardPriority || e1->getPriority() == e2->getPriority() || e1 == myEdge || e2 == myEdge) {
166 return getDiff(e1) > getDiff(e2);
167 } else {
168 return e1->getPriority() > e2->getPriority();
169 }
170 }
171
172 protected:
177 double getDiff(const NBEdge* const e) const {
179 }
180
187 double getEdgeAngleAt(const NBEdge* const e, const NBNode* const n) const {
188 if (e->getFromNode() == n) {
189 return e->getGeometry().angleAt2D(0);
190 } else {
191 return e->getGeometry()[-1].angleTo2D(e->getGeometry()[-2]);
192 }
193 }
194
195 private:
196
198 const NBNode* const myNode;
199
201 const NBEdge* const myEdge;
202
204 double myAngle;
205
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 };
275
276
281 public:
283 node_with_outgoing_finder(const NBEdge* const e);
284
285 bool operator()(const NBNode* const n) const;
286
287 private:
288 const NBEdge* const myEdge;
289
290 };
291
292
293
294
296 public:
299
300 bool operator()(NBEdge* e) const;
301
302 private:
304
305 private:
308
309 };
310
311
314 static NBEdge* findConnectingEdge(const EdgeVector& edges,
315 NBNode* from, NBNode* to);
316
317
319 static double maxSpeed(const EdgeVector& ev);
320
328 public:
331
333 int operator()(NBEdge* e1, NBEdge* e2) const {
334 std::pair<double, double> mm1 = getMinMaxRelAngles(e1);
335 std::pair<double, double> mm2 = getMinMaxRelAngles(e2);
336 if (mm1.first == mm2.first && mm1.second == mm2.second) {
337 // ok, let's simply sort them arbitrarily
338 return e1->getID() < e2->getID();
339 }
340
341 assert(
342 (mm1.first <= mm2.first && mm1.second <= mm2.second)
343 ||
344 (mm1.first >= mm2.first && mm1.second >= mm2.second));
345 return (mm1.first >= mm2.first && mm1.second >= mm2.second);
346 }
347
351 std::pair<double, double> getMinMaxRelAngles(NBEdge* e) const {
352 double min = 360;
353 double max = 360;
354 const EdgeVector& ev = e->getConnectedEdges();
355 for (EdgeVector::const_iterator i = ev.begin(); i != ev.end(); ++i) {
356 double angle = NBHelpers::normRelAngle(
357 e->getTotalAngle(), (*i)->getTotalAngle());
358 if (min == 360 || min > angle) {
359 min = angle;
360 }
361 if (max == 360 || max < angle) {
362 max = angle;
363 }
364 }
365 return std::pair<double, double>(min, max);
366 }
367 };
368
369
370 friend std::ostream& operator<<(std::ostream& os, const EdgeVector& ev);
371
373 public:
376 : myReferenceEdge(edge) { }
377
382
383 private:
385
386 };
387
393 public:
396
397 public:
399 bool operator()(const NBEdge* e1, const NBEdge* e2) const;
400
401 private:
404 };
405
406};
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition NBCont.h:42
long long int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
static double angleDiff(const double angle1, const double angle2)
Returns the difference of the second angle to the first angle in radiants.
bool operator()(const NBEdge *e1, const NBEdge *e2) const
comparing operation
const NBNode * myNode
the edge to compute the relative angle of
edge_by_angle_to_nodeShapeCentroid_sorter(const NBNode *n)
constructor
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operator
edge_by_priority_sorter(SVCPermissions permissions)
Class to sort edges by their angle in relation to the given edge.
double getEdgeAngleAt(const NBEdge *const e, const NBNode *const n) const
Returns the given edge's angle at the given node.
bool myRegardPriority
Whether edge priority may override closer angles.
double getDiff(const NBEdge *const e) const
Computes the angle difference between the related and the given edge.
const NBNode *const myNode
The related node.
const NBEdge *const myEdge
the reference edge
double myAngle
The angle of the related edge at the given node.
edge_opposite_direction_sorter(const NBEdge *const e, const NBNode *const n, bool regardPriority)
Constructor.
int operator()(NBEdge *e1, NBEdge *e2) const
Comparing operation.
double angleDiff(const double angle1, const double angle2) const
bool myCompareOutgoing
the angle to find the edge with the opposite direction
int operator()(const NBEdge *e1, const NBEdge *e2) const
comparing operation
edge_similar_direction_sorter(const NBEdge *const e, bool outgoing=true)
constructor
edge_with_destination_finder & operator=(const edge_with_destination_finder &s)
invalidated assignment operator
bool operator()(const NBNode *const n) const
bool operator()(const NBNode *const n) const
opposite_finder(NBEdge *edge)
constructor
bool operator()(NBEdge *e) const
relative_incoming_edge_sorter(NBEdge *e)
constructor
bool operator()(const NBEdge *e1, const NBEdge *e2) const
comparing operation
double myAngle
the reference angle to compare edges agains
relative_incoming_edge_sorter(double angle)
constructor
bool operator()(const NBEdge *e1, const NBEdge *e2) const
comparing operation
relative_outgoing_edge_sorter(double angle)
constructor
double myAngle
the reference angle to compare edges agains
relative_outgoing_edge_sorter(NBEdge *e)
constructor
int operator()(NBEdge *e1, NBEdge *e2) const
comparing operation
std::pair< double, double > getMinMaxRelAngles(NBEdge *e) const
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:2434
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition NBEdge.h:789
int getNumericalID() const
Returns the index (numeric id) of the edge.
Definition NBEdge.h:1533
double getSpeed() const
Returns the speed allowed on this edge.
Definition NBEdge.h:625
const std::string & getID() const
Definition NBEdge.h:1551
bool isTurningDirectionAt(const NBEdge *const edge) const
Returns whether the given edge is the opposite direction to this edge.
Definition NBEdge.cpp:3807
int getNumLanesThatAllow(SVCPermissions permissions, bool allPermissions=true) const
Definition NBEdge.cpp:4669
EdgeVector getConnectedEdges() const
Returns the list of outgoing edges unsorted.
Definition NBEdge.cpp:1404
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition NBEdge.h:545
double getTotalAngle() const
Returns the angle at the start of the edge.
Definition NBEdge.h:592
int getPriority() const
Returns the priority of the edge.
Definition NBEdge.h:533
double getShapeEndAngle() const
Returns the angle at the end of the edge.
Definition NBEdge.cpp:2442
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)