Eclipse 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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Some helping functions for geometry parsing
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include <sstream>
31 #include "GeomConvHelper.h"
32 
33 
34 // ===========================================================================
35 // method definitions
36 // ===========================================================================
38 GeomConvHelper::parseShapeReporting(const std::string& shpdef, const std::string& objecttype,
39  const char* objectid, bool& ok, bool allowEmpty, bool report) {
40  if (shpdef == "") {
41  if (!allowEmpty) {
42  emitError(report, "Shape", objecttype, objectid, "the shape is empty");
43  ok = false;
44  }
45  return PositionVector();
46  }
47  StringTokenizer st(shpdef, " ");
48  PositionVector shape;
49  while (st.hasNext()) {
50  StringTokenizer pos(st.next(), ",");
51  if (pos.size() != 2 && pos.size() != 3) {
52  emitError(report, "Shape", objecttype, objectid, "the position is neither x,y nor x,y,z");
53  ok = false;
54  return PositionVector();
55  }
56  try {
57  double x = StringUtils::toDouble(pos.next());
58  double y = StringUtils::toDouble(pos.next());
59  if (pos.size() == 2) {
60  shape.push_back(Position(x, y));
61  } else {
62  double z = StringUtils::toDouble(pos.next());
63  shape.push_back(Position(x, y, z));
64  }
65  } catch (NumberFormatException&) {
66  emitError(report, "Shape", objecttype, objectid, "not numeric position entry");
67  ok = false;
68  return PositionVector();
69  } catch (EmptyData&) {
70  emitError(report, "Shape", objecttype, objectid, "empty position entry");
71  ok = false;
72  return PositionVector();
73  }
74  }
75  return shape;
76 }
77 
78 
80 GeomConvHelper::parseBoundaryReporting(const std::string& def, const std::string& objecttype,
81  const char* objectid, bool& ok, bool report) {
82  StringTokenizer st(def, ",");
83  if (st.size() != 4) {
84  emitError(report, "Bounding box", objecttype, objectid, "mismatching entry number");
85  ok = false;
86  return Boundary();
87  }
88  try {
89  double xmin = StringUtils::toDouble(st.next());
90  double ymin = StringUtils::toDouble(st.next());
91  double xmax = StringUtils::toDouble(st.next());
92  double ymax = StringUtils::toDouble(st.next());
93  return Boundary(xmin, ymin, xmax, ymax);
94  } catch (NumberFormatException&) {
95  emitError(report, "Shape", objecttype, objectid, "not numeric entry");
96  } catch (EmptyData&) {
97  emitError(report, "Shape", objecttype, objectid, "empty entry");
98  }
99  ok = false;
100  return Boundary();
101 }
102 
103 
104 void
105 GeomConvHelper::emitError(bool report, const std::string& what, const std::string& objecttype,
106  const char* objectid, const std::string& desc) {
107  if (!report) {
108  return;
109  }
110  std::ostringstream oss;
111  oss << what << " of ";
112  if (objectid == nullptr) {
113  oss << "a(n) ";
114  }
115  oss << objecttype;
116  if (objectid != nullptr) {
117  oss << " '" << objectid << "'";
118  }
119  oss << " is broken: " << desc << ".";
120  WRITE_ERROR(oss.str());
121 }
122 
123 
124 
125 /****************************************************************************/
126 
GeomConvHelper.h
StringTokenizer::hasNext
bool hasNext()
returns the information whether further substrings exist
Definition: StringTokenizer.cpp:94
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:345
MsgHandler.h
EmptyData
Definition: UtilExceptions.h:68
StringTokenizer::next
std::string next()
returns the next substring when it exists. Otherwise the behaviour is undefined
Definition: StringTokenizer.cpp:99
PositionVector
A list of positions.
Definition: PositionVector.h:45
NumberFormatException
Definition: UtilExceptions.h:95
GeomConvHelper::parseBoundaryReporting
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 occurred errors.
Definition: GeomConvHelper.cpp:80
StringTokenizer
Definition: StringTokenizer.h:61
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:41
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
StringTokenizer::size
int size() const
returns the number of existing substrings
Definition: StringTokenizer.cpp:137
StringUtils.h
config.h
StringTokenizer.h
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
PositionVector.h
GeomConvHelper::emitError
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.
Definition: GeomConvHelper.cpp:105
GeomConvHelper::parseShapeReporting
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 occurred errors.
Definition: GeomConvHelper.cpp:38