SUMO - Simulation of Urban MObility
NIVissimNodeCluster.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // -------------------
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 
00034 #include <map>
00035 #include <algorithm>
00036 #include <cassert>
00037 #include <utils/common/VectorHelper.h>
00038 #include <utils/common/ToString.h>
00039 #include <utils/geom/PositionVector.h>
00040 #include <netbuild/NBNode.h>
00041 #include <netbuild/NBNodeCont.h>
00042 #include "NIVissimTL.h"
00043 #include "NIVissimDisturbance.h"
00044 #include "NIVissimConnection.h"
00045 #include "NIVissimNodeCluster.h"
00046 
00047 #ifdef CHECK_MEMORY_LEAKS
00048 #include <foreign/nvwa/debug_new.h>
00049 #endif // CHECK_MEMORY_LEAKS
00050 // ===========================================================================
00051 // used namespaces
00052 // ===========================================================================
00053 
00054 using namespace std;
00055 
00056 NIVissimNodeCluster::DictType NIVissimNodeCluster::myDict;
00057 int NIVissimNodeCluster::myCurrentID = 1;
00058 
00059 
00060 
00061 NIVissimNodeCluster::NIVissimNodeCluster(int id, int nodeid, int tlid,
00062         const std::vector<int>& connectors,
00063         const std::vector<int>& disturbances,
00064         bool amEdgeSplitOnly)
00065     : myID(id), myNodeID(nodeid), myTLID(tlid),
00066       myConnectors(connectors), myDisturbances(disturbances),
00067       myNBNode(0), myAmEdgeSplit(amEdgeSplitOnly) {}
00068 
00069 
00070 NIVissimNodeCluster::~NIVissimNodeCluster() {}
00071 
00072 
00073 
00074 
00075 bool
00076 NIVissimNodeCluster::dictionary(int id, NIVissimNodeCluster* o) {
00077     DictType::iterator i = myDict.find(id);
00078     if (i == myDict.end()) {
00079         myDict[id] = o;
00080         return true;
00081     }
00082     assert(false);
00083     return false;
00084 }
00085 
00086 
00087 int
00088 NIVissimNodeCluster::dictionary(int nodeid, int tlid,
00089                                 const std::vector<int>& connectors,
00090                                 const std::vector<int>& disturbances,
00091                                 bool amEdgeSplitOnly) {
00092     int id = nodeid;
00093     if (nodeid < 0) {
00094         id = myCurrentID++;
00095     }
00096     NIVissimNodeCluster* o = new NIVissimNodeCluster(id,
00097             nodeid, tlid, connectors, disturbances, amEdgeSplitOnly);
00098     dictionary(id, o);
00099     return id;
00100 }
00101 
00102 
00103 NIVissimNodeCluster*
00104 NIVissimNodeCluster::dictionary(int id) {
00105     DictType::iterator i = myDict.find(id);
00106     if (i == myDict.end()) {
00107         return 0;
00108     }
00109     return (*i).second;
00110 }
00111 
00112 
00113 
00114 size_t
00115 NIVissimNodeCluster::contSize() {
00116     return myDict.size();
00117 }
00118 
00119 
00120 
00121 std::string
00122 NIVissimNodeCluster::getNodeName() const {
00123     if (myTLID == -1) {
00124         return toString<int>(myID);
00125     } else {
00126         return toString<int>(myID) + "LSA " + toString<int>(myTLID);
00127     }
00128 }
00129 
00130 
00131 void
00132 NIVissimNodeCluster::buildNBNode(NBNodeCont& nc) {
00133     if (myConnectors.size() == 0) {
00134         return; // !!! Check, whether this can happen
00135     }
00136 
00137     // compute the position
00138     PositionVector crossings;
00139     std::vector<int>::iterator i, j;
00140     // check whether this is a split of an edge only
00141     if (myAmEdgeSplit) {
00142 // !!! should be        assert(myTLID==-1);
00143         for (i = myConnectors.begin(); i != myConnectors.end(); i++) {
00144             NIVissimConnection* c1 = NIVissimConnection::dictionary(*i);
00145             crossings.push_back_noDoublePos(c1->getFromGeomPosition());
00146         }
00147     } else {
00148         // compute the places the connections cross
00149         for (i = myConnectors.begin(); i != myConnectors.end(); i++) {
00150             NIVissimAbstractEdge* c1 = NIVissimAbstractEdge::dictionary(*i);
00151             c1->buildGeom();
00152             for (j = i + 1; j != myConnectors.end(); j++) {
00153                 NIVissimAbstractEdge* c2 = NIVissimAbstractEdge::dictionary(*j);
00154                 c2->buildGeom();
00155                 if (c1->crossesEdge(c2)) {
00156                     crossings.push_back_noDoublePos(c1->crossesEdgeAtPoint(c2));
00157                 }
00158             }
00159         }
00160         // alternative way: compute via positions of crossings
00161         if (crossings.size() == 0) {
00162             for (i = myConnectors.begin(); i != myConnectors.end(); i++) {
00163                 NIVissimConnection* c1 = NIVissimConnection::dictionary(*i);
00164                 crossings.push_back_noDoublePos(c1->getFromGeomPosition());
00165                 crossings.push_back_noDoublePos(c1->getToGeomPosition());
00166             }
00167         }
00168     }
00169     // get the position (center)
00170     Position pos = crossings.getPolygonCenter();
00171     // build the node
00172     /*    if(myTLID!=-1) {
00173      !!!        NIVissimTL *tl = NIVissimTL::dictionary(myTLID);
00174             if(tl->getType()=="festzeit") {
00175                 node = new NBNode(getNodeName(), pos.x(), pos.y(),
00176                     "traffic_light");
00177             } else {
00178                 node = new NBNode(getNodeName(), pos.x(), pos.y(),
00179                     "actuated_traffic_light");
00180             }
00181         }*/
00182     NBNode* node = new NBNode(getNodeName(), pos, NODETYPE_PRIORITY_JUNCTION);
00183     if (!nc.insert(node)) {
00184         delete node;
00185         throw 1;
00186     }
00187     myNBNode = node;
00188 }
00189 
00190 
00191 void
00192 NIVissimNodeCluster::buildNBNodes(NBNodeCont& nc) {
00193     for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
00194         (*i).second->buildNBNode(nc);
00195     }
00196 }
00197 
00198 
00199 
00200 void
00201 NIVissimNodeCluster::dict_recheckEdgeChanges() {
00202     return;
00203 }
00204 
00205 
00206 int
00207 NIVissimNodeCluster::getFromNode(int edgeid) {
00208     int ret = -1;
00209     bool mult = false;
00210     for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
00211         NIVissimNodeCluster* c = (*i).second;
00212         for (std::vector<int>::iterator j = c->myConnectors.begin(); j != c->myConnectors.end(); j++) {
00213             NIVissimConnection* conn = NIVissimConnection::dictionary(*j);
00214             if (conn != 0 && conn->getToEdgeID() == edgeid) {
00215 //                return (*i).first;
00216                 if (ret != -1 && (*i).first != ret) {
00217                     mult = true;
00218 //                     "NIVissimNodeCluster:DoubleNode:" << ret << endl;
00219                     throw 1; // an edge should not outgo from two different nodes
00220 // but actually, a joined cluster may posess a connections more than once
00221                 }
00222                 ret = (*i).first;
00223             }
00224         }
00225     }
00226     return ret;
00227 }
00228 
00229 
00230 int
00231 NIVissimNodeCluster::getToNode(int edgeid) {
00232     int ret = -1;
00233     for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
00234         NIVissimNodeCluster* c = (*i).second;
00235         for (std::vector<int>::iterator j = c->myConnectors.begin(); j != c->myConnectors.end(); j++) {
00236             NIVissimConnection* conn = NIVissimConnection::dictionary(*j);
00237             if (conn != 0 && conn->getFromEdgeID() == edgeid) {
00238 //                return (*i).first;
00239                 if (ret != -1 && ret != (*i).first) {
00240 //                  << "NIVissimNodeCluster: multiple to-nodes" << endl;
00241                     throw 1; // an edge should not outgo from two different nodes
00242 // but actually, a joined cluster may posess a connections more than once
00243 
00244                 }
00245                 ret = (*i).first;
00246             }
00247         }
00248     }
00249     return ret;
00250 }
00251 
00252 
00253 void
00254 NIVissimNodeCluster::_debugOut(std::ostream& into) {
00255     for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
00256         NIVissimNodeCluster* c = (*i).second;
00257         into << endl << c->myID << ":";
00258         for (std::vector<int>::iterator j = c->myConnectors.begin(); j != c->myConnectors.end(); j++) {
00259             if (j != c->myConnectors.begin()) {
00260                 into << ", ";
00261             }
00262             into << (*j);
00263         }
00264     }
00265     into << "=======================" << endl;
00266 }
00267 
00268 
00269 
00270 NBNode*
00271 NIVissimNodeCluster::getNBNode() const {
00272     return myNBNode;
00273 }
00274 
00275 
00276 Position
00277 NIVissimNodeCluster::getPos() const {
00278     return myPosition;
00279 }
00280 
00281 
00282 void
00283 NIVissimNodeCluster::dict_addDisturbances(NBDistrictCont& dc,
00284         NBNodeCont& nc, NBEdgeCont& ec) {
00285     for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
00286         const std::vector<int>& disturbances = (*i).second->myDisturbances;
00287         NBNode* node = nc.retrieve((*i).second->getNodeName());
00288         for (std::vector<int>::const_iterator j = disturbances.begin(); j != disturbances.end(); j++) {
00289             NIVissimDisturbance* disturbance = NIVissimDisturbance::dictionary(*j);
00290             disturbance->addToNode(node, dc, nc, ec);
00291         }
00292     }
00293     NIVissimDisturbance::reportRefused();
00294 }
00295 
00296 
00297 void
00298 NIVissimNodeCluster::clearDict() {
00299     for (DictType::iterator i = myDict.begin(); i != myDict.end(); i++) {
00300         delete(*i).second;
00301     }
00302     myDict.clear();
00303 }
00304 
00305 
00306 void
00307 NIVissimNodeCluster::setCurrentVirtID(int id) {
00308     myCurrentID = id;
00309 }
00310 
00311 
00312 
00313 /****************************************************************************/
00314 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines