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