SUMO - Simulation of Urban MObility
NGNet.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // The class storing the generated network
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #include <iostream>
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include <string.h>
00037 #include <math.h>
00038 #include <netbuild/NBNode.h>
00039 #include <netbuild/NBNodeCont.h>
00040 #include <netbuild/NBEdge.h>
00041 #include <netbuild/NBEdgeCont.h>
00042 #include <netbuild/NBNetBuilder.h>
00043 #include <utils/common/ToString.h>
00044 #include <utils/common/RandHelper.h>
00045 #include <utils/options/OptionsCont.h>
00046 #include "NGNet.h"
00047 
00048 #ifdef CHECK_MEMORY_LEAKS
00049 #include <foreign/nvwa/debug_new.h>
00050 #endif // CHECK_MEMORY_LEAKS
00051 
00052 
00053 // ===========================================================================
00054 // method definitions
00055 // ===========================================================================
00056 NGNet::NGNet(NBNetBuilder& nb)
00057     : myNetBuilder(nb) {
00058     myLastID = 0;
00059 }
00060 
00061 
00062 NGNet::~NGNet() {
00063     for (NGEdgeList::iterator ni = myEdgeList.begin(); ni != myEdgeList.end(); ++ni) {
00064         delete *ni;
00065     }
00066     for (NGNodeList::iterator ni = myNodeList.begin(); ni != myNodeList.end(); ++ni) {
00067         delete *ni;
00068     }
00069 }
00070 
00071 
00072 std::string
00073 NGNet::getNextFreeID() {
00074     return toString<int>(++myLastID);
00075 }
00076 
00077 
00078 NGNode*
00079 NGNet::findNode(int xID, int yID) {
00080     for (NGNodeList::iterator ni = myNodeList.begin(); ni != myNodeList.end(); ++ni) {
00081         if ((*ni)->samePos(xID, yID)) {
00082             return *ni;
00083         }
00084     }
00085     return 0;
00086 }
00087 
00088 
00089 void
00090 NGNet::createChequerBoard(int numX, int numY, SUMOReal spaceX, SUMOReal spaceY, SUMOReal attachLength) {
00091     for (int ix = 0; ix < numX; ix++) {
00092         for (int iy = 0; iy < numY; iy++) {
00093             // create Node
00094             std::string nodeID = toString<int>(ix) + "/" + toString<int>(iy);
00095             NGNode* node = new NGNode(nodeID, ix, iy);
00096             node->setX(ix * spaceX + attachLength);
00097             node->setY(iy * spaceY + attachLength);
00098             myNodeList.push_back(node);
00099             // create Links
00100             if (ix > 0) {
00101                 connect(node, findNode(ix - 1, iy));
00102             }
00103             if (iy > 0) {
00104                 connect(node, findNode(ix, iy - 1));
00105             }
00106         }
00107     }
00108     if (attachLength > 0.0) {
00109         for (int ix = 0; ix < numX; ix++) {
00110             // create nodes
00111             NGNode* topNode = new NGNode("top" + toString<int>(ix), ix, numY);
00112             NGNode* bottomNode = new NGNode("bottom" + toString<int>(ix), ix, numY + 1);
00113             topNode->setX(ix * spaceX + attachLength);
00114             bottomNode->setX(ix * spaceX + attachLength);
00115             topNode->setY((numY - 1) * spaceY + 2 * attachLength);
00116             bottomNode->setY(0);
00117             myNodeList.push_back(topNode);
00118             myNodeList.push_back(bottomNode);
00119             // create links
00120             connect(topNode, findNode(ix, numY - 1));
00121             connect(bottomNode, findNode(ix, 0));
00122         }
00123         for (int iy = 0; iy < numY; iy++) {
00124             // create nodes
00125             NGNode* leftNode = new NGNode("left" + toString<int>(iy), numX, iy);
00126             NGNode* rightNode = new NGNode("right" + toString<int>(iy), numX + 1, iy);
00127             leftNode->setX(0);
00128             rightNode->setX((numX - 1) * spaceX + 2 * attachLength);
00129             leftNode->setY(iy * spaceY + attachLength);
00130             rightNode->setY(iy * spaceY + attachLength);
00131             myNodeList.push_back(leftNode);
00132             myNodeList.push_back(rightNode);
00133             // create links
00134             connect(leftNode, findNode(0, iy));
00135             connect(rightNode, findNode(numX - 1, iy));
00136         }
00137     }
00138 }
00139 
00140 
00141 SUMOReal
00142 NGNet::radialToX(SUMOReal radius, SUMOReal phi) {
00143     return cos(phi) * radius;
00144 }
00145 
00146 
00147 SUMOReal
00148 NGNet::radialToY(SUMOReal radius, SUMOReal phi) {
00149     return sin(phi) * radius;
00150 }
00151 
00152 
00153 void
00154 NGNet::createSpiderWeb(int numRadDiv, int numCircles, SUMOReal spaceRad, bool hasCenter) {
00155     if (numRadDiv < 3) {
00156         numRadDiv = 3;
00157     }
00158     if (numCircles < 1) {
00159         numCircles = 1;
00160     }
00161 
00162     int ir, ic;
00163     SUMOReal angle = (SUMOReal)(2 * PI / numRadDiv); // angle between radial divisions
00164     NGNode* Node;
00165     for (ir = 1; ir < numRadDiv + 1; ir++) {
00166         for (ic = 1; ic < numCircles + 1; ic++) {
00167             // create Node
00168             Node = new NGNode(
00169                 toString<int>(ir) + "/" + toString<int>(ic), ir, ic);
00170             Node->setX(radialToX((ic) * spaceRad, (ir - 1) * angle));
00171             Node->setY(radialToY((ic) * spaceRad, (ir - 1) * angle));
00172             myNodeList.push_back(Node);
00173             // create Links
00174             if (ir > 1) {
00175                 connect(Node, findNode(ir - 1, ic));
00176             }
00177             if (ic > 1) {
00178                 connect(Node, findNode(ir, ic - 1));
00179             }
00180             if (ir == numRadDiv) {
00181                 connect(Node, findNode(1, ic));
00182             }
00183         }
00184     }
00185     if (hasCenter) {
00186         // node
00187         Node = new NGNode(getNextFreeID(), 0, 0, true);
00188         Node->setX(0);
00189         Node->setY(0);
00190         myNodeList.push_back(Node);
00191         // links
00192         for (ir = 1; ir < numRadDiv + 1; ir++) {
00193             connect(Node, findNode(ir, 1));
00194         }
00195     }
00196 }
00197 
00198 
00199 void
00200 NGNet::connect(NGNode* node1, NGNode* node2) {
00201     std::string id1 = node1->getID() + "to" + node2->getID();
00202     std::string id2 = node2->getID() + "to" + node1->getID();
00203     NGEdge* link1 = new NGEdge(id1, node1, node2);
00204     NGEdge* link2 = new NGEdge(id2, node2, node1);
00205     myEdgeList.push_back(link1);
00206     myEdgeList.push_back(link2);
00207 }
00208 
00209 
00210 void
00211 NGNet::toNB() const {
00212     std::vector<NBNode*> nodes;
00213     for (NGNodeList::const_iterator i1 = myNodeList.begin(); i1 != myNodeList.end(); i1++) {
00214         NBNode* node = (*i1)->buildNBNode(myNetBuilder);
00215         nodes.push_back(node);
00216         myNetBuilder.getNodeCont().insert(node);
00217     }
00218     for (NGEdgeList::const_iterator i2 = myEdgeList.begin(); i2 != myEdgeList.end(); i2++) {
00219         NBEdge* edge = (*i2)->buildNBEdge(myNetBuilder);
00220         myNetBuilder.getEdgeCont().insert(edge);
00221     }
00222     // now, let's append the reverse directions...
00223     SUMOReal bidiProb = OptionsCont::getOptions().getFloat("rand.bidi-probability");
00224     for (std::vector<NBNode*>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
00225         NBNode* node = *i;
00226         EdgeVector incoming = node->getIncomingEdges();
00227         for (EdgeVector::const_iterator j = incoming.begin(); j != incoming.end(); ++j) {
00228             if (node->getConnectionTo((*j)->getFromNode()) == 0 && RandHelper::rand() <= bidiProb) {
00229                 NBEdge* back = new NBEdge("-" + (*j)->getID(), node, (*j)->getFromNode(),
00230                                           "", myNetBuilder.getTypeCont().getSpeed(""), myNetBuilder.getTypeCont().getNumLanes(""),
00231                                           myNetBuilder.getTypeCont().getPriority(""), myNetBuilder.getTypeCont().getWidth(""), -1);
00232                 myNetBuilder.getEdgeCont().insert(back);
00233             }
00234         }
00235     }
00236 }
00237 
00238 
00239 void
00240 NGNet::add(NGNode* node) {
00241     myNodeList.push_back(node);
00242 }
00243 
00244 
00245 void
00246 NGNet::add(NGEdge* edge) {
00247     myEdgeList.push_back(edge);
00248 }
00249 
00250 
00251 size_t
00252 NGNet::nodeNo() const {
00253     return myNodeList.size();
00254 }
00255 
00256 
00257 /****************************************************************************/
00258 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines