SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SUMOVehicleClass.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Definitions of SUMO vehicle classes and helper functions
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
13 // Copyright (C) 2001-2012 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 "SUMOVehicleClass.h"
38 #include <utils/common/ToString.h>
41 
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // static members
50 // ===========================================================================
52  {"unknown", SVC_UNKNOWN},
53  {"private", SVC_PRIVATE},
54  {"public_transport", SVC_PUBLIC_TRANSPORT},
55  {"public_emergency", SVC_PUBLIC_EMERGENCY},
56  {"public_authority", SVC_PUBLIC_AUTHORITY},
57  {"public_army", SVC_PUBLIC_ARMY},
58  {"vip", SVC_VIP},
59  {"ignoring", SVC_IGNORING},
60  {"passenger", SVC_PASSENGER},
61  {"hov", SVC_HOV},
62  {"taxi", SVC_TAXI},
63  {"bus", SVC_BUS},
64  {"delivery", SVC_DELIVERY},
65  {"transport", SVC_TRANSPORT},
66  {"lightrail", SVC_LIGHTRAIL},
67  {"cityrail", SVC_CITYRAIL},
68  {"rail_slow", SVC_RAIL_SLOW},
69  {"rail_fast", SVC_RAIL_FAST},
70  {"motorcycle", SVC_MOTORCYCLE},
71  {"bicycle", SVC_BICYCLE},
72  {"pedestrian", SVC_PEDESTRIAN}
73 };
74 
76  sumoVehicleClassStringInitializer, SVC_PEDESTRIAN);
77 
78 
80  {"pedestrian", SVS_PEDESTRIAN},
81  {"bicycle", SVS_BICYCLE},
82  {"motorcycle", SVS_MOTORCYCLE},
83  {"passenger", SVS_PASSENGER},
84  {"passenger/sedan", SVS_PASSENGER_SEDAN},
85  {"passenger/hatchback", SVS_PASSENGER_HATCHBACK},
86  {"passenger/wagon", SVS_PASSENGER_WAGON},
87  {"passenger/van", SVS_PASSENGER_VAN},
88  {"delivery", SVS_DELIVERY},
89  {"transport", SVS_TRANSPORT},
90  {"transport/semitrailer", SVS_TRANSPORT_SEMITRAILER},
91  {"transport/trailer", SVS_TRANSPORT_1TRAILER},
92  {"bus", SVS_BUS},
93  {"bus/city", SVS_BUS_CITY},
94  {"bus/flexible", SVS_BUS_CITY_FLEXIBLE},
95  {"bus/overland", SVS_BUS_OVERLAND},
96  {"bus/trolley", SVS_BUS_TROLLEY},
97  {"rail", SVS_RAIL},
98  {"rail/light", SVS_RAIL_LIGHT},
99  {"rail/city", SVS_RAIL_CITY},
100  {"rail/slow", SVS_RAIL_SLOW},
101  {"rail/fast", SVS_RAIL_FAST},
102  {"rail/cargo", SVS_RAIL_CARGO},
103  {"evehicle", SVS_E_VEHICLE},
104  {"ant", SVS_ANT},
105  {"", SVS_UNKNOWN}
106 };
107 
108 
110  sumoVehicleShapeStringInitializer, SVS_UNKNOWN);
111 
114 
115 // ===========================================================================
116 // method definitions
117 // ===========================================================================
118 // ------------ Conversion of SUMOVehicleClass
119 
120 std::string
122  std::string ret;
123  const std::vector<std::string> names = SumoVehicleClassStrings.getStrings();
124  for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); it++) {
125  if ((id & SumoVehicleClassStrings.get(*it))) {
126  ret += ("|" + *it);
127  }
128  }
129  if (ret.length() > 0) {
130  return ret.substr(1);
131  } else {
132  return ret;
133  }
134 }
135 
136 
137 std::string
139  return joinToString(getAllowedVehicleClassNamesList(permissions), ' ');
140 }
141 
142 
143 std::vector<std::string>
146  const std::vector<std::string> classNames = SumoVehicleClassStrings.getStrings();
147  std::vector<std::string> result;
148  for (std::vector<std::string>::const_iterator it = classNames.begin(); it != classNames.end(); it++) {
149  const int svc = (int)SumoVehicleClassStrings.get(*it);
150  if ((svc & permissions) == svc && svc != SVC_UNKNOWN) {
151  result.push_back(*it);
152  }
153  }
154  return result;
155 }
156 
157 
158 std::pair<std::string, bool>
160  // shortcut the common cases
161  if (permissions == SVCFreeForAll) {
162  return std::pair<std::string, bool>("", false); // nothing disallowed
163  }
164  // figure out whether its shorter to write allow or disallow
165  size_t num_allowed = 0;
166  for (int mask = 1; mask <= SUMOVehicleClass_MAX; mask = mask << 1) {
167  if ((mask & permissions) == mask) {
168  ++num_allowed;
169  }
170  }
171  if (num_allowed <= (SumoVehicleClassStrings.size() - num_allowed) && num_allowed > 0) {
172  return std::pair<std::string, bool>(getAllowedVehicleClassNames(permissions), true);
173  } else {
174  return std::pair<std::string, bool>(getAllowedVehicleClassNames(~permissions), false);
175  }
176 }
177 
178 
180 getVehicleClassID(const std::string& name) {
181  if (SumoVehicleClassStrings.hasString(name)) {
182  return SumoVehicleClassStrings.get(name);
183  }
184  throw ProcessError("Unknown vehicle class '" + name + "'.");
185 }
186 
187 
188 int
189 getVehicleClassCompoundID(const std::string& name) {
190  int ret = SVC_UNKNOWN;
191  const std::vector<std::string> names = SumoVehicleClassStrings.getStrings();
192  for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); it++) {
193  if (name.find(*it) != std::string::npos) {
194  ret = ret | (int) SumoVehicleClassStrings.get(*it);
195  }
196  }
197  return ret;
198 }
199 
200 
202 parseVehicleClasses(const std::string& allowedS) {
203  SVCPermissions result = 0;
204  StringTokenizer sta(allowedS, " ");
205  while (sta.hasNext()) {
206  result |= getVehicleClassID(sta.next());
207  }
208  return result;
209 }
210 
211 
212 bool
213 canParseVehicleClasses(const std::string& classes) {
214  StringTokenizer sta(classes, " ");
215  while (sta.hasNext()) {
216  if (!SumoVehicleClassStrings.hasString(sta.next())) {
217  return false;
218  }
219  }
220  return true;
221 }
222 
223 
224 extern SVCPermissions parseVehicleClasses(const std::string& allowedS, const std::string& disallowedS) {
225  if (allowedS.size() == 0 && disallowedS.size() == 0) {
226  return SVCFreeForAll;
227  } else if (allowedS.size() > 0 && disallowedS.size() > 0) {
228  WRITE_WARNING("SVCPermissions must be specified either via 'allow' or 'disallow'. Ignoring 'disallow'");
229  return parseVehicleClasses(allowedS);
230  } else if (allowedS.size() > 0) {
231  return parseVehicleClasses(allowedS);
232  } else {
233  return ~parseVehicleClasses(disallowedS);
234  }
235 }
236 
237 
239 parseVehicleClasses(const std::vector<std::string>& allowedS) {
240  SVCPermissions result = 0;
241  for (std::vector<std::string>::const_iterator i = allowedS.begin(); i != allowedS.end(); ++i) {
242  result |= getVehicleClassID(*i);
243  }
244  return result;
245 }
246 
247 
249 getVehicleShapeID(const std::string& name) {
250  if (SumoVehicleShapeStrings.hasString(name)) {
251  return SumoVehicleShapeStrings.get(name);
252  } else {
253  throw ProcessError("Unknown vehicle shape '" + name + "'.");
254  }
255 }
256 
257 
258 std::string
260  return SumoVehicleShapeStrings.getString(id);
261 }
262 
263 
264 bool isRailway(SVCPermissions permissions) {
265  const int anyRail = SVC_RAIL_FAST + SVC_RAIL_SLOW + SVC_CITYRAIL + SVC_LIGHTRAIL;
266  return (permissions & anyRail) > 0 && (permissions & SVC_PASSENGER) == 0;
267 }
268 
269 // ------------ Conversion of SUMOEmissionClass
271 getVehicleEmissionTypeID(const std::string& name) {
272  try {
273  if (name == "") {
274  return SVE_UNKNOWN;
275  } else if (name == "zero") {
276  return SVE_ZERO_EMISSIONS;
277  } else if (name.find("HDV_3_") == 0) {
278  return (SUMOEmissionClass)(SVE_HDV_3_1 - 1 + TplConvert::_2int(name.substr(name.rfind("_") + 1).c_str()));
279  } else if (name.find("HDV_6_") == 0) {
280  return (SUMOEmissionClass)(SVE_HDV_6_1 - 1 + TplConvert::_2int(name.substr(name.rfind("_") + 1).c_str()));
281  } else if (name.find("HDV_12_") == 0) {
282  return (SUMOEmissionClass)(SVE_HDV_12_1 - 1 + TplConvert::_2int(name.substr(name.rfind("_") + 1).c_str()));
283  } else if (name.find("P_7_") == 0) {
284  return (SUMOEmissionClass)(SVE_P_LDV_7_1 - 1 + TplConvert::_2int(name.substr(name.rfind("_") + 1).c_str()));
285  } else if (name.find("P_14_") == 0) {
286  return (SUMOEmissionClass)(SVE_P_LDV_14_1 - 1 + TplConvert::_2int(name.substr(name.rfind("_") + 1).c_str()));
287  } else if (name.find("HDV_A0_3_") == 0) {
288  return (SUMOEmissionClass)(SVE_HDV_A0_3_1 - 1 + TplConvert::_2int(name.substr(name.rfind("_") + 1).c_str()));
289  } else if (name.find("HDV_A0_6_") == 0) {
290  return (SUMOEmissionClass)(SVE_HDV_A0_6_1 - 1 + TplConvert::_2int(name.substr(name.rfind("_") + 1).c_str()));
291  } else if (name.find("HDV_A0_12_") == 0) {
292  return (SUMOEmissionClass)(SVE_HDV_A0_12_1 - 1 + TplConvert::_2int(name.substr(name.rfind("_") + 1).c_str()));
293  } else if (name.find("P_A0_7_") == 0) {
294  return (SUMOEmissionClass)(SVE_P_LDV_A0_7_1 - 1 + TplConvert::_2int(name.substr(name.rfind("_") + 1).c_str()));
295  } else if (name.find("P_A0_14_") == 0) {
296  return (SUMOEmissionClass)(SVE_P_LDV_A0_14_1 - 1 + TplConvert::_2int(name.substr(name.rfind("_") + 1).c_str()));
297  }
298  } catch (NumberFormatException&) {
299  }
300  throw ProcessError("Unknown emission type '" + name + "'.");
301 }
302 
303 
304 std::string
306  if (id == SVE_ZERO_EMISSIONS) {
307  return "zero";
308  }
309  if (id < 0) {
310  return "";
311  } else if (id < 3) {
312  return "HDV_3_" + toString(int(id));
313  } else if (id < 3 + 6) {
314  return "HDV_6_" + toString(int(id - 3));
315  } else if (id < 3 + 6 + 12) {
316  return "HDV_12_" + toString(int(id - 3 - 6));
317  } else if (id < 3 + 6 + 12 + 7) {
318  return "P_7_" + toString(int(id - 3 - 6 - 12));
319  } else if (id < 3 + 6 + 12 + 7 + 14) {
320  return "P_14_" + toString(int(id - 3 - 6 - 12 - 7));
321  }
322  return "";
323 }
324 
325 const std::string DEFAULT_VTYPE_ID("DEFAULT_VEHTYPE");
326 const SUMOReal DEFAULT_VEH_MAXSPEED(70.0);
327 const SUMOReal DEFAULT_VEH_ACCEL(2.6);
328 const SUMOReal DEFAULT_VEH_DECEL(4.5);
329 const SUMOReal DEFAULT_VEH_SIGMA(0.5);
330 const SUMOReal DEFAULT_VEH_LENGTH(5.);
331 const SUMOReal DEFAULT_VEH_MINGAP(2.5);
332 const SUMOReal DEFAULT_VEH_TAU(1.);
334 const SUMOReal DEFAULT_VEH_PROB(1.);
337 const SUMOReal DEFAULT_VEH_WIDTH(2.);
338 const SUMOReal DEFAULT_VEH_HEIGHT(1.5);
340 const std::string DEFAULT_VEH_LANE_CHANGE_MODEL("dkrajzew2008");
342 const SUMOReal DEFAULT_VEH_TMP1(1.);
343 const SUMOReal DEFAULT_VEH_TMP2(1.);
344 const SUMOReal DEFAULT_VEH_TMP3(1.);
345 const SUMOReal DEFAULT_VEH_TMP4(1.);
346 const SUMOReal DEFAULT_VEH_TMP5(1.);
347 
348 const SUMOReal DEFAULT_PERSON_SPEED(5. / 3.6);
349 
350 /****************************************************************************/
351