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  std::ostringstream oss;
140  const std::vector<std::string> classNames = getAllowedVehicleClassNamesList(permissions);
141  bool hadOne = false;
142  for (std::vector<std::string>::const_iterator it = classNames.begin(); it != classNames.end(); it++) {
143  if (hadOne) {
144  oss << ' ';
145  }
146  oss << *it;
147  hadOne = true;
148  }
149  return oss.str();
150 }
151 
152 
153 std::vector<std::string>
156  const std::vector<std::string> classNames = SumoVehicleClassStrings.getStrings();
157  std::vector<std::string> result;
158  for (std::vector<std::string>::const_iterator it = classNames.begin(); it != classNames.end(); it++) {
159  const int svc = (int)SumoVehicleClassStrings.get(*it);
160  if ((svc & permissions) == svc && svc != SVC_UNKNOWN) {
161  result.push_back(*it);
162  }
163  }
164  return result;
165 }
166 
167 
168 std::pair<std::string, bool>
170  // shortcut the common cases
171  if (permissions == SVCFreeForAll) {
172  return std::pair<std::string, bool>("", false); // nothing disallowed
173  }
174  // figure out whether its shorter to write allow or disallow
175  size_t num_allowed = 0;
176  for(int mask = 1; mask < SUMOVehicleClass_MAX; mask = mask << 1) {
177  if ((mask & permissions) == mask) {
178  ++num_allowed;
179  }
180  }
181  if (num_allowed <= (SumoVehicleClassStrings.size() - num_allowed)) {
182  return std::pair<std::string, bool>(getAllowedVehicleClassNames(permissions), true);
183  } else {
184  return std::pair<std::string, bool>(getAllowedVehicleClassNames(~permissions), false);
185  }
186 }
187 
188 
190 getVehicleClassID(const std::string& name) {
191  if (SumoVehicleClassStrings.hasString(name)) {
192  return SumoVehicleClassStrings.get(name);
193  }
194  throw ProcessError("Unknown vehicle class '" + name + "'.");
195 }
196 
197 
198 int
199 getVehicleClassCompoundID(const std::string& name) {
200  int ret = SVC_UNKNOWN;
201  const std::vector<std::string> names = SumoVehicleClassStrings.getStrings();
202  for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); it++) {
203  if (name.find(*it) != std::string::npos) {
204  ret = ret | (int) SumoVehicleClassStrings.get(*it);
205  }
206  }
207  return ret;
208 }
209 
210 
212 parseVehicleClasses(const std::string& allowedS) {
213  SVCPermissions result = 0;
214  StringTokenizer sta(allowedS, " ");
215  while (sta.hasNext()) {
216  result |= getVehicleClassID(sta.next());
217  }
218  return result;
219 }
220 
221 
222 bool
223 canParseVehicleClasses(const std::string& classes) {
224  StringTokenizer sta(classes, " ");
225  while (sta.hasNext()) {
226  if (!SumoVehicleClassStrings.hasString(sta.next())) {
227  return false;
228  }
229  }
230  return true;
231 }
232 
233 
234 extern SVCPermissions parseVehicleClasses(const std::string& allowedS, const std::string& disallowedS) {
235  if (allowedS.size() == 0 && disallowedS.size() == 0) {
236  return SVCFreeForAll;
237  } else if (allowedS.size() > 0 && disallowedS.size() > 0) {
238  WRITE_WARNING("SVCPermissions must be specified either via 'allow' or 'disallow'. Ignoring 'disallow'");
239  return parseVehicleClasses(allowedS);
240  } else if (allowedS.size() > 0) {
241  return parseVehicleClasses(allowedS);
242  } else {
243  return ~parseVehicleClasses(disallowedS);
244  }
245 }
246 
247 
249 parseVehicleClasses(const std::vector<std::string> &allowedS) {
250  SVCPermissions result = 0;
251  for (std::vector<std::string>::const_iterator i = allowedS.begin(); i != allowedS.end(); ++i) {
252  result |= getVehicleClassID(*i);
253  }
254  return result;
255 }
256 
257 
259 getVehicleShapeID(const std::string& name) {
260  if (SumoVehicleShapeStrings.hasString(name)) {
261  return SumoVehicleShapeStrings.get(name);
262  } else {
263  throw ProcessError("Unknown vehicle shape '" + name + "'.");
264  }
265 }
266 
267 
268 std::string
270  return SumoVehicleShapeStrings.getString(id);
271 }
272 
273 
274 // ------------ Conversion of SUMOEmissionClass
276 getVehicleEmissionTypeID(const std::string& name) {
277  try {
278  if (name == "") {
279  return SVE_UNKNOWN;
280  } else if (name == "zero") {
281  return SVE_ZERO_EMISSIONS;
282  } else if (name.find("HDV_3_") == 0) {
283  return (SUMOEmissionClass)(SVE_HDV_3_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
284  } else if (name.find("HDV_6_") == 0) {
285  return (SUMOEmissionClass)(SVE_HDV_6_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
286  } else if (name.find("HDV_12_") == 0) {
287  return (SUMOEmissionClass)(SVE_HDV_12_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
288  } else if (name.find("P_7_") == 0) {
289  return (SUMOEmissionClass)(SVE_P_LDV_7_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
290  } else if (name.find("P_14_") == 0) {
291  return (SUMOEmissionClass)(SVE_P_LDV_14_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
292  } else if (name.find("HDV_A0_3_") == 0) {
293  return (SUMOEmissionClass)(SVE_HDV_A0_3_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
294  } else if (name.find("HDV_A0_6_") == 0) {
295  return (SUMOEmissionClass)(SVE_HDV_A0_6_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
296  } else if (name.find("HDV_A0_12_") == 0) {
297  return (SUMOEmissionClass)(SVE_HDV_A0_12_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
298  } else if (name.find("P_A0_7_") == 0) {
299  return (SUMOEmissionClass)(SVE_P_LDV_A0_7_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
300  } else if (name.find("P_A0_14_") == 0) {
301  return (SUMOEmissionClass)(SVE_P_LDV_A0_14_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
302  }
303  } catch (NumberFormatException&) {
304  }
305  throw ProcessError("Unknown emission type '" + name + "'.");
306 }
307 
308 
309 std::string
311  if (id == SVE_ZERO_EMISSIONS) {
312  return "zero";
313  }
314  if (id < 0) {
315  return "";
316  } else if (id < 3) {
317  return "HDV_3_" + toString(int(id));
318  } else if (id < 3 + 6) {
319  return "HDV_6_" + toString(int(id - 3));
320  } else if (id < 3 + 6 + 12) {
321  return "HDV_12_" + toString(int(id - 3 - 6));
322  } else if (id < 3 + 6 + 12 + 7) {
323  return "P_7_" + toString(int(id - 3 - 6 - 12));
324  } else if (id < 3 + 6 + 12 + 7 + 14) {
325  return "P_14_" + toString(int(id - 3 - 6 - 12 - 7));
326  }
327  return "";
328 }
329 
330 const std::string DEFAULT_VTYPE_ID("DEFAULT_VEHTYPE");
331 const SUMOReal DEFAULT_VEH_MAXSPEED(70.0);
332 const SUMOReal DEFAULT_VEH_ACCEL(2.6);
333 const SUMOReal DEFAULT_VEH_DECEL(4.5);
334 const SUMOReal DEFAULT_VEH_SIGMA(0.5);
335 const SUMOReal DEFAULT_VEH_LENGTH(5.);
336 const SUMOReal DEFAULT_VEH_MINGAP(2.5);
337 const SUMOReal DEFAULT_VEH_TAU(1.);
339 const SUMOReal DEFAULT_VEH_PROB(1.);
342 const SUMOReal DEFAULT_VEH_WIDTH(2.);
343 const SUMOReal DEFAULT_VEH_HEIGHT(1.5);
345 const std::string DEFAULT_VEH_LANE_CHANGE_MODEL("dkrajzew2008");
347 
348 /****************************************************************************/
349