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.dev/sumo
3 // Copyright (C) 2001-2024 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, bool offsets) {
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  if (offsets) {
93  Boundary res;
94  res.setOffsets(xmin, ymin, xmax, ymax);
95  return res;
96  } else {
97  return Boundary(xmin, ymin, xmax, ymax);
98  }
99  } catch (NumberFormatException&) {
100  emitError(report, "Shape", objecttype, objectid, "not numeric entry");
101  } catch (EmptyData&) {
102  emitError(report, "Shape", objecttype, objectid, "empty entry");
103  }
104  ok = false;
105  return Boundary();
106 }
107 
108 
109 void
110 GeomConvHelper::emitError(bool report, const std::string& what, const std::string& objecttype,
111  const char* objectid, const std::string& desc) {
112  if (!report) {
113  return;
114  }
115  std::ostringstream oss;
116  oss << what << " of ";
117  if (objectid == nullptr) {
118  oss << "a(n) ";
119  }
120  oss << objecttype;
121  if (objectid != nullptr) {
122  oss << " '" << objectid << "'";
123  }
124  oss << " is broken: " << desc << ".";
125  WRITE_ERROR(oss.str());
126 }
127 
128 
129 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:304
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
void setOffsets(double xmin, double ymin, double xmax, double ymax)
Sets the boundary to the given values, ignoring min < max constraints.
Definition: Boundary.cpp:403
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, bool offsets=false)
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