SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GUIViewTraffic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // A view on the simulation; this view is a microscopic one
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #ifdef HAVE_FFMPEG
37 #endif
38 
39 #include <iostream>
40 #include <utility>
41 #include <cmath>
42 #include <limits>
43 #include <guisim/GUINet.h>
44 #include <guisim/GUIEdge.h>
45 #include <guisim/GUILane.h>
46 #include <guisim/GUIVehicle.h>
47 #include <microsim/MSGlobals.h>
48 #include <microsim/MSEdge.h>
49 #include <microsim/MSLane.h>
53 #include <utils/common/RGBColor.h>
55 #include <utils/shapes/Polygon.h>
56 #include "GUISUMOViewParent.h"
57 #include "GUIViewTraffic.h"
70 #include <utils/gui/div/GLHelper.h>
72 
73 #ifdef CHECK_MEMORY_LEAKS
74 #include <foreign/nvwa/debug_new.h>
75 #endif // CHECK_MEMORY_LEAKS
76 
77 /* -------------------------------------------------------------------------
78  * GUIViewTraffic - FOX callback mapping
79  * ----------------------------------------------------------------------- */
80 FXDEFMAP(GUIViewTraffic) GUIViewTrafficMap[] = {
81  FXMAPFUNC(SEL_COMMAND, MID_CLOSE_LANE, GUIViewTraffic::onCmdCloseLane),
82  FXMAPFUNC(SEL_COMMAND, MID_CLOSE_EDGE, GUIViewTraffic::onCmdCloseEdge),
83  FXMAPFUNC(SEL_COMMAND, MID_ADD_REROUTER, GUIViewTraffic::onCmdAddRerouter),
84 };
85 
86 
87 FXIMPLEMENT_ABSTRACT(GUIViewTraffic, GUISUMOAbstractView, GUIViewTrafficMap, ARRAYNUMBER(GUIViewTrafficMap))
88 
89 
90 // ===========================================================================
91 // member method definitions
92 // ===========================================================================
94  FXComposite* p,
95  GUIMainWindow& app,
96  GUISUMOViewParent* parent,
97  GUINet& net, FXGLVisual* glVis,
98  FXGLCanvas* share) :
99  GUISUMOAbstractView(p, app, parent, net.getVisualisationSpeedUp(), glVis, share),
100  myTrackedID(GUIGlObject::INVALID_ID)
101 #ifdef HAVE_FFMPEG
102  , myCurrentVideo(0)
103 #endif
104 {}
105 
106 
108 }
109 
110 
111 void
113  // build coloring tools
114  {
115  const std::vector<std::string>& names = gSchemeStorage.getNames();
116  for (std::vector<std::string>::const_iterator i = names.begin(); i != names.end(); ++i) {
117  v.getColoringSchemesCombo().appendItem((*i).c_str());
118  if ((*i) == myVisualizationSettings->name) {
119  v.getColoringSchemesCombo().setCurrentItem(v.getColoringSchemesCombo().getNumItems() - 1);
120  }
121  }
122  v.getColoringSchemesCombo().setNumVisible(5);
123  }
124  // for junctions
125  new FXButton(v.getLocatorPopup(),
126  "\tLocate Junction\tLocate a junction within the network.",
128  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
129  // for edges
130  new FXButton(v.getLocatorPopup(),
131  "\tLocate Street\tLocate a street within the network.",
133  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
134 
135  // for vehicles
136  new FXButton(v.getLocatorPopup(),
137  "\tLocate Vehicle\tLocate a vehicle within the network.",
139  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
140 
141  // for persons
142  if (!MSGlobals::gUseMesoSim) { // there are no persons in mesosim (yet)
143  new FXButton(v.getLocatorPopup(),
144  "\tLocate Vehicle\tLocate a person within the network.",
146  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
147  }
148 
149  // for tls
150  new FXButton(v.getLocatorPopup(),
151  "\tLocate TLS\tLocate a tls within the network.",
153  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
154  // for additional stuff
155  new FXButton(v.getLocatorPopup(),
156  "\tLocate Additional\tLocate an additional structure within the network.",
158  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
159  // for pois
160  new FXButton(v.getLocatorPopup(),
161  "\tLocate PoI\tLocate a PoI within the network.",
163  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
164  // for polygons
165  new FXButton(v.getLocatorPopup(),
166  "\tLocate Polygon\tLocate a Polygon within the network.",
168  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
169 }
170 
171 
172 bool
173 GUIViewTraffic::setColorScheme(const std::string& name) {
174  if (!gSchemeStorage.contains(name)) {
175  return false;
176  }
177  if (myVisualizationChanger != 0) {
178  if (myVisualizationChanger->getCurrentScheme() != name) {
180  }
181  }
182  myVisualizationSettings = &gSchemeStorage.get(name.c_str());
184  update();
185  return true;
186 }
187 
188 
189 int
190 GUIViewTraffic::doPaintGL(int mode, const Boundary& bound) {
191  // init view settings
192  glRenderMode(mode);
193  glMatrixMode(GL_MODELVIEW);
194  glPushMatrix();
195  glDisable(GL_TEXTURE_2D);
196  glDisable(GL_ALPHA_TEST);
197  glDisable(GL_BLEND);
198  glEnable(GL_DEPTH_TEST);
199 
200  // draw decals (if not in grabbing mode)
201  if (!myUseToolTips) {
202  drawDecals();
204  paintGLGrid();
205  }
206  }
207 
208  glLineWidth(1);
209  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
210  const float minB[2] = { (float)bound.xmin(), (float)bound.ymin() };
211  const float maxB[2] = { (float)bound.xmax(), (float)bound.ymax() };
213  glEnable(GL_POLYGON_OFFSET_FILL);
214  glEnable(GL_POLYGON_OFFSET_LINE);
215  int hits2 = myGrid->Search(minB, maxB, *myVisualizationSettings);
216  //
217  if (myAdditionallyDrawn.size() > 0) {
218  glTranslated(0, 0, -.01);
220  for (std::map<const GUIGlObject*, int>::iterator i = myAdditionallyDrawn.begin(); i != myAdditionallyDrawn.end(); ++i) {
221  (i->first)->drawGLAdditional(this, *myVisualizationSettings);
222  }
224  glTranslated(0, 0, .01);
225  }
226  glPopMatrix();
227  /*
228  // draw legends
229  glMatrixMode(GL_MODELVIEW);
230  glLoadIdentity();
231  glTranslated(1.-.2, 1.-.5, 0.);
232  glScaled(.2, .5, 1.);
233  GUIColoringSchemesMap<GUILane> &sm = GUIViewTraffic::getLaneSchemesMap(); //!!!
234  sm.getColorer(myVisualizationSettings->laneEdgeMode)->drawLegend();
235  */
236  return hits2;
237 }
238 
239 
240 void
242  myTrackedID = id;
243 }
244 
245 
246 void
249 }
250 
251 
252 GUIGlID
254  return myTrackedID;
255 }
256 
257 
258 void
261  const std::vector<MSTrafficLightLogic*>& logics = tlsControl.getAllLogics();
262  MSTrafficLightLogic* minTll = 0;
263  SUMOReal minDist = std::numeric_limits<SUMOReal>::infinity();
264  for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
265  // get the logic
266  MSTrafficLightLogic* tll = (*i);
267  if (tlsControl.isActive(tll)) {
268  // get the links
269  const MSTrafficLightLogic::LaneVector& lanes = tll->getLanesAt(0);
270  if (lanes.size() > 0) {
271  const Position& endPos = lanes[0]->getShape().back();
272  if (endPos.distanceTo(pos) < minDist) {
273  minDist = endPos.distanceTo(pos);
274  minTll = tll;
275  }
276  }
277  }
278  }
279  if (minTll != 0) {
280  const MSTLLogicControl::TLSLogicVariants& vars = tlsControl.get(minTll->getID());
281  const std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
282  if (logics.size() > 1) {
284  for (int i = 0; i < (int)logics.size() - 1; ++i) {
285  if (minTll->getProgramID() == logics[i]->getProgramID()) {
286  l = (MSSimpleTrafficLightLogic*) logics[i + 1];
287  tlsControl.switchTo(minTll->getID(), l->getProgramID());
288  }
289  }
290  if (l == logics[0]) {
291  tlsControl.switchTo(minTll->getID(), l->getProgramID());
292  }
294  update();
295  }
296  }
297 }
298 
299 
300 SUMOTime
303 }
304 
305 
306 GUILane*
308  if (makeCurrent()) {
309  int id = getObjectUnderCursor();
310  if (id != 0) {
312  if (o != 0) {
313  return dynamic_cast<GUILane*>(o);
314  }
315  }
316  makeNonCurrent();
317  }
318  return 0;
319 }
320 
321 long
322 GUIViewTraffic::onCmdCloseLane(FXObject*, FXSelector, void*) {
323  GUILane* lane = getLaneUnderCursor();
324  if (lane != 0) {
325  lane->closeTraffic();
327  update();
328  }
329  return 1;
330 }
331 
332 
333 long
334 GUIViewTraffic::onCmdCloseEdge(FXObject*, FXSelector, void*) {
335  GUILane* lane = getLaneUnderCursor();
336  if (lane != 0) {
337  dynamic_cast<GUIEdge*>(&lane->getEdge())->closeTraffic(lane);
339  update();
340  }
341  return 1;
342 }
343 
344 
345 long
346 GUIViewTraffic::onCmdAddRerouter(FXObject*, FXSelector, void*) {
347  GUILane* lane = getLaneUnderCursor();
348  if (lane != 0) {
349  dynamic_cast<GUIEdge*>(&lane->getEdge())->addRerouter();
351  update();
352  }
353  return 1;
354 }
355 
356 
357 void
358 GUIViewTraffic::saveFrame(const std::string& destFile, FXColor* buf) {
359 #ifdef HAVE_FFMPEG
360  if (myCurrentVideo == 0) {
361  myCurrentVideo = new GUIVideoEncoder(destFile.c_str(), getWidth(), getHeight(), myApp->getDelay());
362  }
363  myCurrentVideo->writeFrame((uint8_t*)buf);
364 #else
365  UNUSED_PARAMETER(destFile);
366  UNUSED_PARAMETER(buf);
367 #endif
368 }
369 
370 
371 void
373 #ifdef HAVE_FFMPEG
374  if (myCurrentVideo != 0) {
375  delete myCurrentVideo;
376  myCurrentVideo = 0;
377  }
378 #endif
379 }
380 
381 
382 void
385 #ifdef HAVE_FFMPEG
386  if (myCurrentVideo != 0) {
387  std::string error = makeSnapshot("");
388  if (error != "" && error != "video") {
389  WRITE_WARNING(error);
390  }
391  }
392 #endif
393 }
394 
395 
396 /****************************************************************************/
void paintGLGrid()
paints a grid
bool isActive(const MSTrafficLightLogic *tl) const
Returns whether the given tls program is the currently active for his tls.
virtual void buildViewToolBars(GUIGlChildWindow &)
builds the view toolbars
MSEdge & getEdge() const
Returns the lane's edge.
Definition: MSLane.h:571
Locate vehicle - button.
Definition: GUIAppEnum.h:175
GUICompleteSchemeStorage gSchemeStorage
long onCmdCloseLane(FXObject *, FXSelector, void *)
interaction with the simulation
long long int SUMOTime
Definition: SUMOTime.h:43
Locate person - button.
Definition: GUIAppEnum.h:177
const SUMOReal SUMO_const_laneWidth
Definition: StdDefs.h:49
Storage for all programs of a single tls.
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
virtual SUMOReal getDelay() const
Returns the delay (should be overwritten by subclasses if applicable)
Definition: GUIMainWindow.h:99
bool setColorScheme(const std::string &name)
set color scheme
void unlock()
release exclusive access to the simulation state
Definition: GUINet.cpp:516
SUMORTree * myGrid
The visualization speed-up.
void switchTo(const std::string &id, const std::string &programID)
Switches the named (id) tls to the named (programID) program.
Locate TLS - button.
Definition: GUIAppEnum.h:179
void closeTraffic(bool rebuildAllowed=true)
close this lane for traffic
Definition: GUILane.cpp:1105
bool gaming
whether the application is in gaming mode or not
GUIGlID getTrackedID() const
Returns the id of the tracked vehicle (-1 if none)
SUMOTime getCurrentTimeStep() const
get the current simulation time
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:142
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
GUIMainWindow * myApp
The application.
Locate poi - button.
Definition: GUIAppEnum.h:187
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:130
void writeFrame(uint8_t *buffer)
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
void lock()
grant exclusive access to the simulation state
Definition: GUINet.cpp:510
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
SUMOReal distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:221
GUIDialog_ViewSettings * myVisualizationChanger
Visualization changer.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:254
bool isGaming() const
return whether the gui is in gaming mode
Definition: GUIMainWindow.h:85
GUIGlID getGlID() const
Returns the numerical id of the object.
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:136
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
A fixed traffic light logic.
SUMOReal scale
information about a lane's width (temporary, used for a single view)
std::map< const GUIGlObject *, int > myAdditionallyDrawn
List of objects for which GUIGlObject::drawGLAdditional is called.
Representation of a lane in the micro simulation (gui-version)
Definition: GUILane.h:70
A class that stores and controls tls and switching of their programs.
std::vector< MSTrafficLightLogic * > getAllLogics() const
Locate addtional structure - button.
Definition: GUIAppEnum.h:185
A road/street connecting two junctions (gui-version)
Definition: GUIEdge.h:60
const std::string & getID() const
Returns the id.
Definition: Named.h:66
SUMOTime duration
The duration of the phase.
const std::vector< std::string > & getNames() const
Returns a list of stored settings names.
std::string name
The name of this setting.
void setCurrentScheme(const std::string &)
Sets the named scheme as the current.
static GUINet * getGUIInstance()
Returns the pointer to the unique instance of GUINet (singleton).
Definition: GUINet.cpp:494
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
long onCmdAddRerouter(FXObject *, FXSelector, void *)
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition: MSNet.h:380
Locate edge - button.
Definition: GUIAppEnum.h:173
long onCmdCloseEdge(FXObject *, FXSelector, void *)
FXComboBox & getColoringSchemesCombo()
bool contains(const std::string &name) const
Returns the information whether a setting with the given name is stored.
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
GUIVisualizationSettings & get(const std::string &name)
Returns the named scheme.
void onGamingClick(Position pos)
handle mouse click in gaming mode
void startTrack(int id)
Starts vehicle tracking.
Locate junction - button.
Definition: GUIAppEnum.h:171
std::string getCurrentScheme() const
Returns the name of the currently chosen scheme.
virtual void checkSnapshots()
Checks whether it is time for a snapshot.
FXDEFMAP(GUIViewTraffic) GUIViewTrafficMap[]
bool myUseToolTips
use tool tips
std::string makeSnapshot(const std::string &destFile)
Takes a snapshots and writes it into the given file.
void checkSnapshots()
Checks whether it is time for a snapshot.
A single child window which contains a view of the simulation area.
void endSnapshot()
Ends a video snapshot.
unsigned int GUIGlID
Definition: GUIGlObject.h:50
FXbool makeCurrent()
A reimplementation due to some internal reasons.
std::vector< MSLane * > LaneVector
Definition of the list of links that participate in this tl-light.
GUIVisualizationSettings * myVisualizationSettings
visualization settings
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:89
void saveFrame(const std::string &destFile, FXColor *buf)
Adds a frame to a video snapshot which will be initialized if neccessary.
virtual ~GUIViewTraffic()
destructor
int doPaintGL(int mode, const Boundary &bound)
paint GL
SUMOReal m2p(SUMOReal meter) const
meter-to-pixels conversion method
Locate polygons - button.
Definition: GUIAppEnum.h:189
static const GUIGlID INVALID_ID
Definition: GUIGlObject.h:77
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
bool showGrid
Information whether a grid shall be shown.
void drawDecals()
Draws the stored decals.
virtual int Search(const float a_min[2], const float a_max[2], const GUIVisualizationSettings &c) const
Find all within search rectangle.
Definition: SUMORTree.h:120
The parent class for traffic light logics.
#define SUMOReal
Definition: config.h:214
const std::string & getProgramID() const
Returns this tl-logic's id.
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:148
void unblockObject(GUIGlID id)
Marks an object as unblocked.
std::vector< MSTrafficLightLogic * > getAllLogics() const
Returns a vector which contains all logics.
GUIGlID getObjectUnderCursor()
returns the id of the object under the cursor using GL_SELECT
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
static bool gUseMesoSim
Definition: MSGlobals.h:95
static FXIcon * getIcon(GUIIcon which)
returns a icon previously defined in the enum GUIIcon
void stopTrack()
Stops vehicle tracking.
GUILane * getLaneUnderCursor()
FXPopup * getLocatorPopup()