|
SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // The class storing the generated network 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright (C) 2001-2011 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) { 00153 numRadDiv = 3; 00154 } 00155 if (numCircles < 1) { 00156 numCircles = 1; 00157 } 00158 00159 int ir, ic; 00160 SUMOReal angle = (SUMOReal)(2*PI/numRadDiv); // angle between radial divisions 00161 NGNode* Node; 00162 for (ir=1; ir<numRadDiv+1; ir++) { 00163 for (ic=1; ic<numCircles+1; ic++) { 00164 // create Node 00165 Node = new NGNode( 00166 toString<int>(ir) + "/" + toString<int>(ic), ir, ic); 00167 Node->setX(radialToX((ic) * spaceRad, (ir-1) * angle)); 00168 Node->setY(radialToY((ic) * spaceRad, (ir-1) * angle)); 00169 myNodeList.push_back(Node); 00170 // create Links 00171 if (ir > 1) { 00172 connect(Node, findNode(ir-1, ic)); 00173 } 00174 if (ic > 1) { 00175 connect(Node, findNode(ir, ic-1)); 00176 } 00177 if (ir == numRadDiv) { 00178 connect(Node, findNode(1, ic)); 00179 } 00180 } 00181 } 00182 if (hasCenter) { 00183 // node 00184 Node = new NGNode(getNextFreeID(), 0, 0, true); 00185 Node->setX(0); 00186 Node->setY(0); 00187 myNodeList.push_back(Node); 00188 // links 00189 for (ir=1; ir<numRadDiv+1; ir++) { 00190 connect(Node, findNode(ir, 1)); 00191 } 00192 } 00193 } 00194 00195 00196 void 00197 NGNet::connect(NGNode* node1, NGNode* node2) throw() { 00198 std::string id1 = node1->getID() + "to" + node2->getID(); 00199 std::string id2 = node2->getID() + "to" + node1->getID(); 00200 NGEdge* link1 = new NGEdge(id1, node1, node2); 00201 NGEdge* link2 = new NGEdge(id2, node2, node1); 00202 myEdgeList.push_back(link1); 00203 myEdgeList.push_back(link2); 00204 } 00205 00206 00207 void 00208 NGNet::toNB() const throw(ProcessError) { 00209 std::vector<NBNode*> nodes; 00210 for (NGNodeList::const_iterator i1=myNodeList.begin(); i1!=myNodeList.end(); i1++) { 00211 NBNode* node = (*i1)->buildNBNode(myNetBuilder); 00212 nodes.push_back(node); 00213 myNetBuilder.getNodeCont().insert(node); 00214 } 00215 for (NGEdgeList::const_iterator i2=myEdgeList.begin(); i2!=myEdgeList.end(); i2++) { 00216 NBEdge* edge = (*i2)->buildNBEdge(myNetBuilder); 00217 myNetBuilder.getEdgeCont().insert(edge); 00218 } 00219 // now, let's append the reverse directions... 00220 SUMOReal bidiProb = OptionsCont::getOptions().getFloat("rand.bidi-probability"); 00221 for (std::vector<NBNode*>::const_iterator i=nodes.begin(); i!=nodes.end(); ++i) { 00222 NBNode* node = *i; 00223 EdgeVector incoming = node->getIncomingEdges(); 00224 for (EdgeVector::const_iterator j=incoming.begin(); j!=incoming.end(); ++j) { 00225 if (node->getConnectionTo((*j)->getFromNode())==0 && RandHelper::rand()<=bidiProb) { 00226 NBEdge* back = new NBEdge("-" + (*j)->getID(), node, (*j)->getFromNode(), 00227 "", myNetBuilder.getTypeCont().getSpeed(""), myNetBuilder.getTypeCont().getNumLanes(""), 00228 myNetBuilder.getTypeCont().getPriority(""), myNetBuilder.getTypeCont().getWidth(""), -1); 00229 myNetBuilder.getEdgeCont().insert(back); 00230 } 00231 } 00232 } 00233 } 00234 00235 00236 void 00237 NGNet::add(NGNode* node) throw() { 00238 myNodeList.push_back(node); 00239 } 00240 00241 00242 void 00243 NGNet::add(NGEdge* edge) throw() { 00244 myEdgeList.push_back(edge); 00245 } 00246 00247 00248 size_t 00249 NGNet::nodeNo() const throw() { 00250 return myNodeList.size(); 00251 } 00252 00253 00254 /****************************************************************************/ 00255