NIVissimNodeCluster.cpp

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

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