Eclipse SUMO - Simulation of Urban MObility
NLEdgeControlBuilder.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2022 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 <microsim/MSGlobals.h>
31 #include <microsim/MSLane.h>
32 #include <microsim/MSEdge.h>
33 #include <microsim/MSEdgeControl.h>
37 #include "NLBuilder.h"
38 #include "NLEdgeControlBuilder.h"
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
46  : myCurrentNumericalLaneID(0), myCurrentNumericalEdgeID(0), myEdges(0), myCurrentLaneIndex(-1) {
47  myActiveEdge = (MSEdge*) nullptr;
48  myLaneStorage = new std::vector<MSLane*>();
49 }
50 
51 
53  delete myLaneStorage;
54 }
55 
56 
57 void
59  const std::string& id, const SumoXMLEdgeFunc function,
60  const std::string& streetName,
61  const std::string& edgeType,
62  int priority,
63  const std::string& bidi,
64  double distance) {
65  // closeEdge might not have been called because the last edge had an error, so we clear the lane storage
66  myLaneStorage->clear();
67  myActiveEdge = buildEdge(id, function, streetName, edgeType, priority, distance);
68  if (MSEdge::dictionary(id) != nullptr) {
69  throw InvalidArgument("Another edge with the id '" + id + "' exists.");
70  }
71  myEdges.push_back(myActiveEdge);
72  if (bidi != "") {
73  myBidiEdges[myActiveEdge] = bidi;
74  }
75 }
76 
77 
78 MSLane*
79 NLEdgeControlBuilder::addLane(const std::string& id,
80  double maxSpeed, double length,
81  const PositionVector& shape, double width,
82  SVCPermissions permissions,
83  SVCPermissions changeLeft, SVCPermissions changeRight,
84  int index, bool isRampAccel,
85  const std::string& type) {
86  MSLane* lane = new MSLane(id, maxSpeed, length, myActiveEdge, myCurrentNumericalLaneID++, shape, width, permissions, changeLeft, changeRight, index, isRampAccel, type);
87  myLaneStorage->push_back(lane);
88  myCurrentLaneIndex = index;
89  return lane;
90 }
91 
92 
93 void
95  if (myCurrentLaneIndex == -1) {
96  setDefaultStopOffset(stopOffset);
97  } else {
98  updateCurrentLaneStopOffset(stopOffset);
99  }
100 }
101 
102 
103 std::string
105  std::stringstream ss;
106  if (myCurrentLaneIndex != -1) {
107  ss << "lane " << myCurrentLaneIndex << " of ";
108  }
109  ss << "edge '" << myActiveEdge->getID() << "'";
110  return ss.str();
111 }
112 
113 
114 void
116  if (myLaneStorage->size() == 0) {
117  throw ProcessError("myLaneStorage cannot be empty");
118  }
119  if (stopOffset.isDefined()) {
120  if (myLaneStorage->back()->getLaneStopOffsets().isDefined()) {
121  WRITE_WARNING("Duplicate stopOffset definition for lane " + toString(myLaneStorage->back()->getIndex()) +
122  " on edge " + myActiveEdge->getID() + "!")
123  } else {
124  myLaneStorage->back()->setLaneStopOffset(stopOffset);
125  }
126  }
127 }
128 
129 
130 void
133  WRITE_WARNING("Duplicate stopOffset definition for edge " + myActiveEdge->getID() + ". Ignoring duplicate specification.")
134  } else {
135  myCurrentDefaultStopOffset = stopOffsets;
136  }
137 }
138 
139 
140 void
142  if (myActiveEdge == nullptr) {
143  throw ProcessError("myActiveEdge cannot be nullptr");
144  }
146  for (const auto& l : *myLaneStorage) {
147  if (!l->getLaneStopOffsets().isDefined()) {
148  l->setLaneStopOffset(myCurrentDefaultStopOffset);
149  }
150  }
151  }
152 }
153 
154 
155 void
156 NLEdgeControlBuilder::addNeigh(const std::string id) {
157  myLaneStorage->back()->addNeigh(id);
158 }
159 
160 
161 MSEdge*
164  std::vector<MSLane*>* lanes = new std::vector<MSLane*>();
165  lanes->reserve(myLaneStorage->size());
166  copy(myLaneStorage->begin(), myLaneStorage->end(), back_inserter(*lanes));
167  myLaneStorage->clear();
168  myActiveEdge->initialize(lanes);
170  return myActiveEdge;
171 }
172 
173 
174 void
176  myCurrentLaneIndex = -1;
177 }
178 
179 
181 NLEdgeControlBuilder::build(double networkVersion) {
182  if (MSGlobals::gUseMesoSim && !OptionsCont::getOptions().getBool("meso-lane-queue")) {
183  MSEdge::setMesoIgnoredVClasses(parseVehicleClasses(OptionsCont::getOptions().getStringVector("meso-ignore-lanes-by-vclass")));
184  }
185  for (MSEdgeVector::iterator i1 = myEdges.begin(); i1 != myEdges.end(); i1++) {
186  (*i1)->closeBuilding();
187  }
188  for (MSEdgeVector::iterator i1 = myEdges.begin(); i1 != myEdges.end(); i1++) {
189  (*i1)->buildLaneChanger();
190  }
191  // mark internal edges belonging to a roundabout (after all edges are build)
193  for (MSEdgeVector::iterator i1 = myEdges.begin(); i1 != myEdges.end(); i1++) {
194  MSEdge* edge = *i1;
195  if (edge->isInternal()) {
196  if (edge->getNumSuccessors() != 1 || edge->getNumPredecessors() != 1) {
197  throw ProcessError("Internal edge '" + edge->getID() + "' is not properly connected (probably a manually modified net.xml).");
198  }
199  if (edge->getSuccessors()[0]->isRoundabout() || edge->getPredecessors()[0]->isRoundabout()) {
200  edge->markAsRoundabout();
201  }
202  }
203  }
204  }
205  if (!deprecatedVehicleClassesSeen.empty()) {
206  WRITE_WARNING("Deprecated vehicle classes '" + toString(deprecatedVehicleClassesSeen) + "' in input network.");
208  }
209  // check for bi-directional edges (this are edges in opposing direction and superposable/congruent shapes)
210  if (myBidiEdges.size() > 0 || networkVersion > 1.0) {
211  for (auto& item : myBidiEdges) {
212  item.first->checkAndRegisterBiDirEdge(item.second);
213  }
214  //WRITE_MESSAGE("Loaded " + toString(myBidiEdges.size()) + " bidirectional edges");
215  } else {
216  // legacy network
217  for (MSEdge* e : myEdges) {
218  e->checkAndRegisterBiDirEdge();
219  }
220  }
221  return new MSEdgeControl(myEdges);
222 }
223 
224 
225 MSEdge*
226 NLEdgeControlBuilder::buildEdge(const std::string& id, const SumoXMLEdgeFunc function,
227  const std::string& streetName, const std::string& edgeType, const int priority, const double distance) {
228  return new MSEdge(id, myCurrentNumericalEdgeID++, function, streetName, edgeType, priority, distance);
229 }
230 
231 void NLEdgeControlBuilder::addCrossingEdges(const std::vector<std::string>& crossingEdges) {
232  myActiveEdge->setCrossingEdges(crossingEdges);
233 }
234 
235 
236 /****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:280
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...
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::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
Stores edges and lanes, performs moving of vehicle.
Definition: MSEdgeControl.h:81
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:336
int getNumSuccessors() const
Returns the number of edges that may be reached from this edge.
Definition: MSEdge.h:362
static void setMesoIgnoredVClasses(SVCPermissions ignored)
Definition: MSEdge.h:774
const MSEdgeVector & getPredecessors() const
Definition: MSEdge.h:392
void initialize(const std::vector< MSLane * > *lanes)
Initialize the edge.
Definition: MSEdge.cpp:99
bool isInternal() const
return whether this edge is an internal edge
Definition: MSEdge.h:262
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:885
const MSEdgeVector & getSuccessors(SUMOVehicleClass vClass=SVC_IGNORING) const
Returns the following edges, restricted by vClass.
Definition: MSEdge.cpp:1084
int getNumPredecessors() const
Returns the number of edges this edge is connected to.
Definition: MSEdge.h:384
void markAsRoundabout()
Definition: MSEdge.h:689
static bool gUseMesoSim
Definition: MSGlobals.h:94
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:72
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
std::map< MSEdge *, std::string > myBidiEdges
temporary storage for bidi attributes (to be resolved after loading all edges)
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
MSEdgeControl * build(double networkVersion)
builds the MSEdgeControl-class which holds all edges
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::string reportCurrentEdgeOrLane() const
Return info about currently processed edge or lane.
int myCurrentNumericalLaneID
A running number for lane numbering.
void beginEdgeParsing(const std::string &id, const SumoXMLEdgeFunc function, const std::string &streetName, const std::string &edgeType, int priority, const std::string &bidi, double distance)
Begins building of an MSEdge.
virtual MSLane * addLane(const std::string &id, double maxSpeed, double length, const PositionVector &shape, double width, SVCPermissions permissions, SVCPermissions changeLeft, SVCPermissions changeRight, int index, bool isRampAccel, const std::string &type)
Adds a lane to the current edge.
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).
NLEdgeControlBuilder()
Constructor.
virtual MSEdge * buildEdge(const std::string &id, const SumoXMLEdgeFunc function, const std::string &streetName, const std::string &edgeType, const int priority, const double distance)
Builds an edge instance (MSEdge in this case)
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.
const std::string & getID() const
Returns the id.
Definition: Named.h:74
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
A list of positions.
stop offset
bool isDefined() const
check if stopOffset was defined
void reset()
reset stopOffset