SUMO - Simulation of Urban MObility
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  float minB[2];
211  float maxB[2];
212  minB[0] = bound.xmin();
213  minB[1] = bound.ymin();
214  maxB[0] = bound.xmax();
215  maxB[1] = bound.ymax();
217  glEnable(GL_POLYGON_OFFSET_FILL);
218  glEnable(GL_POLYGON_OFFSET_LINE);
219  int hits2 = myGrid->Search(minB, maxB, *myVisualizationSettings);
220  //
221  if (myAdditionallyDrawn.size() > 0) {
222  glTranslated(0, 0, -.01);
224  for (std::map<const GUIGlObject*, int>::iterator i = myAdditionallyDrawn.begin(); i != myAdditionallyDrawn.end(); ++i) {
225  (i->first)->drawGLAdditional(this, *myVisualizationSettings);
226  }
228  glTranslated(0, 0, .01);
229  }
230  glPopMatrix();
231  /*
232  // draw legends
233  glMatrixMode(GL_MODELVIEW);
234  glLoadIdentity();
235  glTranslated(1.-.2, 1.-.5, 0.);
236  glScaled(.2, .5, 1.);
237  GUIColoringSchemesMap<GUILane> &sm = GUIViewTraffic::getLaneSchemesMap(); //!!!
238  sm.getColorer(myVisualizationSettings->laneEdgeMode)->drawLegend();
239  */
240  return hits2;
241 }
242 
243 
244 void
246  myTrackedID = id;
247 }
248 
249 
250 void
253 }
254 
255 
256 GUIGlID
258  return myTrackedID;
259 }
260 
261 
262 void
265  const std::vector<MSTrafficLightLogic*>& logics = tlsControl.getAllLogics();
266  MSTrafficLightLogic* minTll = 0;
267  SUMOReal minDist = std::numeric_limits<SUMOReal>::infinity();
268  for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
269  // get the logic
270  MSTrafficLightLogic* tll = (*i);
271  if (tlsControl.isActive(tll)) {
272  // get the links
273  const MSTrafficLightLogic::LaneVector& lanes = tll->getLanesAt(0);
274  if (lanes.size() > 0) {
275  const Position& endPos = lanes[0]->getShape().back();
276  if (endPos.distanceTo(pos) < minDist) {
277  minDist = endPos.distanceTo(pos);
278  minTll = tll;
279  }
280  }
281  }
282  }
283  if (minTll != 0) {
284  const MSTLLogicControl::TLSLogicVariants& vars = tlsControl.get(minTll->getID());
285  const std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
286  if (logics.size() > 1) {
288  for (int i = 0; i < (int)logics.size() - 1; ++i) {
289  if (minTll->getProgramID() == logics[i]->getProgramID()) {
290  l = (MSSimpleTrafficLightLogic*) logics[i + 1];
291  tlsControl.switchTo(minTll->getID(), l->getProgramID());
292  }
293  }
294  if (l == logics[0]) {
295  tlsControl.switchTo(minTll->getID(), l->getProgramID());
296  }
298  update();
299  }
300  }
301 }
302 
303 
304 SUMOTime
307 }
308 
309 
310 GUILane*
312  if (makeCurrent()) {
313  int id = getObjectUnderCursor();
314  if (id != 0) {
316  if (o != 0) {
317  return dynamic_cast<GUILane*>(o);
318  }
319  }
320  makeNonCurrent();
321  }
322  return 0;
323 }
324 
325 long
326 GUIViewTraffic::onCmdCloseLane(FXObject*, FXSelector, void*) {
327  GUILane* lane = getLaneUnderCursor();
328  if (lane != 0) {
329  lane->closeTraffic();
331  update();
332  }
333  return 1;
334 }
335 
336 
337 long
338 GUIViewTraffic::onCmdCloseEdge(FXObject*, FXSelector, void*) {
339  GUILane* lane = getLaneUnderCursor();
340  if (lane != 0) {
341  dynamic_cast<GUIEdge*>(&lane->getEdge())->closeTraffic(lane);
343  update();
344  }
345  return 1;
346 }
347 
348 
349 long
350 GUIViewTraffic::onCmdAddRerouter(FXObject*, FXSelector, void*) {
351  GUILane* lane = getLaneUnderCursor();
352  if (lane != 0) {
353  dynamic_cast<GUIEdge*>(&lane->getEdge())->addRerouter();
355  update();
356  }
357  return 1;
358 }
359 
360 
361 void
362 GUIViewTraffic::saveFrame(const std::string& destFile, FXColor* buf) {
363 #ifdef HAVE_FFMPEG
364  if (myCurrentVideo == 0) {
365  myCurrentVideo = new GUIVideoEncoder(destFile.c_str(), getWidth(), getHeight(), myApp->getDelay());
366  }
367  myCurrentVideo->writeFrame((uint8_t*)buf);
368 #else
369  UNUSED_PARAMETER(destFile);
370  UNUSED_PARAMETER(buf);
371 #endif
372 }
373 
374 
375 void
377 #ifdef HAVE_FFMPEG
378  if (myCurrentVideo != 0) {
379  delete myCurrentVideo;
380  myCurrentVideo = 0;
381  }
382 #endif
383 }
384 
385 
386 void
389 #ifdef HAVE_FFMPEG
390  if (myCurrentVideo != 0) {
391  std::string error = makeSnapshot("");
392  if (error != "" && error != "video") {
393  WRITE_WARNING(error);
394  }
395  }
396 #endif
397 }
398 
399 
400 /****************************************************************************/
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&#39;s edge.
Definition: MSLane.h:567
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:524
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:1098
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:124
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:112
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:518
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:118
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&#39;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:502
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:213
const std::string & getProgramID() const
Returns this tl-logic&#39;s id.
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:130
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:90
static FXIcon * getIcon(GUIIcon which)
returns a icon previously defined in the enum GUIIcon
void stopTrack()
Stops vehicle tracking.
GUILane * getLaneUnderCursor()
FXPopup * getLocatorPopup()