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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // Some mathematical helper methods
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <cmath>
34 #include <string>
35 #include <sstream>
36 #include <iostream>
37 #include <fstream>
38 //#include <iomanip>
43 #include <utils/geom/Position.h>
44 #include <utils/geom/GeomHelper.h>
45 #include "NBNode.h"
46 #include "NBHelpers.h"
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 double
53 NBHelpers::relAngle(double angle1, double angle2) {
54  angle2 -= angle1;
55  if (angle2 > 180) {
56  angle2 = (360 - angle2) * -1;
57  }
58  while (angle2 < -180) {
59  angle2 = 360 + angle2;
60  }
61  return angle2;
62 }
63 
64 
65 double
66 NBHelpers::normRelAngle(double angle1, double angle2) {
67  double rel = relAngle(angle1, angle2);
68  if (rel + NUMERICAL_EPS >= 180) {
69  return -180;
70  } else {
71  return rel;
72  }
73 }
74 
75 
76 std::string
77 NBHelpers::normalIDRepresentation(const std::string& id) {
78  std::stringstream strm1(id);
79  long numid;
80  strm1 >> numid;
81  std::stringstream strm2;
82  strm2 << numid;
83  return strm2.str();
84 }
85 
86 
87 double
89  return node1->getPosition().distanceTo(node2->getPosition());
90 }
91 
92 
93 void
94 NBHelpers::loadEdgesFromFile(const std::string& file, std::set<std::string>& into) {
95  std::ifstream strm(file.c_str());
96  if (!strm.good()) {
97  throw ProcessError("Could not load names of edges too keep from '" + file + "'.");
98  }
99  while (strm.good()) {
100  std::string name;
101  strm >> name;
102  into.insert(name);
103  // maybe we're loading an edge-selection
104  if (StringUtils::startsWith(name, "edge:")) {
105  into.insert(name.substr(5));
106  }
107  }
108 }
109 
110 
111 void
112 NBHelpers::loadPrefixedIDsFomFile(const std::string& file, const std::string prefix, std::set<std::string>& into) {
113  std::ifstream strm(file.c_str());
114  if (!strm.good()) {
115  throw ProcessError("Could not load IDs from '" + file + "'.");
116  }
117  while (strm.good()) {
118  std::string prefixedID;
119  strm >> prefixedID;
120  if (StringUtils::startsWith(prefixedID, prefix)) {
121  into.insert(prefixedID.substr(prefix.size()));
122  }
123  }
124 }
125 
126 void
127 NBHelpers::interpretLaneID(const std::string& lane_id, std::string& edge_id, int& index) {
128  // assume lane_id = edge_id + '_' + index
129  const std::string::size_type sep_index = lane_id.rfind('_');
130  if (sep_index == std::string::npos) {
131  WRITE_ERROR("Invalid lane id '" + lane_id + "' (missing '_').");
132  }
133  edge_id = lane_id.substr(0, sep_index);
134  std::string index_string = lane_id.substr(sep_index + 1);
135  try {
136  index = TplConvert::_2int(index_string.c_str());
137  } catch (NumberFormatException) {
138  WRITE_ERROR("Invalid lane index '" + index_string + "' for lane '" + lane_id + "'.");
139  }
140 }
141 
142 /****************************************************************************/
static double relAngle(double angle1, double angle2)
computes the relative angle between the two angles
Definition: NBHelpers.cpp:53
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:112
static double normRelAngle(double angle1, double angle2)
ensure that reverse relAngles (>=179.999) always count as turnarounds (-180)
Definition: NBHelpers.cpp:66
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:94
static std::string normalIDRepresentation(const std::string &id)
converts the numerical id to its "normal" string representation
Definition: NBHelpers.cpp:77
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:155
static double distance(NBNode *node1, NBNode *node2)
returns the distance between both nodes
Definition: NBHelpers.cpp:88
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:127
const Position & getPosition() const
Definition: NBNode.h:241
Represents a single node (junction) during network building.
Definition: NBNode.h:74
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:239
#define NUMERICAL_EPS
Definition: config.h:151