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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // The class storing the generated network
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <iostream>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <cmath>
32 #include <netbuild/NBNode.h>
33 #include <netbuild/NBNodeCont.h>
34 #include <netbuild/NBEdge.h>
35 #include <netbuild/NBEdgeCont.h>
36 #include <netbuild/NBNetBuilder.h>
37 #include <utils/common/ToString.h>
40 #include "NGNet.h"
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
47  myLastID(0),
48  myAlphaIDs(OptionsCont::getOptions().getBool("alphanumerical-ids")),
49  myNetBuilder(nb) {
50 }
51 
52 
54  for (NGEdgeList::iterator ni = myEdgeList.begin(); ni != myEdgeList.end(); ++ni) {
55  delete *ni;
56  }
57  for (NGNodeList::iterator ni = myNodeList.begin(); ni != myNodeList.end(); ++ni) {
58  delete *ni;
59  }
60 }
61 
62 
63 std::string
65  return toString<int>(++myLastID);
66 }
67 
68 
69 NGNode*
70 NGNet::findNode(int xID, int yID) {
71  for (NGNodeList::iterator ni = myNodeList.begin(); ni != myNodeList.end(); ++ni) {
72  if ((*ni)->samePos(xID, yID)) {
73  return *ni;
74  }
75  }
76  return 0;
77 }
78 
79 std::string
80 NGNet::alphabeticalCode(int i, int iMax) {
81  // lazy mans 26th root to determine number of characters for x-label
82  int xn = 1;
83  for (; std::pow(26, xn) < iMax; xn++) {};
84  std::string result = "";
85  for (int j = 0; j < xn; j++) {
86  result = char('A' + (i % 26)) + result;
87  i /= 26;
88  }
89  return result;
90 }
91 
92 void
93 NGNet::createChequerBoard(int numX, int numY, double spaceX, double spaceY, double attachLength) {
94 
95  for (int ix = 0; ix < numX; ix++) {
96  const std::string nodeIDStart = (myAlphaIDs ? alphabeticalCode(ix, numX) : toString<int>(ix) + "/");
97  for (int iy = 0; iy < numY; iy++) {
98  // create Node
99  NGNode* node = new NGNode(nodeIDStart + toString(iy), 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 double
146 NGNet::radialToX(double radius, double phi) {
147  return cos(phi) * radius;
148 }
149 
150 
151 double
152 NGNet::radialToY(double radius, double phi) {
153  return sin(phi) * radius;
154 }
155 
156 
157 void
158 NGNet::createSpiderWeb(int numRadDiv, int numCircles, double 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  double angle = (double)(2 * M_PI / numRadDiv); // angle between radial divisions
168  NGNode* Node;
169  for (ic = 1; ic < numCircles + 1; ic++) {
170  const std::string nodeIDStart = alphabeticalCode(ic, numCircles);
171  for (ir = 1; ir < numRadDiv + 1; ir++) {
172  // create Node
173  const std::string nodeID = (myAlphaIDs ?
174  nodeIDStart + toString<int>(ir) :
175  toString<int>(ir) + "/" + toString<int>(ic));
176  Node = new NGNode(nodeID, ir, ic);
177  Node->setX(radialToX((ic) * spaceRad, (ir - 1) * angle));
178  Node->setY(radialToY((ic) * spaceRad, (ir - 1) * angle));
179  myNodeList.push_back(Node);
180  // create Links
181  if (ir > 1) {
182  connect(Node, findNode(ir - 1, ic));
183  }
184  if (ic > 1) {
185  connect(Node, findNode(ir, ic - 1));
186  }
187  if (ir == numRadDiv) {
188  connect(Node, findNode(1, ic));
189  }
190  }
191  }
192  if (hasCenter) {
193  // node
194  Node = new NGNode(myAlphaIDs ? "A1" : "1", 0, 0, true);
195  Node->setX(0);
196  Node->setY(0);
197  myNodeList.push_back(Node);
198  // links
199  for (ir = 1; ir < numRadDiv + 1; ir++) {
200  connect(Node, findNode(ir, 1));
201  }
202  }
203 }
204 
205 
206 void
207 NGNet::connect(NGNode* node1, NGNode* node2) {
208  std::string id1 = node1->getID() + (myAlphaIDs ? "" : "to") + node2->getID();
209  std::string id2 = node2->getID() + (myAlphaIDs ? "" : "to") + node1->getID();
210  NGEdge* link1 = new NGEdge(id1, node1, node2);
211  NGEdge* link2 = new NGEdge(id2, node2, node1);
212  myEdgeList.push_back(link1);
213  myEdgeList.push_back(link2);
214 }
215 
216 
217 void
218 NGNet::toNB() const {
219  std::vector<NBNode*> nodes;
220  for (NGNodeList::const_iterator i1 = myNodeList.begin(); i1 != myNodeList.end(); i1++) {
221  NBNode* node = (*i1)->buildNBNode(myNetBuilder);
222  nodes.push_back(node);
224  }
225  for (NGEdgeList::const_iterator i2 = myEdgeList.begin(); i2 != myEdgeList.end(); i2++) {
226  NBEdge* edge = (*i2)->buildNBEdge(myNetBuilder);
228  }
229  // now, let's append the reverse directions...
230  double bidiProb = OptionsCont::getOptions().getFloat("rand.bidi-probability");
231  for (std::vector<NBNode*>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
232  NBNode* node = *i;
233  for (NBEdge* e : node->getIncomingEdges()) {
234  if (node->getConnectionTo(e->getFromNode()) == 0 && RandHelper::rand() <= bidiProb) {
235  NBEdge* back = new NBEdge("-" + e->getID(), node, e->getFromNode(),
237  e->getNumLanes(),
238  e->getPriority(),
241  }
242  }
243  }
244  // add splits depending on turn-lane options
245  const int turnLanes = OptionsCont::getOptions().getInt("turn-lanes");
246  const bool lefthand = OptionsCont::getOptions().getBool("lefthand");
247  if (turnLanes > 0) {
248  const double turnLaneLength = OptionsCont::getOptions().getFloat("turn-lanes.length");
250  EdgeVector allEdges;
251  for (auto it = ec.begin(); it != ec.end(); ++it) {
252  allEdges.push_back(it->second);
253  }
254  for (NBEdge* e : allEdges) {
255  if (e->getToNode()->geometryLike()) {
256  continue;
257  }
258  std::vector<NBEdgeCont::Split> splits;
260  for (int i = 0; i < e->getNumLanes() + turnLanes; ++i) {
261  split.lanes.push_back(i);
262  }
263  split.pos = MAX2(0.0, e->getLength() - turnLaneLength);
264  split.speed = e->getSpeed();
265  split.node = new NBNode(e->getID() + "." + toString(split.pos), e->getGeometry().positionAtOffset(split.pos));
266  split.idBefore = e->getID();
267  split.idAfter = split.node->getID();
268  split.offsetFactor = lefthand ? -1 : 1;
269  if (turnLaneLength <= e->getLength() / 2) {
270  split.offset = -0.5 * split.offsetFactor * turnLanes * e->getLaneWidth(0);
271  if (e->getFromNode()->geometryLike()) {
272  // shift the reverse direction explicitly as it will not get a turn lane
273  NBEdge* reverse = 0;
274  for (NBEdge* reverseCand : e->getFromNode()->getIncomingEdges()) {
275  if (reverseCand->getFromNode() == e->getToNode()) {
276  reverse = reverseCand;
277  }
278  }
279  if (reverse != 0) {
280  PositionVector g = reverse->getGeometry();
281  g.move2side(-split.offset);
282  reverse->setGeometry(g);
283  }
284  }
285  }
286  splits.push_back(split);
287  ec.processSplits(e, splits,
291  }
292  }
293 }
294 
295 
296 void
298  myNodeList.push_back(node);
299 }
300 
301 
302 void
304  myEdgeList.push_back(edge);
305 }
306 
307 
308 int
309 NGNet::nodeNo() const {
310  return (int)myNodeList.size();
311 }
312 
313 
314 /****************************************************************************/
315 
NBNetBuilder & myNetBuilder
The builder used to build NB*-structures.
Definition: NGNet.h:206
NGNet(NBNetBuilder &nb)
Constructor.
Definition: NGNet.cpp:46
double getSpeed(const std::string &type) const
Returns the maximal velocity for the given type [m/s].
Definition: NBTypeCont.cpp:174
std::string alphabeticalCode(int i, int iMax)
return a letter code for the given integer index
Definition: NGNet.cpp:80
NBTypeCont & getTypeCont()
Returns a reference to the type container.
Definition: NBNetBuilder.h:160
A netgen-representation of an edge.
Definition: NGEdge.h:55
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
A structure which describes changes of lane number or speed along the road.
Definition: NBEdgeCont.h:206
double radialToX(double radius, double phi)
Returns the x-position resulting from the given radius and angle.
Definition: NGNet.cpp:146
void connect(NGNode *node1, NGNode *node2)
Connects both nodes with two edges, one for each direction.
Definition: NGNet.cpp:207
void toNB() const
Converts the stored network into its netbuilder-representation.
Definition: NGNet.cpp:218
The representation of a single edge during network building.
Definition: NBEdge.h:65
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:61
NGNode * findNode(int xPos, int yPos)
Returns the node at the given position.
Definition: NGNet.cpp:70
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:259
double radialToY(double radius, double phi)
Returns the y-position resulting from the given radius and angle.
Definition: NGNet.cpp:152
int nodeNo() const
Returns the number of stored nodes.
Definition: NGNet.cpp:309
T MAX2(T a, T b)
Definition: StdDefs.h:76
std::string idAfter
The id for the edge after the split.
Definition: NBEdgeCont.h:219
std::map< std::string, NBEdge * >::const_iterator end() const
Returns the pointer to the end of the stored edges.
Definition: NBEdgeCont.h:193
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const std::string & getID() const
Returns the id.
Definition: Named.h:69
void setGeometry(const PositionVector &g, bool inner=false)
(Re)sets the edge&#39;s geometry
Definition: NBEdge.cpp:552
NGEdgeList myEdgeList
The list of links.
Definition: NGNet.h:212
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
double getWidth(const std::string &type) const
Returns the lane width for the given type [m].
Definition: NBTypeCont.cpp:210
double pos
The position of this change.
Definition: NBEdgeCont.h:211
NBEdge * getConnectionTo(NBNode *n) const
get connection to certain node
Definition: NBNode.cpp:1929
NBNode * node
The new node that is created for this split.
Definition: NBEdgeCont.h:215
void setX(double x)
Sets a new value for x-position.
Definition: NGNode.h:114
std::map< std::string, NBEdge * >::const_iterator begin() const
Returns the pointer to the begin of the stored edges.
Definition: NBEdgeCont.h:185
bool insert(NBEdge *edge, bool ignorePrunning=false)
Adds an edge to the dictionary.
Definition: NBEdgeCont.cpp:152
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
int offsetFactor
direction in which to apply the offset (used by netgenerate for lefthand networks) ...
Definition: NBEdgeCont.h:225
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:150
A list of positions.
std::string getNextFreeID()
Returns the next free id.
Definition: NGNet.cpp:64
NGNodeList myNodeList
The list of nodes.
Definition: NGNet.h:209
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:61
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
void processSplits(NBEdge *e, std::vector< Split > splits, NBNodeCont &nc, NBDistrictCont &dc, NBTrafficLightLogicCont &tlc)
Definition: NBEdgeCont.cpp:411
std::vector< int > lanes
The lanes after this change.
Definition: NBEdgeCont.h:209
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
void move2side(double amount)
move position vector to side using certain ammount
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:620
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:155
int myLastID
The last ID given to node or link.
Definition: NGNet.h:200
Instance responsible for building networks.
Definition: NBNetBuilder.h:109
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:34
void createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter)
Creates a spider network.
Definition: NGNet.cpp:158
A storage for options typed value containers)
Definition: OptionsCont.h:92
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:79
NBTrafficLightLogicCont & getTLLogicCont()
Returns a reference to the traffic light logics container.
Definition: NBNetBuilder.h:165
double speed
The speed after this change.
Definition: NBEdgeCont.h:213
void createChequerBoard(int numX, int numY, double spaceX, double spaceY, double attachLength)
Creates a grid network.
Definition: NGNet.cpp:93
Represents a single node (junction) during network building.
Definition: NBNode.h:68
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:432
A netgen-representation of a node.
Definition: NGNode.h:51
void setY(double y)
Sets a new value for y-position.
Definition: NGNode.h:123
NBDistrictCont & getDistrictCont()
Returns a reference the districts container.
Definition: NBNetBuilder.h:170
const bool myAlphaIDs
Whether to use alphanumericalIDs.
Definition: NGNet.h:203
~NGNet()
Destructor.
Definition: NGNet.cpp:53
std::string idBefore
The id for the edge before the split.
Definition: NBEdgeCont.h:217
void add(NGNode *node)
Adds the given node to the network.
Definition: NGNet.cpp:297
double offset
lateral offset to edge geometry
Definition: NBEdgeCont.h:223