Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/netbuild/NBHelpers.cpp
169665 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 NBHelpers.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Sascha Krieg
17
/// @author Michael Behrisch
18
/// @author Jakob Erdmann
19
/// @date Tue, 20 Nov 2001
20
///
21
// Some mathematical helper methods
22
/****************************************************************************/
23
#include <config.h>
24
25
#include <cmath>
26
#include <string>
27
#include <sstream>
28
#include <iostream>
29
#include <fstream>
30
//#include <iomanip>
31
#include <utils/common/StringUtils.h>
32
#include <utils/common/StringTokenizer.h>
33
#include <utils/common/MsgHandler.h>
34
#include <utils/common/StringUtils.h>
35
#include <utils/geom/Position.h>
36
#include <utils/geom/GeomHelper.h>
37
#include "NBNode.h"
38
#include "NBHelpers.h"
39
40
41
// ===========================================================================
42
// method definitions
43
// ===========================================================================
44
double
45
NBHelpers::relAngle(double angle1, double angle2) {
46
angle2 -= angle1;
47
while (angle2 > 180.) {
48
angle2 -= 360.;
49
}
50
while (angle2 < -180.) {
51
angle2 += 360.;
52
}
53
return angle2;
54
}
55
56
57
double
58
NBHelpers::normRelAngle(double angle1, double angle2) {
59
double rel = relAngle(angle1, angle2);
60
if (rel + NUMERICAL_EPS >= 180) {
61
return -180;
62
} else {
63
return rel;
64
}
65
}
66
67
68
std::string
69
NBHelpers::normalIDRepresentation(const std::string& id) {
70
std::stringstream strm1(id);
71
long numid;
72
strm1 >> numid;
73
std::stringstream strm2;
74
strm2 << numid;
75
return strm2.str();
76
}
77
78
79
double
80
NBHelpers::distance(NBNode* node1, NBNode* node2) {
81
return node1->getPosition().distanceTo(node2->getPosition());
82
}
83
84
85
void
86
NBHelpers::loadEdgesFromFile(const std::string& file, std::set<std::string>& into) {
87
std::ifstream strm(file.c_str());
88
if (!strm.good()) {
89
throw ProcessError(TLF("Could not load names of edges to keep from '%'.", file));
90
}
91
while (strm.good()) {
92
std::string name;
93
strm >> name;
94
into.insert(name);
95
// maybe we're loading an edge-selection
96
if (StringUtils::startsWith(name, "edge:")) {
97
into.insert(name.substr(5));
98
}
99
}
100
}
101
102
103
void
104
NBHelpers::loadPrefixedIDsFomFile(const std::string& file, const std::string prefix, std::set<std::string>& into) {
105
std::ifstream strm(file.c_str());
106
if (!strm.good()) {
107
throw ProcessError(TLF("Could not load IDs from '%'.", file));
108
}
109
while (strm.good()) {
110
std::string prefixedID;
111
strm >> prefixedID;
112
if (StringUtils::startsWith(prefixedID, prefix)) {
113
into.insert(prefixedID.substr(prefix.size()));
114
}
115
}
116
}
117
118
void
119
NBHelpers::interpretLaneID(const std::string& lane_id, std::string& edge_id, int& index) {
120
// assume lane_id = edge_id + '_' + index
121
const std::string::size_type sep_index = lane_id.rfind('_');
122
if (sep_index == std::string::npos) {
123
WRITE_ERRORF(TL("Invalid lane id '%' (missing '_')."), lane_id);
124
}
125
edge_id = lane_id.substr(0, sep_index);
126
std::string index_string = lane_id.substr(sep_index + 1);
127
try {
128
index = StringUtils::toInt(index_string);
129
} catch (NumberFormatException&) {
130
WRITE_ERRORF(TL("Invalid lane index '%' for lane '%'."), index_string, lane_id);
131
}
132
}
133
134
135
/****************************************************************************/
136
137