SUMO - Simulation of Urban MObility
NBTypeCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A storage for the available types of an edge
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
35 #include <map>
36 #include <iostream>
38 #include <utils/common/ToString.h>
40 #include "NBTypeCont.h"
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 void
47 NBTypeCont::setDefaults(int defaultNumLanes,
48  double defaultLaneWidth,
49  double defaultSpeed,
50  int defaultPriority,
51  SVCPermissions defaultPermissions) {
52  myDefaultType.numLanes = defaultNumLanes;
53  myDefaultType.width = defaultLaneWidth;
54  myDefaultType.speed = defaultSpeed;
55  myDefaultType.priority = defaultPriority;
56  myDefaultType.permissions = defaultPermissions;
57 }
58 
59 
60 void
61 NBTypeCont::insert(const std::string& id, int numLanes, double maxSpeed, int prio,
62  SVCPermissions permissions, double width, bool oneWayIsDefault, double sidewalkWidth, double bikeLaneWidth) {
63 
64  TypeDefinition newType(numLanes, maxSpeed, prio, width, permissions, oneWayIsDefault, sidewalkWidth, bikeLaneWidth);
65  TypesCont::iterator old = myTypes.find(id);
66  if (old != myTypes.end()) {
67  newType.restrictions.insert(old->second.restrictions.begin(), old->second.restrictions.end());
68  newType.attrs.insert(old->second.attrs.begin(), old->second.attrs.end());
69  }
70  myTypes[id] = newType;
71 }
72 
73 
74 bool
75 NBTypeCont::knows(const std::string& type) const {
76  return myTypes.find(type) != myTypes.end();
77 }
78 
79 
80 bool
81 NBTypeCont::markAsToDiscard(const std::string& id) {
82  TypesCont::iterator i = myTypes.find(id);
83  if (i == myTypes.end()) {
84  return false;
85  }
86  (*i).second.discard = true;
87  return true;
88 }
89 
90 
91 bool
92 NBTypeCont::markAsSet(const std::string& id, const SumoXMLAttr attr) {
93  TypesCont::iterator i = myTypes.find(id);
94  if (i == myTypes.end()) {
95  return false;
96  }
97  (*i).second.attrs.insert(attr);
98  return true;
99 }
100 
101 
102 bool
103 NBTypeCont::addRestriction(const std::string& id, const SUMOVehicleClass svc, const double speed) {
104  TypesCont::iterator i = myTypes.find(id);
105  if (i == myTypes.end()) {
106  return false;
107  }
108  (*i).second.restrictions[svc] = speed;
109  return true;
110 }
111 
112 
113 bool
114 NBTypeCont::copyRestrictionsAndAttrs(const std::string& fromId, const std::string& toId) {
115  TypesCont::iterator from = myTypes.find(fromId);
116  TypesCont::iterator to = myTypes.find(toId);
117  if (from == myTypes.end() || to == myTypes.end()) {
118  return false;
119  }
120  to->second.restrictions.insert(from->second.restrictions.begin(), from->second.restrictions.end());
121  to->second.attrs.insert(from->second.attrs.begin(), from->second.attrs.end());
122  return true;
123 }
124 
125 
126 void
128  for (TypesCont::const_iterator i = myTypes.begin(); i != myTypes.end(); ++i) {
129  into.openTag(SUMO_TAG_TYPE);
130  into.writeAttr(SUMO_ATTR_ID, i->first);
131  const NBTypeCont::TypeDefinition& type = i->second;
132  if (type.attrs.count(SUMO_ATTR_PRIORITY) > 0) {
134  }
135  if (type.attrs.count(SUMO_ATTR_NUMLANES) > 0) {
137  }
138  if (type.attrs.count(SUMO_ATTR_SPEED) > 0) {
139  into.writeAttr(SUMO_ATTR_SPEED, type.speed);
140  }
141  if (type.attrs.count(SUMO_ATTR_DISALLOW) > 0 || type.attrs.count(SUMO_ATTR_ALLOW) > 0) {
142  writePermissions(into, type.permissions);
143  }
144  if (type.attrs.count(SUMO_ATTR_ONEWAY) > 0) {
145  into.writeAttr(SUMO_ATTR_ONEWAY, type.oneWay);
146  }
147  if (type.attrs.count(SUMO_ATTR_DISCARD) > 0) {
148  into.writeAttr(SUMO_ATTR_DISCARD, type.discard);
149  }
150  if (type.attrs.count(SUMO_ATTR_WIDTH) > 0) {
151  into.writeAttr(SUMO_ATTR_WIDTH, type.width);
152  }
153  if (type.attrs.count(SUMO_ATTR_SIDEWALKWIDTH) > 0) {
155  }
156  if (type.attrs.count(SUMO_ATTR_BIKELANEWIDTH) > 0) {
158  }
159  for (std::map<SUMOVehicleClass, double>::const_iterator j = type.restrictions.begin(); j != type.restrictions.end(); ++j) {
162  into.writeAttr(SUMO_ATTR_SPEED, j->second);
163  into.closeTag();
164  }
165  into.closeTag();
166  }
167  if (!myTypes.empty()) {
168  into.lf();
169  }
170 }
171 
172 
173 // ------------ Type-dependant Retrieval methods
174 int
175 NBTypeCont::getNumLanes(const std::string& type) const {
176  return getType(type).numLanes;
177 }
178 
179 
180 double
181 NBTypeCont::getSpeed(const std::string& type) const {
182  return getType(type).speed;
183 }
184 
185 
186 int
187 NBTypeCont::getPriority(const std::string& type) const {
188  return getType(type).priority;
189 }
190 
191 
192 bool
193 NBTypeCont::getIsOneWay(const std::string& type) const {
194  return getType(type).oneWay;
195 }
196 
197 
198 bool
199 NBTypeCont::getShallBeDiscarded(const std::string& type) const {
200  return getType(type).discard;
201 }
202 
203 
204 bool
205 NBTypeCont::wasSet(const std::string& type, const SumoXMLAttr attr) const {
206  return getType(type).attrs.count(attr) > 0;
207 }
208 
209 
211 NBTypeCont::getPermissions(const std::string& type) const {
212  return getType(type).permissions;
213 }
214 
215 
216 double
217 NBTypeCont::getWidth(const std::string& type) const {
218  return getType(type).width;
219 }
220 
221 
222 double
223 NBTypeCont::getSidewalkWidth(const std::string& type) const {
224  return getType(type).sidewalkWidth;
225 }
226 
227 
228 double
229 NBTypeCont::getBikeLaneWidth(const std::string& type) const {
230  return getType(type).bikeLaneWidth;
231 }
232 
233 
235 NBTypeCont::getType(const std::string& name) const {
236  TypesCont::const_iterator i = myTypes.find(name);
237  if (i == myTypes.end()) {
238  return myDefaultType;
239  }
240  return (*i).second;
241 }
242 
243 /****************************************************************************/
244 
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:61
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:283
double getSpeed(const std::string &type) const
Returns the maximal velocity for the given type [m/s].
Definition: NBTypeCont.cpp:181
int numLanes
The number of lanes of an edge.
Definition: NBTypeCont.h:261
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:187
bool markAsSet(const std::string &id, const SumoXMLAttr attr)
Marks an attribute of a type as set.
Definition: NBTypeCont.cpp:92
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:175
bool getShallBeDiscarded(const std::string &type) const
Returns the information whether edges of this type shall be discarded.
Definition: NBTypeCont.cpp:199
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:269
double getWidth(const std::string &type) const
Returns the lane width for the given type [m].
Definition: NBTypeCont.cpp:217
const TypeDefinition & getType(const std::string &name) const
Retrieve the name or the default type.
Definition: NBTypeCont.cpp:235
bool addRestriction(const std::string &id, const SUMOVehicleClass svc, const double speed)
Adds a restriction to a type.
Definition: NBTypeCont.cpp:103
bool knows(const std::string &type) const
Returns whether the named type is in the container.
Definition: NBTypeCont.cpp:75
double getBikeLaneWidth(const std::string &type) const
Returns the lane width for a bike lane to be added [m].
Definition: NBTypeCont.cpp:229
double getSidewalkWidth(const std::string &type) const
Returns the lane width for a sidewalk to be added [m].
Definition: NBTypeCont.cpp:223
void setDefaults(int defaultNumLanes, double defaultLaneWidth, double defaultSpeed, int defaultPriority, SVCPermissions defaultPermissions)
Sets the default values.
Definition: NBTypeCont.cpp:47
double width
The width of lanes of edges of this type [m].
Definition: NBTypeCont.h:273
bool markAsToDiscard(const std::string &id)
Marks a type as to be discarded.
Definition: NBTypeCont.cpp:81
TypeDefinition myDefaultType
The default type.
Definition: NBTypeCont.h:299
double speed
The maximal velocity on an edge in m/s.
Definition: NBTypeCont.h:263
bool copyRestrictionsAndAttrs(const std::string &fromId, const std::string &toId)
Copy restrictions to a type.
Definition: NBTypeCont.cpp:114
std::map< SUMOVehicleClass, double > restrictions
The vehicle class specific speed restrictions.
Definition: NBTypeCont.h:281
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
int priority
The priority of an edge.
Definition: NBTypeCont.h:265
SVCPermissions permissions
List of vehicle types that are allowed on this edge.
Definition: NBTypeCont.h:267
bool getIsOneWay(const std::string &type) const
Returns whether edges are one-way per default for the given type.
Definition: NBTypeCont.cpp:193
TypesCont myTypes
The container of types.
Definition: NBTypeCont.h:305
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:205
bool discard
Whether edges of this type shall be discarded.
Definition: NBTypeCont.h:271
SVCPermissions getPermissions(const std::string &type) const
Returns allowed vehicle classes for the given type.
Definition: NBTypeCont.cpp:211
void writeTypes(OutputDevice &into) const
writes all types a s XML
Definition: NBTypeCont.cpp:127