SUMO - Simulation of Urban MObility

GUIViewTraffic.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A view on the simulation; this view is a microscopic one
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 <utility>
00032 #include <cmath>
00033 #include <limits>
00034 #include <guisim/GUINet.h>
00035 #include <guisim/GUIEdge.h>
00036 #include <guisim/GUILane.h>
00037 #include <guisim/GUIVehicle.h>
00038 #include <microsim/MSEdge.h>
00039 #include <microsim/MSLane.h>
00040 #include <microsim/MSJunctionControl.h>
00041 #include <microsim/traffic_lights/MSTLLogicControl.h>
00042 #include <utils/common/RGBColor.h>
00043 #include <utils/geom/PositionVector.h>
00044 #include <utils/shapes/Polygon.h>
00045 #include "GUISUMOViewParent.h"
00046 #include "GUIViewTraffic.h"
00047 #include <utils/gui/windows/GUISUMOAbstractView.h>
00048 #include <utils/gui/windows/GUIPerspectiveChanger.h>
00049 #include <utils/gui/windows/GUIAppEnum.h>
00050 #include <utils/foxtools/MFXCheckableButton.h>
00051 #include <utils/gui/images/GUIIconSubSys.h>
00052 #include <gui/GUIApplicationWindow.h>
00053 #include <foreign/polyfonts/polyfonts.h>
00054 #include <utils/gui/windows/GUIDialog_ViewSettings.h>
00055 #include <utils/gui/settings/GUICompleteSchemeStorage.h>
00056 #include <utils/gui/images/GUITexturesHelper.h>
00057 #include <utils/foxtools/MFXImageHelper.h>
00058 #include <utils/gui/globjects/GUIGlObjectStorage.h>
00059 #include <foreign/rtree/SUMORTree.h>
00060 #include <utils/gui/div/GLHelper.h>
00061 
00062 #ifdef _WIN32
00063 #include <windows.h>
00064 #endif
00065 
00066 #include <GL/gl.h>
00067 #include <GL/glu.h>
00068 
00069 #ifdef CHECK_MEMORY_LEAKS
00070 #include <foreign/nvwa/debug_new.h>
00071 #endif // CHECK_MEMORY_LEAKS
00072 
00073 
00074 // ===========================================================================
00075 // member method definitions
00076 // ===========================================================================
00077 GUIViewTraffic::GUIViewTraffic(
00078     FXComposite* p,
00079     GUIMainWindow& app,
00080     GUISUMOViewParent* parent,
00081     GUINet& net, FXGLVisual* glVis,
00082     FXGLCanvas* share) :
00083     GUISUMOAbstractView(p, app, parent, net.getVisualisationSpeedUp(), glVis, share),
00084     myTrackedID(-1) {}
00085 
00086 
00087 GUIViewTraffic::~GUIViewTraffic() {
00088 }
00089 
00090 
00091 void
00092 GUIViewTraffic::buildViewToolBars(GUIGlChildWindow& v) {
00093     // build coloring tools
00094     {
00095         const std::vector<std::string> &names = gSchemeStorage.getNames();
00096         for (std::vector<std::string>::const_iterator i=names.begin(); i!=names.end(); ++i) {
00097             v.getColoringSchemesCombo().appendItem((*i).c_str());
00098             if ((*i) == myVisualizationSettings->name) {
00099                 v.getColoringSchemesCombo().setCurrentItem(v.getColoringSchemesCombo().getNumItems()-1);
00100             }
00101         }
00102         v.getColoringSchemesCombo().setNumVisible(5);
00103     }
00104     // for junctions
00105     new FXButton(v.getLocatorPopup(),
00106                  "\tLocate Junction\tLocate a junction within the network.",
00107                  GUIIconSubSys::getIcon(ICON_LOCATEJUNCTION), &v, MID_LOCATEJUNCTION,
00108                  ICON_ABOVE_TEXT|FRAME_THICK|FRAME_RAISED);
00109     // for edges
00110     new FXButton(v.getLocatorPopup(),
00111                  "\tLocate Street\tLocate a street within the network.",
00112                  GUIIconSubSys::getIcon(ICON_LOCATEEDGE), &v, MID_LOCATEEDGE,
00113                  ICON_ABOVE_TEXT|FRAME_THICK|FRAME_RAISED);
00114     // for vehicles
00115     new FXButton(v.getLocatorPopup(),
00116                  "\tLocate Vehicle\tLocate a vehicle within the network.",
00117                  GUIIconSubSys::getIcon(ICON_LOCATEVEHICLE), &v, MID_LOCATEVEHICLE,
00118                  ICON_ABOVE_TEXT|FRAME_THICK|FRAME_RAISED);
00119     // for tls
00120     new FXButton(v.getLocatorPopup(),
00121                  "\tLocate TLS\tLocate a tls within the network.",
00122                  GUIIconSubSys::getIcon(ICON_LOCATETLS), &v, MID_LOCATETLS,
00123                  ICON_ABOVE_TEXT|FRAME_THICK|FRAME_RAISED);
00124     // for additional stuff
00125     new FXButton(v.getLocatorPopup(),
00126                  "\tLocate Additional\tLocate an additional structure within the network.",
00127                  GUIIconSubSys::getIcon(ICON_LOCATEADD), &v, MID_LOCATEADD,
00128                  ICON_ABOVE_TEXT|FRAME_THICK|FRAME_RAISED);
00129     // for shapes
00130     new FXButton(v.getLocatorPopup(),
00131                  "\tLocate Shape\tLocate a shape within the network.",
00132                  GUIIconSubSys::getIcon(ICON_LOCATESHAPE), &v, MID_LOCATESHAPE,
00133                  ICON_ABOVE_TEXT|FRAME_THICK|FRAME_RAISED);
00134 }
00135 
00136 
00137 bool
00138 GUIViewTraffic::setColorScheme(const std::string& name) {
00139     if (!gSchemeStorage.contains(name)) {
00140         return false;
00141     }
00142     if (myVisualizationChanger!=0) {
00143         if (myVisualizationChanger->getCurrentScheme()!=name) {
00144             myVisualizationChanger->setCurrentScheme(name);
00145         }
00146     }
00147     myVisualizationSettings = &gSchemeStorage.get(name.c_str());
00148     myVisualizationSettings->gaming = myApp->isGaming();
00149     update();
00150     return true;
00151 }
00152 
00153 
00154 int
00155 GUIViewTraffic::doPaintGL(int mode, const Boundary& bound) {
00156     // init view settings
00157     glRenderMode(mode);
00158     glMatrixMode(GL_MODELVIEW);
00159     glPushMatrix();
00160     glDisable(GL_TEXTURE_2D);
00161     glDisable(GL_ALPHA_TEST);
00162     glDisable(GL_BLEND);
00163     glEnable(GL_DEPTH_TEST);
00164 
00165     // draw decals (if not in grabbing mode)
00166     if (!myUseToolTips) {
00167         drawDecals();
00168         if (myVisualizationSettings->showGrid) {
00169             paintGLGrid();
00170         }
00171     }
00172 
00173     glLineWidth(1);
00174     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00175     float minB[2];
00176     float maxB[2];
00177     minB[0] = bound.xmin();
00178     minB[1] = bound.ymin();
00179     maxB[0] = bound.xmax();
00180     maxB[1] = bound.ymax();
00181     myVisualizationSettings->scale = m2p(SUMO_const_laneWidth);
00182     glEnable(GL_POLYGON_OFFSET_FILL);
00183     glEnable(GL_POLYGON_OFFSET_LINE);
00184     int hits2 = myGrid->Search(minB, maxB, *myVisualizationSettings);
00185     //
00186     glTranslated(0, 0, -.01);
00187     for (std::map<GUIGlObject*, int>::iterator i=myAdditionallyDrawn.begin(); i!=myAdditionallyDrawn.end(); ++i) {
00188         (i->first)->drawGLAdditional(this, *myVisualizationSettings);
00189     }
00190     glTranslated(0, 0, .01);
00191     glPopMatrix();
00192     /*
00193     // draw legends
00194     glMatrixMode(GL_MODELVIEW);
00195     glLoadIdentity();
00196     glTranslated(1.-.2, 1.-.5, 0.);
00197     glScaled(.2, .5, 1.);
00198     GUIColoringSchemesMap<GUILaneWrapper> &sm = GUIViewTraffic::getLaneSchemesMap(); //!!!
00199     sm.getColorer(myVisualizationSettings->laneEdgeMode)->drawLegend();
00200     */
00201     return hits2;
00202 }
00203 
00204 
00205 void
00206 GUIViewTraffic::startTrack(int id) {
00207     myTrackedID = id;
00208 }
00209 
00210 
00211 void
00212 GUIViewTraffic::stopTrack() {
00213     myTrackedID = -1;
00214 }
00215 
00216 
00217 int
00218 GUIViewTraffic::getTrackedID() const {
00219     return myTrackedID;
00220 }
00221 
00222 
00223 void
00224 GUIViewTraffic::showViewschemeEditor() {
00225     if (myVisualizationChanger==0) {
00226         myVisualizationChanger =
00227             new GUIDialog_ViewSettings(
00228             this, myVisualizationSettings,
00229             &myDecals, &myDecalsLock);
00230         myVisualizationChanger->create();
00231     } else {
00232         myVisualizationChanger->setCurrent(myVisualizationSettings);
00233     }
00234     myVisualizationChanger->show();
00235 }
00236 
00237 
00238 void
00239 GUIViewTraffic::onGamingClick(Position pos) {
00240     MSTLLogicControl& tlsControl = MSNet::getInstance()->getTLSControl();
00241     const std::vector<MSTrafficLightLogic*> &logics = tlsControl.getAllLogics();
00242     MSTrafficLightLogic* minTll = 0;
00243     SUMOReal minDist = std::numeric_limits<SUMOReal>::infinity();
00244     for (std::vector<MSTrafficLightLogic*>::const_iterator i=logics.begin(); i!=logics.end(); ++i) {
00245         // get the logic
00246         MSTrafficLightLogic* tll = (*i);
00247         if (tlsControl.isActive(tll)) {
00248             // get the links
00249             const MSTrafficLightLogic::LaneVector& lanes = tll->getLanesAt(0);
00250             if (lanes.size()>0) {
00251                 const Position& endPos = lanes[0]->getShape().getEnd();
00252                 if (endPos.distanceTo(pos) < minDist) {
00253                     minDist = endPos.distanceTo(pos);
00254                     minTll = tll;
00255                 }
00256             }
00257         }
00258     }
00259     if (minTll != 0) {
00260         const MSTLLogicControl::TLSLogicVariants& vars = tlsControl.get(minTll->getID());
00261         const std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
00262         if (logics.size() > 1) {
00263             MSSimpleTrafficLightLogic* l = (MSSimpleTrafficLightLogic*) logics[0];
00264             for (unsigned int i = 0; i < logics.size()-1; i++) {
00265                 if (minTll->getProgramID() == logics[i]->getProgramID()) {
00266                     l = (MSSimpleTrafficLightLogic*) logics[i+1];
00267                     tlsControl.switchTo(minTll->getID(), l->getProgramID());
00268                 }
00269             }
00270             if (l == logics[0]) {
00271                 tlsControl.switchTo(minTll->getID(), l->getProgramID());
00272             }
00273             l->changeStepAndDuration(tlsControl, MSNet::getInstance()->getCurrentTimeStep(), 0, l->getPhase(0).duration);
00274             update();
00275         }
00276     }
00277 }
00278 
00279 
00280 SUMOTime
00281 GUIViewTraffic::getCurrentTimeStep() const {
00282     return MSNet::getInstance()->getCurrentTimeStep();
00283 }
00284 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines