NIVissimTL.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 <string>
00033 #include <cassert>
00034 #include <utils/geom/GeomHelper.h>
00035 #include <utils/geom/Boundary.h>
00036 #include <utils/common/MsgHandler.h>
00037 #include <utils/common/ToString.h>
00038 #include "NIVissimConnection.h"
00039 #include <netbuild/NBLoadedTLDef.h>
00040 #include <netbuild/NBEdge.h>
00041 #include <netbuild/NBEdgeCont.h>
00042 #include <netbuild/NBTrafficLightLogicCont.h>
00043 #include <netbuild/NBLoadedTLDef.h>
00044 #include "NIVissimConnection.h"
00045 #include "NIVissimDisturbance.h"
00046 #include "NIVissimNodeDef.h"
00047 #include "NIVissimEdge.h"
00048 #include "NIVissimTL.h"
00049 
00050 #ifdef CHECK_MEMORY_LEAKS
00051 #include <foreign/nvwa/debug_new.h>
00052 #endif // CHECK_MEMORY_LEAKS
00053 // ===========================================================================
00054 // used namespaces
00055 // ===========================================================================
00056 
00057 using namespace std;
00058 
00059 
00060 NIVissimTL::SignalDictType NIVissimTL::NIVissimTLSignal::myDict;
00061 
00062 NIVissimTL::NIVissimTLSignal::NIVissimTLSignal(int lsaid, int id,
00063         const std::string &name,
00064         const IntVector &groupids,
00065         int edgeid,
00066         int laneno,
00067         SUMOReal position,
00068         const IntVector &vehicleTypes)
00069         : myLSA(lsaid), myID(id), myName(name), myGroupIDs(groupids),
00070         myEdgeID(edgeid), myLane(laneno), myPosition(position),
00071         myVehicleTypes(vehicleTypes) {}
00072 
00073 
00074 NIVissimTL::NIVissimTLSignal::~NIVissimTLSignal() {}
00075 
00076 bool
00077 NIVissimTL::NIVissimTLSignal::isWithin(const Position2DVector &poly) const {
00078     return poly.around(getPosition());
00079 }
00080 
00081 
00082 Position2D
00083 NIVissimTL::NIVissimTLSignal::getPosition() const {
00084     return NIVissimAbstractEdge::dictionary(myEdgeID)->getGeomPosition(myPosition);
00085 }
00086 
00087 
00088 bool
00089 NIVissimTL::NIVissimTLSignal::dictionary(int lsaid, int id,
00090         NIVissimTL::NIVissimTLSignal *o) {
00091     SignalDictType::iterator i = myDict.find(lsaid);
00092     if (i==myDict.end()) {
00093         myDict[lsaid] = SSignalDictType();
00094         i = myDict.find(lsaid);
00095     }
00096     SSignalDictType::iterator j = (*i).second.find(id);
00097     if (j==(*i).second.end()) {
00098         myDict[lsaid][id] = o;
00099         return true;
00100     }
00101     return false;
00102 }
00103 
00104 
00105 NIVissimTL::NIVissimTLSignal*
00106 NIVissimTL::NIVissimTLSignal::dictionary(int lsaid, int id) {
00107     SignalDictType::iterator i = myDict.find(lsaid);
00108     if (i==myDict.end()) {
00109         return 0;
00110     }
00111     SSignalDictType::iterator j = (*i).second.find(id);
00112     if (j==(*i).second.end()) {
00113         return 0;
00114     }
00115     return (*j).second;
00116 }
00117 
00118 
00119 void
00120 NIVissimTL::NIVissimTLSignal::clearDict() {
00121     for (SignalDictType::iterator i=myDict.begin(); i!=myDict.end(); i++) {
00122         for (SSignalDictType::iterator j=(*i).second.begin(); j!=(*i).second.end(); j++) {
00123             delete(*j).second;
00124         }
00125     }
00126     myDict.clear();
00127 }
00128 
00129 
00130 NIVissimTL::SSignalDictType
00131 NIVissimTL::NIVissimTLSignal::getSignalsFor(int tlid) {
00132     SignalDictType::iterator i = myDict.find(tlid);
00133     if (i==myDict.end()) {
00134         return SSignalDictType();
00135     }
00136     return (*i).second;
00137 }
00138 
00139 
00140 bool
00141 NIVissimTL::NIVissimTLSignal::addTo(NBEdgeCont &ec, NBLoadedTLDef *tl) const {
00142     NIVissimConnection *c = NIVissimConnection::dictionary(myEdgeID);
00143     NBConnectionVector assignedConnections;
00144     if (c==0) {
00145         // What to do if on an edge? -> close all outgoing connections
00146         NBEdge *edge = ec.retrievePossiblySplitted(toString<int>(myEdgeID), myPosition);
00147         if (edge==0) {
00148             MsgHandler::getWarningInstance()->inform("Could not set tls signal at edge '" + toString(myEdgeID) + "' - the edge was not built.");
00149             return false;
00150         }
00151         // Check whether it is already known, which edges are approached
00152         //  by which lanes
00153         // check whether to use the original lanes only
00154         if (edge->lanesWereAssigned()) {
00155             std::vector<NBEdge::Connection> connections = edge->getConnectionsFromLane(myLane-1);
00156             for (std::vector<NBEdge::Connection>::iterator i=connections.begin(); i!=connections.end(); i++) {
00157                 const NBEdge::Connection &conn = *i;
00158                 assert(myLane-1<(int)edge->getNoLanes());
00159                 assignedConnections.push_back(NBConnection(edge, myLane-1, conn.toEdge, conn.toLane));
00160             }
00161         } else {
00162             WRITE_WARNING("Edge : Lanes were not assigned(!)");
00163             for (unsigned int j=0; j<edge->getNoLanes(); j++) {
00164                 std::vector<NBEdge::Connection> connections = edge->getConnectionsFromLane(j);
00165                 for (std::vector<NBEdge::Connection>::iterator i=connections.begin(); i!=connections.end(); i++) {
00166                     const NBEdge::Connection &conn = *i;
00167                     assignedConnections.push_back(NBConnection(edge, j, conn.toEdge, conn.toLane));
00168                 }
00169             }
00170         }
00171     } else {
00172         // get the edges
00173         NBEdge *tmpFrom = ec.retrievePossiblySplitted(
00174                               toString<int>(c->getFromEdgeID()),
00175                               toString<int>(c->getToEdgeID()),
00176                               true);
00177         NBEdge *tmpTo = ec.retrievePossiblySplitted(
00178                             toString<int>(c->getToEdgeID()),
00179                             toString<int>(c->getFromEdgeID()),
00180                             false);
00181         // check whether the edges are known
00182         if (tmpFrom!=0&&tmpTo!=0) {
00183             // add connections this signal is responsible for
00184             assignedConnections.push_back(NBConnection(tmpFrom, -1, tmpTo, -1));
00185         } else {
00186             return false;
00187             // !!! one of the edges could not be build
00188         }
00189     }
00190     // add to the group
00191     assert(myGroupIDs.size()!=0);
00192     if (myGroupIDs.size()==1) {
00193         return tl->addToSignalGroup(toString<int>(*(myGroupIDs.begin())),
00194                                     assignedConnections);
00195     } else {
00196         // !!!
00197         return tl->addToSignalGroup(toString<int>(*(myGroupIDs.begin())),
00198                                     assignedConnections);
00199     }
00200     return true;
00201 }
00202 
00203 
00204 
00205 
00206 
00207 
00208 
00209 
00210 NIVissimTL::GroupDictType NIVissimTL::NIVissimTLSignalGroup::myDict;
00211 
00212 NIVissimTL::NIVissimTLSignalGroup::NIVissimTLSignalGroup(
00213     int lsaid, int id,
00214     const std::string &name,
00215     bool isGreenBegin, const DoubleVector &times,
00216     SUMOTime tredyellow, SUMOTime tyellow)
00217         : myLSA(lsaid), myID(id), myName(name), myTimes(times),
00218         myFirstIsRed(!isGreenBegin), myTRedYellow(tredyellow),
00219         myTYellow(tyellow) {}
00220 
00221 
00222 NIVissimTL::NIVissimTLSignalGroup::~NIVissimTLSignalGroup() {}
00223 
00224 
00225 bool
00226 NIVissimTL::NIVissimTLSignalGroup::dictionary(int lsaid, int id,
00227         NIVissimTL::NIVissimTLSignalGroup *o) {
00228     GroupDictType::iterator i = myDict.find(lsaid);
00229     if (i==myDict.end()) {
00230         myDict[lsaid] = SGroupDictType();
00231         i = myDict.find(lsaid);
00232     }
00233     SGroupDictType::iterator j = (*i).second.find(id);
00234     if (j==(*i).second.end()) {
00235         myDict[lsaid][id] = o;
00236         return true;
00237     }
00238     return false;
00239     /*
00240         GroupDictType::iterator i=myDict.find(id);
00241         if(i==myDict.end()) {
00242             myDict[id] = o;
00243             return true;
00244         }
00245         return false;
00246         */
00247 }
00248 
00249 
00250 NIVissimTL::NIVissimTLSignalGroup*
00251 NIVissimTL::NIVissimTLSignalGroup::dictionary(int lsaid, int id) {
00252     GroupDictType::iterator i = myDict.find(lsaid);
00253     if (i==myDict.end()) {
00254         return 0;
00255     }
00256     SGroupDictType::iterator j = (*i).second.find(id);
00257     if (j==(*i).second.end()) {
00258         return 0;
00259     }
00260     return (*j).second;
00261 }
00262 
00263 void
00264 NIVissimTL::NIVissimTLSignalGroup::clearDict() {
00265     for (GroupDictType::iterator i=myDict.begin(); i!=myDict.end(); i++) {
00266         for (SGroupDictType::iterator j=(*i).second.begin(); j!=(*i).second.end(); j++) {
00267             delete(*j).second;
00268         }
00269     }
00270     myDict.clear();
00271 }
00272 
00273 
00274 NIVissimTL::SGroupDictType
00275 NIVissimTL::NIVissimTLSignalGroup::getGroupsFor(int tlid) {
00276     GroupDictType::iterator i = myDict.find(tlid);
00277     if (i==myDict.end()) {
00278         return SGroupDictType();
00279     }
00280     return (*i).second;
00281 }
00282 
00283 
00284 bool
00285 NIVissimTL::NIVissimTLSignalGroup::addTo(NBLoadedTLDef *tl) const {
00286     // get the color at the begin
00287     NBTrafficLightDefinition::TLColor color = myFirstIsRed
00288             ? NBTrafficLightDefinition::TLCOLOR_RED : NBTrafficLightDefinition::TLCOLOR_GREEN;
00289     std::string id = toString<int>(myID);
00290     tl->addSignalGroup(id); // !!! myTimes als SUMOTime
00291     for (DoubleVector::const_iterator i=myTimes.begin(); i!=myTimes.end(); i++) {
00292         tl->addSignalGroupPhaseBegin(id, (SUMOTime) *i, color);
00293         color = color==NBTrafficLightDefinition::TLCOLOR_RED
00294                 ? NBTrafficLightDefinition::TLCOLOR_GREEN : NBTrafficLightDefinition::TLCOLOR_RED;
00295     }
00296     if (myTimes.size()==0) {
00297         if (myFirstIsRed) {
00298             tl->addSignalGroupPhaseBegin(id, 0, NBTrafficLightDefinition::TLCOLOR_RED);
00299         } else {
00300             tl->addSignalGroupPhaseBegin(id, 0, NBTrafficLightDefinition::TLCOLOR_GREEN);
00301         }
00302     }
00303     tl->setSignalYellowTimes(id, myTRedYellow, myTYellow);
00304     return true;
00305 }
00306 
00307 
00308 
00309 
00310 
00311 
00312 
00313 
00314 NIVissimTL::DictType NIVissimTL::myDict;
00315 
00316 NIVissimTL::NIVissimTL(int id, const std::string &type,
00317                        const std::string &name, SUMOTime absdur,
00318                        SUMOTime offset)
00319         : myID(id), myName(name), myAbsDuration(absdur), myOffset(offset),
00320         myCurrentGroup(0), myType(type)
00321 
00322 {}
00323 
00324 
00325 NIVissimTL::~NIVissimTL() {}
00326 
00327 
00328 
00329 
00330 
00331 bool
00332 NIVissimTL::dictionary(int id, const std::string &type,
00333                        const std::string &name, SUMOTime absdur,
00334                        SUMOTime offset) {
00335     NIVissimTL *o = new NIVissimTL(id, type, name, absdur, offset);
00336     if (!dictionary(id, o)) {
00337         delete o;
00338         return false;
00339     }
00340     return true;
00341 }
00342 
00343 bool
00344 NIVissimTL::dictionary(int id, NIVissimTL *o) {
00345     DictType::iterator i=myDict.find(id);
00346     if (i==myDict.end()) {
00347         myDict[id] = o;
00348         return true;
00349     }
00350     return false;
00351 }
00352 
00353 
00354 NIVissimTL *
00355 NIVissimTL::dictionary(int id) {
00356     DictType::iterator i=myDict.find(id);
00357     if (i==myDict.end()) {
00358         return 0;
00359     }
00360     return (*i).second;
00361 }
00362 
00363 
00364 void
00365 NIVissimTL::clearDict() {
00366     for (DictType::iterator i=myDict.begin(); i!=myDict.end(); i++) {
00367         delete(*i).second;
00368     }
00369     myDict.clear();
00370 }
00371 
00372 
00373 
00374 
00375 
00376 bool
00377 NIVissimTL::dict_SetSignals(NBTrafficLightLogicCont &tlc,
00378                             NBEdgeCont &ec) {
00379     size_t ref = 0;
00380     size_t ref_groups = 0;
00381     size_t ref_signals = 0;
00382     size_t no_signals = 0;
00383     size_t no_groups = 0;
00384     for (DictType::iterator i=myDict.begin(); i!=myDict.end(); i++) {
00385         NIVissimTL *tl = (*i).second;
00386         /*      if(tl->myType!="festzeit") {
00387                     cout << " Warning: The traffic light '" << tl->myID
00388                         << "' could not be assigned to a node." << endl;
00389                     ref++;
00390                     continue;
00391                 }*/
00392         std::string id = toString<int>(tl->myID);
00393         NBLoadedTLDef *def = new NBLoadedTLDef(id);
00394         if (!tlc.insert(def)) {
00395             MsgHandler::getErrorInstance()->inform("Error on adding a traffic light\n Must be a multiple id ('" + id + "')");
00396             continue;
00397         }
00398         def->setCycleDuration((unsigned int) tl->myAbsDuration);
00399         // add each group to the node's container
00400         SGroupDictType sgs = NIVissimTLSignalGroup::getGroupsFor(tl->getID());
00401         for (SGroupDictType::const_iterator j=sgs.begin(); j!=sgs.end(); j++) {
00402             if (!(*j).second->addTo(def)) {
00403                 WRITE_WARNING("The signal group '" + toString<int>((*j).first)+ "' could not be assigned to tl '"+ toString<int>(tl->myID) + "'.");
00404                 ref_groups++;
00405             }
00406             no_groups++;
00407         }
00408         // add the signal group signals to the node
00409         SSignalDictType signals = NIVissimTLSignal::getSignalsFor(tl->getID());
00410         for (SSignalDictType::const_iterator k=signals.begin(); k!=signals.end(); k++) {
00411             if (!(*k).second->addTo(ec, def)) {
00412                 WRITE_WARNING("The signal '" + toString<int>((*k).first)+ "' could not be assigned to tl '" + toString<int>(tl->myID) + "'.");
00413                 ref_signals++;
00414             }
00415             no_signals++;
00416         }
00417     }
00418     if (ref!=0) {
00419         WRITE_WARNING("Could not set " + toString<size_t>(ref)+ " of " + toString<size_t>(myDict.size())+ " traffic lights.");
00420     }
00421     if (ref_groups!=0) {
00422         WRITE_WARNING("Could not set " + toString<size_t>(ref_groups)+ " of " + toString<size_t>(no_groups)+ " groups.");
00423     }
00424     if (ref_signals!=0) {
00425         WRITE_WARNING("Could not set " + toString<size_t>(ref_signals)+ " of " + toString<size_t>(no_signals)+ " signals.");
00426     }
00427     return true;
00428 
00429 }
00430 
00431 
00432 std::string
00433 NIVissimTL::getType() const {
00434     return myType;
00435 }
00436 
00437 
00438 int
00439 NIVissimTL::getID() const {
00440     return myID;
00441 }
00442 
00443 
00444 
00445 /****************************************************************************/
00446 

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