SUMO - Simulation of Urban MObility
GeomConvHelper.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // Some helping functions for geometry parsing
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #include <string>
00034 #include <sstream>
00035 #include <utils/geom/PositionVector.h>
00036 #include <utils/common/MsgHandler.h>
00037 #include <utils/common/StringTokenizer.h>
00038 #include <utils/common/TplConvert.h>
00039 #include "GeomConvHelper.h"
00040 
00041 #ifdef CHECK_MEMORY_LEAKS
00042 #include <foreign/nvwa/debug_new.h>
00043 #endif // CHECK_MEMORY_LEAKS
00044 
00045 
00046 // ===========================================================================
00047 // method definitions
00048 // ===========================================================================
00049 PositionVector
00050 GeomConvHelper::parseShapeReporting(const std::string& shpdef, const std::string& objecttype,
00051                                     const char* objectid, bool& ok, bool allowEmpty, bool report) {
00052     if (shpdef == "") {
00053         if (!allowEmpty) {
00054             emitError(report, "Shape", objecttype, objectid, "the shape is empty");
00055             ok = false;
00056         }
00057         return PositionVector();
00058     }
00059     StringTokenizer st(shpdef, " ");
00060     PositionVector shape;
00061     while (st.hasNext()) {
00062         StringTokenizer pos(st.next(), ",");
00063         if (pos.size() != 2 && pos.size() != 3) {
00064             emitError(report, "Shape", objecttype, objectid, "the position is neither x,y nor x,y,z");
00065             ok = false;
00066             return PositionVector();
00067         }
00068         try {
00069             SUMOReal x = TplConvert<char>::_2SUMOReal(pos.next().c_str());
00070             SUMOReal y = TplConvert<char>::_2SUMOReal(pos.next().c_str());
00071             if (pos.size() == 2) {
00072                 shape.push_back(Position(x, y));
00073             } else {
00074                 SUMOReal z = TplConvert<char>::_2SUMOReal(pos.next().c_str());
00075                 shape.push_back(Position(x, y, z));
00076             }
00077         } catch (NumberFormatException&) {
00078             emitError(report, "Shape", objecttype, objectid, "not numeric position entry");
00079             ok = false;
00080             return PositionVector();
00081         } catch (EmptyData&) {
00082             emitError(report, "Shape", objecttype, objectid, "empty position entry");
00083             ok = false;
00084             return PositionVector();
00085         }
00086     }
00087     return shape;
00088 }
00089 
00090 
00091 Boundary
00092 GeomConvHelper::parseBoundaryReporting(const std::string& def, const std::string& objecttype,
00093                                        const char* objectid, bool& ok, bool report) {
00094     StringTokenizer st(def, ",");
00095     if (st.size() != 4) {
00096         emitError(report, "Bounding box", objecttype, objectid, "mismatching entry number");
00097         ok = false;
00098         return Boundary();
00099     }
00100     try {
00101         SUMOReal xmin = TplConvert<char>::_2SUMOReal(st.next().c_str());
00102         SUMOReal ymin = TplConvert<char>::_2SUMOReal(st.next().c_str());
00103         SUMOReal xmax = TplConvert<char>::_2SUMOReal(st.next().c_str());
00104         SUMOReal ymax = TplConvert<char>::_2SUMOReal(st.next().c_str());
00105         return Boundary(xmin, ymin, xmax, ymax);
00106     } catch (NumberFormatException&) {
00107         emitError(report, "Shape", objecttype, objectid, "not numeric entry");
00108     } catch (EmptyData&) {
00109         emitError(report, "Shape", objecttype, objectid, "empty entry");
00110     }
00111     ok = false;
00112     return Boundary();
00113 }
00114 
00115 
00116 void
00117 GeomConvHelper::emitError(bool report, const std::string& what, const std::string& objecttype,
00118                           const char* objectid, const std::string& desc) {
00119     if (!report) {
00120         return;
00121     }
00122     std::ostringstream oss;
00123     oss << what << " of ";
00124     if (objectid == 0) {
00125         oss << "a(n) ";
00126     }
00127     oss << objecttype;
00128     if (objectid != 0) {
00129         oss << " '" << objectid << "'";
00130     }
00131     oss << " is broken: " << desc << ".";
00132     WRITE_ERROR(oss.str());
00133 }
00134 
00135 
00136 
00137 /****************************************************************************/
00138 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines