SUMO - Simulation of Urban MObility
NBHelpers.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Some mathematical helper methods
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <cmath>
35 #include <string>
36 #include <sstream>
37 #include <iostream>
38 #include <fstream>
39 //#include <iomanip>
42 #include <utils/geom/Position.h>
43 #include <utils/geom/GeomHelper.h>
44 #include "NBNode.h"
45 #include "NBHelpers.h"
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 double
52 NBHelpers::relAngle(double angle1, double angle2) {
53  angle2 -= angle1;
54  if (angle2 > 180) {
55  angle2 = (360 - angle2) * -1;
56  }
57  while (angle2 < -180) {
58  angle2 = 360 + angle2;
59  }
60  return angle2;
61 }
62 
63 
64 double
65 NBHelpers::normRelAngle(double angle1, double angle2) {
66  double rel = relAngle(angle1, angle2);
67  if (rel + NUMERICAL_EPS >= 180) {
68  return -180;
69  } else {
70  return rel;
71  }
72 }
73 
74 
75 std::string
76 NBHelpers::normalIDRepresentation(const std::string& id) {
77  std::stringstream strm1(id);
78  long numid;
79  strm1 >> numid;
80  std::stringstream strm2;
81  strm2 << numid;
82  return strm2.str();
83 }
84 
85 
86 double
88  return node1->getPosition().distanceTo(node2->getPosition());
89 }
90 
91 
92 void
93 NBHelpers::loadEdgesFromFile(const std::string& file, std::set<std::string>& into) {
94  std::ifstream strm(file.c_str());
95  if (!strm.good()) {
96  throw ProcessError("Could not load names of edges too keep from '" + file + "'.");
97  }
98  while (strm.good()) {
99  std::string name;
100  strm >> name;
101  into.insert(name);
102  // maybe we're loading an edge-selection
103  if (StringUtils::startsWith(name, "edge:")) {
104  into.insert(name.substr(5));
105  }
106  }
107 }
108 
109 
110 void
111 NBHelpers::loadPrefixedIDsFomFile(const std::string& file, const std::string prefix, std::set<std::string>& into) {
112  std::ifstream strm(file.c_str());
113  if (!strm.good()) {
114  throw ProcessError("Could not load IDs from '" + file + "'.");
115  }
116  while (strm.good()) {
117  std::string prefixedID;
118  strm >> prefixedID;
119  if (StringUtils::startsWith(prefixedID, prefix)) {
120  into.insert(prefixedID.substr(prefix.size()));
121  }
122  }
123 }
124 
125 
126 /****************************************************************************/
static double relAngle(double angle1, double angle2)
computes the relative angle between the two angles
Definition: NBHelpers.cpp:52
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:111
static double normRelAngle(double angle1, double angle2)
ensure that reverse relAngles (>=179.999) always count as turnarounds (-180)
Definition: NBHelpers.cpp:65
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:93
static std::string normalIDRepresentation(const std::string &id)
converts the numerical id to its "normal" string representation
Definition: NBHelpers.cpp:76
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
static double distance(NBNode *node1, NBNode *node2)
returns the distance between both nodes
Definition: NBHelpers.cpp:87
const Position & getPosition() const
Definition: NBNode.h:232
Represents a single node (junction) during network building.
Definition: NBNode.h:75
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:240
#define NUMERICAL_EPS
Definition: config.h:151