SUMO - Simulation of Urban MObility
NBTypeCont.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-2018 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 // A storage for the available types of an edge
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <string>
28 #include <map>
29 #include <iostream>
31 #include <utils/common/ToString.h>
33 #include "NBTypeCont.h"
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
39 void
40 NBTypeCont::setDefaults(int defaultNumLanes,
41  double defaultLaneWidth,
42  double defaultSpeed,
43  int defaultPriority,
44  SVCPermissions defaultPermissions) {
45  myDefaultType.numLanes = defaultNumLanes;
46  myDefaultType.width = defaultLaneWidth;
47  myDefaultType.speed = defaultSpeed;
48  myDefaultType.priority = defaultPriority;
49  myDefaultType.permissions = defaultPermissions;
50 }
51 
52 
53 void
54 NBTypeCont::insert(const std::string& id, int numLanes, double maxSpeed, int prio,
55  SVCPermissions permissions, double width, bool oneWayIsDefault, double sidewalkWidth, double bikeLaneWidth) {
56 
57  TypeDefinition newType(numLanes, maxSpeed, prio, width, permissions, oneWayIsDefault, sidewalkWidth, bikeLaneWidth);
58  TypesCont::iterator old = myTypes.find(id);
59  if (old != myTypes.end()) {
60  newType.restrictions.insert(old->second.restrictions.begin(), old->second.restrictions.end());
61  newType.attrs.insert(old->second.attrs.begin(), old->second.attrs.end());
62  }
63  myTypes[id] = newType;
64 }
65 
66 
67 bool
68 NBTypeCont::knows(const std::string& type) const {
69  return myTypes.find(type) != myTypes.end();
70 }
71 
72 
73 bool
74 NBTypeCont::markAsToDiscard(const std::string& id) {
75  TypesCont::iterator i = myTypes.find(id);
76  if (i == myTypes.end()) {
77  return false;
78  }
79  (*i).second.discard = true;
80  return true;
81 }
82 
83 
84 bool
85 NBTypeCont::markAsSet(const std::string& id, const SumoXMLAttr attr) {
86  TypesCont::iterator i = myTypes.find(id);
87  if (i == myTypes.end()) {
88  return false;
89  }
90  (*i).second.attrs.insert(attr);
91  return true;
92 }
93 
94 
95 bool
96 NBTypeCont::addRestriction(const std::string& id, const SUMOVehicleClass svc, const double speed) {
97  TypesCont::iterator i = myTypes.find(id);
98  if (i == myTypes.end()) {
99  return false;
100  }
101  (*i).second.restrictions[svc] = speed;
102  return true;
103 }
104 
105 
106 bool
107 NBTypeCont::copyRestrictionsAndAttrs(const std::string& fromId, const std::string& toId) {
108  TypesCont::iterator from = myTypes.find(fromId);
109  TypesCont::iterator to = myTypes.find(toId);
110  if (from == myTypes.end() || to == myTypes.end()) {
111  return false;
112  }
113  to->second.restrictions.insert(from->second.restrictions.begin(), from->second.restrictions.end());
114  to->second.attrs.insert(from->second.attrs.begin(), from->second.attrs.end());
115  return true;
116 }
117 
118 
119 void
121  for (TypesCont::const_iterator i = myTypes.begin(); i != myTypes.end(); ++i) {
122  into.openTag(SUMO_TAG_TYPE);
123  into.writeAttr(SUMO_ATTR_ID, i->first);
124  const NBTypeCont::TypeDefinition& type = i->second;
125  if (type.attrs.count(SUMO_ATTR_PRIORITY) > 0) {
127  }
128  if (type.attrs.count(SUMO_ATTR_NUMLANES) > 0) {
130  }
131  if (type.attrs.count(SUMO_ATTR_SPEED) > 0) {
132  into.writeAttr(SUMO_ATTR_SPEED, type.speed);
133  }
134  if (type.attrs.count(SUMO_ATTR_DISALLOW) > 0 || type.attrs.count(SUMO_ATTR_ALLOW) > 0) {
135  writePermissions(into, type.permissions);
136  }
137  if (type.attrs.count(SUMO_ATTR_ONEWAY) > 0) {
138  into.writeAttr(SUMO_ATTR_ONEWAY, type.oneWay);
139  }
140  if (type.attrs.count(SUMO_ATTR_DISCARD) > 0) {
141  into.writeAttr(SUMO_ATTR_DISCARD, type.discard);
142  }
143  if (type.attrs.count(SUMO_ATTR_WIDTH) > 0) {
144  into.writeAttr(SUMO_ATTR_WIDTH, type.width);
145  }
146  if (type.attrs.count(SUMO_ATTR_SIDEWALKWIDTH) > 0) {
148  }
149  if (type.attrs.count(SUMO_ATTR_BIKELANEWIDTH) > 0) {
151  }
152  for (std::map<SUMOVehicleClass, double>::const_iterator j = type.restrictions.begin(); j != type.restrictions.end(); ++j) {
155  into.writeAttr(SUMO_ATTR_SPEED, j->second);
156  into.closeTag();
157  }
158  into.closeTag();
159  }
160  if (!myTypes.empty()) {
161  into.lf();
162  }
163 }
164 
165 
166 // ------------ Type-dependant Retrieval methods
167 int
168 NBTypeCont::getNumLanes(const std::string& type) const {
169  return getType(type).numLanes;
170 }
171 
172 
173 double
174 NBTypeCont::getSpeed(const std::string& type) const {
175  return getType(type).speed;
176 }
177 
178 
179 int
180 NBTypeCont::getPriority(const std::string& type) const {
181  return getType(type).priority;
182 }
183 
184 
185 bool
186 NBTypeCont::getIsOneWay(const std::string& type) const {
187  return getType(type).oneWay;
188 }
189 
190 
191 bool
192 NBTypeCont::getShallBeDiscarded(const std::string& type) const {
193  return getType(type).discard;
194 }
195 
196 
197 bool
198 NBTypeCont::wasSet(const std::string& type, const SumoXMLAttr attr) const {
199  return getType(type).attrs.count(attr) > 0;
200 }
201 
202 
204 NBTypeCont::getPermissions(const std::string& type) const {
205  return getType(type).permissions;
206 }
207 
208 
209 double
210 NBTypeCont::getWidth(const std::string& type) const {
211  return getType(type).width;
212 }
213 
214 
215 double
216 NBTypeCont::getSidewalkWidth(const std::string& type) const {
217  return getType(type).sidewalkWidth;
218 }
219 
220 
221 double
222 NBTypeCont::getBikeLaneWidth(const std::string& type) const {
223  return getType(type).bikeLaneWidth;
224 }
225 
226 
228 NBTypeCont::getType(const std::string& name) const {
229  TypesCont::const_iterator i = myTypes.find(name);
230  if (i == myTypes.end()) {
231  return myDefaultType;
232  }
233  return (*i).second;
234 }
235 
236 /****************************************************************************/
237 
std::string getVehicleClassNames(SVCPermissions permissions, bool expand)
Returns the ids of the given classes, divided using a &#39; &#39;.
void insert(const std::string &id, int numLanes, double maxSpeed, int prio, SVCPermissions permissions, double width, bool oneWayIsDefault, double sidewalkWidth, double bikeLaneWidth)
Adds a type into the list.
Definition: NBTypeCont.cpp:54
void writePermissions(OutputDevice &into, SVCPermissions permissions)
writes allowed disallowed attributes if needed;
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
std::set< SumoXMLAttr > attrs
The attributes which have been set.
Definition: NBTypeCont.h:276
double getSpeed(const std::string &type) const
Returns the maximal velocity for the given type [m/s].
Definition: NBTypeCont.cpp:174
int numLanes
The number of lanes of an edge.
Definition: NBTypeCont.h:254
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
int getPriority(const std::string &type) const
Returns the priority for the given type.
Definition: NBTypeCont.cpp:180
bool markAsSet(const std::string &id, const SumoXMLAttr attr)
Marks an attribute of a type as set.
Definition: NBTypeCont.cpp:85
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
int getNumLanes(const std::string &type) const
Returns the number of lanes for the given type.
Definition: NBTypeCont.cpp:168
bool getShallBeDiscarded(const std::string &type) const
Returns the information whether edges of this type shall be discarded.
Definition: NBTypeCont.cpp:192
begin/end of the description of an edge restriction
bool oneWay
Whether one-way traffic is mostly common for this type (mostly unused)
Definition: NBTypeCont.h:262
double getWidth(const std::string &type) const
Returns the lane width for the given type [m].
Definition: NBTypeCont.cpp:210
const TypeDefinition & getType(const std::string &name) const
Retrieve the name or the default type.
Definition: NBTypeCont.cpp:228
bool addRestriction(const std::string &id, const SUMOVehicleClass svc, const double speed)
Adds a restriction to a type.
Definition: NBTypeCont.cpp:96
bool knows(const std::string &type) const
Returns whether the named type is in the container.
Definition: NBTypeCont.cpp:68
double getBikeLaneWidth(const std::string &type) const
Returns the lane width for a bike lane to be added [m].
Definition: NBTypeCont.cpp:222
double getSidewalkWidth(const std::string &type) const
Returns the lane width for a sidewalk to be added [m].
Definition: NBTypeCont.cpp:216
void setDefaults(int defaultNumLanes, double defaultLaneWidth, double defaultSpeed, int defaultPriority, SVCPermissions defaultPermissions)
Sets the default values.
Definition: NBTypeCont.cpp:40
double width
The width of lanes of edges of this type [m].
Definition: NBTypeCont.h:266
bool markAsToDiscard(const std::string &id)
Marks a type as to be discarded.
Definition: NBTypeCont.cpp:74
TypeDefinition myDefaultType
The default type.
Definition: NBTypeCont.h:292
double speed
The maximal velocity on an edge in m/s.
Definition: NBTypeCont.h:256
bool copyRestrictionsAndAttrs(const std::string &fromId, const std::string &toId)
Copy restrictions to a type.
Definition: NBTypeCont.cpp:107
std::map< SUMOVehicleClass, double > restrictions
The vehicle class specific speed restrictions.
Definition: NBTypeCont.h:274
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
int priority
The priority of an edge.
Definition: NBTypeCont.h:258
SVCPermissions permissions
List of vehicle types that are allowed on this edge.
Definition: NBTypeCont.h:260
bool getIsOneWay(const std::string &type) const
Returns whether edges are one-way per default for the given type.
Definition: NBTypeCont.cpp:186
TypesCont myTypes
The container of types.
Definition: NBTypeCont.h:298
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:234
bool wasSet(const std::string &type, const SumoXMLAttr attr) const
Returns whether an attribute of a type was set.
Definition: NBTypeCont.cpp:198
bool discard
Whether edges of this type shall be discarded.
Definition: NBTypeCont.h:264
SVCPermissions getPermissions(const std::string &type) const
Returns allowed vehicle classes for the given type.
Definition: NBTypeCont.cpp:204
void writeTypes(OutputDevice &into) const
writes all types a s XML
Definition: NBTypeCont.cpp:120