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 /****************************************************************************/
10 /// @file    NBTypeCont.cpp
11 /// @author  Daniel Krajzewicz
12 /// @author  Jakob Erdmann
13 /// @author  Michael Behrisch
14 /// @author  Walter Bamberger
15 /// @date    Tue, 20 Nov 2001
16 /// @version $Id$
17 ///
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>
30 #include <utils/common/MsgHandler.h>
31 #include <utils/common/ToString.h>
32 #include <utils/iodevices/OutputDevice.h>
33 #include "NBTypeCont.h"
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
39 void
setDefaults(int defaultNumLanes,double defaultLaneWidth,double defaultSpeed,int defaultPriority,SVCPermissions defaultPermissions)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
insert(const std::string & id,int numLanes,double maxSpeed,int prio,SVCPermissions permissions,double width,bool oneWayIsDefault,double sidewalkWidth,double bikeLaneWidth)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
knows(const std::string & type) const68 NBTypeCont::knows(const std::string& type) const {
69     return myTypes.find(type) != myTypes.end();
70 }
71 
72 
73 bool
markAsToDiscard(const std::string & id)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
markAsSet(const std::string & id,const SumoXMLAttr attr)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
addRestriction(const std::string & id,const SUMOVehicleClass svc,const double speed)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
copyRestrictionsAndAttrs(const std::string & fromId,const std::string & toId)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
writeTypes(OutputDevice & into) const120 NBTypeCont::writeTypes(OutputDevice& into) const {
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) {
126             into.writeAttr(SUMO_ATTR_PRIORITY, type.priority);
127         }
128         if (type.attrs.count(SUMO_ATTR_NUMLANES) > 0) {
129             into.writeAttr(SUMO_ATTR_NUMLANES, type.numLanes);
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) {
147             into.writeAttr(SUMO_ATTR_SIDEWALKWIDTH, type.sidewalkWidth);
148         }
149         if (type.attrs.count(SUMO_ATTR_BIKELANEWIDTH) > 0) {
150             into.writeAttr(SUMO_ATTR_BIKELANEWIDTH, type.bikeLaneWidth);
151         }
152         for (std::map<SUMOVehicleClass, double>::const_iterator j = type.restrictions.begin(); j != type.restrictions.end(); ++j) {
153             into.openTag(SUMO_TAG_RESTRICTION);
154             into.writeAttr(SUMO_ATTR_VCLASS, getVehicleClassNames(j->first));
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
getNumLanes(const std::string & type) const168 NBTypeCont::getNumLanes(const std::string& type) const {
169     return getType(type).numLanes;
170 }
171 
172 
173 double
getSpeed(const std::string & type) const174 NBTypeCont::getSpeed(const std::string& type) const {
175     return getType(type).speed;
176 }
177 
178 
179 int
getPriority(const std::string & type) const180 NBTypeCont::getPriority(const std::string& type) const {
181     return getType(type).priority;
182 }
183 
184 
185 bool
getIsOneWay(const std::string & type) const186 NBTypeCont::getIsOneWay(const std::string& type) const {
187     return getType(type).oneWay;
188 }
189 
190 
191 bool
getShallBeDiscarded(const std::string & type) const192 NBTypeCont::getShallBeDiscarded(const std::string& type) const {
193     return getType(type).discard;
194 }
195 
196 
197 bool
wasSet(const std::string & type,const SumoXMLAttr attr) const198 NBTypeCont::wasSet(const std::string& type, const SumoXMLAttr attr) const {
199     return getType(type).attrs.count(attr) > 0;
200 }
201 
202 
203 SVCPermissions
getPermissions(const std::string & type) const204 NBTypeCont::getPermissions(const std::string& type) const {
205     return getType(type).permissions;
206 }
207 
208 
209 double
getWidth(const std::string & type) const210 NBTypeCont::getWidth(const std::string& type) const {
211     return getType(type).width;
212 }
213 
214 
215 double
getSidewalkWidth(const std::string & type) const216 NBTypeCont::getSidewalkWidth(const std::string& type) const {
217     return getType(type).sidewalkWidth;
218 }
219 
220 
221 double
getBikeLaneWidth(const std::string & type) const222 NBTypeCont::getBikeLaneWidth(const std::string& type) const {
223     return getType(type).bikeLaneWidth;
224 }
225 
226 
227 const NBTypeCont::TypeDefinition&
getType(const std::string & name) const228 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 
238