SUMO - Simulation of Urban MObility
NGNet.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2003-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 // The class storing the generated network
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 <iostream>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <cmath>
38 #include <netbuild/NBNode.h>
39 #include <netbuild/NBNodeCont.h>
40 #include <netbuild/NBEdge.h>
41 #include <netbuild/NBEdgeCont.h>
42 #include <netbuild/NBNetBuilder.h>
43 #include <utils/common/ToString.h>
46 #include "NGNet.h"
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
53  : myNetBuilder(nb) {
54  myLastID = 0;
55 }
56 
57 
59  for (NGEdgeList::iterator ni = myEdgeList.begin(); ni != myEdgeList.end(); ++ni) {
60  delete *ni;
61  }
62  for (NGNodeList::iterator ni = myNodeList.begin(); ni != myNodeList.end(); ++ni) {
63  delete *ni;
64  }
65 }
66 
67 
68 std::string
70  return toString<int>(++myLastID);
71 }
72 
73 
74 NGNode*
75 NGNet::findNode(int xID, int yID) {
76  for (NGNodeList::iterator ni = myNodeList.begin(); ni != myNodeList.end(); ++ni) {
77  if ((*ni)->samePos(xID, yID)) {
78  return *ni;
79  }
80  }
81  return 0;
82 }
83 
84 
85 void
86 NGNet::createChequerBoard(int numX, int numY, double spaceX, double spaceY, double attachLength, bool alphaIDs) {
87  for (int ix = 0; ix < numX; ix++) {
88  for (int iy = 0; iy < numY; iy++) {
89  // create Node
90  std::string nodeID = toString<int>(ix) + "/" + toString<int>(iy);
91  if (alphaIDs) {
92  nodeID = toString(char('A' + ix)) + toString(iy);
93  }
94  NGNode* node = new NGNode(nodeID, ix, iy);
95  node->setX(ix * spaceX + attachLength);
96  node->setY(iy * spaceY + attachLength);
97  myNodeList.push_back(node);
98  // create Links
99  if (ix > 0) {
100  connect(node, findNode(ix - 1, iy));
101  }
102  if (iy > 0) {
103  connect(node, findNode(ix, iy - 1));
104  }
105  }
106  }
107  if (attachLength > 0.0) {
108  for (int ix = 0; ix < numX; ix++) {
109  // create nodes
110  NGNode* topNode = new NGNode("top" + toString<int>(ix), ix, numY);
111  NGNode* bottomNode = new NGNode("bottom" + toString<int>(ix), ix, numY + 1);
112  topNode->setX(ix * spaceX + attachLength);
113  bottomNode->setX(ix * spaceX + attachLength);
114  topNode->setY((numY - 1) * spaceY + 2 * attachLength);
115  bottomNode->setY(0);
116  myNodeList.push_back(topNode);
117  myNodeList.push_back(bottomNode);
118  // create links
119  connect(topNode, findNode(ix, numY - 1));
120  connect(bottomNode, findNode(ix, 0));
121  }
122  for (int iy = 0; iy < numY; iy++) {
123  // create nodes
124  NGNode* leftNode = new NGNode("left" + toString<int>(iy), numX, iy);
125  NGNode* rightNode = new NGNode("right" + toString<int>(iy), numX + 1, iy);
126  leftNode->setX(0);
127  rightNode->setX((numX - 1) * spaceX + 2 * attachLength);
128  leftNode->setY(iy * spaceY + attachLength);
129  rightNode->setY(iy * spaceY + attachLength);
130  myNodeList.push_back(leftNode);
131  myNodeList.push_back(rightNode);
132  // create links
133  connect(leftNode, findNode(0, iy));
134  connect(rightNode, findNode(numX - 1, iy));
135  }
136  }
137 }
138 
139 
140 double
141 NGNet::radialToX(double radius, double phi) {
142  return cos(phi) * radius;
143 }
144 
145 
146 double
147 NGNet::radialToY(double radius, double phi) {
148  return sin(phi) * radius;
149 }
150 
151 
152 void
153 NGNet::createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter) {
154  if (numRadDiv < 3) {
155  numRadDiv = 3;
156  }
157  if (numCircles < 1) {
158  numCircles = 1;
159  }
160 
161  int ir, ic;
162  double angle = (double)(2 * M_PI / numRadDiv); // angle between radial divisions
163  NGNode* Node;
164  for (ir = 1; ir < numRadDiv + 1; ir++) {
165  for (ic = 1; ic < numCircles + 1; ic++) {
166  // create Node
167  Node = new NGNode(
168  toString<int>(ir) + "/" + toString<int>(ic), ir, ic);
169  Node->setX(radialToX((ic) * spaceRad, (ir - 1) * angle));
170  Node->setY(radialToY((ic) * spaceRad, (ir - 1) * angle));
171  myNodeList.push_back(Node);
172  // create Links
173  if (ir > 1) {
174  connect(Node, findNode(ir - 1, ic));
175  }
176  if (ic > 1) {
177  connect(Node, findNode(ir, ic - 1));
178  }
179  if (ir == numRadDiv) {
180  connect(Node, findNode(1, ic));
181  }
182  }
183  }
184  if (hasCenter) {
185  // node
186  Node = new NGNode(getNextFreeID(), 0, 0, true);
187  Node->setX(0);
188  Node->setY(0);
189  myNodeList.push_back(Node);
190  // links
191  for (ir = 1; ir < numRadDiv + 1; ir++) {
192  connect(Node, findNode(ir, 1));
193  }
194  }
195 }
196 
197 
198 void
199 NGNet::connect(NGNode* node1, NGNode* node2) {
200  std::string id1 = node1->getID() + "to" + node2->getID();
201  std::string id2 = node2->getID() + "to" + node1->getID();
202  NGEdge* link1 = new NGEdge(id1, node1, node2);
203  NGEdge* link2 = new NGEdge(id2, node2, node1);
204  myEdgeList.push_back(link1);
205  myEdgeList.push_back(link2);
206 }
207 
208 
209 void
210 NGNet::toNB() const {
211  std::vector<NBNode*> nodes;
212  for (NGNodeList::const_iterator i1 = myNodeList.begin(); i1 != myNodeList.end(); i1++) {
213  NBNode* node = (*i1)->buildNBNode(myNetBuilder);
214  nodes.push_back(node);
216  }
217  for (NGEdgeList::const_iterator i2 = myEdgeList.begin(); i2 != myEdgeList.end(); i2++) {
218  NBEdge* edge = (*i2)->buildNBEdge(myNetBuilder);
220  }
221  // now, let's append the reverse directions...
222  double bidiProb = OptionsCont::getOptions().getFloat("rand.bidi-probability");
223  for (std::vector<NBNode*>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
224  NBNode* node = *i;
225  EdgeVector incoming = node->getIncomingEdges();
226  for (EdgeVector::const_iterator j = incoming.begin(); j != incoming.end(); ++j) {
227  if (node->getConnectionTo((*j)->getFromNode()) == 0 && RandHelper::rand() <= bidiProb) {
228  NBEdge* back = new NBEdge("-" + (*j)->getID(), node, (*j)->getFromNode(),
233  }
234  }
235  }
236 }
237 
238 
239 void
241  myNodeList.push_back(node);
242 }
243 
244 
245 void
247  myEdgeList.push_back(edge);
248 }
249 
250 
251 int
252 NGNet::nodeNo() const {
253  return (int)myNodeList.size();
254 }
255 
256 
257 /****************************************************************************/
258 
NBNetBuilder & myNetBuilder
The builder used to build NB*-structures.
Definition: NGNet.h:207
NGNet(NBNetBuilder &nb)
Constructor.
Definition: NGNet.cpp:52
double getSpeed(const std::string &type) const
Returns the maximal velocity for the given type [m/s].
Definition: NBTypeCont.cpp:180
NBTypeCont & getTypeCont()
Returns a reference to the type container.
Definition: NBNetBuilder.h:166
A netgen-representation of an edge.
Definition: NGEdge.h:61
double radialToX(double radius, double phi)
Returns the x-position resulting from the given radius and angle.
Definition: NGNet.cpp:141
void connect(NGNode *node1, NGNode *node2)
Connects both nodes with two edges, one for each direction.
Definition: NGNet.cpp:199
void toNB() const
Converts the stored network into its netbuilder-representation.
Definition: NGNet.cpp:210
The representation of a single edge during network building.
Definition: NBEdge.h:70
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:64
NGNode * findNode(int xPos, int yPos)
Returns the node at the given position.
Definition: NGNet.cpp:75
int getPriority(const std::string &type) const
Returns the priority for the given type.
Definition: NBTypeCont.cpp:186
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:257
double radialToY(double radius, double phi)
Returns the y-position resulting from the given radius and angle.
Definition: NGNet.cpp:147
int nodeNo() const
Returns the number of stored nodes.
Definition: NGNet.cpp:252
int getNumLanes(const std::string &type) const
Returns the number of lanes for the given type.
Definition: NBTypeCont.cpp:174
const std::string & getID() const
Returns the id.
Definition: Named.h:65
NGEdgeList myEdgeList
The list of links.
Definition: NGNet.h:213
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
double getWidth(const std::string &type) const
Returns the lane width for the given type [m].
Definition: NBTypeCont.cpp:216
NBEdge * getConnectionTo(NBNode *n) const
get connection to certain node
Definition: NBNode.cpp:1744
void setX(double x)
Sets a new value for x-position.
Definition: NGNode.h:120
bool insert(NBEdge *edge, bool ignorePrunning=false)
Adds an edge to the dictionary.
Definition: NBEdgeCont.cpp:157
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:156
std::string getNextFreeID()
Returns the next free id.
Definition: NGNet.cpp:69
NGNodeList myNodeList
The list of nodes.
Definition: NGNet.h:210
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
const EdgeVector & getIncomingEdges() const
Returns this node&#39;s incoming edges (The edges which yield in this node)
Definition: NBNode.h:249
#define M_PI
Definition: odrSpiral.cpp:40
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:161
int myLastID
The last ID given to node or link.
Definition: NGNet.h:204
Instance responsible for building networks.
Definition: NBNetBuilder.h:115
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:40
void createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter)
Creates a spider network.
Definition: NGNet.cpp:153
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:79
Represents a single node (junction) during network building.
Definition: NBNode.h:74
void createChequerBoard(int numX, int numY, double spaceX, double spaceY, double attachLength, bool alphaIDs)
Creates a grid network.
Definition: NGNet.cpp:86
A netgen-representation of a node.
Definition: NGNode.h:57
void setY(double y)
Sets a new value for y-position.
Definition: NGNode.h:129
~NGNet()
Destructor.
Definition: NGNet.cpp:58
void add(NGNode *node)
Adds the given node to the network.
Definition: NGNet.cpp:240