Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
NLEdgeControlBuilder.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 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/****************************************************************************/
21// Interface for building edges
22/****************************************************************************/
23#include <config.h>
24
25#include <vector>
26#include <string>
27#include <map>
28#include <algorithm>
29#include <iterator>
30#include <mesosim/MELoop.h>
31#include <microsim/MSGlobals.h>
32#include <microsim/MSLane.h>
33#include <microsim/MSEdge.h>
38#include "NLBuilder.h"
41
42
43// ===========================================================================
44// method definitions
45// ===========================================================================
47 : myCurrentNumericalLaneID(0), myCurrentNumericalEdgeID(0), myEdges(0), myCurrentLaneIndex(-1) {
48 myActiveEdge = (MSEdge*) nullptr;
49 myLaneStorage = new std::vector<MSLane*>();
50}
51
52
59
60
61void
63 const std::string& id, const SumoXMLEdgeFunc function,
64 const std::string& streetName,
65 const std::string& edgeType,
66 const std::string& routingType,
67 int priority,
68 const std::string& bidi,
69 double distance) {
70 // closeEdge might not have been called because the last edge had an error, so we clear the lane storage
72 // if the previous edge was broken (never registered in the static dict), remove and delete it
74 myEdges.pop_back();
75 delete myActiveEdge;
76 myActiveEdge = nullptr;
77 }
78 if (MSEdge::dictionary(id) != nullptr) {
79 throw InvalidArgument("Another edge with the id '" + id + "' exists.");
80 }
81 myActiveEdge = buildEdge(id, function, streetName, edgeType, routingType, priority, distance);
82 myEdges.push_back(myActiveEdge);
83 if (bidi != "") {
85 }
86}
87
88
89MSLane*
90NLEdgeControlBuilder::addLane(const std::string& id,
91 double maxSpeed, double friction, double length,
92 const PositionVector& shape, double width,
93 SVCPermissions permissions,
94 SVCPermissions changeLeft, SVCPermissions changeRight,
95 int index, bool isRampAccel,
96 const std::string& type,
97 const PositionVector& outlineShape) {
98 MSLane* lane = new MSLane(id, maxSpeed, friction, length, myActiveEdge, myCurrentNumericalLaneID++, shape, width, permissions, changeLeft, changeRight, index, isRampAccel, type, outlineShape);
99 myLaneStorage->push_back(lane);
100 myCurrentLaneIndex = index;
101 return lane;
102}
103
104
105void
107 if (myCurrentLaneIndex == -1) {
108 setDefaultStopOffset(stopOffset);
109 } else {
110 updateCurrentLaneStopOffset(stopOffset);
111 }
112}
113
114
115std::string
117 std::stringstream ss;
118 if (myCurrentLaneIndex != -1) {
119 ss << "lane " << myCurrentLaneIndex << " of ";
120 }
121 ss << "edge '" << myActiveEdge->getID() << "'";
122 return ss.str();
123}
124
125
126void
128 if (myLaneStorage->size() == 0) {
129 throw ProcessError("myLaneStorage cannot be empty");
130 }
131 if (stopOffset.isDefined()) {
132 if (myLaneStorage->back()->getLaneStopOffsets().isDefined()) {
133 WRITE_WARNING("Duplicate stopOffset definition for lane " + toString(myLaneStorage->back()->getIndex()) +
134 " on edge " + myActiveEdge->getID() + "!")
135 } else {
136 myLaneStorage->back()->setLaneStopOffset(stopOffset);
137 }
138 }
139}
140
141
142void
145 WRITE_WARNING("Duplicate stopOffset definition for edge " + myActiveEdge->getID() + ". Ignoring duplicate specification.")
146 } else {
147 myCurrentDefaultStopOffset = stopOffsets;
148 }
149}
150
151
152void
154 if (myActiveEdge == nullptr) {
155 throw ProcessError("myActiveEdge cannot be nullptr");
156 }
158 for (const auto& l : *myLaneStorage) {
159 if (!l->getLaneStopOffsets().isDefined()) {
160 l->setLaneStopOffset(myCurrentDefaultStopOffset);
161 }
162 }
163 }
164}
165
166
167void
168NLEdgeControlBuilder::addNeigh(const std::string id) {
169 myOppositeLanes.push_back({myLaneStorage->back(), id});
170}
171
172
173MSEdge*
176 std::vector<MSLane*>* lanes = new std::vector<MSLane*>();
177 lanes->reserve(myLaneStorage->size());
178 copy(myLaneStorage->begin(), myLaneStorage->end(), back_inserter(*lanes));
179 myLaneStorage->clear();
180 myActiveEdge->initialize(lanes);
182 MSEdge* result = myActiveEdge;
183 myActiveEdge = nullptr;
184 return result;
185}
186
187
188void
192
193
196 if (MSGlobals::gUseMesoSim && !OptionsCont::getOptions().getBool("meso-lane-queue")) {
197 MSEdge::setMesoIgnoredVClasses(parseVehicleClasses(OptionsCont::getOptions().getStringVector("meso-ignore-lanes-by-vclass")));
198 }
199 // connecting opposite lanes must happen before MSEdge::closeBuilding
200 for (auto item : myOppositeLanes) {
201 MSLane* oppo = MSLane::dictionary(item.second);
202 if (oppo == nullptr) {
203 WRITE_ERRORF("Unknown neigh lane '%' for lane '%'", item.second, item.first->getID());
204 } else {
205 item.first->setOpposite(oppo);
206 }
207 }
208 // consistency check
209 std::set<const MSLane*> checked;
210 for (auto item : myOppositeLanes) {
211 if (item.first->getOpposite() != nullptr) {
212 if (item.first->getOpposite()->getOpposite() != item.first) {
213 if (checked.count(item.first->getOpposite()) == 0) {
214 WRITE_WARNINGF(TL("Asymmetrical neigh lane '%' for lane '%'"), item.second, item.first->getID());
215 item.first->getOpposite()->setOpposite(item.first);
216 } else {
217 throw ProcessError(TLF("Mutually inconsistent neigh lane definitions for lanes '%', '%' and '%'",
218 item.first->getID(), item.first->getOpposite()->getID(), Named::getIDSecure(item.first->getOpposite()->getOpposite())));
219 }
220 }
221 checked.insert(item.first);
222 checked.insert(item.first->getOpposite());
223 }
224 }
225 for (MSEdge* const edge : myEdges) {
226 edge->closeBuilding();
227 }
228 for (MSEdge* const edge : myEdges) {
229 edge->rebuildAllowedTargets(false);
230 }
231 // mark internal edges belonging to a roundabout (after all edges are build)
233 for (MSEdge* const edge : myEdges) {
234 if (edge->isInternal()) {
235 if (edge->getNumSuccessors() != 1 || edge->getNumPredecessors() != 1) {
236 throw ProcessError(TLF("Internal edge '%' is not properly connected (probably a manually modified net.xml).", edge->getID()));
237 }
238 if (edge->getSuccessors()[0]->isRoundabout() || edge->getPredecessors()[0]->isRoundabout()) {
239 edge->markAsRoundabout();
240 }
241 }
242 }
243 }
244 if (!deprecatedVehicleClassesSeen.empty()) {
245 WRITE_WARNINGF(TL("Deprecated vehicle classes '%' in input network."), toString(deprecatedVehicleClassesSeen));
247 }
248 // check for bi-directional edges (this are edges in opposing direction and superposable/congruent shapes)
249 if (myBidiEdges.size() > 0 || networkVersion > MMVersion(1, 0)) {
250 for (auto& item : myBidiEdges) {
251 item.first->checkAndRegisterBiDirEdge(item.second);
252 }
253 //WRITE_MESSAGEF(TL("Loaded % bidirectional edges"), toString(myBidiEdges.size()));
254 } else {
255 // legacy network
256 for (MSEdge* e : myEdges) {
257 e->checkAndRegisterBiDirEdge();
258 }
259 }
260 // take into account bidi lanes when deciding on whether an edge allows changing
261 for (MSEdge* const edge : myEdges) {
262 edge->buildLaneChanger();
263 }
264 return new MSEdgeControl(myEdges);
265}
266
267
268MSEdge*
269NLEdgeControlBuilder::buildEdge(const std::string& id, const SumoXMLEdgeFunc function,
270 const std::string& streetName, const std::string& edgeType, const std::string& routingType,
271 const int priority, const double distance) {
272 return new MSEdge(id, myCurrentNumericalEdgeID++, function, streetName, edgeType, routingType, priority, distance);
273}
274
275void NLEdgeControlBuilder::addCrossingEdges(const std::vector<std::string>& crossingEdges) {
276 myActiveEdge->setCrossingEdges(crossingEdges);
277}
278
279
282 if (myActiveEdge == nullptr) {
284 } else {
285 return myActiveEdge->getFunction();
286 }
287}
288
289/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:287
#define WRITE_ERRORF(...)
Definition MsgHandler.h:296
#define WRITE_WARNING(msg)
Definition MsgHandler.h:286
#define TL(string)
Definition MsgHandler.h:304
#define TLF(string,...)
Definition MsgHandler.h:306
std::set< std::string > deprecatedVehicleClassesSeen
SVCPermissions parseVehicleClasses(const std::string &allowedS)
Parses the given definition of allowed vehicle classes into the given containers Deprecated classes g...
long long int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
std::pair< int, double > MMVersion
(M)ajor/(M)inor version for written networks and default version for loading
Definition StdDefs.h:71
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:49
Stores edges and lanes, performs moving of vehicle.
A road/street connecting two junctions.
Definition MSEdge.h:77
void setCrossingEdges(const std::vector< std::string > &crossingEdges)
Sets the crossed edge ids for a crossing edge.
Definition MSEdge.h:360
static void clear()
Clears the dictionary.
Definition MSEdge.cpp:1120
static void setMesoIgnoredVClasses(SVCPermissions ignored)
Definition MSEdge.h:840
void initialize(const std::vector< MSLane * > *lanes)
Initialize the edge.
Definition MSEdge.cpp:105
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn't already in the dictionary....
Definition MSEdge.cpp:1075
SumoXMLEdgeFunc getFunction() const
Returns the edge type (SumoXMLEdgeFunc)
Definition MSEdge.h:259
static bool gUseMesoSim
Definition MSGlobals.h:106
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition MSGlobals.h:81
Representation of a lane in the micro simulation.
Definition MSLane.h:84
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition MSLane.cpp:2495
virtual MSEdge * closeEdge()
Closes the building of an edge; The edge is completely described by now and may not be opened again.
virtual void addNeigh(const std::string id)
Adds a neighbor to the current lane.
MSEdgeVector myEdges
Temporary, internal storage for built edges.
MSEdge * myActiveEdge
pointer to the currently chosen edge
void updateCurrentLaneStopOffset(const StopOffset &stopOffset)
set the stopOffset for the last added lane.
void closeLane()
Closes the building of a lane; The edge is completely described by now and may not be opened again.
int myCurrentLaneIndex
The index of the currently active lane (-1 if none is active)
std::vector< std::pair< MSLane *, std::string > > myOppositeLanes
std::string reportCurrentEdgeOrLane() const
Return info about currently processed edge or lane.
void beginEdgeParsing(const std::string &id, const SumoXMLEdgeFunc function, const std::string &streetName, const std::string &edgeType, const std::string &routingType, int priority, const std::string &bidi, double distance)
Begins building of an MSEdge.
int myCurrentNumericalLaneID
A running number for lane numbering.
virtual MSEdge * buildEdge(const std::string &id, const SumoXMLEdgeFunc function, const std::string &streetName, const std::string &edgeType, const std::string &routingType, const int priority, const double distance)
Builds an edge instance (MSEdge in this case)
SumoXMLEdgeFunc getCurrentEdgeFunction() const
void setDefaultStopOffset(const StopOffset &stopOffset)
set the stopOffset for the last added lane.
virtual void addCrossingEdges(const std::vector< std::string > &)
add the crossingEdges in a crossing edge if present
void addStopOffsets(const StopOffset &stopOffsets)
process a stopOffset element (originates either from the active edge or lane).
virtual MSLane * addLane(const std::string &id, double maxSpeed, double friction, double length, const PositionVector &shape, double width, SVCPermissions permissions, SVCPermissions changeLeft, SVCPermissions changeRight, int index, bool isRampAccel, const std::string &type, const PositionVector &outlineShape)
Adds a lane to the current edge.
std::vector< MSLane * > * myLaneStorage
pointer to a temporary lane storage
int myCurrentNumericalEdgeID
A running number for edge numbering.
StopOffset myCurrentDefaultStopOffset
The default stop offset for all lanes belonging to the active edge (this is set if the edge was given...
virtual ~NLEdgeControlBuilder()
Destructor.
std::map< MSEdge *, std::string, ComparatorNumericalIdLess > myBidiEdges
temporary storage for bidi attributes (to be resolved after loading all edges)
MSEdgeControl * build(const MMVersion &networkVersion)
builds the MSEdgeControl-class which holds all edges
static std::string getIDSecure(const T *obj, const std::string &fallBack="NULL")
get an identifier for Named-like object which may be Null
Definition Named.h:67
const std::string & getID() const
Returns the id.
Definition Named.h:74
static OptionsCont & getOptions()
Retrieves the options.
A list of positions.
stop offset
bool isDefined() const
check if stopOffset was defined
void reset()
reset stopOffset