SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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-sim.org/
13 // Copyright (C) 2003-2014 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) {
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  NGNode* node = new NGNode(nodeID, ix, iy);
97  node->setX(ix * spaceX + attachLength);
98  node->setY(iy * spaceY + attachLength);
99  myNodeList.push_back(node);
100  // create Links
101  if (ix > 0) {
102  connect(node, findNode(ix - 1, iy));
103  }
104  if (iy > 0) {
105  connect(node, findNode(ix, iy - 1));
106  }
107  }
108  }
109  if (attachLength > 0.0) {
110  for (int ix = 0; ix < numX; ix++) {
111  // create nodes
112  NGNode* topNode = new NGNode("top" + toString<int>(ix), ix, numY);
113  NGNode* bottomNode = new NGNode("bottom" + toString<int>(ix), ix, numY + 1);
114  topNode->setX(ix * spaceX + attachLength);
115  bottomNode->setX(ix * spaceX + attachLength);
116  topNode->setY((numY - 1) * spaceY + 2 * attachLength);
117  bottomNode->setY(0);
118  myNodeList.push_back(topNode);
119  myNodeList.push_back(bottomNode);
120  // create links
121  connect(topNode, findNode(ix, numY - 1));
122  connect(bottomNode, findNode(ix, 0));
123  }
124  for (int iy = 0; iy < numY; iy++) {
125  // create nodes
126  NGNode* leftNode = new NGNode("left" + toString<int>(iy), numX, iy);
127  NGNode* rightNode = new NGNode("right" + toString<int>(iy), numX + 1, iy);
128  leftNode->setX(0);
129  rightNode->setX((numX - 1) * spaceX + 2 * attachLength);
130  leftNode->setY(iy * spaceY + attachLength);
131  rightNode->setY(iy * spaceY + attachLength);
132  myNodeList.push_back(leftNode);
133  myNodeList.push_back(rightNode);
134  // create links
135  connect(leftNode, findNode(0, iy));
136  connect(rightNode, findNode(numX - 1, iy));
137  }
138  }
139 }
140 
141 
142 SUMOReal
144  return cos(phi) * radius;
145 }
146 
147 
148 SUMOReal
150  return sin(phi) * radius;
151 }
152 
153 
154 void
155 NGNet::createSpiderWeb(int numRadDiv, int numCircles, SUMOReal spaceRad, bool hasCenter) {
156  if (numRadDiv < 3) {
157  numRadDiv = 3;
158  }
159  if (numCircles < 1) {
160  numCircles = 1;
161  }
162 
163  int ir, ic;
164  SUMOReal angle = (SUMOReal)(2 * M_PI / numRadDiv); // angle between radial divisions
165  NGNode* Node;
166  for (ir = 1; ir < numRadDiv + 1; ir++) {
167  for (ic = 1; ic < numCircles + 1; ic++) {
168  // create Node
169  Node = new NGNode(
170  toString<int>(ir) + "/" + toString<int>(ic), ir, ic);
171  Node->setX(radialToX((ic) * spaceRad, (ir - 1) * angle));
172  Node->setY(radialToY((ic) * spaceRad, (ir - 1) * angle));
173  myNodeList.push_back(Node);
174  // create Links
175  if (ir > 1) {
176  connect(Node, findNode(ir - 1, ic));
177  }
178  if (ic > 1) {
179  connect(Node, findNode(ir, ic - 1));
180  }
181  if (ir == numRadDiv) {
182  connect(Node, findNode(1, ic));
183  }
184  }
185  }
186  if (hasCenter) {
187  // node
188  Node = new NGNode(getNextFreeID(), 0, 0, true);
189  Node->setX(0);
190  Node->setY(0);
191  myNodeList.push_back(Node);
192  // links
193  for (ir = 1; ir < numRadDiv + 1; ir++) {
194  connect(Node, findNode(ir, 1));
195  }
196  }
197 }
198 
199 
200 void
201 NGNet::connect(NGNode* node1, NGNode* node2) {
202  std::string id1 = node1->getID() + "to" + node2->getID();
203  std::string id2 = node2->getID() + "to" + node1->getID();
204  NGEdge* link1 = new NGEdge(id1, node1, node2);
205  NGEdge* link2 = new NGEdge(id2, node2, node1);
206  myEdgeList.push_back(link1);
207  myEdgeList.push_back(link2);
208 }
209 
210 
211 void
212 NGNet::toNB() const {
213  std::vector<NBNode*> nodes;
214  for (NGNodeList::const_iterator i1 = myNodeList.begin(); i1 != myNodeList.end(); i1++) {
215  NBNode* node = (*i1)->buildNBNode(myNetBuilder);
216  nodes.push_back(node);
218  }
219  for (NGEdgeList::const_iterator i2 = myEdgeList.begin(); i2 != myEdgeList.end(); i2++) {
220  NBEdge* edge = (*i2)->buildNBEdge(myNetBuilder);
222  }
223  // now, let's append the reverse directions...
224  SUMOReal bidiProb = OptionsCont::getOptions().getFloat("rand.bidi-probability");
225  for (std::vector<NBNode*>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
226  NBNode* node = *i;
227  EdgeVector incoming = node->getIncomingEdges();
228  for (EdgeVector::const_iterator j = incoming.begin(); j != incoming.end(); ++j) {
229  if (node->getConnectionTo((*j)->getFromNode()) == 0 && RandHelper::rand() <= bidiProb) {
230  NBEdge* back = new NBEdge("-" + (*j)->getID(), node, (*j)->getFromNode(),
235  }
236  }
237  }
238 }
239 
240 
241 void
243  myNodeList.push_back(node);
244 }
245 
246 
247 void
249  myEdgeList.push_back(edge);
250 }
251 
252 
253 size_t
254 NGNet::nodeNo() const {
255  return myNodeList.size();
256 }
257 
258 
259 /****************************************************************************/
260