SUMO - Simulation of Urban MObility
SUMOVehicleClass.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00010 // Definitions of SUMO vehicle classes and helper functions
00011 /****************************************************************************/
00012 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00013 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00014 /****************************************************************************/
00015 //
00016 //   This file is part of SUMO.
00017 //   SUMO is free software: you can redistribute it and/or modify
00018 //   it under the terms of the GNU General Public License as published by
00019 //   the Free Software Foundation, either version 3 of the License, or
00020 //   (at your option) any later version.
00021 //
00022 /****************************************************************************/
00023 
00024 
00025 // ===========================================================================
00026 // included modules
00027 // ===========================================================================
00028 #ifdef _MSC_VER
00029 #include <windows_config.h>
00030 #else
00031 #include <config.h>
00032 #endif
00033 
00034 #include <string>
00035 #include <map>
00036 #include "SUMOVehicleClass.h"
00037 #include <utils/common/TplConvert.h>
00038 #include <utils/common/ToString.h>
00039 #include <utils/common/MsgHandler.h>
00040 #include <utils/common/StringTokenizer.h>
00041 
00042 
00043 #ifdef CHECK_MEMORY_LEAKS
00044 #include <foreign/nvwa/debug_new.h>
00045 #endif // CHECK_MEMORY_LEAKS
00046 
00047 
00048 // ===========================================================================
00049 // static members
00050 // ===========================================================================
00051 StringBijection<SUMOVehicleClass>::Entry sumoVehicleClassStringInitializer[] = {
00052     {"unknown",           SVC_UNKNOWN},
00053     {"private",           SVC_PRIVATE},
00054     {"public_transport",  SVC_PUBLIC_TRANSPORT},
00055     {"public_emergency",  SVC_PUBLIC_EMERGENCY},
00056     {"public_authority",  SVC_PUBLIC_AUTHORITY},
00057     {"public_army",       SVC_PUBLIC_ARMY},
00058     {"vip",               SVC_VIP},
00059     {"ignoring",          SVC_IGNORING},
00060     {"passenger",         SVC_PASSENGER},
00061     {"hov",               SVC_HOV},
00062     {"taxi",              SVC_TAXI},
00063     {"bus",               SVC_BUS},
00064     {"delivery",          SVC_DELIVERY},
00065     {"transport",         SVC_TRANSPORT},
00066     {"lightrail",         SVC_LIGHTRAIL},
00067     {"cityrail",          SVC_CITYRAIL},
00068     {"rail_slow",         SVC_RAIL_SLOW},
00069     {"rail_fast",         SVC_RAIL_FAST},
00070     {"motorcycle",        SVC_MOTORCYCLE},
00071     {"bicycle",           SVC_BICYCLE},
00072     {"pedestrian",        SVC_PEDESTRIAN}
00073 };
00074 
00075 StringBijection<SUMOVehicleClass> SumoVehicleClassStrings(
00076     sumoVehicleClassStringInitializer, SVC_PEDESTRIAN);
00077 
00078 
00079 StringBijection<SUMOVehicleShape>::Entry sumoVehicleShapeStringInitializer[] = {
00080     {"pedestrian",            SVS_PEDESTRIAN},
00081     {"bicycle",               SVS_BICYCLE},
00082     {"motorcycle",            SVS_MOTORCYCLE},
00083     {"passenger",             SVS_PASSENGER},
00084     {"passenger/sedan",       SVS_PASSENGER_SEDAN},
00085     {"passenger/hatchback",   SVS_PASSENGER_HATCHBACK},
00086     {"passenger/wagon",       SVS_PASSENGER_WAGON},
00087     {"passenger/van",         SVS_PASSENGER_VAN},
00088     {"delivery",              SVS_DELIVERY},
00089     {"transport",             SVS_TRANSPORT},
00090     {"transport/semitrailer", SVS_TRANSPORT_SEMITRAILER},
00091     {"transport/trailer",     SVS_TRANSPORT_1TRAILER},
00092     {"bus",                   SVS_BUS},
00093     {"bus/city",              SVS_BUS_CITY},
00094     {"bus/flexible",          SVS_BUS_CITY_FLEXIBLE},
00095     {"bus/overland",          SVS_BUS_OVERLAND},
00096     {"bus/trolley",           SVS_BUS_TROLLEY},
00097     {"rail",                  SVS_RAIL},
00098     {"rail/light",            SVS_RAIL_LIGHT},
00099     {"rail/city",             SVS_RAIL_CITY},
00100     {"rail/slow",             SVS_RAIL_SLOW},
00101     {"rail/fast",             SVS_RAIL_FAST},
00102     {"rail/cargo",            SVS_RAIL_CARGO},
00103     {"evehicle",              SVS_E_VEHICLE},
00104     {"ant",                   SVS_ANT},
00105     {"",                      SVS_UNKNOWN}
00106 };
00107 
00108 
00109 StringBijection<SUMOVehicleShape> SumoVehicleShapeStrings(
00110     sumoVehicleShapeStringInitializer, SVS_UNKNOWN);
00111 
00112 const int SUMOVehicleClass_MAX = SVC_PEDESTRIAN;
00113 const SVCPermissions SVCFreeForAll = std::numeric_limits<SVCPermissions>::max(); // all bits set to 1
00114 
00115 // ===========================================================================
00116 // method definitions
00117 // ===========================================================================
00118 // ------------ Conversion of SUMOVehicleClass
00119 
00120 std::string
00121 getVehicleClassCompoundName(int id) {
00122     std::string ret;
00123     const std::vector<std::string> names = SumoVehicleClassStrings.getStrings();
00124     for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); it++) {
00125         if ((id & SumoVehicleClassStrings.get(*it))) {
00126             ret += ("|" + *it);
00127         }
00128     }
00129     if (ret.length() > 0) {
00130         return ret.substr(1);
00131     } else {
00132         return ret;
00133     }
00134 }
00135 
00136 
00137 std::string 
00138 getAllowedVehicleClassNames(SVCPermissions permissions) {
00139     std::ostringstream oss;
00140     const std::vector<std::string> classNames = getAllowedVehicleClassNamesList(permissions);
00141     bool hadOne = false;
00142     for (std::vector<std::string>::const_iterator it = classNames.begin(); it != classNames.end(); it++) {
00143         if (hadOne) {
00144             oss << ' ';
00145         }
00146         oss << *it;
00147         hadOne = true;
00148     }
00149     return oss.str();
00150 }
00151 
00152 
00153 std::vector<std::string>
00154 getAllowedVehicleClassNamesList(SVCPermissions permissions) {
00156     const std::vector<std::string> classNames = SumoVehicleClassStrings.getStrings();
00157     std::vector<std::string> result;
00158     for (std::vector<std::string>::const_iterator it = classNames.begin(); it != classNames.end(); it++) {
00159         const int svc = (int)SumoVehicleClassStrings.get(*it);
00160         if ((svc & permissions) == svc && svc != SVC_UNKNOWN) {
00161             result.push_back(*it);
00162         }
00163     }
00164     return result;
00165 }
00166 
00167 
00168 std::pair<std::string, bool> 
00169 getPermissionEncoding(SVCPermissions permissions) {
00170     // shortcut the common cases
00171     if (permissions == SVCFreeForAll) {
00172         return std::pair<std::string, bool>("", false); // nothing disallowed
00173     }
00174     // figure out whether its shorter to write allow or disallow
00175     size_t num_allowed = 0;
00176     for(int mask = 1; mask < SUMOVehicleClass_MAX; mask = mask << 1) {
00177         if ((mask & permissions) == mask) {
00178             ++num_allowed;
00179         }
00180     }
00181     if (num_allowed <= (SumoVehicleClassStrings.size() - num_allowed)) {
00182         return std::pair<std::string, bool>(getAllowedVehicleClassNames(permissions), true);
00183     } else {
00184         return std::pair<std::string, bool>(getAllowedVehicleClassNames(~permissions), false);
00185     }
00186 }
00187 
00188 
00189 SUMOVehicleClass
00190 getVehicleClassID(const std::string& name) {
00191     if (SumoVehicleClassStrings.hasString(name)) {
00192         return SumoVehicleClassStrings.get(name);
00193     }
00194     throw ProcessError("Unknown vehicle class '" + name + "'.");
00195 }
00196 
00197 
00198 int
00199 getVehicleClassCompoundID(const std::string& name) {
00200     int ret = SVC_UNKNOWN;
00201     const std::vector<std::string> names = SumoVehicleClassStrings.getStrings();
00202     for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); it++) {
00203         if (name.find(*it) != std::string::npos) {
00204             ret = ret | (int) SumoVehicleClassStrings.get(*it);
00205         }
00206     }
00207     return ret;
00208 }
00209 
00210 
00211 SVCPermissions 
00212 parseVehicleClasses(const std::string& allowedS) {
00213     SVCPermissions result = 0;
00214     StringTokenizer sta(allowedS, " ");
00215     while (sta.hasNext()) {
00216         result |= getVehicleClassID(sta.next());
00217     }
00218     return result;
00219 }
00220 
00221 
00222 bool 
00223 canParseVehicleClasses(const std::string& classes) {
00224     StringTokenizer sta(classes, " ");
00225     while (sta.hasNext()) {
00226         if (!SumoVehicleClassStrings.hasString(sta.next())) {
00227             return false;
00228         }
00229     }
00230     return true;
00231 }
00232 
00233 
00234 extern SVCPermissions parseVehicleClasses(const std::string& allowedS, const std::string& disallowedS) {
00235     if (allowedS.size() == 0 && disallowedS.size() == 0) {
00236         return SVCFreeForAll;
00237     } else if (allowedS.size() > 0 && disallowedS.size() > 0) {
00238         WRITE_WARNING("SVCPermissions must be specified either via 'allow' or 'disallow'. Ignoring 'disallow'");
00239         return parseVehicleClasses(allowedS);
00240     } else if (allowedS.size() > 0) {
00241         return parseVehicleClasses(allowedS);
00242     } else {
00243         return ~parseVehicleClasses(disallowedS);
00244     }
00245 }
00246 
00247 
00248 SVCPermissions 
00249 parseVehicleClasses(const std::vector<std::string> &allowedS) {
00250     SVCPermissions result = 0;
00251     for (std::vector<std::string>::const_iterator i = allowedS.begin(); i != allowedS.end(); ++i) {
00252         result |= getVehicleClassID(*i);
00253     }
00254     return result;
00255 }
00256 
00257 
00258 SUMOVehicleShape
00259 getVehicleShapeID(const std::string& name) {
00260     if (SumoVehicleShapeStrings.hasString(name)) {
00261         return SumoVehicleShapeStrings.get(name);
00262     } else {
00263         throw ProcessError("Unknown vehicle shape '" + name + "'.");
00264     }
00265 }
00266 
00267 
00268 std::string
00269 getVehicleShapeName(SUMOVehicleShape id) {
00270     return SumoVehicleShapeStrings.getString(id);
00271 }
00272 
00273 
00274 // ------------ Conversion of SUMOEmissionClass
00275 SUMOEmissionClass
00276 getVehicleEmissionTypeID(const std::string& name) {
00277     try {
00278         if (name == "") {
00279             return SVE_UNKNOWN;
00280         } else if (name == "zero") {
00281             return SVE_ZERO_EMISSIONS;
00282         } else if (name.find("HDV_3_") == 0) {
00283             return (SUMOEmissionClass)(SVE_HDV_3_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
00284         } else if (name.find("HDV_6_") == 0) {
00285             return (SUMOEmissionClass)(SVE_HDV_6_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
00286         } else if (name.find("HDV_12_") == 0) {
00287             return (SUMOEmissionClass)(SVE_HDV_12_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
00288         } else if (name.find("P_7_") == 0) {
00289             return (SUMOEmissionClass)(SVE_P_LDV_7_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
00290         } else if (name.find("P_14_") == 0) {
00291             return (SUMOEmissionClass)(SVE_P_LDV_14_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
00292         } else if (name.find("HDV_A0_3_") == 0) {
00293             return (SUMOEmissionClass)(SVE_HDV_A0_3_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
00294         } else if (name.find("HDV_A0_6_") == 0) {
00295             return (SUMOEmissionClass)(SVE_HDV_A0_6_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
00296         } else if (name.find("HDV_A0_12_") == 0) {
00297             return (SUMOEmissionClass)(SVE_HDV_A0_12_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
00298         } else if (name.find("P_A0_7_") == 0) {
00299             return (SUMOEmissionClass)(SVE_P_LDV_A0_7_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
00300         } else if (name.find("P_A0_14_") == 0) {
00301             return (SUMOEmissionClass)(SVE_P_LDV_A0_14_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_") + 1).c_str()));
00302         }
00303     } catch (NumberFormatException&) {
00304     }
00305     throw ProcessError("Unknown emission type '" + name + "'.");
00306 }
00307 
00308 
00309 std::string
00310 getVehicleEmissionTypeName(SUMOEmissionClass id) {
00311     if (id == SVE_ZERO_EMISSIONS) {
00312         return "zero";
00313     }
00314     if (id < 0) {
00315         return "";
00316     } else if (id < 3) {
00317         return "HDV_3_" + toString(int(id));
00318     } else if (id < 3 + 6) {
00319         return "HDV_6_" + toString(int(id - 3));
00320     } else if (id < 3 + 6 + 12) {
00321         return "HDV_12_" + toString(int(id - 3 - 6));
00322     } else if (id < 3 + 6 + 12 + 7) {
00323         return "P_7_" + toString(int(id - 3 - 6 - 12));
00324     } else if (id < 3 + 6 + 12 + 7 + 14) {
00325         return "P_14_" + toString(int(id - 3 - 6 - 12 - 7));
00326     }
00327     return "";
00328 }
00329 
00330 const std::string DEFAULT_VTYPE_ID("DEFAULT_VEHTYPE");
00331 const SUMOReal DEFAULT_VEH_MAXSPEED(70.0);
00332 const SUMOReal DEFAULT_VEH_ACCEL(2.6);
00333 const SUMOReal DEFAULT_VEH_DECEL(4.5);
00334 const SUMOReal DEFAULT_VEH_SIGMA(0.5);
00335 const SUMOReal DEFAULT_VEH_LENGTH(5.);
00336 const SUMOReal DEFAULT_VEH_MINGAP(2.5);
00337 const SUMOReal DEFAULT_VEH_TAU(1.);
00338 const SUMOVehicleClass DEFAULT_VEH_CLASS(SVC_UNKNOWN);
00339 const SUMOReal DEFAULT_VEH_PROB(1.);
00340 const SUMOReal DEFAULT_VEH_SPEEDFACTOR(1.);
00341 const SUMOReal DEFAULT_VEH_SPEEDDEV(0.);
00342 const SUMOReal DEFAULT_VEH_WIDTH(2.);
00343 const SUMOReal DEFAULT_VEH_HEIGHT(1.5);
00344 const SumoXMLTag DEFAULT_VEH_FOLLOW_MODEL(SUMO_TAG_CF_KRAUSS);
00345 const std::string DEFAULT_VEH_LANE_CHANGE_MODEL("dkrajzew2008");
00346 const SUMOVehicleShape DEFAULT_VEH_SHAPE(SVS_UNKNOWN);
00347 
00348 /****************************************************************************/
00349 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines