SUMO - Simulation of Urban MObility

GeomConvHelper.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Some helping functions for geometry parsing
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 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 <sstream>
00032 #include <utils/geom/PositionVector.h>
00033 #include <utils/common/MsgHandler.h>
00034 #include <utils/common/StringTokenizer.h>
00035 #include <utils/common/TplConvert.h>
00036 #include "GeomConvHelper.h"
00037 
00038 #ifdef CHECK_MEMORY_LEAKS
00039 #include <foreign/nvwa/debug_new.h>
00040 #endif // CHECK_MEMORY_LEAKS
00041 
00042 
00043 // ===========================================================================
00044 // method definitions
00045 // ===========================================================================
00046 PositionVector
00047 GeomConvHelper::parseShapeReporting(const std::string& shpdef, const std::string& objecttype,
00048                                     const char* objectid, bool& ok, bool allowEmpty, bool report) throw() {
00049     if (shpdef=="") {
00050         if (!allowEmpty) {
00051             emitError(report, "Shape", objecttype, objectid, "the shape is empty");
00052             ok = false;
00053         }
00054         return PositionVector();
00055     }
00056     StringTokenizer st(shpdef, " ");
00057     PositionVector shape;
00058     while (st.hasNext()) {
00059         StringTokenizer pos(st.next(), ",");
00060         if (pos.size()!=2 && pos.size()!=3) {
00061             emitError(report, "Shape", objecttype, objectid, "the position is neither x,y nor x,y,z");
00062             ok = false;
00063             return PositionVector();
00064         }
00065         try {
00066             SUMOReal x = TplConvert<char>::_2SUMOReal(pos.next().c_str());
00067             SUMOReal y = TplConvert<char>::_2SUMOReal(pos.next().c_str());
00068             if (pos.size()==2) {
00069                 shape.push_back(Position(x, y));
00070             } else {
00071                 SUMOReal z = TplConvert<char>::_2SUMOReal(pos.next().c_str());
00072                 shape.push_back(Position(x, y, z));
00073             }
00074         } catch (NumberFormatException&) {
00075             emitError(report, "Shape", objecttype, objectid, "not numeric position entry");
00076             ok = false;
00077             return PositionVector();
00078         } catch (EmptyData&) {
00079             emitError(report, "Shape", objecttype, objectid, "empty position entry");
00080             ok = false;
00081             return PositionVector();
00082         }
00083     }
00084     return shape;
00085 }
00086 
00087 
00088 Boundary
00089 GeomConvHelper::parseBoundaryReporting(const std::string& def, const std::string& objecttype,
00090                                        const char* objectid, bool& ok, bool report) throw() {
00091     StringTokenizer st(def, ",");
00092     if (st.size()!=4) {
00093         emitError(report, "Bounding box", objecttype, objectid, "mismatching entry number");
00094         ok = false;
00095         return Boundary();
00096     }
00097     try {
00098         SUMOReal xmin = TplConvert<char>::_2SUMOReal(st.next().c_str());
00099         SUMOReal ymin = TplConvert<char>::_2SUMOReal(st.next().c_str());
00100         SUMOReal xmax = TplConvert<char>::_2SUMOReal(st.next().c_str());
00101         SUMOReal ymax = TplConvert<char>::_2SUMOReal(st.next().c_str());
00102         return Boundary(xmin, ymin, xmax, ymax);
00103     } catch (NumberFormatException&) {
00104         emitError(report, "Shape", objecttype, objectid, "not numeric entry");
00105     } catch (EmptyData&) {
00106         emitError(report, "Shape", objecttype, objectid, "empty entry");
00107     }
00108     ok = false;
00109     return Boundary();
00110 }
00111 
00112 
00113 void
00114 GeomConvHelper::emitError(bool report, const std::string& what, const std::string& objecttype,
00115                           const char* objectid, const std::string& desc) throw() {
00116     if (!report) {
00117         return;
00118     }
00119     std::ostringstream oss;
00120     oss << what << " of ";
00121     if (objectid==0) {
00122         oss << "a(n) ";
00123     }
00124     oss << objecttype;
00125     if (objectid!=0) {
00126         oss << " '" << objectid << "'";
00127     }
00128     oss << " is broken: " << desc << ".";
00129     WRITE_ERROR(oss.str());
00130 }
00131 
00132 
00133 
00134 /****************************************************************************/
00135 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines