Line data Source code
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 : /****************************************************************************/
14 : /// @file GeomConvHelper.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @date Sept 2003
19 : ///
20 : // Some helping functions for geometry parsing
21 : /****************************************************************************/
22 : #include <config.h>
23 :
24 : #include <string>
25 : #include <sstream>
26 : #include <utils/geom/PositionVector.h>
27 : #include <utils/common/MsgHandler.h>
28 : #include <utils/common/StringTokenizer.h>
29 : #include <utils/common/StringUtils.h>
30 : #include "GeomConvHelper.h"
31 :
32 :
33 : // ===========================================================================
34 : // method definitions
35 : // ===========================================================================
36 : PositionVector
37 14 : GeomConvHelper::parseShapeReporting(const std::string& shpdef, const std::string& objecttype,
38 : const char* objectid, bool& ok, bool allowEmpty, bool report) {
39 14 : if (shpdef == "") {
40 0 : if (!allowEmpty) {
41 0 : emitError(report, "Shape", objecttype, objectid, "the shape is empty");
42 0 : ok = false;
43 : }
44 0 : return PositionVector();
45 : }
46 42 : StringTokenizer st(shpdef, " ");
47 14 : PositionVector shape;
48 18 : while (st.hasNext()) {
49 32 : StringTokenizer pos(st.next(), ",");
50 16 : if (pos.size() != 2 && pos.size() != 3) {
51 24 : emitError(report, "Shape", objecttype, objectid, "the position is neither x,y nor x,y,z");
52 12 : ok = false;
53 12 : return PositionVector();
54 : }
55 : try {
56 4 : double x = StringUtils::toDouble(pos.next());
57 4 : double y = StringUtils::toDouble(pos.next());
58 4 : if (pos.size() == 2) {
59 4 : shape.push_back(Position(x, y));
60 : } else {
61 0 : double z = StringUtils::toDouble(pos.next());
62 0 : shape.push_back(Position(x, y, z));
63 : }
64 0 : } catch (NumberFormatException&) {
65 0 : emitError(report, "Shape", objecttype, objectid, "not numeric position entry");
66 0 : ok = false;
67 0 : return PositionVector();
68 0 : } catch (EmptyData&) {
69 0 : emitError(report, "Shape", objecttype, objectid, "empty position entry");
70 0 : ok = false;
71 0 : return PositionVector();
72 0 : }
73 16 : }
74 : return shape;
75 14 : }
76 :
77 :
78 : Boundary
79 1 : GeomConvHelper::parseBoundaryReporting(const std::string& def, const std::string& objecttype,
80 : const char* objectid, bool& ok, bool report, bool offsets) {
81 3 : StringTokenizer st(def, ",");
82 1 : if (st.size() != 4) {
83 0 : emitError(report, "Bounding box", objecttype, objectid, "mismatching entry number");
84 0 : ok = false;
85 0 : return Boundary();
86 : }
87 : try {
88 1 : double xmin = StringUtils::toDouble(st.next());
89 1 : double ymin = StringUtils::toDouble(st.next());
90 1 : double xmax = StringUtils::toDouble(st.next());
91 1 : double ymax = StringUtils::toDouble(st.next());
92 1 : if (offsets) {
93 1 : Boundary res;
94 1 : res.setOffsets(xmin, ymin, xmax, ymax);
95 : return res;
96 1 : } else {
97 0 : return Boundary(xmin, ymin, xmax, ymax);
98 : }
99 0 : } catch (NumberFormatException&) {
100 0 : emitError(report, "Shape", objecttype, objectid, "not numeric entry");
101 0 : } catch (EmptyData&) {
102 0 : emitError(report, "Shape", objecttype, objectid, "empty entry");
103 0 : }
104 0 : ok = false;
105 0 : return Boundary();
106 1 : }
107 :
108 :
109 : void
110 12 : GeomConvHelper::emitError(bool report, const std::string& what, const std::string& objecttype,
111 : const char* objectid, const std::string& desc) {
112 12 : if (!report) {
113 12 : return;
114 : }
115 0 : std::ostringstream oss;
116 0 : oss << what << " of ";
117 0 : if (objectid == nullptr) {
118 0 : oss << "a(n) ";
119 : }
120 : oss << objecttype;
121 0 : if (objectid != nullptr) {
122 0 : oss << " '" << objectid << "'";
123 : }
124 0 : oss << " is broken: " << desc << ".";
125 0 : WRITE_ERROR(oss.str());
126 0 : }
127 :
128 :
129 : /****************************************************************************/
|