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 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
57  angle2 -= angle1;
58  if (angle2 > 180) {
59  angle2 = (360 - angle2) * -1;
60  }
61  while (angle2 < -180) {
62  angle2 = 360 + angle2;
63  }
64  return angle2;
65 }
66 
67 
70  SUMOReal rel = relAngle(angle1, angle2);
71  if (rel + NUMERICAL_EPS >= 180) {
72  return -180;
73  } else {
74  return rel;
75  }
76 }
77 
78 
79 std::string
80 NBHelpers::normalIDRepresentation(const std::string& id) {
81  std::stringstream strm1(id);
82  long numid;
83  strm1 >> numid;
84  std::stringstream strm2;
85  strm2 << numid;
86  return strm2.str();
87 }
88 
89 
92  return node1->getPosition().distanceTo(node2->getPosition());
93 }
94 
95 
96 void
97 NBHelpers::loadEdgesFromFile(const std::string& file, std::set<std::string>& into) {
98  std::ifstream strm(file.c_str());
99  if (!strm.good()) {
100  throw ProcessError("Could not load names of edges too keep from '" + file + "'.");
101  }
102  while (strm.good()) {
103  std::string name;
104  strm >> name;
105  into.insert(name);
106  // maybe we're loading an edge-selection
107  if (StringUtils::startsWith(name, "edge:")) {
108  into.insert(name.substr(5));
109  }
110  }
111 }
112 
113 
114 /****************************************************************************/
SUMOReal distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:221
static SUMOReal normRelAngle(SUMOReal angle1, SUMOReal angle2)
ensure that reverse relAngles (>=179.999) always count as turnarounds (-180)
Definition: NBHelpers.cpp:69
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:97
static std::string normalIDRepresentation(const std::string &id)
Definition: NBHelpers.cpp:80
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
const Position & getPosition() const
Returns the position of this node.
Definition: NBNode.h:228
Represents a single node (junction) during network building.
Definition: NBNode.h:74
#define SUMOReal
Definition: config.h:213
static SUMOReal relAngle(SUMOReal angle1, SUMOReal angle2)
Definition: NBHelpers.cpp:56
#define NUMERICAL_EPS
Definition: config.h:160
static SUMOReal distance(NBNode *node1, NBNode *node2)
Definition: NBHelpers.cpp:91