SUMO - Simulation of Urban MObility
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.dlr.de/
12 // Copyright (C) 2001-2017 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 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
46 GeomConvHelper::parseShapeReporting(const std::string& shpdef, const std::string& objecttype,
47  const char* objectid, bool& ok, bool allowEmpty, bool report) {
48  if (shpdef == "") {
49  if (!allowEmpty) {
50  emitError(report, "Shape", objecttype, objectid, "the shape is empty");
51  ok = false;
52  }
53  return PositionVector();
54  }
55  StringTokenizer st(shpdef, " ");
56  PositionVector shape;
57  while (st.hasNext()) {
58  StringTokenizer pos(st.next(), ",");
59  if (pos.size() != 2 && pos.size() != 3) {
60  emitError(report, "Shape", objecttype, objectid, "the position is neither x,y nor x,y,z");
61  ok = false;
62  return PositionVector();
63  }
64  try {
65  double x = TplConvert::_2double(pos.next().c_str());
66  double y = TplConvert::_2double(pos.next().c_str());
67  if (pos.size() == 2) {
68  shape.push_back(Position(x, y));
69  } else {
70  double z = TplConvert::_2double(pos.next().c_str());
71  shape.push_back(Position(x, y, z));
72  }
73  } catch (NumberFormatException&) {
74  emitError(report, "Shape", objecttype, objectid, "not numeric position entry");
75  ok = false;
76  return PositionVector();
77  } catch (EmptyData&) {
78  emitError(report, "Shape", objecttype, objectid, "empty position entry");
79  ok = false;
80  return PositionVector();
81  }
82  }
83  return shape;
84 }
85 
86 
88 GeomConvHelper::parseBoundaryReporting(const std::string& def, const std::string& objecttype,
89  const char* objectid, bool& ok, bool report) {
90  StringTokenizer st(def, ",");
91  if (st.size() != 4) {
92  emitError(report, "Bounding box", objecttype, objectid, "mismatching entry number");
93  ok = false;
94  return Boundary();
95  }
96  try {
97  double xmin = TplConvert::_2double(st.next().c_str());
98  double ymin = TplConvert::_2double(st.next().c_str());
99  double xmax = TplConvert::_2double(st.next().c_str());
100  double ymax = TplConvert::_2double(st.next().c_str());
101  return Boundary(xmin, ymin, xmax, ymax);
102  } catch (NumberFormatException&) {
103  emitError(report, "Shape", objecttype, objectid, "not numeric entry");
104  } catch (EmptyData&) {
105  emitError(report, "Shape", objecttype, objectid, "empty entry");
106  }
107  ok = false;
108  return Boundary();
109 }
110 
111 
112 void
113 GeomConvHelper::emitError(bool report, const std::string& what, const std::string& objecttype,
114  const char* objectid, const std::string& desc) {
115  if (!report) {
116  return;
117  }
118  std::ostringstream oss;
119  oss << what << " of ";
120  if (objectid == 0) {
121  oss << "a(n) ";
122  }
123  oss << objecttype;
124  if (objectid != 0) {
125  oss << " '" << objectid << "'";
126  }
127  oss << " is broken: " << desc << ".";
128  WRITE_ERROR(oss.str());
129 }
130 
131 
132 
133 /****************************************************************************/
134 
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 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.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:297
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.