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 /****************************************************************************/
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,
56  double sidewalkWidth, double bikeLaneWidth,
57  double widthResolution,
58  double maxWidth,
59  double minWidth) {
60 
61  TypeDefinition newType(numLanes, maxSpeed, prio, width, permissions, oneWayIsDefault, sidewalkWidth, bikeLaneWidth, widthResolution, maxWidth, minWidth);
62  TypesCont::iterator old = myTypes.find(id);
63  if (old != myTypes.end()) {
64  newType.restrictions.insert(old->second.restrictions.begin(), old->second.restrictions.end());
65  newType.attrs.insert(old->second.attrs.begin(), old->second.attrs.end());
66  }
67  myTypes[id] = newType;
68 }
69 
70 
71 bool
72 NBTypeCont::knows(const std::string& type) const {
73  return myTypes.find(type) != myTypes.end();
74 }
75 
76 
77 bool
78 NBTypeCont::markAsToDiscard(const std::string& id) {
79  TypesCont::iterator i = myTypes.find(id);
80  if (i == myTypes.end()) {
81  return false;
82  }
83  (*i).second.discard = true;
84  return true;
85 }
86 
87 
88 bool
89 NBTypeCont::markAsSet(const std::string& id, const SumoXMLAttr attr) {
90  TypesCont::iterator i = myTypes.find(id);
91  if (i == myTypes.end()) {
92  return false;
93  }
94  (*i).second.attrs.insert(attr);
95  return true;
96 }
97 
98 
99 bool
100 NBTypeCont::addRestriction(const std::string& id, const SUMOVehicleClass svc, const double speed) {
101  TypesCont::iterator i = myTypes.find(id);
102  if (i == myTypes.end()) {
103  return false;
104  }
105  (*i).second.restrictions[svc] = speed;
106  return true;
107 }
108 
109 
110 bool
111 NBTypeCont::copyRestrictionsAndAttrs(const std::string& fromId, const std::string& toId) {
112  TypesCont::iterator from = myTypes.find(fromId);
113  TypesCont::iterator to = myTypes.find(toId);
114  if (from == myTypes.end() || to == myTypes.end()) {
115  return false;
116  }
117  to->second.restrictions.insert(from->second.restrictions.begin(), from->second.restrictions.end());
118  to->second.attrs.insert(from->second.attrs.begin(), from->second.attrs.end());
119  return true;
120 }
121 
122 
123 void
125  for (TypesCont::const_iterator i = myTypes.begin(); i != myTypes.end(); ++i) {
126  into.openTag(SUMO_TAG_TYPE);
127  into.writeAttr(SUMO_ATTR_ID, i->first);
128  const NBTypeCont::TypeDefinition& type = i->second;
129  if (type.attrs.count(SUMO_ATTR_PRIORITY) > 0) {
131  }
132  if (type.attrs.count(SUMO_ATTR_NUMLANES) > 0) {
134  }
135  if (type.attrs.count(SUMO_ATTR_SPEED) > 0) {
136  into.writeAttr(SUMO_ATTR_SPEED, type.speed);
137  }
138  if (type.attrs.count(SUMO_ATTR_DISALLOW) > 0 || type.attrs.count(SUMO_ATTR_ALLOW) > 0) {
139  writePermissions(into, type.permissions);
140  }
141  if (type.attrs.count(SUMO_ATTR_ONEWAY) > 0) {
142  into.writeAttr(SUMO_ATTR_ONEWAY, type.oneWay);
143  }
144  if (type.attrs.count(SUMO_ATTR_DISCARD) > 0) {
145  into.writeAttr(SUMO_ATTR_DISCARD, type.discard);
146  }
147  if (type.attrs.count(SUMO_ATTR_WIDTH) > 0) {
148  into.writeAttr(SUMO_ATTR_WIDTH, type.width);
149  }
150  if (type.attrs.count(SUMO_ATTR_SIDEWALKWIDTH) > 0) {
152  }
153  if (type.attrs.count(SUMO_ATTR_BIKELANEWIDTH) > 0) {
155  }
156  for (std::map<SUMOVehicleClass, double>::const_iterator j = type.restrictions.begin(); j != type.restrictions.end(); ++j) {
159  into.writeAttr(SUMO_ATTR_SPEED, j->second);
160  into.closeTag();
161  }
162  into.closeTag();
163  }
164  if (!myTypes.empty()) {
165  into.lf();
166  }
167 }
168 
169 
170 // ------------ Type-dependant Retrieval methods
171 int
172 NBTypeCont::getNumLanes(const std::string& type) const {
173  return getType(type).numLanes;
174 }
175 
176 
177 double
178 NBTypeCont::getSpeed(const std::string& type) const {
179  return getType(type).speed;
180 }
181 
182 
183 int
184 NBTypeCont::getPriority(const std::string& type) const {
185  return getType(type).priority;
186 }
187 
188 
189 bool
190 NBTypeCont::getIsOneWay(const std::string& type) const {
191  return getType(type).oneWay;
192 }
193 
194 
195 bool
196 NBTypeCont::getShallBeDiscarded(const std::string& type) const {
197  return getType(type).discard;
198 }
199 
200 double
201 NBTypeCont::getWidthResolution(const std::string& type) const {
202  return getType(type).widthResolution;
203 }
204 
205 double
206 NBTypeCont::getMaxWidth(const std::string& type) const {
207  return getType(type).maxWidth;
208 }
209 
210 double
211 NBTypeCont::getMinWidth(const std::string& type) const {
212  return getType(type).minWidth;
213 }
214 
215 bool
216 NBTypeCont::wasSet(const std::string& type, const SumoXMLAttr attr) const {
217  return getType(type).attrs.count(attr) > 0;
218 }
219 
220 
222 NBTypeCont::getPermissions(const std::string& type) const {
223  return getType(type).permissions;
224 }
225 
226 
227 double
228 NBTypeCont::getWidth(const std::string& type) const {
229  return getType(type).width;
230 }
231 
232 
233 double
234 NBTypeCont::getSidewalkWidth(const std::string& type) const {
235  return getType(type).sidewalkWidth;
236 }
237 
238 
239 double
240 NBTypeCont::getBikeLaneWidth(const std::string& type) const {
241  return getType(type).bikeLaneWidth;
242 }
243 
244 
246 NBTypeCont::getType(const std::string& name) const {
247  TypesCont::const_iterator i = myTypes.find(name);
248  if (i == myTypes.end()) {
249  return myDefaultType;
250  }
251  return (*i).second;
252 }
253 
254 /****************************************************************************/
255 
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:322
double getSpeed(const std::string &type) const
Returns the maximal velocity for the given type [m/s].
Definition: NBTypeCont.cpp:178
int numLanes
The number of lanes of an edge.
Definition: NBTypeCont.h:294
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:184
double widthResolution
The resolution for interpreting custom (noisy) lane widths of this type [m].
Definition: NBTypeCont.h:308
bool markAsSet(const std::string &id, const SumoXMLAttr attr)
Marks an attribute of a type as set.
Definition: NBTypeCont.cpp:89
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:172
bool getShallBeDiscarded(const std::string &type) const
Returns the information whether edges of this type shall be discarded.
Definition: NBTypeCont.cpp:196
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:302
double getWidth(const std::string &type) const
Returns the lane width for the given type [m].
Definition: NBTypeCont.cpp:228
const TypeDefinition & getType(const std::string &name) const
Retrieve the name or the default type.
Definition: NBTypeCont.cpp:246
bool addRestriction(const std::string &id, const SUMOVehicleClass svc, const double speed)
Adds a restriction to a type.
Definition: NBTypeCont.cpp:100
bool knows(const std::string &type) const
Returns whether the named type is in the container.
Definition: NBTypeCont.cpp:72
double getBikeLaneWidth(const std::string &type) const
Returns the lane width for a bike lane to be added [m].
Definition: NBTypeCont.cpp:240
double getSidewalkWidth(const std::string &type) const
Returns the lane width for a sidewalk to be added [m].
Definition: NBTypeCont.cpp:234
void setDefaults(int defaultNumLanes, double defaultLaneWidth, double defaultSpeed, int defaultPriority, SVCPermissions defaultPermissions)
Sets the default values.
Definition: NBTypeCont.cpp:40
double getMinWidth(const std::string &type) const
Returns the minimum edge/lane widths of the given type.
Definition: NBTypeCont.cpp:211
double width
The width of lanes of edges of this type [m].
Definition: NBTypeCont.h:306
double minWidth
The minimum width for lanes of this type [m].
Definition: NBTypeCont.h:312
bool markAsToDiscard(const std::string &id)
Marks a type as to be discarded.
Definition: NBTypeCont.cpp:78
double getMaxWidth(const std::string &type) const
Returns the maximum edge/lane widths of the given type.
Definition: NBTypeCont.cpp:206
const std::string & getVehicleClassNames(SVCPermissions permissions, bool expand)
Returns the ids of the given classes, divided using a &#39; &#39;.
TypeDefinition myDefaultType
The default type.
Definition: NBTypeCont.h:338
double speed
The maximal velocity on an edge in m/s.
Definition: NBTypeCont.h:296
bool copyRestrictionsAndAttrs(const std::string &fromId, const std::string &toId)
Copy restrictions to a type.
Definition: NBTypeCont.cpp:111
std::map< SUMOVehicleClass, double > restrictions
The vehicle class specific speed restrictions.
Definition: NBTypeCont.h:320
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.
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:54
int priority
The priority of an edge.
Definition: NBTypeCont.h:298
SVCPermissions permissions
List of vehicle types that are allowed on this edge.
Definition: NBTypeCont.h:300
bool getIsOneWay(const std::string &type) const
Returns whether edges are one-way per default for the given type.
Definition: NBTypeCont.cpp:190
double getWidthResolution(const std::string &type) const
Returns the resolution for interpreting edge/lane widths of the given type.
Definition: NBTypeCont.cpp:201
double maxWidth
The maximum width for lanes of this type [m].
Definition: NBTypeCont.h:310
TypesCont myTypes
The container of types.
Definition: NBTypeCont.h:344
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:216
bool discard
Whether edges of this type shall be discarded.
Definition: NBTypeCont.h:304
SVCPermissions getPermissions(const std::string &type) const
Returns allowed vehicle classes for the given type.
Definition: NBTypeCont.cpp:222
void writeTypes(OutputDevice &into) const
writes all types a s XML
Definition: NBTypeCont.cpp:124