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 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 #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/MSCORN.h>
00041 #include <microsim/MSJunctionControl.h>
00042 #include <microsim/traffic_lights/MSTLLogicControl.h>
00043 #include <utils/common/RGBColor.h>
00044 #include <utils/geom/Position2DVector.h>
00045 #include <utils/shapes/Polygon2D.h>
00046 #include "GUISUMOViewParent.h"
00047 #include "GUIViewTraffic.h"
00048 #include <utils/gui/windows/GUISUMOAbstractView.h>
00049 #include <utils/gui/windows/GUIPerspectiveChanger.h>
00050 #include <utils/gui/windows/GUIAppEnum.h>
00051 #include <utils/gui/globjects/GUIGlObject_AbstractAdd.h>
00052 #include <utils/foxtools/MFXCheckableButton.h>
00053 #include <utils/gui/images/GUIIconSubSys.h>
00054 #include <gui/GUIApplicationWindow.h>
00055 #include <foreign/polyfonts/polyfonts.h>
00056 #include <utils/gui/windows/GUIDialog_ViewSettings.h>
00057 #include <utils/gui/settings/GUICompleteSchemeStorage.h>
00058 #include <utils/gui/images/GUITexturesHelper.h>
00059 #include <utils/foxtools/MFXImageHelper.h>
00060 #include <utils/gui/globjects/GUIGlObjectStorage.h>
00061 #include <foreign/rtree/SUMORTree.h>
00062 #include <utils/gui/div/GLHelper.h>
00063 
00064 #ifdef _WIN32
00065 #include <windows.h>
00066 #endif
00067 
00068 #include <GL/gl.h>
00069 #include <GL/glu.h>
00070 
00071 #ifdef CHECK_MEMORY_LEAKS
00072 #include <foreign/nvwa/debug_new.h>
00073 #endif // CHECK_MEMORY_LEAKS
00074 
00075 
00076 // ===========================================================================
00077 // member method definitions
00078 // ===========================================================================
00079 GUIViewTraffic::GUIViewTraffic(FXComposite *p,
00080                                GUIMainWindow &app,
00081                                GUISUMOViewParent *parent,
00082                                GUINet &net, FXGLVisual *glVis,
00083                                FXGLCanvas *share)
00084         : GUISUMOAbstractView(p, app, parent, *net.myGrid, glVis, share),
00085         myTrackedID(-1) {
00086 }
00087 
00088 
00089 GUIViewTraffic::~GUIViewTraffic() {
00090 }
00091 
00092 
00093 void
00094 GUIViewTraffic::buildViewToolBars(GUIGlChildWindow &v) {
00095     // build coloring tools
00096     {
00097         const std::vector<std::string> &names = gSchemeStorage.getNames();
00098         for (std::vector<std::string>::const_iterator i=names.begin(); i!=names.end(); ++i) {
00099             v.getColoringSchemesCombo().appendItem((*i).c_str());
00100             if ((*i) == myVisualizationSettings->name) {
00101                 v.getColoringSchemesCombo().setCurrentItem(v.getColoringSchemesCombo().getNumItems()-1);
00102             }
00103         }
00104         v.getColoringSchemesCombo().setNumVisible(5);
00105     }
00106     // for junctions
00107     new FXButton(v.getLocatorPopup(),
00108                  "\tLocate Junction\tLocate a junction within the network.",
00109                  GUIIconSubSys::getIcon(ICON_LOCATEJUNCTION), &v, MID_LOCATEJUNCTION,
00110                  ICON_ABOVE_TEXT|FRAME_THICK|FRAME_RAISED);
00111     // for edges
00112     new FXButton(v.getLocatorPopup(),
00113                  "\tLocate Street\tLocate a street within the network.",
00114                  GUIIconSubSys::getIcon(ICON_LOCATEEDGE), &v, MID_LOCATEEDGE,
00115                  ICON_ABOVE_TEXT|FRAME_THICK|FRAME_RAISED);
00116     // for vehicles
00117     new FXButton(v.getLocatorPopup(),
00118                  "\tLocate Vehicle\tLocate a vehicle within the network.",
00119                  GUIIconSubSys::getIcon(ICON_LOCATEVEHICLE), &v, MID_LOCATEVEHICLE,
00120                  ICON_ABOVE_TEXT|FRAME_THICK|FRAME_RAISED);
00121     // for tls
00122     new FXButton(v.getLocatorPopup(),
00123                  "\tLocate TLS\tLocate a tls within the network.",
00124                  GUIIconSubSys::getIcon(ICON_LOCATETLS), &v, MID_LOCATETLS,
00125                  ICON_ABOVE_TEXT|FRAME_THICK|FRAME_RAISED);
00126     // for additional stuff
00127     new FXButton(v.getLocatorPopup(),
00128                  "\tLocate Additional\tLocate an additional structure within the network.",
00129                  GUIIconSubSys::getIcon(ICON_LOCATEADD), &v, MID_LOCATEADD,
00130                  ICON_ABOVE_TEXT|FRAME_THICK|FRAME_RAISED);
00131     // for shapes
00132     new FXButton(v.getLocatorPopup(),
00133                  "\tLocate Shape\tLocate a shape within the network.",
00134                  GUIIconSubSys::getIcon(ICON_LOCATESHAPE), &v, MID_LOCATESHAPE,
00135                  ICON_ABOVE_TEXT|FRAME_THICK|FRAME_RAISED);
00136 }
00137 
00138 
00139 void
00140 GUIViewTraffic::setColorScheme(const std::string &name) {
00141     if (myVisualizationChanger!=0) {
00142         if (myVisualizationChanger->getCurrentScheme()!=name) {
00143             myVisualizationChanger->setCurrentScheme(name);
00144         }
00145     }
00146     myVisualizationSettings = &gSchemeStorage.get(name.c_str());
00147     update();
00148 }
00149 
00150 
00151 int
00152 GUIViewTraffic::doPaintGL(int mode, SUMOReal scale) {
00153     // init view settings
00154     glRenderMode(mode);
00155     glMatrixMode(GL_MODELVIEW);
00156     glPushMatrix();
00157     glDisable(GL_TEXTURE_2D);
00158     glDisable(GL_ALPHA_TEST);
00159     glDisable(GL_BLEND);
00160     glEnable(GL_DEPTH_TEST);
00161 
00162     GLdouble sxmin = myCX - myX1;
00163     GLdouble sxmax = myCX + myX1;
00164     GLdouble symin = myCY - myY1;
00165     GLdouble symax = myCY + myY1;
00166 
00167     // compute lane width
00168     SUMOReal lw = m2p(3.0) * scale;
00169     // draw decals (if not in grabbing mode)
00170     if (!myUseToolTips) {
00171         drawDecals();
00172         if (myVisualizationSettings->showGrid) {
00173             paintGLGrid();
00174         }
00175     }
00176 
00177     glLineWidth(1);
00178     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00179     float minB[2];
00180     float maxB[2];
00181     minB[0] = sxmin;
00182     minB[1] = symin;
00183     maxB[0] = sxmax;
00184     maxB[1] = symax;
00185     myVisualizationSettings->needsGlID = myUseToolTips;
00186     myVisualizationSettings->scale = lw;
00187     glEnable(GL_POLYGON_OFFSET_FILL);
00188     glEnable(GL_POLYGON_OFFSET_LINE);
00189     int hits2 = myGrid->Search(minB, maxB, *myVisualizationSettings);
00190     //
00191     glTranslated(0, 0, -.01);
00192     for (std::vector<VehicleOps>::iterator i=myVehicleOps.begin(); i!=myVehicleOps.end(); ++i) {
00193         const VehicleOps &vo = *i;
00194         switch (vo.type) {
00195         case VO_SHOW_ROUTE: {
00196             if (vo.routeNo>=0) {
00197                 drawRoute(vo, vo.routeNo, 0.25);
00198             } else {
00199                 if (vo.vehicle->hasCORNIntValue(MSCORN::CORN_VEH_NUMBERROUTE)) {
00200                     int noReroutePlus1 = vo.vehicle->getCORNIntValue(MSCORN::CORN_VEH_NUMBERROUTE) + 1;
00201                     for (int i=noReroutePlus1-1; i>=0; i--) {
00202                         SUMOReal darken = SUMOReal(0.4) / SUMOReal(noReroutePlus1) * SUMOReal(i);
00203                         drawRoute(vo, i, darken);
00204                     }
00205                 } else {
00206                     drawRoute(vo, 0, 0.25);
00207                 }
00208             }
00209         }
00210         break;
00211         case VO_SHOW_BEST_LANES: {
00212             drawBestLanes(vo);
00213             break;
00214         }
00215         break;
00216         default:
00217             break;
00218         }
00219     }
00220     glTranslated(0, 0, .01);
00221     glPopMatrix();
00222     /*
00223     // draw legends
00224     glMatrixMode(GL_MODELVIEW);
00225     glLoadIdentity();
00226     glTranslated(1.-.2, 1.-.5, 0.);
00227     glScaled(.2, .5, 1.);
00228     GUIColoringSchemesMap<GUILaneWrapper> &sm = GUIViewTraffic::getLaneSchemesMap(); //!!!
00229     sm.getColorer(myVisualizationSettings->laneEdgeMode)->drawLegend();
00230     */
00231     return hits2;
00232 }
00233 
00234 
00235 void
00236 GUIViewTraffic::startTrack(int id) {
00237     myTrackedID = id;
00238 }
00239 
00240 
00241 void
00242 GUIViewTraffic::stopTrack() {
00243     myTrackedID = -1;
00244 }
00245 
00246 
00247 int
00248 GUIViewTraffic::getTrackedID() const {
00249     return myTrackedID;
00250 }
00251 
00252 
00253 void
00254 GUIViewTraffic::drawRoute(const VehicleOps &vo, int routeNo, SUMOReal darken) {
00255     if (myUseToolTips) {
00256         glPushName(vo.vehicle->getGlID());
00257     }
00258     myVisualizationSettings->vehicleColorer.setGlColor(*(vo.vehicle));
00259     GLdouble colors[4];
00260     glGetDoublev(GL_CURRENT_COLOR, colors);
00261     colors[0] -= darken;
00262     if (colors[0]<0) colors[0] = 0;
00263     colors[1] -= darken;
00264     if (colors[1]<0) colors[1] = 0;
00265     colors[2] -= darken;
00266     if (colors[2]<0) colors[2] = 0;
00267     colors[3] -= darken;
00268     if (colors[3]<0) colors[3] = 0;
00269     glColor3dv(colors);
00270     draw(vo.vehicle->getRoute(routeNo));
00271     if (myUseToolTips) {
00272         glPopName();
00273     }
00274 }
00275 
00276 
00277 void
00278 GUIViewTraffic::drawBestLanes(const VehicleOps &vo) {
00279     if (myUseToolTips) {
00280         glPushName(vo.vehicle->getGlID());
00281     }
00282     const std::vector<MSVehicle::LaneQ> &lanes = vo.vehicle->getBestLanes();
00283     SUMOReal gmax = -1;
00284     SUMOReal rmax = -1;
00285     for (std::vector<MSVehicle::LaneQ>::const_iterator i=lanes.begin(); i!=lanes.end(); ++i) {
00286         gmax = MAX2((*i).length, gmax);
00287         rmax = MAX2((*i).occupied, rmax);
00288     }
00289     for (std::vector<MSVehicle::LaneQ>::const_iterator i=lanes.begin(); i!=lanes.end(); ++i) {
00290         const Position2DVector &shape = (*i).lane->getShape();
00291         SUMOReal g = (*i).length / gmax;
00292         SUMOReal r = (*i).occupied / rmax;
00293         glColor3d(r, g, 0);
00294         GLHelper::drawBoxLines(shape, 0.5);
00295 
00296         Position2DVector s1 = shape;
00297         s1.move2side((SUMOReal) .1);
00298         glColor3d(r, 0, 0);
00299         GLHelper::drawLine(s1);
00300         s1.move2side((SUMOReal) -.2);
00301         glColor3d(0, g, 0);
00302         GLHelper::drawLine(s1);
00303 
00304         glColor3d(r, g, 0);
00305         Position2D lastPos = shape[-1];
00306         for (std::vector<MSLane*>::const_iterator j=(*i).joined.begin(); j!=(*i).joined.end(); ++j) {
00307             const Position2DVector &shape = (*j)->getShape();
00308             GLHelper::drawLine(lastPos, shape[0]);
00309             GLHelper::drawBoxLines(shape, (SUMOReal) 0.2);
00310             lastPos = shape[-1];
00311         }
00312     }
00313     if (myUseToolTips) {
00314         glPopName();
00315     }
00316 }
00317 
00318 
00319 void
00320 GUIViewTraffic::draw(const MSRoute &r) {
00321     MSRouteIterator i = r.begin();
00322     for (; i!=r.end(); ++i) {
00323         const MSEdge *e = *i;
00324         const GUIEdge *ge = static_cast<const GUIEdge*>(e);
00325         const GUILaneWrapper &lane = ge->getLaneGeometry((size_t) 0);
00326         GLHelper::drawBoxLines(lane.getShape(), lane.getShapeRotations(), lane.getShapeLengths(), 1.0);
00327     }
00328 }
00329 
00330 
00331 void
00332 GUIViewTraffic::showRoute(GUIVehicle * v, int index) throw() {
00333     VehicleOps vo;
00334     vo.vehicle = v;
00335     vo.type = VO_SHOW_ROUTE;
00336     vo.routeNo = index;
00337     myVehicleOps.push_back(vo);
00338     update();
00339 }
00340 
00341 
00342 void
00343 GUIViewTraffic::showBestLanes(GUIVehicle *v) {
00344     VehicleOps vo;
00345     vo.vehicle = v;
00346     vo.type = VO_SHOW_BEST_LANES;
00347     myVehicleOps.push_back(vo);
00348     update();
00349 }
00350 
00351 
00352 void
00353 GUIViewTraffic::hideRoute(GUIVehicle * v, int index) throw() {
00354     std::vector<VehicleOps>::iterator i =
00355         find_if(myVehicleOps.begin(), myVehicleOps.end(), vehicle_in_ops_finder(v));
00356     while (i!=myVehicleOps.end()) {
00357         if ((*i).type==VO_SHOW_ROUTE&&(*i).routeNo==index) {
00358             i = myVehicleOps.erase(i);
00359             update();
00360             return;
00361         }
00362         i = find_if(i+1, myVehicleOps.end(), vehicle_in_ops_finder(v));
00363     }
00364     update();
00365 }
00366 
00367 
00368 void
00369 GUIViewTraffic::hideBestLanes(GUIVehicle *v) {
00370     std::vector<VehicleOps>::iterator i =
00371         find_if(myVehicleOps.begin(), myVehicleOps.end(), vehicle_in_ops_finder(v));
00372     while (i!=myVehicleOps.end()) {
00373         if ((*i).type==VO_SHOW_BEST_LANES) {
00374             i = myVehicleOps.erase(i);
00375             update();
00376             return;
00377         }
00378         i = find_if(i+1, myVehicleOps.end(), vehicle_in_ops_finder(v));
00379     }
00380     update();
00381 }
00382 
00383 
00384 bool
00385 GUIViewTraffic::amShowingRouteFor(GUIVehicle * v, int index) throw() {
00386     std::vector<VehicleOps>::iterator i =
00387         find_if(myVehicleOps.begin(), myVehicleOps.end(), vehicle_in_ops_finder(v));
00388     while (i!=myVehicleOps.end()) {
00389         if ((*i).type==VO_SHOW_ROUTE&&(*i).routeNo==index) {
00390             return true;
00391         }
00392         i = find_if(i+1, myVehicleOps.end(), vehicle_in_ops_finder(v));
00393     }
00394     return false;
00395 }
00396 
00397 
00398 bool
00399 GUIViewTraffic::amShowingBestLanesFor(GUIVehicle *v) {
00400     std::vector<VehicleOps>::iterator i =
00401         find_if(myVehicleOps.begin(), myVehicleOps.end(), vehicle_in_ops_finder(v));
00402     while (i!=myVehicleOps.end()) {
00403         if ((*i).type==VO_SHOW_BEST_LANES) {
00404             return true;
00405         }
00406         i = find_if(i+1, myVehicleOps.end(), vehicle_in_ops_finder(v));
00407     }
00408     return false;
00409 }
00410 
00411 
00412 
00413 void
00414 GUIViewTraffic::showViewschemeEditor() {
00415     if (myVisualizationChanger==0) {
00416         myVisualizationChanger =
00417             new GUIDialog_ViewSettings(
00418             this, myVisualizationSettings,
00419             &myDecals, &myDecalsLock);
00420         myVisualizationChanger->create();
00421     } else {
00422         myVisualizationChanger->setCurrent(myVisualizationSettings);
00423     }
00424     myVisualizationChanger->show();
00425 }
00426 
00427 
00428 void
00429 GUIViewTraffic::onGamingClick(Position2D pos) {
00430     MSTLLogicControl &tlsControl = MSNet::getInstance()->getTLSControl();
00431     const std::vector<MSTrafficLightLogic*> &logics = tlsControl.getAllLogics();
00432     MSTrafficLightLogic *minTll = 0;
00433     SUMOReal minDist = std::numeric_limits<SUMOReal>::infinity();
00434     for (std::vector<MSTrafficLightLogic*>::const_iterator i=logics.begin(); i!=logics.end(); ++i) {
00435         // get the logic
00436         MSTrafficLightLogic *tll = (*i);
00437         if (tlsControl.isActive(tll)) {
00438             // get the links
00439             const MSTrafficLightLogic::LaneVector &lanes = tll->getLanesAt(0);
00440             if (lanes.size()>0) {
00441                 const Position2D &endPos = lanes[0]->getShape().getEnd();
00442                 if (endPos.distanceTo(pos) < minDist) {
00443                     minDist = endPos.distanceTo(pos);
00444                     minTll = tll;
00445                 }
00446             }
00447         }
00448     }
00449     if (minTll != 0) {
00450         const MSTLLogicControl::TLSLogicVariants &vars = tlsControl.get(minTll->getID());
00451         const std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
00452         if (logics.size() > 1) {
00453             MSSimpleTrafficLightLogic *l = (MSSimpleTrafficLightLogic*) logics[0];
00454             for (unsigned int i = 0; i < logics.size()-1; i++) {
00455                 if (minTll->getSubID() == logics[i]->getSubID()) {
00456                     l = (MSSimpleTrafficLightLogic*) logics[i+1];
00457                     tlsControl.switchTo(minTll->getID(), l->getSubID());
00458                 }
00459             }
00460             if (l == logics[0]) {
00461                 tlsControl.switchTo(minTll->getID(), l->getSubID());
00462             }
00463             l->changeStepAndDuration(tlsControl, MSNet::getInstance()->getCurrentTimeStep(), 0, l->getPhase(0).duration);
00464             update();
00465         }
00466     }
00467 }
00468 
00469 
00470 /****************************************************************************/

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