Eclipse SUMO - Simulation of Urban MObility
NBHelpers.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 /****************************************************************************/
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>
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
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 too 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 /****************************************************************************/
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define TL(string)
Definition: MsgHandler.h:315
#define TLF(string,...)
Definition: MsgHandler.h:317
static void loadPrefixedIDsFomFile(const std::string &file, const std::string prefix, std::set< std::string > &into)
Add prefixed ids defined in file.
Definition: NBHelpers.cpp:104
static void interpretLaneID(const std::string &lane_id, std::string &edge_id, int &index)
parses edge-id and index from lane-id
Definition: NBHelpers.cpp:119
static std::string normalIDRepresentation(const std::string &id)
converts the numerical id to its "normal" string representation
Definition: NBHelpers.cpp:69
static double relAngle(double angle1, double angle2)
computes the relative angle between the two angles
Definition: NBHelpers.cpp:45
static double normRelAngle(double angle1, double angle2)
ensure that reverse relAngles (>=179.999) always count as turnarounds (-180)
Definition: NBHelpers.cpp:58
static double distance(NBNode *node1, NBNode *node2)
returns the distance between both nodes
Definition: NBHelpers.cpp:80
static void loadEdgesFromFile(const std::string &file, std::set< std::string > &into)
Add edge ids defined in file (either ID or edge:ID per line) into the given set.
Definition: NBHelpers.cpp:86
Represents a single node (junction) during network building.
Definition: NBNode.h:66
const Position & getPosition() const
Definition: NBNode.h:260
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:261
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...