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-2020 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>
36 #include "NLBuilder.h"
37 #include "NLEdgeControlBuilder.h"
39 
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
45  : myCurrentNumericalLaneID(0), myCurrentNumericalEdgeID(0), myEdges(0), myCurrentLaneIndex(-1) {
46  myActiveEdge = (MSEdge*) nullptr;
47  myLaneStorage = new std::vector<MSLane*>();
48 }
49 
50 
52  delete myLaneStorage;
53 }
54 
55 
56 void
58  const std::string& id, const SumoXMLEdgeFunc function,
59  const std::string& streetName,
60  const std::string& edgeType,
61  int priority,
62  const std::string& bidi,
63  double distance) {
64  // closeEdge might not have been called because the last edge had an error, so we clear the lane storage
65  myLaneStorage->clear();
66  myActiveEdge = buildEdge(id, function, streetName, edgeType, priority, distance);
67  if (MSEdge::dictionary(id) != nullptr) {
68  throw InvalidArgument("Another edge with the id '" + id + "' exists.");
69  }
70  myEdges.push_back(myActiveEdge);
71  if (bidi != "") {
72  myBidiEdges[myActiveEdge] = bidi;
73  }
74 }
75 
76 
77 MSLane*
78 NLEdgeControlBuilder::addLane(const std::string& id,
79  double maxSpeed, double length,
80  const PositionVector& shape, double width,
81  SVCPermissions permissions, int index, bool isRampAccel,
82  const std::string& type) {
83  MSLane* lane = new MSLane(id, maxSpeed, length, myActiveEdge, myCurrentNumericalLaneID++, shape, width, permissions, index, isRampAccel, type);
84  myLaneStorage->push_back(lane);
85  myCurrentLaneIndex = index;
86  return lane;
87 }
88 
89 
90 void
91 NLEdgeControlBuilder::addStopOffsets(const std::map<SVCPermissions, double>& stopOffsets) {
92 
93  if (myCurrentLaneIndex == -1) {
94  setDefaultStopOffsets(stopOffsets);
95  } else {
96  updateCurrentLaneStopOffsets(stopOffsets);
97  }
98 }
99 
100 
101 
102 std::string
104  std::stringstream ss;
105  if (myCurrentLaneIndex != -1) {
106  ss << "lane " << myCurrentLaneIndex << " of ";
107  }
108  ss << "edge '" << myActiveEdge->getID() << "'";
109  return ss.str();
110 }
111 
112 
113 void
114 NLEdgeControlBuilder::updateCurrentLaneStopOffsets(const std::map<SVCPermissions, double>& stopOffsets) {
115  assert(myLaneStorage->size() != 0);
116  if (stopOffsets.size() == 0) {
117  return;
118  }
119  if (myLaneStorage->back()->getStopOffsets().size() != 0) {
120  std::stringstream ss;
121  ss << "Duplicate stopOffset definition for lane " << myLaneStorage->back()->getIndex() << " on edge " << myActiveEdge->getID() << "!";
122  WRITE_WARNING(ss.str())
123  } else {
124  myLaneStorage->back()->setStopOffsets(stopOffsets);
125  }
126 }
127 
128 
129 void
130 NLEdgeControlBuilder::setDefaultStopOffsets(std::map<SVCPermissions, double> stopOffsets) {
131  if (myCurrentDefaultStopOffsets.size() != 0) {
132  std::stringstream ss;
133  ss << "Duplicate stopOffset definition for edge " << myActiveEdge->getID() << ". Ignoring duplicate specification.";
134  WRITE_WARNING(ss.str())
135  } else {
136  myCurrentDefaultStopOffsets = stopOffsets;
137  }
138 }
139 
140 
141 void
143  assert(myActiveEdge != 0);
144  if (myCurrentDefaultStopOffsets.size() == 0) {
145  return;
146  }
147  for (MSLane* l : *myLaneStorage) {
148  if (l->getStopOffsets().size() == 0) {
149  l->setStopOffsets(myCurrentDefaultStopOffsets);
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  for (MSEdgeVector::iterator i1 = myEdges.begin(); i1 != myEdges.end(); i1++) {
183  (*i1)->closeBuilding();
184  }
185  for (MSEdgeVector::iterator i1 = myEdges.begin(); i1 != myEdges.end(); i1++) {
186  (*i1)->buildLaneChanger();
187  }
188  // mark internal edges belonging to a roundabout (after all edges are build)
190  for (MSEdgeVector::iterator i1 = myEdges.begin(); i1 != myEdges.end(); i1++) {
191  MSEdge* edge = *i1;
192  if (edge->isInternal()) {
193  if (edge->getNumSuccessors() != 1 || edge->getNumPredecessors() != 1) {
194  throw ProcessError("Internal edge '" + edge->getID() + "' is not properly connected (probably a manually modified net.xml).");
195  }
196  if (edge->getSuccessors()[0]->isRoundabout() || edge->getPredecessors()[0]->isRoundabout()) {
197  edge->markAsRoundabout();
198  }
199  }
200  }
201  }
202  if (!deprecatedVehicleClassesSeen.empty()) {
203  WRITE_WARNING("Deprecated vehicle classes '" + toString(deprecatedVehicleClassesSeen) + "' in input network.");
205  }
206  // check for bi-directional edges (this are edges in opposing direction and superposable/congruent shapes)
207  if (myBidiEdges.size() > 0 || networkVersion > 1.0) {
208  for (auto& item : myBidiEdges) {
209  item.first->checkAndRegisterBiDirEdge(item.second);
210  }
211  //WRITE_MESSAGE("Loaded " + toString(myBidiEdges.size()) + " bidirectional edges");
212  } else {
213  // legacy network
214  for (MSEdge* e : myEdges) {
215  e->checkAndRegisterBiDirEdge();
216  }
217  }
218  return new MSEdgeControl(myEdges);
219 }
220 
221 
222 MSEdge*
223 NLEdgeControlBuilder::buildEdge(const std::string& id, const SumoXMLEdgeFunc function,
224  const std::string& streetName, const std::string& edgeType, const int priority, const double distance) {
225  return new MSEdge(id, myCurrentNumericalEdgeID++, function, streetName, edgeType, priority, distance);
226 }
227 
228 void NLEdgeControlBuilder::addCrossingEdges(const std::vector<std::string>& crossingEdges) {
229  myActiveEdge->setCrossingEdges(crossingEdges);
230 }
231 
232 
233 /****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:276
std::set< std::string > deprecatedVehicleClassesSeen
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:44
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:327
int getNumSuccessors() const
Returns the number of edges that may be reached from this edge.
Definition: MSEdge.h:353
const MSEdgeVector & getPredecessors() const
Definition: MSEdge.h:383
void initialize(const std::vector< MSLane * > *lanes)
Initialize the edge.
Definition: MSEdge.cpp:94
bool isInternal() const
return whether this edge is an internal edge
Definition: MSEdge.h:256
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:814
const MSEdgeVector & getSuccessors(SUMOVehicleClass vClass=SVC_IGNORING) const
Returns the following edges, restricted by vClass.
Definition: MSEdge.cpp:1013
int getNumPredecessors() const
Returns the number of edges this edge is connected to.
Definition: MSEdge.h:375
void markAsRoundabout()
Definition: MSEdge.h:678
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:66
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 closeLane()
Closes the building of a lane; The edge is completely described by now and may not be opened again.
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.
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...
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 setDefaultStopOffsets(std::map< SVCPermissions, double > stopOffsets)
set the stopOffset for the last added lane.
void addStopOffsets(const std::map< SVCPermissions, double > &stopOffsets)
process a stopOffset element (originates either from the active edge or lane).
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 void addCrossingEdges(const std::vector< std::string > &)
add the crossingEdges in a crossing edge if present
void updateCurrentLaneStopOffsets(const std::map< SVCPermissions, double > &stopOffsets)
set the stopOffset for the last added 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.
virtual ~NLEdgeControlBuilder()
Destructor.
const std::string & getID() const
Returns the id.
Definition: Named.h:73
A list of positions.