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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Interface for building edges
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <vector>
27 #include <string>
28 #include <map>
29 #include <algorithm>
30 #include <iterator>
31 #include <microsim/MSGlobals.h>
32 #include <microsim/MSLane.h>
33 #include <microsim/MSEdge.h>
34 #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, int index, bool isRampAccel,
83  const std::string& type) {
84  MSLane* lane = new MSLane(id, maxSpeed, length, myActiveEdge, myCurrentNumericalLaneID++, shape, width, permissions, index, isRampAccel, type);
85  myLaneStorage->push_back(lane);
86  myCurrentLaneIndex = index;
87  return lane;
88 }
89 
90 
91 void
92 NLEdgeControlBuilder::addStopOffsets(const std::map<SVCPermissions, double>& stopOffsets) {
93 
94  if (myCurrentLaneIndex == -1) {
95  setDefaultStopOffsets(stopOffsets);
96  } else {
97  updateCurrentLaneStopOffsets(stopOffsets);
98  }
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
115 NLEdgeControlBuilder::updateCurrentLaneStopOffsets(const std::map<SVCPermissions, double>& stopOffsets) {
116  assert(myLaneStorage->size() != 0);
117  if (stopOffsets.size() == 0) {
118  return;
119  }
120  if (myLaneStorage->back()->getStopOffsets().size() != 0) {
121  std::stringstream ss;
122  ss << "Duplicate stopOffset definition for lane " << myLaneStorage->back()->getIndex() << " on edge " << myActiveEdge->getID() << "!";
123  WRITE_WARNING(ss.str())
124  } else {
125  myLaneStorage->back()->setStopOffsets(stopOffsets);
126  }
127 }
128 
129 
130 void
131 NLEdgeControlBuilder::setDefaultStopOffsets(std::map<SVCPermissions, double> stopOffsets) {
132  if (myCurrentDefaultStopOffsets.size() != 0) {
133  std::stringstream ss;
134  ss << "Duplicate stopOffset definition for edge " << myActiveEdge->getID() << ". Ignoring duplicate specification.";
135  WRITE_WARNING(ss.str())
136  } else {
137  myCurrentDefaultStopOffsets = stopOffsets;
138  }
139 }
140 
141 
142 void
144  assert(myActiveEdge != 0);
145  if (myCurrentDefaultStopOffsets.size() == 0) {
146  return;
147  }
148  for (MSLane* l : *myLaneStorage) {
149  if (l->getStopOffsets().size() == 0) {
150  l->setStopOffsets(myCurrentDefaultStopOffsets);
151  }
152  }
153 }
154 
155 
156 void
157 NLEdgeControlBuilder::addNeigh(const std::string id) {
158  myLaneStorage->back()->addNeigh(id);
159 }
160 
161 
162 MSEdge*
165  std::vector<MSLane*>* lanes = new std::vector<MSLane*>();
166  lanes->reserve(myLaneStorage->size());
167  copy(myLaneStorage->begin(), myLaneStorage->end(), back_inserter(*lanes));
168  myLaneStorage->clear();
169  myActiveEdge->initialize(lanes);
171  return myActiveEdge;
172 }
173 
174 
175 void
177  myCurrentLaneIndex = -1;
178 }
179 
180 
182 NLEdgeControlBuilder::build(double networkVersion) {
183  for (MSEdgeVector::iterator i1 = myEdges.begin(); i1 != myEdges.end(); i1++) {
184  (*i1)->closeBuilding();
185  }
186  for (MSEdgeVector::iterator i1 = myEdges.begin(); i1 != myEdges.end(); i1++) {
187  (*i1)->buildLaneChanger();
188  }
189  // mark internal edges belonging to a roundabout (after all edges are build)
191  for (MSEdgeVector::iterator i1 = myEdges.begin(); i1 != myEdges.end(); i1++) {
192  MSEdge* edge = *i1;
193  if (edge->isInternal()) {
194  if (edge->getNumSuccessors() != 1 || edge->getNumPredecessors() != 1) {
195  throw ProcessError("Internal edge '" + edge->getID() + "' is not properly connected (probably a manually modified net.xml).");
196  }
197  if (edge->getSuccessors()[0]->isRoundabout() || edge->getPredecessors()[0]->isRoundabout()) {
198  edge->markAsRoundabout();
199  }
200  }
201  }
202  }
203  if (!deprecatedVehicleClassesSeen.empty()) {
204  WRITE_WARNING("Deprecated vehicle classes '" + toString(deprecatedVehicleClassesSeen) + "' in input network.");
206  }
207  // check for bi-directional edges (this are edges in opposing direction and superposable/congruent shapes)
208  if (myBidiEdges.size() > 0 || networkVersion > 1.0) {
209  for (auto& item : myBidiEdges) {
210  item.first->checkAndRegisterBiDirEdge(item.second);
211  }
212  //WRITE_MESSAGE("Loaded " + toString(myBidiEdges.size()) + " bidirectional edges");
213  } else {
214  // legacy network
215  for (MSEdge* e : myEdges) {
216  e->checkAndRegisterBiDirEdge();
217  }
218  }
219  return new MSEdgeControl(myEdges);
220 }
221 
222 
223 MSEdge*
224 NLEdgeControlBuilder::buildEdge(const std::string& id, const SumoXMLEdgeFunc function,
225  const std::string& streetName, const std::string& edgeType, const int priority, const double distance) {
226  return new MSEdge(id, myCurrentNumericalEdgeID++, function, streetName, edgeType, priority, distance);
227 }
228 
229 void NLEdgeControlBuilder::addCrossingEdges(const std::vector<std::string>& crossingEdges) {
230  myActiveEdge->setCrossingEdges(crossingEdges);
231 }
232 
233 /****************************************************************************/
234 
MSEdge::getNumPredecessors
int getNumPredecessors() const
Returns the number of edges this edge is connected to.
Definition: MSEdge.h:346
MSEdge::getSuccessors
const MSEdgeVector & getSuccessors(SUMOVehicleClass vClass=SVC_IGNORING) const
Returns the following edges, restricted by vClass.
Definition: MSEdge.cpp:959
NLEdgeControlBuilder::myCurrentNumericalEdgeID
int myCurrentNumericalEdgeID
A running number for edge numbering.
Definition: NLEdgeControlBuilder.h:159
NLEdgeControlBuilder::addLane
virtual MSLane * addLane(const std::string &id, double maxSpeed, double length, const PositionVector &shape, double width, SVCPermissions permissions, int index, bool isRampAccel, const std::string &type)
Adds a lane to the current edge.
Definition: NLEdgeControlBuilder.cpp:79
NLEdgeControlBuilder::~NLEdgeControlBuilder
virtual ~NLEdgeControlBuilder()
Destructor.
Definition: NLEdgeControlBuilder.cpp:52
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
NLEdgeControlBuilder::myActiveEdge
MSEdge * myActiveEdge
pointer to the currently chosen edge
Definition: NLEdgeControlBuilder.h:165
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
NLEdgeControlBuilder::addNeigh
virtual void addNeigh(const std::string id)
Adds a neighbor to the current lane.
Definition: NLEdgeControlBuilder.cpp:157
NLEdgeControlBuilder::myCurrentLaneIndex
int myCurrentLaneIndex
The index of the currently active lane (-1 if none is active)
Definition: NLEdgeControlBuilder.h:171
NLBuilder.h
NLEdgeControlBuilder::applyDefaultStopOffsetsToLanes
void applyDefaultStopOffsetsToLanes()
Definition: NLEdgeControlBuilder.cpp:143
NLEdgeControlBuilder::myCurrentNumericalLaneID
int myCurrentNumericalLaneID
A running number for lane numbering.
Definition: NLEdgeControlBuilder.h:156
NLEdgeControlBuilder::addCrossingEdges
virtual void addCrossingEdges(const std::vector< std::string > &)
add the crossingEdges in a crossing edge if present
Definition: NLEdgeControlBuilder.cpp:229
SumoXMLEdgeFunc
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
Definition: SUMOXMLDefinitions.h:1079
NLEdgeControlBuilder::build
MSEdgeControl * build(double networkVersion)
builds the MSEdgeControl-class which holds all edges
Definition: NLEdgeControlBuilder.cpp:182
NLEdgeControlBuilder::buildEdge
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)
Definition: NLEdgeControlBuilder.cpp:224
MSEdge.h
PositionVector
A list of positions.
Definition: PositionVector.h:45
NLEdgeControlBuilder::addStopOffsets
void addStopOffsets(const std::map< SVCPermissions, double > &stopOffsets)
process a stopOffset element (originates either from the active edge or lane).
Definition: NLEdgeControlBuilder.cpp:92
MSEdge::isInternal
bool isInternal() const
return whether this edge is an internal edge
Definition: MSEdge.h:235
SVCPermissions
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
Definition: SUMOVehicleClass.h:218
NLEdgeControlBuilder::closeLane
void closeLane()
Closes the building of a lane; The edge is completely described by now and may not be opened again.
Definition: NLEdgeControlBuilder.cpp:176
NLEdgeControlBuilder::beginEdgeParsing
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.
Definition: NLEdgeControlBuilder.cpp:58
NLEdgeControlBuilder::NLEdgeControlBuilder
NLEdgeControlBuilder()
Constructor.
Definition: NLEdgeControlBuilder.cpp:45
MSEdge::dictionary
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:765
MSEdge::initialize
void initialize(const std::vector< MSLane * > *lanes)
Initialize the edge.
Definition: MSEdge.cpp:94
OutputDevice.h
ProcessError
Definition: UtilExceptions.h:39
MSGlobals.h
UtilExceptions.h
MSEdge
A road/street connecting two junctions.
Definition: MSEdge.h:78
NLEdgeControlBuilder.h
MSGlobals::gUsingInternalLanes
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:68
MSEdgeControl.h
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
deprecatedVehicleClassesSeen
std::set< std::string > deprecatedVehicleClassesSeen
Definition: SUMOVehicleClass.cpp:84
InvalidArgument
Definition: UtilExceptions.h:56
NLEdgeControlBuilder::myCurrentDefaultStopOffsets
std::map< SVCPermissions, double > myCurrentDefaultStopOffsets
The default stop offset for all lanes belonging to the active edge (this is set if the edge was given...
Definition: NLEdgeControlBuilder.h:168
NLEdgeControlBuilder::myLaneStorage
std::vector< MSLane * > * myLaneStorage
pointer to a temporary lane storage
Definition: NLEdgeControlBuilder.h:174
NLEdgeControlBuilder::myEdges
MSEdgeVector myEdges
Temporary, internal storage for built edges.
Definition: NLEdgeControlBuilder.h:162
MSEdge::getNumSuccessors
int getNumSuccessors() const
Returns the number of edges that may be reached from this edge.
Definition: MSEdge.h:324
config.h
NLEdgeControlBuilder::reportCurrentEdgeOrLane
std::string reportCurrentEdgeOrLane() const
Return info about currently processed edge or lane.
Definition: NLEdgeControlBuilder.cpp:104
MSEdge::setCrossingEdges
void setCrossingEdges(const std::vector< std::string > &crossingEdges)
Sets the crossed edge ids for a crossing edge.
Definition: MSEdge.h:298
MSEdgeControl
Stores edges and lanes, performs moving of vehicle.
Definition: MSEdgeControl.h:74
NLEdgeControlBuilder::setDefaultStopOffsets
void setDefaultStopOffsets(std::map< SVCPermissions, double > stopOffsets)
set the stopOffset for the last added lane.
Definition: NLEdgeControlBuilder.cpp:131
NLEdgeControlBuilder::closeEdge
virtual MSEdge * closeEdge()
Closes the building of an edge; The edge is completely described by now and may not be opened again.
Definition: NLEdgeControlBuilder.cpp:163
StringTokenizer.h
MSLane.h
NLEdgeControlBuilder::myBidiEdges
std::map< MSEdge *, std::string > myBidiEdges
temporary storage for bidi attributes (to be resolved after loading all edges)
Definition: NLEdgeControlBuilder.h:177
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
MSEdge::markAsRoundabout
void markAsRoundabout()
Definition: MSEdge.h:644
NLEdgeControlBuilder::updateCurrentLaneStopOffsets
void updateCurrentLaneStopOffsets(const std::map< SVCPermissions, double > &stopOffsets)
set the stopOffset for the last added lane.
Definition: NLEdgeControlBuilder.cpp:115
MSEdge::getPredecessors
const MSEdgeVector & getPredecessors() const
Definition: MSEdge.h:354