SUMO - Simulation of Urban MObility
GeomConvHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Some helping functions for geometry parsing
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <sstream>
38 #include "GeomConvHelper.h"
39 
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
45 GeomConvHelper::parseShapeReporting(const std::string& shpdef, const std::string& objecttype,
46  const char* objectid, bool& ok, bool allowEmpty, bool report) {
47  if (shpdef == "") {
48  if (!allowEmpty) {
49  emitError(report, "Shape", objecttype, objectid, "the shape is empty");
50  ok = false;
51  }
52  return PositionVector();
53  }
54  StringTokenizer st(shpdef, " ");
55  PositionVector shape;
56  while (st.hasNext()) {
57  StringTokenizer pos(st.next(), ",");
58  if (pos.size() != 2 && pos.size() != 3) {
59  emitError(report, "Shape", objecttype, objectid, "the position is neither x,y nor x,y,z");
60  ok = false;
61  return PositionVector();
62  }
63  try {
64  double x = TplConvert::_2double(pos.next().c_str());
65  double y = TplConvert::_2double(pos.next().c_str());
66  if (pos.size() == 2) {
67  shape.push_back(Position(x, y));
68  } else {
69  double z = TplConvert::_2double(pos.next().c_str());
70  shape.push_back(Position(x, y, z));
71  }
72  } catch (NumberFormatException&) {
73  emitError(report, "Shape", objecttype, objectid, "not numeric position entry");
74  ok = false;
75  return PositionVector();
76  } catch (EmptyData&) {
77  emitError(report, "Shape", objecttype, objectid, "empty position entry");
78  ok = false;
79  return PositionVector();
80  }
81  }
82  return shape;
83 }
84 
85 
87 GeomConvHelper::parseBoundaryReporting(const std::string& def, const std::string& objecttype,
88  const char* objectid, bool& ok, bool report) {
89  StringTokenizer st(def, ",");
90  if (st.size() != 4) {
91  emitError(report, "Bounding box", objecttype, objectid, "mismatching entry number");
92  ok = false;
93  return Boundary();
94  }
95  try {
96  double xmin = TplConvert::_2double(st.next().c_str());
97  double ymin = TplConvert::_2double(st.next().c_str());
98  double xmax = TplConvert::_2double(st.next().c_str());
99  double ymax = TplConvert::_2double(st.next().c_str());
100  return Boundary(xmin, ymin, xmax, ymax);
101  } catch (NumberFormatException&) {
102  emitError(report, "Shape", objecttype, objectid, "not numeric entry");
103  } catch (EmptyData&) {
104  emitError(report, "Shape", objecttype, objectid, "empty entry");
105  }
106  ok = false;
107  return Boundary();
108 }
109 
110 
111 void
112 GeomConvHelper::emitError(bool report, const std::string& what, const std::string& objecttype,
113  const char* objectid, const std::string& desc) {
114  if (!report) {
115  return;
116  }
117  std::ostringstream oss;
118  oss << what << " of ";
119  if (objectid == 0) {
120  oss << "a(n) ";
121  }
122  oss << objecttype;
123  if (objectid != 0) {
124  oss << " '" << objectid << "'";
125  }
126  oss << " is broken: " << desc << ".";
127  WRITE_ERROR(oss.str());
128 }
129 
130 
131 
132 /****************************************************************************/
133 
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:47
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
A list of positions.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:311
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.