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