SUMO - Simulation of Urban MObility

GUITrafficLightLogicWrapper.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A wrapper for tl-logics to allow their visualisation and interaction
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 #ifdef WIN32
00031 #include <windows.h>
00032 #endif
00033 
00034 #include <GL/gl.h>
00035 
00036 #include <cassert>
00037 #include <utils/gui/globjects/GUIGlObject.h>
00038 #include <utils/gui/div/GLObjectValuePassConnector.h>
00039 #include <utils/gui/windows/GUIAppEnum.h>
00040 #include <utils/gui/images/GUIIconSubSys.h>
00041 #include <utils/gui/div/GLHelper.h>
00042 #include <utils/gui/globjects/GUIGLObjectPopupMenu.h>
00043 #include <utils/gui/div/GUIGlobalSelection.h>
00044 #include <microsim/MSLane.h>
00045 #include <microsim/traffic_lights/MSTrafficLightLogic.h>
00046 #include <microsim/traffic_lights/MSTLLogicControl.h>
00047 #include <microsim/logging/FunctionBinding.h>
00048 #include <microsim/logging/FuncBinding_StringParam.h>
00049 #include <gui/GUIApplicationWindow.h>
00050 #include <gui/GUITLLogicPhasesTrackerWindow.h>
00051 #include <gui/GUIGlobals.h>
00052 #include "GUITrafficLightLogicWrapper.h"
00053 #include "GUINet.h"
00054 
00055 #ifdef CHECK_MEMORY_LEAKS
00056 #include <foreign/nvwa/debug_new.h>
00057 #endif // CHECK_MEMORY_LEAKS
00058 
00059 
00060 // ===========================================================================
00061 // FOX callback mapping
00062 // ===========================================================================
00063 FXDEFMAP(GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu)
00064 GUITrafficLightLogicWrapperPopupMenuMap[]= {
00065     FXMAPFUNC(SEL_COMMAND,  MID_SHOWPHASES,             GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu::onCmdShowPhases),
00066     FXMAPFUNC(SEL_COMMAND,  MID_TRACKPHASES,            GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu::onCmdBegin2TrackPhases),
00067     FXMAPFUNC(SEL_COMMAND,  MID_SWITCH_OFF,             GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu::onCmdSwitchTLS2Off),
00068     FXMAPFUNCS(SEL_COMMAND, MID_SWITCH, MID_SWITCH+20, GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu::onCmdSwitchTLSLogic),
00069 };
00070 
00071 // Object implementation
00072 FXIMPLEMENT(GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu, GUIGLObjectPopupMenu, GUITrafficLightLogicWrapperPopupMenuMap, ARRAYNUMBER(GUITrafficLightLogicWrapperPopupMenuMap))
00073 
00074 
00075 // ===========================================================================
00076 // method definitions
00077 // ===========================================================================
00078 /* -------------------------------------------------------------------------
00079  * GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu - methods
00080  * ----------------------------------------------------------------------- */
00081 GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu::GUITrafficLightLogicWrapperPopupMenu(
00082     GUIMainWindow& app, GUISUMOAbstractView& parent,
00083     GUIGlObject& o)
00084     : GUIGLObjectPopupMenu(app, parent, o) {}
00085 
00086 
00087 GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu::~GUITrafficLightLogicWrapperPopupMenu() throw() {}
00088 
00089 
00090 
00091 long
00092 GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu::onCmdBegin2TrackPhases(
00093     FXObject*,FXSelector,void*) {
00094     assert(myObject->getType()==GLO_TLLOGIC);
00095     static_cast<GUITrafficLightLogicWrapper*>(myObject)->begin2TrackPhases();
00096     return 1;
00097 }
00098 
00099 
00100 long
00101 GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu::onCmdShowPhases(
00102     FXObject*,FXSelector,void*) {
00103     assert(myObject->getType()==GLO_TLLOGIC);
00104     static_cast<GUITrafficLightLogicWrapper*>(myObject)->showPhases();
00105     return 1;
00106 }
00107 
00108 
00109 long
00110 GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu::onCmdSwitchTLS2Off(
00111     FXObject*,FXSelector /*sel*/,void*) {
00112     assert(myObject->getType()==GLO_TLLOGIC);
00113     static_cast<GUITrafficLightLogicWrapper*>(myObject)->switchTLSLogic(-1);
00114     return 1;
00115 }
00116 
00117 
00118 long
00119 GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu::onCmdSwitchTLSLogic(
00120     FXObject*,FXSelector sel,void*) {
00121     assert(myObject->getType()==GLO_TLLOGIC);
00122     static_cast<GUITrafficLightLogicWrapper*>(myObject)->switchTLSLogic(FXSELID(sel)-MID_SWITCH);
00123     return 1;
00124 }
00125 
00126 
00127 
00128 /* -------------------------------------------------------------------------
00129  * GUITrafficLightLogicWrapper - methods
00130  * ----------------------------------------------------------------------- */
00131 GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapper(
00132     MSTLLogicControl& control, MSTrafficLightLogic& tll) throw() :
00133     GUIGlObject(GLO_TLLOGIC, tll.getID()),
00134     myTLLogicControl(control), myTLLogic(tll) {}
00135 
00136 
00137 GUITrafficLightLogicWrapper::~GUITrafficLightLogicWrapper() throw() {}
00138 
00139 
00140 GUIGLObjectPopupMenu*
00141 GUITrafficLightLogicWrapper::getPopUpMenu(GUIMainWindow& app,
00142         GUISUMOAbstractView& parent) throw() {
00143     myApp = &app;
00144     GUIGLObjectPopupMenu* ret = new GUITrafficLightLogicWrapperPopupMenu(app, parent, *this);
00145     buildPopupHeader(ret, app);
00146     buildCenterPopupEntry(ret);
00147     //
00148     const MSTLLogicControl::TLSLogicVariants& vars = myTLLogicControl.get(myTLLogic.getID());
00149     std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
00150     if (logics.size()>1) {
00151         std::vector<MSTrafficLightLogic*>::const_iterator i;
00152         size_t index = 0;
00153         for (i=logics.begin(); i!=logics.end(); ++i, ++index) {
00154             if (!vars.isActive(*i)) {
00155                 new FXMenuCommand(ret, ("Switch to '" + (*i)->getProgramID() + "'").c_str(),
00156                                   GUIIconSubSys::getIcon(ICON_FLAG_MINUS), ret, (FXSelector)(MID_SWITCH+index));
00157             }
00158         }
00159         new FXMenuSeparator(ret);
00160     }
00161     new FXMenuCommand(ret, "Switch off", GUIIconSubSys::getIcon(ICON_FLAG_MINUS), ret, MID_SWITCH_OFF);
00162     new FXMenuCommand(ret, "Track Phases", 0, ret, MID_TRACKPHASES);
00163     new FXMenuCommand(ret, "Show Phases", 0, ret, MID_SHOWPHASES);
00164     new FXMenuSeparator(ret);
00165     //
00166     buildNameCopyPopupEntry(ret);
00167     buildSelectionPopupEntry(ret);
00168     buildPositionCopyEntry(ret, false);
00169     return ret;
00170 }
00171 
00172 
00173 void
00174 GUITrafficLightLogicWrapper::begin2TrackPhases() {
00175     GUITLLogicPhasesTrackerWindow* window =
00176         new GUITLLogicPhasesTrackerWindow(*myApp, myTLLogic, *this,
00177                                           new FuncBinding_StringParam<MSTLLogicControl, std::pair<SUMOTime, MSPhaseDefinition> >
00178                                           (&MSNet::getInstance()->getTLSControl(), &MSTLLogicControl::getPhaseDef, myTLLogic.getID()));
00179     window->create();
00180     window->show();
00181 }
00182 
00183 
00184 void
00185 GUITrafficLightLogicWrapper::showPhases() {
00186     GUITLLogicPhasesTrackerWindow* window =
00187         new GUITLLogicPhasesTrackerWindow(*myApp, myTLLogic, *this,
00188                                           static_cast<MSSimpleTrafficLightLogic&>(myTLLogic).getPhases());
00189     window->setBeginTime(0);
00190     window->create();
00191     window->show();
00192 }
00193 
00194 
00195 GUIParameterTableWindow*
00196 GUITrafficLightLogicWrapper::getParameterWindow(GUIMainWindow&,
00197         GUISUMOAbstractView&) throw() {
00198     return 0;
00199 }
00200 
00201 
00202 Boundary
00203 GUITrafficLightLogicWrapper::getCenteringBoundary() const throw() {
00204     Boundary ret;
00205     const MSTrafficLightLogic::LaneVectorVector& lanes = myTLLogic.getLanes();
00206     for (MSTrafficLightLogic::LaneVectorVector::const_iterator i=lanes.begin(); i!=lanes.end(); ++i) {
00207         const MSTrafficLightLogic::LaneVector& lanes2 = (*i);
00208         for (MSTrafficLightLogic::LaneVector::const_iterator j=lanes2.begin(); j!=lanes2.end(); ++j) {
00209             ret.add((*j)->getShape()[-1]);
00210         }
00211     }
00212     ret.grow(20);
00213     return ret;
00214 }
00215 
00216 
00217 void
00218 GUITrafficLightLogicWrapper::switchTLSLogic(int to) {
00219     if (to==-1) {
00220         myTLLogicControl.switchTo(myTLLogic.getID(), "off");
00221         MSTrafficLightLogic* tll = myTLLogicControl.getActive(myTLLogic.getID());
00222         GUINet::getGUIInstance()->createTLWrapper(tll);
00223     } else {
00224         const MSTLLogicControl::TLSLogicVariants& vars = myTLLogicControl.get(myTLLogic.getID());
00225         std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
00226         myTLLogicControl.switchTo(myTLLogic.getID(), logics[to]->getProgramID());
00227     }
00228 }
00229 
00230 
00231 int
00232 GUITrafficLightLogicWrapper::getLinkIndex(const MSLink* const link) const {
00233     return myTLLogic.getLinkIndex(link);
00234 }
00235 
00236 
00237 void
00238 GUITrafficLightLogicWrapper::drawGL(const GUIVisualizationSettings& s) const throw() {
00239     if (s.gaming) {
00240         if (!MSNet::getInstance()->getTLSControl().isActive(&myTLLogic)) {
00241             return;
00242         };
00243         const std::string& curState = myTLLogic.getCurrentPhaseDef().getState();
00244         if (curState.find_first_of("gG") == std::string::npos) {
00245             // no link is 'green' at the moment. find those that turn green next
00246             const MSTrafficLightLogic::Phases& phases = myTLLogic.getPhases();
00247             unsigned int curPhaseIdx = myTLLogic.getCurrentPhaseIndex();
00248             unsigned int phaseIdx = (curPhaseIdx + 1) % phases.size();
00249             std::vector<unsigned int> nextGreen;
00250             while (phaseIdx != curPhaseIdx) {
00251                 const std::string& state = phases[phaseIdx]->getState();
00252                 for (unsigned int linkIdx = 0; linkIdx < state.size(); linkIdx++) {
00253                     if ((LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MINOR ||
00254                             (LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MAJOR) {
00255                         nextGreen.push_back(linkIdx);
00256                     }
00257                 }
00258                 if (nextGreen.size() > 0) {
00259                     break;
00260                 }
00261                 phaseIdx = (phaseIdx + 1) % phases.size();
00262             }
00263             // highlight nextGreen links
00264             for (std::vector<unsigned int>::iterator it_idx = nextGreen.begin(); it_idx != nextGreen.end(); it_idx++) {
00265                 const MSTrafficLightLogic::LaneVector& lanes = myTLLogic.getLanesAt(*it_idx);
00266                 for (MSTrafficLightLogic::LaneVector::const_iterator it_lane = lanes.begin(); it_lane != lanes.end(); it_lane++) {
00267                     glPushMatrix();
00268                     glColor3d(0,1,0);
00269                     Position pos = (*it_lane)->getShape().getEnd();
00270                     glTranslated(pos.x(), pos.y(), GLO_MAX);
00271                     GLHelper::drawFilledCircle((*it_lane)->getWidth() / 2.);
00272                     glPopMatrix();
00273                 }
00274             }
00275         }
00276     }
00277 }
00278 
00279 
00280 /****************************************************************************/
00281 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines