Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/geom/GeomConvHelper.cpp
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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
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
78
Boundary
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
/****************************************************************************/
130
131