1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file NBHelpers.cpp
11 /// @author Daniel Krajzewicz
12 /// @author Sascha Krieg
13 /// @author Michael Behrisch
14 /// @author Jakob Erdmann
15 /// @date Tue, 20 Nov 2001
16 /// @version $Id$
17 ///
18 // Some mathematical helper methods
19 /****************************************************************************/
20
21
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26
27 #include <cmath>
28 #include <string>
29 #include <sstream>
30 #include <iostream>
31 #include <fstream>
32 //#include <iomanip>
33 #include <utils/common/StringUtils.h>
34 #include <utils/common/StringTokenizer.h>
35 #include <utils/common/MsgHandler.h>
36 #include <utils/common/StringUtils.h>
37 #include <utils/geom/Position.h>
38 #include <utils/geom/GeomHelper.h>
39 #include "NBNode.h"
40 #include "NBHelpers.h"
41
42
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 double
relAngle(double angle1,double angle2)47 NBHelpers::relAngle(double angle1, double angle2) {
48 angle2 -= angle1;
49 while (angle2 > 180.) {
50 angle2 -= 360.;
51 }
52 while (angle2 < -180.) {
53 angle2 += 360.;
54 }
55 return angle2;
56 }
57
58
59 double
normRelAngle(double angle1,double angle2)60 NBHelpers::normRelAngle(double angle1, double angle2) {
61 double rel = relAngle(angle1, angle2);
62 if (rel + NUMERICAL_EPS >= 180) {
63 return -180;
64 } else {
65 return rel;
66 }
67 }
68
69
70 std::string
normalIDRepresentation(const std::string & id)71 NBHelpers::normalIDRepresentation(const std::string& id) {
72 std::stringstream strm1(id);
73 long numid;
74 strm1 >> numid;
75 std::stringstream strm2;
76 strm2 << numid;
77 return strm2.str();
78 }
79
80
81 double
distance(NBNode * node1,NBNode * node2)82 NBHelpers::distance(NBNode* node1, NBNode* node2) {
83 return node1->getPosition().distanceTo(node2->getPosition());
84 }
85
86
87 void
loadEdgesFromFile(const std::string & file,std::set<std::string> & into)88 NBHelpers::loadEdgesFromFile(const std::string& file, std::set<std::string>& into) {
89 std::ifstream strm(file.c_str());
90 if (!strm.good()) {
91 throw ProcessError("Could not load names of edges too keep from '" + file + "'.");
92 }
93 while (strm.good()) {
94 std::string name;
95 strm >> name;
96 into.insert(name);
97 // maybe we're loading an edge-selection
98 if (StringUtils::startsWith(name, "edge:")) {
99 into.insert(name.substr(5));
100 }
101 }
102 }
103
104
105 void
loadPrefixedIDsFomFile(const std::string & file,const std::string prefix,std::set<std::string> & into)106 NBHelpers::loadPrefixedIDsFomFile(const std::string& file, const std::string prefix, std::set<std::string>& into) {
107 std::ifstream strm(file.c_str());
108 if (!strm.good()) {
109 throw ProcessError("Could not load IDs from '" + file + "'.");
110 }
111 while (strm.good()) {
112 std::string prefixedID;
113 strm >> prefixedID;
114 if (StringUtils::startsWith(prefixedID, prefix)) {
115 into.insert(prefixedID.substr(prefix.size()));
116 }
117 }
118 }
119
120 void
interpretLaneID(const std::string & lane_id,std::string & edge_id,int & index)121 NBHelpers::interpretLaneID(const std::string& lane_id, std::string& edge_id, int& index) {
122 // assume lane_id = edge_id + '_' + index
123 const std::string::size_type sep_index = lane_id.rfind('_');
124 if (sep_index == std::string::npos) {
125 WRITE_ERROR("Invalid lane id '" + lane_id + "' (missing '_').");
126 }
127 edge_id = lane_id.substr(0, sep_index);
128 std::string index_string = lane_id.substr(sep_index + 1);
129 try {
130 index = StringUtils::toInt(index_string);
131 } catch (NumberFormatException&) {
132 WRITE_ERROR("Invalid lane index '" + index_string + "' for lane '" + lane_id + "'.");
133 }
134 }
135
136 /****************************************************************************/
137