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 /****************************************************************************/
18 // Interface for building edges
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <vector>
28 #include <string>
29 #include <map>
30 #include <algorithm>
31 #include <iterator>
32 #include <microsim/MSGlobals.h>
33 #include <microsim/MSLane.h>
34 #include <microsim/MSEdge.h>
35 #include <microsim/MSEdgeControl.h>
38 #include "NLBuilder.h"
39 #include "NLEdgeControlBuilder.h"
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
48  : myCurrentNumericalLaneID(0), myCurrentNumericalEdgeID(0), myEdges(0), myCurrentLaneIndex(-1) {
49  myActiveEdge = (MSEdge*) nullptr;
50  myLaneStorage = new std::vector<MSLane*>();
51 }
52 
53 
55  delete myLaneStorage;
56 }
57 
58 
59 void
61  const std::string& id, const SumoXMLEdgeFunc function,
62  const std::string& streetName,
63  const std::string& edgeType,
64  int priority,
65  const std::string& bidi,
66  double distance) {
67  // closeEdge might not have been called because the last edge had an error, so we clear the lane storage
68  myLaneStorage->clear();
69  myActiveEdge = buildEdge(id, function, streetName, edgeType, priority, distance);
70  if (MSEdge::dictionary(id) != nullptr) {
71  throw InvalidArgument("Another edge with the id '" + id + "' exists.");
72  }
73  myEdges.push_back(myActiveEdge);
74  if (bidi != "") {
75  myBidiEdges[myActiveEdge] = bidi;
76  }
77 }
78 
79 
80 MSLane*
81 NLEdgeControlBuilder::addLane(const std::string& id,
82  double maxSpeed, double length,
83  const PositionVector& shape, double width,
84  SVCPermissions permissions, int index, bool isRampAccel,
85  const std::string& type) {
86  MSLane* lane = new MSLane(id, maxSpeed, length, myActiveEdge, myCurrentNumericalLaneID++, shape, width, permissions, index, isRampAccel, type);
87  myLaneStorage->push_back(lane);
88  myCurrentLaneIndex = index;
89  return lane;
90 }
91 
92 
93 void
94 NLEdgeControlBuilder::addStopOffsets(const std::map<SVCPermissions, double>& stopOffsets) {
95 
96  if (myCurrentLaneIndex == -1) {
97  setDefaultStopOffsets(stopOffsets);
98  } else {
99  updateCurrentLaneStopOffsets(stopOffsets);
100  }
101 }
102 
103 
104 
105 std::string
107  std::stringstream ss;
108  if (myCurrentLaneIndex != -1) {
109  ss << "lane " << myCurrentLaneIndex << " of ";
110  }
111  ss << "edge '" << myActiveEdge->getID() << "'";
112  return ss.str();
113 }
114 
115 
116 void
117 NLEdgeControlBuilder::updateCurrentLaneStopOffsets(const std::map<SVCPermissions, double>& stopOffsets) {
118  assert(myLaneStorage->size() != 0);
119  if (stopOffsets.size() == 0) {
120  return;
121  }
122  if (myLaneStorage->back()->getStopOffsets().size() != 0) {
123  std::stringstream ss;
124  ss << "Duplicate stopOffset definition for lane " << myLaneStorage->back()->getIndex() << " on edge " << myActiveEdge->getID() << "!";
125  WRITE_WARNING(ss.str())
126  } else {
127  myLaneStorage->back()->setStopOffsets(stopOffsets);
128  }
129 }
130 
131 
132 void
133 NLEdgeControlBuilder::setDefaultStopOffsets(std::map<SVCPermissions, double> stopOffsets) {
134  if (myCurrentDefaultStopOffsets.size() != 0) {
135  std::stringstream ss;
136  ss << "Duplicate stopOffset definition for edge " << myActiveEdge->getID() << ". Ignoring duplicate specification.";
137  WRITE_WARNING(ss.str())
138  } else {
139  myCurrentDefaultStopOffsets = stopOffsets;
140  }
141 }
142 
143 
144 void
146  assert(myActiveEdge != 0);
147  if (myCurrentDefaultStopOffsets.size() == 0) {
148  return;
149  }
150  for (MSLane* l : *myLaneStorage) {
151  if (l->getStopOffsets().size() == 0) {
152  l->setStopOffsets(myCurrentDefaultStopOffsets);
153  }
154  }
155 }
156 
157 
158 void
159 NLEdgeControlBuilder::addNeigh(const std::string id) {
160  myLaneStorage->back()->addNeigh(id);
161 }
162 
163 
164 MSEdge*
167  std::vector<MSLane*>* lanes = new std::vector<MSLane*>();
168  lanes->reserve(myLaneStorage->size());
169  copy(myLaneStorage->begin(), myLaneStorage->end(), back_inserter(*lanes));
170  myLaneStorage->clear();
171  myActiveEdge->initialize(lanes);
173  return myActiveEdge;
174 }
175 
176 
177 void
179  myCurrentLaneIndex = -1;
180 }
181 
182 
184 NLEdgeControlBuilder::build(double networkVersion) {
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 
int getNumPredecessors() const
Returns the number of edges this edge is connected to.
Definition: MSEdge.h:344
std::set< std::string > deprecatedVehicleClassesSeen
virtual MSEdge * closeEdge()
Closes the building of an edge; The edge is completely described by now and may not be opened again...
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.
const MSEdgeVector & getPredecessors() const
Definition: MSEdge.h:352
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
void setCrossingEdges(const std::vector< std::string > &crossingEdges)
Sets the crossed edge ids for a crossing edge.
Definition: MSEdge.h:296
std::map< MSEdge *, std::string > myBidiEdges
temporary storage for bidi attributes (to be resolved after loading all edges)
void addStopOffsets(const std::map< SVCPermissions, double > &stopOffsets)
process a stopOffset element (originates either from the active edge or lane).
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn&#39;t already in the dictionary...
Definition: MSEdge.cpp:804
MSEdgeVector myEdges
Temporary, internal storage for built edges.
void initialize(const std::vector< MSLane *> *lanes)
Initialize the edge.
Definition: MSEdge.cpp:108
const std::string & getID() const
Returns the id.
Definition: Named.h:77
MSEdge * myActiveEdge
pointer to the currently chosen edge
void updateCurrentLaneStopOffsets(const std::map< SVCPermissions, double > &stopOffsets)
set the stopOffset for the last added lane.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
int getNumSuccessors() const
Returns the number of edges that may be reached from this edge.
Definition: MSEdge.h:322
void setDefaultStopOffsets(std::map< SVCPermissions, double > stopOffsets)
set the stopOffset for the last added lane.
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.
A road/street connecting two junctions.
Definition: MSEdge.h:76
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
A list of positions.
std::vector< MSLane * > * myLaneStorage
pointer to a temporary lane storage
NLEdgeControlBuilder()
Constructor.
Stores edges and lanes, performs moving of vehicle.
Definition: MSEdgeControl.h:73
virtual ~NLEdgeControlBuilder()
Destructor.
std::string reportCurrentEdgeOrLane() const
Return info about currently processed edge or lane.
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:69
int myCurrentNumericalEdgeID
A running number for edge numbering.
bool isInternal() const
return whether this edge is an internal edge
Definition: MSEdge.h:233
virtual void addNeigh(const std::string id)
Adds a neighbor to the current lane.
int myCurrentLaneIndex
The index of the currently active lane (-1 if none is active)
const MSEdgeVector & getSuccessors(SUMOVehicleClass vClass=SVC_IGNORING) const
Returns the following edges, restricted by vClass.
Definition: MSEdge.cpp:998
void closeLane()
Closes the building of a lane; The edge is completely described by now and may not be opened again...
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)
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
int myCurrentNumericalLaneID
A running number for lane numbering.
virtual void addCrossingEdges(const std::vector< std::string > &)
add the crossingEdges in a crossing edge if present
void markAsRoundabout()
Definition: MSEdge.h:637
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
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...
MSEdgeControl * build(double networkVersion)
builds the MSEdgeControl-class which holds all edges