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