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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // A storage for the available types of an edge
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <map>
35 #include <iostream>
37 #include <utils/common/ToString.h>
39 #include "NBTypeCont.h"
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
45 void
46 NBTypeCont::setDefaults(int defaultNumLanes,
47  double defaultLaneWidth,
48  double defaultSpeed,
49  int defaultPriority,
50  SVCPermissions defaultPermissions) {
51  myDefaultType.numLanes = defaultNumLanes;
52  myDefaultType.width = defaultLaneWidth;
53  myDefaultType.speed = defaultSpeed;
54  myDefaultType.priority = defaultPriority;
55  myDefaultType.permissions = defaultPermissions;
56 }
57 
58 
59 void
60 NBTypeCont::insert(const std::string& id, int numLanes, double maxSpeed, int prio,
61  SVCPermissions permissions, double width, bool oneWayIsDefault, double sidewalkWidth, double bikeLaneWidth) {
62 
63  TypeDefinition newType(numLanes, maxSpeed, prio, width, permissions, oneWayIsDefault, sidewalkWidth, bikeLaneWidth);
64  TypesCont::iterator old = myTypes.find(id);
65  if (old != myTypes.end()) {
66  newType.restrictions.insert(old->second.restrictions.begin(), old->second.restrictions.end());
67  newType.attrs.insert(old->second.attrs.begin(), old->second.attrs.end());
68  }
69  myTypes[id] = newType;
70 }
71 
72 
73 bool
74 NBTypeCont::knows(const std::string& type) const {
75  return myTypes.find(type) != myTypes.end();
76 }
77 
78 
79 bool
80 NBTypeCont::markAsToDiscard(const std::string& id) {
81  TypesCont::iterator i = myTypes.find(id);
82  if (i == myTypes.end()) {
83  return false;
84  }
85  (*i).second.discard = true;
86  return true;
87 }
88 
89 
90 bool
91 NBTypeCont::markAsSet(const std::string& id, const SumoXMLAttr attr) {
92  TypesCont::iterator i = myTypes.find(id);
93  if (i == myTypes.end()) {
94  return false;
95  }
96  (*i).second.attrs.insert(attr);
97  return true;
98 }
99 
100 
101 bool
102 NBTypeCont::addRestriction(const std::string& id, const SUMOVehicleClass svc, const double speed) {
103  TypesCont::iterator i = myTypes.find(id);
104  if (i == myTypes.end()) {
105  return false;
106  }
107  (*i).second.restrictions[svc] = speed;
108  return true;
109 }
110 
111 
112 bool
113 NBTypeCont::copyRestrictionsAndAttrs(const std::string& fromId, const std::string& toId) {
114  TypesCont::iterator from = myTypes.find(fromId);
115  TypesCont::iterator to = myTypes.find(toId);
116  if (from == myTypes.end() || to == myTypes.end()) {
117  return false;
118  }
119  to->second.restrictions.insert(from->second.restrictions.begin(), from->second.restrictions.end());
120  to->second.attrs.insert(from->second.attrs.begin(), from->second.attrs.end());
121  return true;
122 }
123 
124 
125 void
127  for (TypesCont::const_iterator i = myTypes.begin(); i != myTypes.end(); ++i) {
128  into.openTag(SUMO_TAG_TYPE);
129  into.writeAttr(SUMO_ATTR_ID, i->first);
130  const NBTypeCont::TypeDefinition& type = i->second;
131  if (type.attrs.count(SUMO_ATTR_PRIORITY) > 0) {
133  }
134  if (type.attrs.count(SUMO_ATTR_NUMLANES) > 0) {
136  }
137  if (type.attrs.count(SUMO_ATTR_SPEED) > 0) {
138  into.writeAttr(SUMO_ATTR_SPEED, type.speed);
139  }
140  if (type.attrs.count(SUMO_ATTR_DISALLOW) > 0 || type.attrs.count(SUMO_ATTR_ALLOW) > 0) {
141  writePermissions(into, type.permissions);
142  }
143  if (type.attrs.count(SUMO_ATTR_ONEWAY) > 0) {
144  into.writeAttr(SUMO_ATTR_ONEWAY, type.oneWay);
145  }
146  if (type.attrs.count(SUMO_ATTR_DISCARD) > 0) {
147  into.writeAttr(SUMO_ATTR_DISCARD, type.discard);
148  }
149  if (type.attrs.count(SUMO_ATTR_WIDTH) > 0) {
150  into.writeAttr(SUMO_ATTR_WIDTH, type.width);
151  }
152  if (type.attrs.count(SUMO_ATTR_SIDEWALKWIDTH) > 0) {
154  }
155  if (type.attrs.count(SUMO_ATTR_BIKELANEWIDTH) > 0) {
157  }
158  for (std::map<SUMOVehicleClass, double>::const_iterator j = type.restrictions.begin(); j != type.restrictions.end(); ++j) {
161  into.writeAttr(SUMO_ATTR_SPEED, j->second);
162  into.closeTag();
163  }
164  into.closeTag();
165  }
166  if (!myTypes.empty()) {
167  into.lf();
168  }
169 }
170 
171 
172 // ------------ Type-dependant Retrieval methods
173 int
174 NBTypeCont::getNumLanes(const std::string& type) const {
175  return getType(type).numLanes;
176 }
177 
178 
179 double
180 NBTypeCont::getSpeed(const std::string& type) const {
181  return getType(type).speed;
182 }
183 
184 
185 int
186 NBTypeCont::getPriority(const std::string& type) const {
187  return getType(type).priority;
188 }
189 
190 
191 bool
192 NBTypeCont::getIsOneWay(const std::string& type) const {
193  return getType(type).oneWay;
194 }
195 
196 
197 bool
198 NBTypeCont::getShallBeDiscarded(const std::string& type) const {
199  return getType(type).discard;
200 }
201 
202 
203 bool
204 NBTypeCont::wasSet(const std::string& type, const SumoXMLAttr attr) const {
205  return getType(type).attrs.count(attr) > 0;
206 }
207 
208 
210 NBTypeCont::getPermissions(const std::string& type) const {
211  return getType(type).permissions;
212 }
213 
214 
215 double
216 NBTypeCont::getWidth(const std::string& type) const {
217  return getType(type).width;
218 }
219 
220 
221 double
222 NBTypeCont::getSidewalkWidth(const std::string& type) const {
223  return getType(type).sidewalkWidth;
224 }
225 
226 
227 double
228 NBTypeCont::getBikeLaneWidth(const std::string& type) const {
229  return getType(type).bikeLaneWidth;
230 }
231 
232 
234 NBTypeCont::getType(const std::string& name) const {
235  TypesCont::const_iterator i = myTypes.find(name);
236  if (i == myTypes.end()) {
237  return myDefaultType;
238  }
239  return (*i).second;
240 }
241 
242 /****************************************************************************/
243 
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:60
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:260
std::set< SumoXMLAttr > attrs
The attributes which have been set.
Definition: NBTypeCont.h:282
double getSpeed(const std::string &type) const
Returns the maximal velocity for the given type [m/s].
Definition: NBTypeCont.cpp:180
int numLanes
The number of lanes of an edge.
Definition: NBTypeCont.h:260
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:186
bool markAsSet(const std::string &id, const SumoXMLAttr attr)
Marks an attribute of a type as set.
Definition: NBTypeCont.cpp:91
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:174
bool getShallBeDiscarded(const std::string &type) const
Returns the information whether edges of this type shall be discarded.
Definition: NBTypeCont.cpp:198
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:268
double getWidth(const std::string &type) const
Returns the lane width for the given type [m].
Definition: NBTypeCont.cpp:216
const TypeDefinition & getType(const std::string &name) const
Retrieve the name or the default type.
Definition: NBTypeCont.cpp:234
bool addRestriction(const std::string &id, const SUMOVehicleClass svc, const double speed)
Adds a restriction to a type.
Definition: NBTypeCont.cpp:102
bool knows(const std::string &type) const
Returns whether the named type is in the container.
Definition: NBTypeCont.cpp:74
double getBikeLaneWidth(const std::string &type) const
Returns the lane width for a bike lane to be added [m].
Definition: NBTypeCont.cpp:228
double getSidewalkWidth(const std::string &type) const
Returns the lane width for a sidewalk to be added [m].
Definition: NBTypeCont.cpp:222
void setDefaults(int defaultNumLanes, double defaultLaneWidth, double defaultSpeed, int defaultPriority, SVCPermissions defaultPermissions)
Sets the default values.
Definition: NBTypeCont.cpp:46
double width
The width of lanes of edges of this type [m].
Definition: NBTypeCont.h:272
bool markAsToDiscard(const std::string &id)
Marks a type as to be discarded.
Definition: NBTypeCont.cpp:80
TypeDefinition myDefaultType
The default type.
Definition: NBTypeCont.h:298
double speed
The maximal velocity on an edge in m/s.
Definition: NBTypeCont.h:262
bool copyRestrictionsAndAttrs(const std::string &fromId, const std::string &toId)
Copy restrictions to a type.
Definition: NBTypeCont.cpp:113
std::map< SUMOVehicleClass, double > restrictions
The vehicle class specific speed restrictions.
Definition: NBTypeCont.h:280
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
int priority
The priority of an edge.
Definition: NBTypeCont.h:264
SVCPermissions permissions
List of vehicle types that are allowed on this edge.
Definition: NBTypeCont.h:266
bool getIsOneWay(const std::string &type) const
Returns whether edges are one-way per default for the given type.
Definition: NBTypeCont.cpp:192
TypesCont myTypes
The container of types.
Definition: NBTypeCont.h:304
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:238
bool wasSet(const std::string &type, const SumoXMLAttr attr) const
Returns whether an attribute of a type was set.
Definition: NBTypeCont.cpp:204
bool discard
Whether edges of this type shall be discarded.
Definition: NBTypeCont.h:270
SVCPermissions getPermissions(const std::string &type) const
Returns allowed vehicle classes for the given type.
Definition: NBTypeCont.cpp:210
void writeTypes(OutputDevice &into) const
writes all types a s XML
Definition: NBTypeCont.cpp:126