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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
21 // The class storing the generated network
22 /****************************************************************************/
23 #include <config.h>
24 
25 #include <iostream>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <cmath>
30 #include <netbuild/NBNode.h>
31 #include <netbuild/NBNodeCont.h>
32 #include <netbuild/NBEdge.h>
33 #include <netbuild/NBEdgeCont.h>
34 #include <netbuild/NBNetBuilder.h>
35 #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 nullptr;
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  topNode->setFringe();
122  bottomNode->setFringe();
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  leftNode->setFringe();
138  rightNode->setFringe();
139  myNodeList.push_back(leftNode);
140  myNodeList.push_back(rightNode);
141  // create links
142  connect(leftNode, findNode(0, iy));
143  connect(rightNode, findNode(numX - 1, iy));
144  }
145  }
146 }
147 
148 
149 double
150 NGNet::radialToX(double radius, double phi) {
151  return cos(phi) * radius;
152 }
153 
154 
155 double
156 NGNet::radialToY(double radius, double phi) {
157  return sin(phi) * radius;
158 }
159 
160 
161 void
162 NGNet::createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter) {
163  if (numRadDiv < 3) {
164  numRadDiv = 3;
165  }
166  if (numCircles < 1) {
167  numCircles = 1;
168  }
169 
170  int ir, ic;
171  double angle = (double)(2 * M_PI / numRadDiv); // angle between radial divisions
172  NGNode* Node;
173  for (ic = 1; ic < numCircles + 1; ic++) {
174  const std::string nodeIDStart = alphabeticalCode(ic, numCircles);
175  for (ir = 1; ir < numRadDiv + 1; ir++) {
176  // create Node
177  const std::string nodeID = (myAlphaIDs ?
178  nodeIDStart + toString<int>(ir) :
179  toString<int>(ir) + "/" + toString<int>(ic));
180  Node = new NGNode(nodeID, ir, ic);
181  Node->setX(radialToX((ic) * spaceRad, (ir - 1) * angle));
182  Node->setY(radialToY((ic) * spaceRad, (ir - 1) * angle));
183  myNodeList.push_back(Node);
184  // create Links
185  if (ir > 1) {
186  connect(Node, findNode(ir - 1, ic));
187  }
188  if (ic > 1) {
189  connect(Node, findNode(ir, ic - 1));
190  }
191  if (ir == numRadDiv) {
192  connect(Node, findNode(1, ic));
193  }
194  }
195  }
196  if (hasCenter) {
197  // node
198  Node = new NGNode(myAlphaIDs ? "A1" : "1", 0, 0, true);
199  Node->setX(0);
200  Node->setY(0);
201  myNodeList.push_back(Node);
202  // links
203  for (ir = 1; ir < numRadDiv + 1; ir++) {
204  connect(Node, findNode(ir, 1));
205  }
206  }
207 }
208 
209 
210 void
211 NGNet::connect(NGNode* node1, NGNode* node2) {
212  std::string id1 = node1->getID() + (myAlphaIDs ? "" : "to") + node2->getID();
213  std::string id2 = node2->getID() + (myAlphaIDs ? "" : "to") + node1->getID();
214  NGEdge* link1 = new NGEdge(id1, node1, node2);
215  NGEdge* link2 = new NGEdge(id2, node2, node1);
216  myEdgeList.push_back(link1);
217  myEdgeList.push_back(link2);
218 }
219 
221 NGNet::getDistribution(const std::string& option) {
222  std::string val = OptionsCont::getOptions().getString(option);
223  try {
224  return Distribution_Parameterized("peturb", 0, StringUtils::toDouble(val));
225  } catch (NumberFormatException&) {
226  Distribution_Parameterized result("perturb", 0, 0);
227  result.parse(val, true);
228  return result;
229  }
230 }
231 
232 void
233 NGNet::toNB() const {
234  Distribution_Parameterized perturbx = getDistribution("perturb-x");
235  Distribution_Parameterized perturby = getDistribution("perturb-y");
236  Distribution_Parameterized perturbz = getDistribution("perturb-z");
237  std::vector<NBNode*> nodes;
238  for (NGNodeList::const_iterator i1 = myNodeList.begin(); i1 != myNodeList.end(); i1++) {
239  Position perturb(
240  perturbx.sample(),
241  perturby.sample(),
242  perturbz.sample());
243  NBNode* node = (*i1)->buildNBNode(myNetBuilder, perturb);
244  nodes.push_back(node);
246  }
247  const std::string type = OptionsCont::getOptions().getString("default.type");
248  for (NGEdgeList::const_iterator i2 = myEdgeList.begin(); i2 != myEdgeList.end(); i2++) {
249  NBEdge* edge = (*i2)->buildNBEdge(myNetBuilder, type);
251  }
252  // now, let's append the reverse directions...
253  double bidiProb = OptionsCont::getOptions().getFloat("rand.bidi-probability");
254  for (std::vector<NBNode*>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
255  NBNode* node = *i;
256  for (NBEdge* e : node->getIncomingEdges()) {
257  if (node->getConnectionTo(e->getFromNode()) == nullptr && RandHelper::rand() <= bidiProb) {
258  NBEdge* back = new NBEdge("-" + e->getID(), node, e->getFromNode(),
260  e->getNumLanes(),
261  e->getPriority(),
264  }
265  }
266  }
267  // add splits depending on turn-lane options
268  const int turnLanes = OptionsCont::getOptions().getInt("turn-lanes");
269  const bool lefthand = OptionsCont::getOptions().getBool("lefthand");
270  if (turnLanes > 0) {
271  const double turnLaneLength = OptionsCont::getOptions().getFloat("turn-lanes.length");
273  EdgeVector allEdges;
274  for (auto it = ec.begin(); it != ec.end(); ++it) {
275  allEdges.push_back(it->second);
276  }
277  for (NBEdge* e : allEdges) {
278  if (e->getToNode()->geometryLike()) {
279  continue;
280  }
281  std::vector<NBEdgeCont::Split> splits;
283  for (int i = 0; i < e->getNumLanes() + turnLanes; ++i) {
284  split.lanes.push_back(i);
285  }
286  split.pos = MAX2(0.0, e->getLength() - turnLaneLength);
287  split.speed = e->getSpeed();
288  split.node = new NBNode(e->getID() + "." + toString(split.pos), e->getGeometry().positionAtOffset(split.pos));
289  split.idBefore = e->getID();
290  split.idAfter = split.node->getID();
291  split.offsetFactor = lefthand ? -1 : 1;
292  if (turnLaneLength <= e->getLength() / 2) {
293  split.offset = -0.5 * split.offsetFactor * turnLanes * e->getLaneWidth(0);
294  if (e->getFromNode()->geometryLike()) {
295  // shift the reverse direction explicitly as it will not get a turn lane
296  NBEdge* reverse = nullptr;
297  for (NBEdge* reverseCand : e->getFromNode()->getIncomingEdges()) {
298  if (reverseCand->getFromNode() == e->getToNode()) {
299  reverse = reverseCand;
300  }
301  }
302  if (reverse != nullptr) {
303  PositionVector g = reverse->getGeometry();
304  g.move2side(-split.offset);
305  reverse->setGeometry(g);
306  }
307  }
308  }
309  splits.push_back(split);
310  ec.processSplits(e, splits,
314  }
315  }
316 }
317 
318 
319 void
321  myNodeList.push_back(node);
322 }
323 
324 
325 void
327  myEdgeList.push_back(edge);
328 }
329 
330 
331 int
332 NGNet::nodeNo() const {
333  return (int)myNodeList.size();
334 }
335 
336 
337 /****************************************************************************/
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:34
T MAX2(T a, T b)
Definition: StdDefs.h:79
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
void parse(const std::string &description, const bool hardFail)
Overwrite by parsable distribution description.
double sample(std::mt19937 *which=0) const
Draw a sample of the distribution.
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:59
std::map< std::string, NBEdge * >::const_iterator begin() const
Returns the pointer to the begin of the stored edges.
Definition: NBEdgeCont.h:183
std::map< std::string, NBEdge * >::const_iterator end() const
Returns the pointer to the end of the stored edges.
Definition: NBEdgeCont.h:191
void processSplits(NBEdge *e, std::vector< Split > splits, NBNodeCont &nc, NBDistrictCont &dc, NBTrafficLightLogicCont &tlc)
Definition: NBEdgeCont.cpp:441
bool insert(NBEdge *edge, bool ignorePrunning=false)
Adds an edge to the dictionary.
Definition: NBEdgeCont.cpp:178
The representation of a single edge during network building.
Definition: NBEdge.h:91
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:716
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:327
void setGeometry(const PositionVector &g, bool inner=false)
(Re)sets the edge's geometry
Definition: NBEdge.cpp:604
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:509
Instance responsible for building networks.
Definition: NBNetBuilder.h:107
NBDistrictCont & getDistrictCont()
Returns a reference the districts container.
Definition: NBNetBuilder.h:168
NBTypeCont & getTypeCont()
Returns a reference to the type container.
Definition: NBNetBuilder.h:158
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:148
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:153
NBTrafficLightLogicCont & getTLLogicCont()
Returns a reference to the traffic light logics container.
Definition: NBNetBuilder.h:163
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:90
Represents a single node (junction) during network building.
Definition: NBNode.h:66
const EdgeVector & getIncomingEdges() const
Returns this node's incoming edges (The edges which yield in this node)
Definition: NBNode.h:254
NBEdge * getConnectionTo(NBNode *n) const
get connection to certain node
Definition: NBNode.cpp:2303
double getEdgeTypeSpeed(const std::string &edgeType) const
Returns the maximal velocity for the given edgeType [m/s].
Definition: NBTypeCont.cpp:451
double getEdgeTypeWidth(const std::string &edgeType) const
Returns the lane width for the given edgeType [m].
Definition: NBTypeCont.cpp:501
A netgen-representation of an edge.
Definition: NGEdge.h:52
double radialToX(double radius, double phi)
Returns the x-position resulting from the given radius and angle.
Definition: NGNet.cpp:150
int myLastID
The last ID given to node or link.
Definition: NGNet.h:200
void toNB() const
Converts the stored network into its netbuilder-representation.
Definition: NGNet.cpp:233
void connect(NGNode *node1, NGNode *node2)
Connects both nodes with two edges, one for each direction.
Definition: NGNet.cpp:211
void add(NGNode *node)
Adds the given node to the network.
Definition: NGNet.cpp:320
NGNet(NBNetBuilder &nb)
Constructor.
Definition: NGNet.cpp:46
int nodeNo() const
Returns the number of stored nodes.
Definition: NGNet.cpp:332
void createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter)
Creates a spider network.
Definition: NGNet.cpp:162
double radialToY(double radius, double phi)
Returns the y-position resulting from the given radius and angle.
Definition: NGNet.cpp:156
NGNode * findNode(int xPos, int yPos)
Returns the node at the given position.
Definition: NGNet.cpp:70
std::string alphabeticalCode(int i, int iMax)
return a letter code for the given integer index
Definition: NGNet.cpp:80
std::string getNextFreeID()
Returns the next free id.
Definition: NGNet.cpp:64
static Distribution_Parameterized getDistribution(const std::string &option)
get distribution from option
Definition: NGNet.cpp:221
NGNodeList myNodeList
The list of nodes.
Definition: NGNet.h:209
const bool myAlphaIDs
Whether to use alphanumericalIDs.
Definition: NGNet.h:203
NBNetBuilder & myNetBuilder
The builder used to build NB*-structures.
Definition: NGNet.h:206
NGEdgeList myEdgeList
The list of links.
Definition: NGNet.h:212
void createChequerBoard(int numX, int numY, double spaceX, double spaceY, double attachLength)
Creates a grid network.
Definition: NGNet.cpp:93
~NGNet()
Destructor.
Definition: NGNet.cpp:53
A netgen-representation of a node.
Definition: NGNode.h:48
void setFringe()
mark node as fringe
Definition: NGNode.h:125
void setY(double y)
Sets a new value for y-position.
Definition: NGNode.h:120
void setX(double x)
Sets a new value for x-position.
Definition: NGNode.h:111
const std::string & getID() const
Returns the id.
Definition: Named.h:73
Definition: Node.h:31
A storage for options typed value containers)
Definition: OptionsCont.h:89
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
A list of positions.
void move2side(double amount, double maxExtension=100)
move position vector to side using certain ammount
static double rand(std::mt19937 *rng=nullptr)
Returns a random real number in [0, 1)
Definition: RandHelper.h:51
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
#define M_PI
Definition: odrSpiral.cpp:40
A structure which describes changes of lane number or speed along the road.
Definition: NBEdgeCont.h:204