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.org/sumo
3 // Copyright (C) 2001-2018 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 /****************************************************************************/
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>
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
47 NBHelpers::relAngle(double angle1, double angle2) {
48  angle2 -= angle1;
49  if (angle2 > 180) {
50  angle2 = (360 - angle2) * -1;
51  }
52  while (angle2 < -180) {
53  angle2 = 360 + angle2;
54  }
55  return angle2;
56 }
57 
58 
59 double
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
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
83  return node1->getPosition().distanceTo(node2->getPosition());
84 }
85 
86 
87 void
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
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
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 /****************************************************************************/
static double relAngle(double angle1, double angle2)
computes the relative angle between the two angles
Definition: NBHelpers.cpp:47
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:106
static double normRelAngle(double angle1, double angle2)
ensure that reverse relAngles (>=179.999) always count as turnarounds (-180)
Definition: NBHelpers.cpp:60
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:88
static std::string normalIDRepresentation(const std::string &id)
converts the numerical id to its "normal" string representation
Definition: NBHelpers.cpp:71
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...
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:247
static double distance(NBNode *node1, NBNode *node2)
returns the distance between both nodes
Definition: NBHelpers.cpp:82
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:121
const Position & getPosition() const
Definition: NBNode.h:242
Represents a single node (junction) during network building.
Definition: NBNode.h:68
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:234
#define NUMERICAL_EPS
Definition: config.h:148