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