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 /****************************************************************************/
9 // The class storing the generated network
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
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 <math.h>
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 #ifdef CHECK_MEMORY_LEAKS
49 #include <foreign/nvwa/debug_new.h>
50 #endif // CHECK_MEMORY_LEAKS
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
57  : myNetBuilder(nb) {
58  myLastID = 0;
59 }
60 
61 
63  for (NGEdgeList::iterator ni = myEdgeList.begin(); ni != myEdgeList.end(); ++ni) {
64  delete *ni;
65  }
66  for (NGNodeList::iterator ni = myNodeList.begin(); ni != myNodeList.end(); ++ni) {
67  delete *ni;
68  }
69 }
70 
71 
72 std::string
74  return toString<int>(++myLastID);
75 }
76 
77 
78 NGNode*
79 NGNet::findNode(int xID, int yID) {
80  for (NGNodeList::iterator ni = myNodeList.begin(); ni != myNodeList.end(); ++ni) {
81  if ((*ni)->samePos(xID, yID)) {
82  return *ni;
83  }
84  }
85  return 0;
86 }
87 
88 
89 void
90 NGNet::createChequerBoard(int numX, int numY, SUMOReal spaceX, SUMOReal spaceY, SUMOReal attachLength) {
91  for (int ix = 0; ix < numX; ix++) {
92  for (int iy = 0; iy < numY; iy++) {
93  // create Node
94  std::string nodeID = toString<int>(ix) + "/" + toString<int>(iy);
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 SUMOReal
143  return cos(phi) * radius;
144 }
145 
146 
147 SUMOReal
149  return sin(phi) * radius;
150 }
151 
152 
153 void
154 NGNet::createSpiderWeb(int numRadDiv, int numCircles, SUMOReal 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  SUMOReal angle = (SUMOReal)(2 * 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  SUMOReal 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 size_t
253 NGNet::nodeNo() const {
254  return myNodeList.size();
255 }
256 
257 
258 /****************************************************************************/
259