SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeomConvHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Some helping functions for geometry parsing
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <sstream>
39 #include "GeomConvHelper.h"
40 
41 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
50 GeomConvHelper::parseShapeReporting(const std::string& shpdef, const std::string& objecttype,
51  const char* objectid, bool& ok, bool allowEmpty, bool report) {
52  if (shpdef == "") {
53  if (!allowEmpty) {
54  emitError(report, "Shape", objecttype, objectid, "the shape is empty");
55  ok = false;
56  }
57  return PositionVector();
58  }
59  StringTokenizer st(shpdef, " ");
60  PositionVector shape;
61  while (st.hasNext()) {
62  StringTokenizer pos(st.next(), ",");
63  if (pos.size() != 2 && pos.size() != 3) {
64  emitError(report, "Shape", objecttype, objectid, "the position is neither x,y nor x,y,z");
65  ok = false;
66  return PositionVector();
67  }
68  try {
69  SUMOReal x = TplConvert::_2SUMOReal(pos.next().c_str());
70  SUMOReal y = TplConvert::_2SUMOReal(pos.next().c_str());
71  if (pos.size() == 2) {
72  shape.push_back(Position(x, y));
73  } else {
74  SUMOReal z = TplConvert::_2SUMOReal(pos.next().c_str());
75  shape.push_back(Position(x, y, z));
76  }
77  } catch (NumberFormatException&) {
78  emitError(report, "Shape", objecttype, objectid, "not numeric position entry");
79  ok = false;
80  return PositionVector();
81  } catch (EmptyData&) {
82  emitError(report, "Shape", objecttype, objectid, "empty position entry");
83  ok = false;
84  return PositionVector();
85  }
86  }
87  return shape;
88 }
89 
90 
92 GeomConvHelper::parseBoundaryReporting(const std::string& def, const std::string& objecttype,
93  const char* objectid, bool& ok, bool report) {
94  StringTokenizer st(def, ",");
95  if (st.size() != 4) {
96  emitError(report, "Bounding box", objecttype, objectid, "mismatching entry number");
97  ok = false;
98  return Boundary();
99  }
100  try {
101  SUMOReal xmin = TplConvert::_2SUMOReal(st.next().c_str());
102  SUMOReal ymin = TplConvert::_2SUMOReal(st.next().c_str());
103  SUMOReal xmax = TplConvert::_2SUMOReal(st.next().c_str());
104  SUMOReal ymax = TplConvert::_2SUMOReal(st.next().c_str());
105  return Boundary(xmin, ymin, xmax, ymax);
106  } catch (NumberFormatException&) {
107  emitError(report, "Shape", objecttype, objectid, "not numeric entry");
108  } catch (EmptyData&) {
109  emitError(report, "Shape", objecttype, objectid, "empty entry");
110  }
111  ok = false;
112  return Boundary();
113 }
114 
115 
116 void
117 GeomConvHelper::emitError(bool report, const std::string& what, const std::string& objecttype,
118  const char* objectid, const std::string& desc) {
119  if (!report) {
120  return;
121  }
122  std::ostringstream oss;
123  oss << what << " of ";
124  if (objectid == 0) {
125  oss << "a(n) ";
126  }
127  oss << objecttype;
128  if (objectid != 0) {
129  oss << " '" << objectid << "'";
130  }
131  oss << " is broken: " << desc << ".";
132  WRITE_ERROR(oss.str());
133 }
134 
135 
136 
137 /****************************************************************************/
138 
static void emitError(bool report, const std::string &what, const std::string &objecttype, const char *objectid, const std::string &desc)
Writes an error message into the MessageHandler.
std::string next()
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:223
static Boundary parseBoundaryReporting(const std::string &def, const std::string &objecttype, const char *objectid, bool &ok, bool report=true)
Builds a boundary from its string representation, reporting occured errors.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
size_t size() const
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
void push_back(const PositionVector &p)
Appends all positions from the given vector.
#define SUMOReal
Definition: config.h:215
static PositionVector parseShapeReporting(const std::string &shpdef, const std::string &objecttype, const char *objectid, bool &ok, bool allowEmpty, bool report=true)
Builds a PositionVector from a string representation, reporting occured errors.