NGNet.cpp

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

Generated on Wed May 5 00:06:33 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6