Eclipse 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-2019 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>
42 #include "NGNet.h"
43 
44 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
49  myLastID(0),
50  myAlphaIDs(OptionsCont::getOptions().getBool("alphanumerical-ids")),
51  myNetBuilder(nb) {
52 }
53 
54 
56  for (NGEdgeList::iterator ni = myEdgeList.begin(); ni != myEdgeList.end(); ++ni) {
57  delete *ni;
58  }
59  for (NGNodeList::iterator ni = myNodeList.begin(); ni != myNodeList.end(); ++ni) {
60  delete *ni;
61  }
62 }
63 
64 
65 std::string
67  return toString<int>(++myLastID);
68 }
69 
70 
71 NGNode*
72 NGNet::findNode(int xID, int yID) {
73  for (NGNodeList::iterator ni = myNodeList.begin(); ni != myNodeList.end(); ++ni) {
74  if ((*ni)->samePos(xID, yID)) {
75  return *ni;
76  }
77  }
78  return nullptr;
79 }
80 
81 std::string
82 NGNet::alphabeticalCode(int i, int iMax) {
83  // lazy mans 26th root to determine number of characters for x-label
84  int xn = 1;
85  for (; std::pow(26, xn) < iMax; xn++) {};
86  std::string result = "";
87  for (int j = 0; j < xn; j++) {
88  result = char('A' + (i % 26)) + result;
89  i /= 26;
90  }
91  return result;
92 }
93 
94 void
95 NGNet::createChequerBoard(int numX, int numY, double spaceX, double spaceY, double attachLength) {
96 
97  for (int ix = 0; ix < numX; ix++) {
98  const std::string nodeIDStart = (myAlphaIDs ? alphabeticalCode(ix, numX) : toString<int>(ix) + "/");
99  for (int iy = 0; iy < numY; iy++) {
100  // create Node
101  NGNode* node = new NGNode(nodeIDStart + toString(iy), ix, iy);
102  node->setX(ix * spaceX + attachLength);
103  node->setY(iy * spaceY + attachLength);
104  myNodeList.push_back(node);
105  // create Links
106  if (ix > 0) {
107  connect(node, findNode(ix - 1, iy));
108  }
109  if (iy > 0) {
110  connect(node, findNode(ix, iy - 1));
111  }
112  }
113  }
114  if (attachLength > 0.0) {
115  for (int ix = 0; ix < numX; ix++) {
116  // create nodes
117  NGNode* topNode = new NGNode("top" + toString<int>(ix), ix, numY);
118  NGNode* bottomNode = new NGNode("bottom" + toString<int>(ix), ix, numY + 1);
119  topNode->setX(ix * spaceX + attachLength);
120  bottomNode->setX(ix * spaceX + attachLength);
121  topNode->setY((numY - 1) * spaceY + 2 * attachLength);
122  bottomNode->setY(0);
123  myNodeList.push_back(topNode);
124  myNodeList.push_back(bottomNode);
125  // create links
126  connect(topNode, findNode(ix, numY - 1));
127  connect(bottomNode, findNode(ix, 0));
128  }
129  for (int iy = 0; iy < numY; iy++) {
130  // create nodes
131  NGNode* leftNode = new NGNode("left" + toString<int>(iy), numX, iy);
132  NGNode* rightNode = new NGNode("right" + toString<int>(iy), numX + 1, iy);
133  leftNode->setX(0);
134  rightNode->setX((numX - 1) * spaceX + 2 * attachLength);
135  leftNode->setY(iy * spaceY + attachLength);
136  rightNode->setY(iy * spaceY + attachLength);
137  myNodeList.push_back(leftNode);
138  myNodeList.push_back(rightNode);
139  // create links
140  connect(leftNode, findNode(0, iy));
141  connect(rightNode, findNode(numX - 1, iy));
142  }
143  }
144 }
145 
146 
147 double
148 NGNet::radialToX(double radius, double phi) {
149  return cos(phi) * radius;
150 }
151 
152 
153 double
154 NGNet::radialToY(double radius, double phi) {
155  return sin(phi) * radius;
156 }
157 
158 
159 void
160 NGNet::createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter) {
161  if (numRadDiv < 3) {
162  numRadDiv = 3;
163  }
164  if (numCircles < 1) {
165  numCircles = 1;
166  }
167 
168  int ir, ic;
169  double angle = (double)(2 * M_PI / numRadDiv); // angle between radial divisions
170  NGNode* Node;
171  for (ic = 1; ic < numCircles + 1; ic++) {
172  const std::string nodeIDStart = alphabeticalCode(ic, numCircles);
173  for (ir = 1; ir < numRadDiv + 1; ir++) {
174  // create Node
175  const std::string nodeID = (myAlphaIDs ?
176  nodeIDStart + toString<int>(ir) :
177  toString<int>(ir) + "/" + toString<int>(ic));
178  Node = new NGNode(nodeID, ir, ic);
179  Node->setX(radialToX((ic) * spaceRad, (ir - 1) * angle));
180  Node->setY(radialToY((ic) * spaceRad, (ir - 1) * angle));
181  myNodeList.push_back(Node);
182  // create Links
183  if (ir > 1) {
184  connect(Node, findNode(ir - 1, ic));
185  }
186  if (ic > 1) {
187  connect(Node, findNode(ir, ic - 1));
188  }
189  if (ir == numRadDiv) {
190  connect(Node, findNode(1, ic));
191  }
192  }
193  }
194  if (hasCenter) {
195  // node
196  Node = new NGNode(myAlphaIDs ? "A1" : "1", 0, 0, true);
197  Node->setX(0);
198  Node->setY(0);
199  myNodeList.push_back(Node);
200  // links
201  for (ir = 1; ir < numRadDiv + 1; ir++) {
202  connect(Node, findNode(ir, 1));
203  }
204  }
205 }
206 
207 
208 void
209 NGNet::connect(NGNode* node1, NGNode* node2) {
210  std::string id1 = node1->getID() + (myAlphaIDs ? "" : "to") + node2->getID();
211  std::string id2 = node2->getID() + (myAlphaIDs ? "" : "to") + node1->getID();
212  NGEdge* link1 = new NGEdge(id1, node1, node2);
213  NGEdge* link2 = new NGEdge(id2, node2, node1);
214  myEdgeList.push_back(link1);
215  myEdgeList.push_back(link2);
216 }
217 
219 NGNet::getDistribution(const std::string& option) {
220  std::string val = OptionsCont::getOptions().getString(option);
221  try {
222  return Distribution_Parameterized("peturb", 0, StringUtils::toDouble(val));
223  } catch (NumberFormatException) {
224  Distribution_Parameterized result("perturb", 0, 0);
225  result.parse(val, true);
226  return result;
227  }
228 }
229 
230 void
231 NGNet::toNB() const {
232  Distribution_Parameterized perturbx = getDistribution("perturb-x");
233  Distribution_Parameterized perturby = getDistribution("perturb-y");
234  Distribution_Parameterized perturbz = getDistribution("perturb-z");
235  std::vector<NBNode*> nodes;
236  for (NGNodeList::const_iterator i1 = myNodeList.begin(); i1 != myNodeList.end(); i1++) {
237  Position perturb(
238  perturbx.sample(),
239  perturby.sample(),
240  perturbz.sample());
241  NBNode* node = (*i1)->buildNBNode(myNetBuilder, perturb);
242  nodes.push_back(node);
244  }
245  for (NGEdgeList::const_iterator i2 = myEdgeList.begin(); i2 != myEdgeList.end(); i2++) {
246  NBEdge* edge = (*i2)->buildNBEdge(myNetBuilder);
248  }
249  // now, let's append the reverse directions...
250  double bidiProb = OptionsCont::getOptions().getFloat("rand.bidi-probability");
251  for (std::vector<NBNode*>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
252  NBNode* node = *i;
253  for (NBEdge* e : node->getIncomingEdges()) {
254  if (node->getConnectionTo(e->getFromNode()) == nullptr && RandHelper::rand() <= bidiProb) {
255  NBEdge* back = new NBEdge("-" + e->getID(), node, e->getFromNode(),
257  e->getNumLanes(),
258  e->getPriority(),
261  }
262  }
263  }
264  // add splits depending on turn-lane options
265  const int turnLanes = OptionsCont::getOptions().getInt("turn-lanes");
266  const bool lefthand = OptionsCont::getOptions().getBool("lefthand");
267  if (turnLanes > 0) {
268  const double turnLaneLength = OptionsCont::getOptions().getFloat("turn-lanes.length");
270  EdgeVector allEdges;
271  for (auto it = ec.begin(); it != ec.end(); ++it) {
272  allEdges.push_back(it->second);
273  }
274  for (NBEdge* e : allEdges) {
275  if (e->getToNode()->geometryLike()) {
276  continue;
277  }
278  std::vector<NBEdgeCont::Split> splits;
280  for (int i = 0; i < e->getNumLanes() + turnLanes; ++i) {
281  split.lanes.push_back(i);
282  }
283  split.pos = MAX2(0.0, e->getLength() - turnLaneLength);
284  split.speed = e->getSpeed();
285  split.node = new NBNode(e->getID() + "." + toString(split.pos), e->getGeometry().positionAtOffset(split.pos));
286  split.idBefore = e->getID();
287  split.idAfter = split.node->getID();
288  split.offsetFactor = lefthand ? -1 : 1;
289  if (turnLaneLength <= e->getLength() / 2) {
290  split.offset = -0.5 * split.offsetFactor * turnLanes * e->getLaneWidth(0);
291  if (e->getFromNode()->geometryLike()) {
292  // shift the reverse direction explicitly as it will not get a turn lane
293  NBEdge* reverse = nullptr;
294  for (NBEdge* reverseCand : e->getFromNode()->getIncomingEdges()) {
295  if (reverseCand->getFromNode() == e->getToNode()) {
296  reverse = reverseCand;
297  }
298  }
299  if (reverse != nullptr) {
300  PositionVector g = reverse->getGeometry();
301  g.move2side(-split.offset);
302  reverse->setGeometry(g);
303  }
304  }
305  }
306  splits.push_back(split);
307  ec.processSplits(e, splits,
311  }
312  }
313 }
314 
315 
316 void
318  myNodeList.push_back(node);
319 }
320 
321 
322 void
324  myEdgeList.push_back(edge);
325 }
326 
327 
328 int
329 NGNet::nodeNo() const {
330  return (int)myNodeList.size();
331 }
332 
333 
334 /****************************************************************************/
335 
NBNetBuilder & myNetBuilder
The builder used to build NB*-structures.
Definition: NGNet.h:209
NGNet(NBNetBuilder &nb)
Constructor.
Definition: NGNet.cpp:48
double getSpeed(const std::string &type) const
Returns the maximal velocity for the given type [m/s].
Definition: NBTypeCont.cpp:178
std::string alphabeticalCode(int i, int iMax)
return a letter code for the given integer index
Definition: NGNet.cpp:82
NBTypeCont & getTypeCont()
Returns a reference to the type container.
Definition: NBNetBuilder.h:161
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
void parse(const std::string &description, const bool hardFail)
Overwrite by parsable distribution description.
double radialToX(double radius, double phi)
Returns the x-position resulting from the given radius and angle.
Definition: NGNet.cpp:148
void connect(NGNode *node1, NGNode *node2)
Connects both nodes with two edges, one for each direction.
Definition: NGNet.cpp:209
void toNB() const
Converts the stored network into its netbuilder-representation.
Definition: NGNet.cpp:231
The representation of a single edge during network building.
Definition: NBEdge.h:86
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:60
NGNode * findNode(int xPos, int yPos)
Returns the node at the given position.
Definition: NGNet.cpp:72
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:306
double radialToY(double radius, double phi)
Returns the y-position resulting from the given radius and angle.
Definition: NGNet.cpp:154
int nodeNo() const
Returns the number of stored nodes.
Definition: NGNet.cpp:329
T MAX2(T a, T b)
Definition: StdDefs.h:80
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:77
void setGeometry(const PositionVector &g, bool inner=false)
(Re)sets the edge&#39;s geometry
Definition: NBEdge.cpp:574
NGEdgeList myEdgeList
The list of links.
Definition: NGNet.h:215
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:228
double pos
The position of this change.
Definition: NBEdgeCont.h:211
NBEdge * getConnectionTo(NBNode *n) const
get connection to certain node
Definition: NBNode.cpp:2161
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:48
int offsetFactor
direction in which to apply the offset (used by netgenerate for lefthand networks) ...
Definition: NBEdgeCont.h:225
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:151
A list of positions.
std::string getNextFreeID()
Returns the next free id.
Definition: NGNet.cpp:66
NGNodeList myNodeList
The list of nodes.
Definition: NGNet.h:212
void move2side(double amount, double maxExtension=100)
move position vector to side using certain ammount
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
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)
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:680
static Distribution_Parameterized getDistribution(const std::string &option)
get distribution from option
Definition: NGNet.cpp:219
const EdgeVector & getIncomingEdges() const
Returns this node&#39;s incoming edges (The edges which yield in this node)
Definition: NBNode.h:259
#define M_PI
Definition: odrSpiral.cpp:40
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:156
int myLastID
The last ID given to node or link.
Definition: NGNet.h:203
Instance responsible for building networks.
Definition: NBNetBuilder.h:110
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:35
void createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter)
Creates a spider network.
Definition: NGNet.cpp:160
A storage for options typed value containers)
Definition: OptionsCont.h:90
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:166
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:95
Represents a single node (junction) during network building.
Definition: NBNode.h:68
double sample(std::mt19937 *which=0) const
Draw a sample of the distribution.
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:479
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:171
const bool myAlphaIDs
Whether to use alphanumericalIDs.
Definition: NGNet.h:206
~NGNet()
Destructor.
Definition: NGNet.cpp:55
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:317
double offset
lateral offset to edge geometry
Definition: NBEdgeCont.h:223