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.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 /****************************************************************************/
17 // Some mathematical helper methods
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <cmath>
27 #include <string>
28 #include <sstream>
29 #include <iostream>
30 #include <fstream>
31 //#include <iomanip>
36 #include <utils/geom/Position.h>
37 #include <utils/geom/GeomHelper.h>
38 #include "NBNode.h"
39 #include "NBHelpers.h"
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
45 double
46 NBHelpers::relAngle(double angle1, double angle2) {
47  angle2 -= angle1;
48  while (angle2 > 180.) {
49  angle2 -= 360.;
50  }
51  while (angle2 < -180.) {
52  angle2 += 360.;
53  }
54  return angle2;
55 }
56 
57 
58 double
59 NBHelpers::normRelAngle(double angle1, double angle2) {
60  double rel = relAngle(angle1, angle2);
61  if (rel + NUMERICAL_EPS >= 180) {
62  return -180;
63  } else {
64  return rel;
65  }
66 }
67 
68 
69 std::string
70 NBHelpers::normalIDRepresentation(const std::string& id) {
71  std::stringstream strm1(id);
72  long numid;
73  strm1 >> numid;
74  std::stringstream strm2;
75  strm2 << numid;
76  return strm2.str();
77 }
78 
79 
80 double
82  return node1->getPosition().distanceTo(node2->getPosition());
83 }
84 
85 
86 void
87 NBHelpers::loadEdgesFromFile(const std::string& file, std::set<std::string>& into) {
88  std::ifstream strm(file.c_str());
89  if (!strm.good()) {
90  throw ProcessError("Could not load names of edges too keep from '" + file + "'.");
91  }
92  while (strm.good()) {
93  std::string name;
94  strm >> name;
95  into.insert(name);
96  // maybe we're loading an edge-selection
97  if (StringUtils::startsWith(name, "edge:")) {
98  into.insert(name.substr(5));
99  }
100  }
101 }
102 
103 
104 void
105 NBHelpers::loadPrefixedIDsFomFile(const std::string& file, const std::string prefix, std::set<std::string>& into) {
106  std::ifstream strm(file.c_str());
107  if (!strm.good()) {
108  throw ProcessError("Could not load IDs from '" + file + "'.");
109  }
110  while (strm.good()) {
111  std::string prefixedID;
112  strm >> prefixedID;
113  if (StringUtils::startsWith(prefixedID, prefix)) {
114  into.insert(prefixedID.substr(prefix.size()));
115  }
116  }
117 }
118 
119 void
120 NBHelpers::interpretLaneID(const std::string& lane_id, std::string& edge_id, int& index) {
121  // assume lane_id = edge_id + '_' + index
122  const std::string::size_type sep_index = lane_id.rfind('_');
123  if (sep_index == std::string::npos) {
124  WRITE_ERROR("Invalid lane id '" + lane_id + "' (missing '_').");
125  }
126  edge_id = lane_id.substr(0, sep_index);
127  std::string index_string = lane_id.substr(sep_index + 1);
128  try {
129  index = StringUtils::toInt(index_string);
130  } catch (NumberFormatException&) {
131  WRITE_ERROR("Invalid lane index '" + index_string + "' for lane '" + lane_id + "'.");
132  }
133 }
134 
135 /****************************************************************************/
NUMERICAL_EPS
#define NUMERICAL_EPS
Definition: config.h:148
NBHelpers::normalIDRepresentation
static std::string normalIDRepresentation(const std::string &id)
converts the numerical id to its "normal" string representation
Definition: NBHelpers.cpp:70
MsgHandler.h
NBHelpers::normRelAngle
static double normRelAngle(double angle1, double angle2)
ensure that reverse relAngles (>=179.999) always count as turnarounds (-180)
Definition: NBHelpers.cpp:59
NBHelpers::relAngle
static double relAngle(double angle1, double angle2)
computes the relative angle between the two angles
Definition: NBHelpers.cpp:46
NBNode::getPosition
const Position & getPosition() const
Definition: NBNode.h:247
NumberFormatException
Definition: UtilExceptions.h:95
NBHelpers::loadPrefixedIDsFomFile
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:105
NBHelpers::distance
static double distance(NBNode *node1, NBNode *node2)
returns the distance between both nodes
Definition: NBHelpers.cpp:81
Position::distanceTo
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:233
ProcessError
Definition: UtilExceptions.h:39
NBHelpers.h
StringUtils::startsWith
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
Definition: StringUtils.cpp:174
NBHelpers::interpretLaneID
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:120
Position.h
StringUtils.h
StringUtils::toInt
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
Definition: StringUtils.cpp:278
config.h
GeomHelper.h
StringTokenizer.h
NBNode
Represents a single node (junction) during network building.
Definition: NBNode.h:67
NBNode.h
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
NBHelpers::loadEdgesFromFile
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:87