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