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