SUMOVehicleClass.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Definitions of SUMO vehicle classes and helper functions
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #include <string>
00031 #include <map>
00032 #include "SUMOVehicleClass.h"
00033 #include <utils/common/TplConvert.h>
00034 #include <utils/common/ToString.h>
00035 #include <utils/common/MsgHandler.h>
00036 #include <utils/common/StringTokenizer.h>
00037 
00038 
00039 #ifdef CHECK_MEMORY_LEAKS
00040 #include <foreign/nvwa/debug_new.h>
00041 #endif // CHECK_MEMORY_LEAKS
00042 
00043 
00044 // ===========================================================================
00045 // definitions of string representations
00046 // ===========================================================================
00047 // ---------------------------------------------------------------------------
00048 // string representations of SUMOVehicleClass
00049 // ---------------------------------------------------------------------------
00050 std::string sSVC_PRIVATE("private");
00051 std::string sSVC_PUBLIC_TRANSPORT("public_transport");
00052 std::string sSVC_PUBLIC_EMERGENCY("public_emergency");
00053 std::string sSVC_PUBLIC_AUTHORITY("public_authority");
00054 std::string sSVC_PUBLIC_ARMY("public_army");
00055 std::string sSVC_VIP("vip");
00056 std::string sSVC_IGNORING("ignoring");
00057 
00058 std::string sSVC_PASSENGER("passenger");
00059 std::string sSVC_HOV("hov");
00060 std::string sSVC_TAXI("taxi");
00061 std::string sSVC_BUS("bus");
00062 std::string sSVC_DELIVERY("delivery");
00063 std::string sSVC_TRANSPORT("transport");
00064 std::string sSVC_LIGHTRAIL("lightrail");
00065 std::string sSVC_CITYRAIL("cityrail");
00066 std::string sSVC_RAIL_SLOW("rail_slow");
00067 std::string sSVC_RAIL_FAST("rail_fast");
00068 std::string sSVC_MOTORCYCLE("motorcycle");
00069 std::string sSVC_BICYCLE("bicycle");
00070 std::string sSVC_PEDESTRIAN("pedestrian");
00071 
00072 
00073 // ===========================================================================
00074 // static members
00075 // ===========================================================================
00076 std::map<SUMOVehicleShape, std::string> gVehicleShapeID2Name;
00077 std::map<std::string, SUMOVehicleShape> gVehicleShapeName2ID;
00078 
00079 
00080 // ===========================================================================
00081 // method definitions
00082 // ===========================================================================
00083 // ------------ Conversion of SUMOVehicleClass
00084 std::string
00085 getVehicleClassName(SUMOVehicleClass id) throw() {
00086     std::string ret;
00087     if ((id&SVC_PRIVATE)!=0) {
00088         ret += ("|" + sSVC_PRIVATE);
00089     }
00090     if ((id&SVC_PUBLIC_TRANSPORT)!=0) {
00091         ret += ("|" + sSVC_PUBLIC_TRANSPORT);
00092     }
00093     if ((id&SVC_PUBLIC_EMERGENCY)!=0) {
00094         ret += ("|" + sSVC_PUBLIC_EMERGENCY);
00095     }
00096     if ((id&SVC_PUBLIC_AUTHORITY)!=0) {
00097         ret += ("|" + sSVC_PUBLIC_AUTHORITY);
00098     }
00099     if ((id&SVC_PUBLIC_ARMY)!=0) {
00100         ret += ("|" + sSVC_PUBLIC_ARMY);
00101     }
00102     if ((id&SVC_VIP)!=0) {
00103         ret += ("|" + sSVC_VIP);
00104     }
00105     if ((id&SVC_IGNORING)!=0) {
00106         ret += ("|" + sSVC_IGNORING);
00107     }
00108 
00109     if ((id&SVC_PASSENGER)!=0) {
00110         ret += ("|" + sSVC_PASSENGER);
00111     }
00112     if ((id&SVC_HOV)!=0) {
00113         ret += ("|" + sSVC_HOV);
00114     }
00115     if ((id&SVC_TAXI)!=0) {
00116         ret += ("|" + sSVC_TAXI);
00117     }
00118     if ((id&SVC_BUS)!=0) {
00119         ret += ("|" + sSVC_BUS);
00120     }
00121     if ((id&SVC_DELIVERY)!=0) {
00122         ret += ("|" + sSVC_DELIVERY);
00123     }
00124     if ((id&SVC_TRANSPORT)!=0) {
00125         ret += ("|" + sSVC_TRANSPORT);
00126     }
00127     if ((id&SVC_LIGHTRAIL)!=0) {
00128         ret += ("|" + sSVC_LIGHTRAIL);
00129     }
00130     if ((id&SVC_CITYRAIL)!=0) {
00131         ret += ("|" + sSVC_CITYRAIL);
00132     }
00133     if ((id&SVC_RAIL_SLOW)!=0) {
00134         ret += ("|" + sSVC_RAIL_SLOW);
00135     }
00136     if ((id&SVC_RAIL_FAST)!=0) {
00137         ret += ("|" + sSVC_RAIL_FAST);
00138     }
00139     if ((id&SVC_MOTORCYCLE)!=0) {
00140         ret += ("|" + sSVC_MOTORCYCLE);
00141     }
00142     if ((id&SVC_BICYCLE)!=0) {
00143         ret += ("|" + sSVC_BICYCLE);
00144     }
00145     if ((id&SVC_PEDESTRIAN)!=0) {
00146         ret += ("|" + sSVC_PEDESTRIAN);
00147     }
00148 
00149     if (ret.length()>0) {
00150         return ret.substr(1);
00151     } else {
00152         return ret;
00153     }
00154 }
00155 
00156 
00157 SUMOVehicleClass
00158 getVehicleClassID(const std::string &name) throw() {
00159     SUMOVehicleClass ret = SVC_UNKNOWN;
00160     if (name.find(sSVC_PRIVATE)!=std::string::npos) {
00161         ret = (SUMOVehicleClass)((int) ret | (int) SVC_PRIVATE);
00162     }
00163     if (name.find(sSVC_PUBLIC_TRANSPORT)!=std::string::npos) {
00164         ret = (SUMOVehicleClass)((int) ret | (int) SVC_PUBLIC_TRANSPORT);
00165     }
00166     if (name.find(sSVC_PUBLIC_EMERGENCY)!=std::string::npos) {
00167         ret = (SUMOVehicleClass)((int) ret | (int) SVC_PUBLIC_EMERGENCY);
00168     }
00169     if (name.find(sSVC_PUBLIC_AUTHORITY)!=std::string::npos) {
00170         ret = (SUMOVehicleClass)((int) ret | (int) SVC_PUBLIC_AUTHORITY);
00171     }
00172     if (name.find(sSVC_PUBLIC_ARMY)!=std::string::npos) {
00173         ret = (SUMOVehicleClass)((int) ret | (int) SVC_PUBLIC_ARMY);
00174     }
00175     if (name.find(sSVC_VIP)!=std::string::npos) {
00176         ret = (SUMOVehicleClass)((int) ret | (int) SVC_VIP);
00177     }
00178     if (name.find(sSVC_IGNORING)!=std::string::npos) {
00179         ret = (SUMOVehicleClass)((int) ret | (int) SVC_IGNORING);
00180     }
00181 
00182 
00183     if (name.find(sSVC_PASSENGER)!=std::string::npos) {
00184         ret = (SUMOVehicleClass)((int) ret | (int) SVC_PASSENGER);
00185     }
00186     if (name.find(sSVC_HOV)!=std::string::npos) {
00187         ret = (SUMOVehicleClass)((int) ret | (int) SVC_HOV);
00188     }
00189     if (name.find(sSVC_TAXI)!=std::string::npos) {
00190         ret = (SUMOVehicleClass)((int) ret | (int) SVC_TAXI);
00191     }
00192     if (name.find(sSVC_BUS)!=std::string::npos) {
00193         ret = (SUMOVehicleClass)((int) ret | (int) SVC_BUS);
00194     }
00195     if (name.find(sSVC_DELIVERY)!=std::string::npos) {
00196         ret = (SUMOVehicleClass)((int) ret | (int) SVC_DELIVERY);
00197     }
00198     if (name.find(sSVC_TRANSPORT)!=std::string::npos) {
00199         ret = (SUMOVehicleClass)((int) ret | (int) SVC_TRANSPORT);
00200     }
00201     if (name.find(sSVC_LIGHTRAIL)!=std::string::npos) {
00202         ret = (SUMOVehicleClass)((int) ret | (int) SVC_LIGHTRAIL);
00203     }
00204     if (name.find(sSVC_CITYRAIL)!=std::string::npos) {
00205         ret = (SUMOVehicleClass)((int) ret | (int) SVC_CITYRAIL);
00206     }
00207     if (name.find(sSVC_RAIL_SLOW)!=std::string::npos) {
00208         ret = (SUMOVehicleClass)((int) ret | (int) SVC_RAIL_SLOW);
00209     }
00210     if (name.find(sSVC_RAIL_FAST)!=std::string::npos) {
00211         ret = (SUMOVehicleClass)((int) ret | (int) SVC_RAIL_FAST);
00212     }
00213     if (name.find(sSVC_MOTORCYCLE)!=std::string::npos) {
00214         ret = (SUMOVehicleClass)((int) ret | (int) SVC_MOTORCYCLE);
00215     }
00216     if (name.find(sSVC_BICYCLE)!=std::string::npos) {
00217         ret = (SUMOVehicleClass)((int) ret | (int) SVC_BICYCLE);
00218     }
00219     if (name.find(sSVC_PEDESTRIAN)!=std::string::npos) {
00220         ret = (SUMOVehicleClass)((int) ret | (int) SVC_PEDESTRIAN);
00221     }
00222 
00223     return ret;
00224 }
00225 
00226 
00227 void
00228 parseVehicleClasses(const std::string &classesS,
00229                     const std::string &allowedS,
00230                     const std::string &disallowedS,
00231                     std::vector<SUMOVehicleClass> &allowed,
00232                     std::vector<SUMOVehicleClass> &disallowed,
00233                     bool &warnedAboutDeprecatedVClass) throw() {
00234     if (classesS.length()!=0) {
00235         if (!warnedAboutDeprecatedVClass) {
00236             MsgHandler::getWarningInstance()->inform("The vclasses attribute is deprecated. Please rebuilt your network.");
00237             warnedAboutDeprecatedVClass = true;
00238         }
00239         StringTokenizer st(classesS, ";");
00240         while (st.hasNext()) {
00241             std::string next = st.next();
00242             if (next[0]=='-') {
00243                 disallowed.push_back(getVehicleClassID(next.substr(1)));
00244             } else {
00245                 allowed.push_back(getVehicleClassID(next));
00246             }
00247         }
00248     }
00249     StringTokenizer sta(allowedS, " ");
00250     while (sta.hasNext()) {
00251         allowed.push_back(getVehicleClassID(sta.next()));
00252     }
00253     StringTokenizer std(disallowedS, " ");
00254     while (std.hasNext()) {
00255         disallowed.push_back(getVehicleClassID(std.next()));
00256     }
00257 }
00258 
00259 
00260 void
00261 parseVehicleClasses(const std::vector<std::string> &classesS,
00262                     std::vector<SUMOVehicleClass> &classes) throw() {
00263     for (std::vector<std::string>::const_iterator i=classesS.begin(); i!=classesS.end(); ++i) {
00264         classes.push_back(getVehicleClassID(*i));
00265     }
00266 }
00267 
00268 
00269 
00270 
00271 
00272 // ------------ Conversion of SUMOVehicleShape
00273 void
00274 addToShapeNames(SUMOVehicleShape id, const std::string &name) throw() {
00275     gVehicleShapeID2Name[id] = name;
00276     gVehicleShapeName2ID[name] = id;
00277 }
00278 
00279 
00280 void
00281 initGuiShapeNames() throw() {
00282     addToShapeNames(SVS_PEDESTRIAN, "pedestrian");
00283     addToShapeNames(SVS_BICYCLE, "bicycle");
00284     addToShapeNames(SVS_MOTORCYCLE, "motorcycle");
00285     addToShapeNames(SVS_PASSENGER, "passenger");
00286     addToShapeNames(SVS_PASSENGER_SEDAN, "passenger/sedan");
00287     addToShapeNames(SVS_PASSENGER_HATCHBACK, "passenger/hatchback");
00288     addToShapeNames(SVS_PASSENGER_WAGON, "passenger/wagon");
00289     addToShapeNames(SVS_PASSENGER_VAN, "passenger/van");
00290     addToShapeNames(SVS_DELIVERY, "delivery");
00291     addToShapeNames(SVS_TRANSPORT, "transport");
00292     addToShapeNames(SVS_TRANSPORT_SEMITRAILER, "transport/semitrailer");
00293     addToShapeNames(SVS_TRANSPORT_1TRAILER, "transport/trailer");
00294     addToShapeNames(SVS_BUS, "bus");
00295     addToShapeNames(SVS_BUS_CITY, "bus/city");
00296     addToShapeNames(SVS_BUS_CITY_FLEXIBLE, "bus/flexible");
00297     addToShapeNames(SVS_BUS_OVERLAND, "bus/overland");
00298     addToShapeNames(SVS_RAIL, "rail");
00299     addToShapeNames(SVS_RAIL_LIGHT, "rail/light");
00300     addToShapeNames(SVS_RAIL_CITY, "rail/city");
00301     addToShapeNames(SVS_RAIL_SLOW, "rail/slow");
00302     addToShapeNames(SVS_RAIL_FAST, "rail/fast");
00303     addToShapeNames(SVS_RAIL_CARGO, "rail/cargo");
00304     addToShapeNames(SVS_E_VEHICLE, "evehicle");
00305 }
00306 
00307 
00308 std::string
00309 getVehicleShapeName(SUMOVehicleShape id) throw() {
00310     if (id==SVS_UNKNOWN) {
00311         return "";
00312     }
00313     return gVehicleShapeID2Name[id];
00314 }
00315 
00316 
00317 SUMOVehicleShape
00318 getVehicleShapeID(const std::string &name) throw() {
00319     if (name=="") {
00320         return SVS_UNKNOWN;
00321     }
00322     if (gVehicleShapeName2ID.find(name)!=gVehicleShapeName2ID.end()) {
00323         return gVehicleShapeName2ID[name];
00324     }
00325     return SVS_UNKNOWN;
00327 }
00328 
00329 
00330 
00331 // ------------ Conversion of SUMOEmissionClass
00332 SUMOEmissionClass
00333 getVehicleEmissionTypeID(const std::string &name) throw(ProcessError) {
00334     try {
00335         if (name=="") {
00336             return SVE_UNKNOWN;
00337         } else if (name=="zero") {
00338             return SVE_ZERO_EMISSIONS;
00339         } else if (name.find("HDV_3_")==0) {
00340             return (SUMOEmissionClass)(SVE_HDV_3_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_")+1).c_str()));
00341         } else if (name.find("HDV_6_")==0) {
00342             return (SUMOEmissionClass)(SVE_HDV_6_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_")+1).c_str()));
00343         } else if (name.find("HDV_12_")==0) {
00344             return (SUMOEmissionClass)(SVE_HDV_12_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_")+1).c_str()));
00345         } else if (name.find("P_7_")==0) {
00346             return (SUMOEmissionClass)(SVE_P_LDV_7_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_")+1).c_str()));
00347         } else if (name.find("P_14_")==0) {
00348             return (SUMOEmissionClass)(SVE_P_LDV_14_1 - 1 + TplConvert<char>::_2int(name.substr(name.rfind("_")+1).c_str()));
00349         }
00350     } catch (NumberFormatException &) {
00351         throw ProcessError("Unknown emission type '" + name + "'.");
00352     }
00353     return SVE_UNKNOWN;
00354 }
00355 
00356 
00357 std::string
00358 getVehicleEmissionTypeName(SUMOEmissionClass id) throw() {
00359     if (id==SVE_ZERO_EMISSIONS) {
00360         return "zero";
00361     }
00362     if (id<0) {
00363         return "";
00364     } else if (id<3) {
00365         return "HDV_3_" + toString(int(id));
00366     } else if (id<3+6) {
00367         return "HDV_6_" + toString(int(id-3));
00368     } else if (id<3+6+12) {
00369         return "HDV_12_" + toString(int(id-3-6));
00370     } else if (id<3+6+12+7) {
00371         return "P_7_" + toString(int(id-3-6-12));
00372     } else if (id<3+6+12+7+14) {
00373         return "P_14_" + toString(int(id-3-6-12-7));
00374     }
00375     return "";
00376 }
00377 
00378 
00379 
00380 /****************************************************************************/
00381 

Generated on Wed May 5 00:06:36 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6