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-2017 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 "GUISUMOViewParent.h"
56 #include "GUIViewTraffic.h"
69 #include <utils/gui/div/GLHelper.h>
71 
72 /* -------------------------------------------------------------------------
73  * GUIViewTraffic - FOX callback mapping
74  * ----------------------------------------------------------------------- */
75 FXDEFMAP(GUIViewTraffic) GUIViewTrafficMap[] = {
76  FXMAPFUNC(SEL_COMMAND, MID_CLOSE_LANE, GUIViewTraffic::onCmdCloseLane),
77  FXMAPFUNC(SEL_COMMAND, MID_CLOSE_EDGE, GUIViewTraffic::onCmdCloseEdge),
78  FXMAPFUNC(SEL_COMMAND, MID_ADD_REROUTER, GUIViewTraffic::onCmdAddRerouter),
79 };
80 
81 
82 FXIMPLEMENT_ABSTRACT(GUIViewTraffic, GUISUMOAbstractView, GUIViewTrafficMap, ARRAYNUMBER(GUIViewTrafficMap))
83 
84 
85 // ===========================================================================
86 // member method definitions
87 // ===========================================================================
89  FXComposite* p,
90  GUIMainWindow& app,
91  GUISUMOViewParent* parent,
92  GUINet& net, FXGLVisual* glVis,
93  FXGLCanvas* share) :
94  GUISUMOAbstractView(p, app, parent, net.getVisualisationSpeedUp(), glVis, share),
95  myTrackedID(GUIGlObject::INVALID_ID)
96 #ifdef HAVE_FFMPEG
97  , myCurrentVideo(0)
98 #endif
99 {}
100 
101 
103 }
104 
105 
106 void
108  // build coloring tools
109  {
110  const std::vector<std::string>& names = gSchemeStorage.getNames();
111  for (std::vector<std::string>::const_iterator i = names.begin(); i != names.end(); ++i) {
112  v.getColoringSchemesCombo().appendItem((*i).c_str());
113  if ((*i) == myVisualizationSettings->name) {
114  v.getColoringSchemesCombo().setCurrentItem(v.getColoringSchemesCombo().getNumItems() - 1);
115  }
116  }
117  v.getColoringSchemesCombo().setNumVisible(MAX2(5, (int)names.size() + 1));
118  }
119  // for junctions
120  new FXButton(v.getLocatorPopup(),
121  "\tLocate Junction\tLocate a junction within the network.",
123  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
124  // for edges
125  new FXButton(v.getLocatorPopup(),
126  "\tLocate Street\tLocate a street within the network.",
128  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
129 
130  // for vehicles
131  new FXButton(v.getLocatorPopup(),
132  "\tLocate Vehicle\tLocate a vehicle within the network.",
134  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
135 
136  // for persons
137  if (!MSGlobals::gUseMesoSim) { // there are no persons in mesosim (yet)
138  new FXButton(v.getLocatorPopup(),
139  "\tLocate Vehicle\tLocate a person within the network.",
141  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
142  }
143 
144  // for tls
145  new FXButton(v.getLocatorPopup(),
146  "\tLocate TLS\tLocate a tls within the network.",
148  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
149  // for additional stuff
150  new FXButton(v.getLocatorPopup(),
151  "\tLocate Additional\tLocate an additional structure within the network.",
153  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
154  // for pois
155  new FXButton(v.getLocatorPopup(),
156  "\tLocate PoI\tLocate a PoI within the network.",
158  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
159  // for polygons
160  new FXButton(v.getLocatorPopup(),
161  "\tLocate Polygon\tLocate a Polygon within the network.",
163  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
164 }
165 
166 
167 bool
168 GUIViewTraffic::setColorScheme(const std::string& name) {
169  if (!gSchemeStorage.contains(name)) {
170  return false;
171  }
172  if (myVisualizationChanger != 0) {
173  if (myVisualizationChanger->getCurrentScheme() != name) {
175  }
176  }
177  myVisualizationSettings = &gSchemeStorage.get(name.c_str());
179  update();
180  return true;
181 }
182 
183 
184 void
186  if (objectType == GLO_LANE) {
187  assert(!scheme.isFixed());
188  // retrieve range
189  double minValue = std::numeric_limits<double>::infinity();
190  double maxValue = -std::numeric_limits<double>::infinity();
191  // XXX (see #3409) multi-colors are not currently handled. this is a quick hack
192  if (active == 22) {
193  active = 21; // segment height, fall back to start height
194  }
195  const MSEdgeVector& edges = MSEdge::getAllEdges();
196  for (MSEdgeVector::const_iterator it = edges.begin(); it != edges.end(); ++it) {
198  const double val = static_cast<GUIEdge*>(*it)->getColorValue(active);
199  minValue = MIN2(minValue, val);
200  maxValue = MAX2(maxValue, val);
201  } else {
202  const std::vector<MSLane*>& lanes = (*it)->getLanes();
203  for (std::vector<MSLane*>::const_iterator it_l = lanes.begin(); it_l != lanes.end(); it_l++) {
204  const double val = static_cast<GUILane*>(*it_l)->getColorValue(active);
205  minValue = MIN2(minValue, val);
206  maxValue = MAX2(maxValue, val);
207  }
208  }
209  }
210  scheme.clear();
211  // add new thresholds
212  double range = maxValue - minValue;
213  scheme.addColor(RGBColor::RED, (minValue));
214  scheme.addColor(RGBColor::ORANGE, (minValue + range * 1 / 6.0));
215  scheme.addColor(RGBColor::YELLOW, (minValue + range * 2 / 6.0));
216  scheme.addColor(RGBColor::GREEN, (minValue + range * 3 / 6.0));
217  scheme.addColor(RGBColor::CYAN, (minValue + range * 4 / 6.0));
218  scheme.addColor(RGBColor::BLUE, (minValue + range * 5 / 6.0));
219  scheme.addColor(RGBColor::MAGENTA, (maxValue));
220  }
221 }
222 
223 
224 int
225 GUIViewTraffic::doPaintGL(int mode, const Boundary& bound) {
226  // init view settings
227  glRenderMode(mode);
228  glMatrixMode(GL_MODELVIEW);
229  glPushMatrix();
230  glDisable(GL_TEXTURE_2D);
231  glDisable(GL_ALPHA_TEST);
232  glDisable(GL_BLEND);
233  glEnable(GL_DEPTH_TEST);
234 
235  // draw decals (if not in grabbing mode)
236  if (!myUseToolTips) {
237  drawDecals();
239  paintGLGrid();
240  }
241  }
242 
243  glLineWidth(1);
244  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
245  const float minB[2] = { (float)bound.xmin(), (float)bound.ymin() };
246  const float maxB[2] = { (float)bound.xmax(), (float)bound.ymax() };
248  glEnable(GL_POLYGON_OFFSET_FILL);
249  glEnable(GL_POLYGON_OFFSET_LINE);
250  int hits2 = myGrid->Search(minB, maxB, *myVisualizationSettings);
251  //
252  if (myAdditionallyDrawn.size() > 0) {
253  glTranslated(0, 0, -.01);
255  for (std::map<const GUIGlObject*, int>::iterator i = myAdditionallyDrawn.begin(); i != myAdditionallyDrawn.end(); ++i) {
256  (i->first)->drawGLAdditional(this, *myVisualizationSettings);
257  }
259  glTranslated(0, 0, .01);
260  }
261  glPopMatrix();
262  /*
263  // draw legends
264  glMatrixMode(GL_MODELVIEW);
265  glLoadIdentity();
266  glTranslated(1.-.2, 1.-.5, 0.);
267  glScaled(.2, .5, 1.);
268  GUIColoringSchemesMap<GUILane> &sm = GUIViewTraffic::getLaneSchemesMap(); //!!!
269  sm.getColorer(myVisualizationSettings->laneEdgeMode)->drawLegend();
270  */
271  return hits2;
272 }
273 
274 
275 void
277  myTrackedID = id;
278 }
279 
280 
281 void
284 }
285 
286 
287 GUIGlID
289  return myTrackedID;
290 }
291 
292 
293 void
296  const std::vector<MSTrafficLightLogic*>& logics = tlsControl.getAllLogics();
297  MSTrafficLightLogic* minTll = 0;
298  double minDist = std::numeric_limits<double>::infinity();
299  for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
300  // get the logic
301  MSTrafficLightLogic* tll = (*i);
302  if (tlsControl.isActive(tll)) {
303  // get the links
304  const MSTrafficLightLogic::LaneVector& lanes = tll->getLanesAt(0);
305  if (lanes.size() > 0) {
306  const Position& endPos = lanes[0]->getShape().back();
307  if (endPos.distanceTo(pos) < minDist) {
308  minDist = endPos.distanceTo(pos);
309  minTll = tll;
310  }
311  }
312  }
313  }
314  if (minTll != 0) {
315  const MSTLLogicControl::TLSLogicVariants& vars = tlsControl.get(minTll->getID());
316  const std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
317  if (logics.size() > 1) {
319  for (int i = 0; i < (int)logics.size() - 1; ++i) {
320  if (minTll->getProgramID() == logics[i]->getProgramID()) {
321  l = (MSSimpleTrafficLightLogic*) logics[i + 1];
322  tlsControl.switchTo(minTll->getID(), l->getProgramID());
323  }
324  }
325  if (l == logics[0]) {
326  tlsControl.switchTo(minTll->getID(), l->getProgramID());
327  }
329  update();
330  }
331  }
332 }
333 
334 
335 SUMOTime
338 }
339 
340 
341 GUILane*
343  if (makeCurrent()) {
344  int id = getObjectUnderCursor();
345  if (id != 0) {
347  if (o != 0) {
348  return dynamic_cast<GUILane*>(o);
349  }
350  }
351  makeNonCurrent();
352  }
353  return 0;
354 }
355 
356 long
357 GUIViewTraffic::onCmdCloseLane(FXObject*, FXSelector, void*) {
358  GUILane* lane = getLaneUnderCursor();
359  if (lane != 0) {
360  lane->closeTraffic();
362  update();
363  }
364  return 1;
365 }
366 
367 
368 long
369 GUIViewTraffic::onCmdCloseEdge(FXObject*, FXSelector, void*) {
370  GUILane* lane = getLaneUnderCursor();
371  if (lane != 0) {
372  dynamic_cast<GUIEdge*>(&lane->getEdge())->closeTraffic(lane);
374  update();
375  }
376  return 1;
377 }
378 
379 
380 long
381 GUIViewTraffic::onCmdAddRerouter(FXObject*, FXSelector, void*) {
382  GUILane* lane = getLaneUnderCursor();
383  if (lane != 0) {
384  dynamic_cast<GUIEdge*>(&lane->getEdge())->addRerouter();
386  update();
387  }
388  return 1;
389 }
390 
391 
392 void
393 GUIViewTraffic::saveFrame(const std::string& destFile, FXColor* buf) {
394 #ifdef HAVE_FFMPEG
395  if (myCurrentVideo == 0) {
396  myCurrentVideo = new GUIVideoEncoder(destFile.c_str(), getWidth(), getHeight(), myApp->getDelay());
397  }
398  myCurrentVideo->writeFrame((uint8_t*)buf);
399 #else
400  UNUSED_PARAMETER(destFile);
401  UNUSED_PARAMETER(buf);
402 #endif
403 }
404 
405 
406 void
408 #ifdef HAVE_FFMPEG
409  if (myCurrentVideo != 0) {
410  delete myCurrentVideo;
411  myCurrentVideo = 0;
412  }
413 #endif
414 }
415 
416 
417 void
420 #ifdef HAVE_FFMPEG
421  if (myCurrentVideo != 0) {
422  std::string error = makeSnapshot("");
423  if (error != "" && error != "video") {
424  WRITE_WARNING(error);
425  }
426  }
427 #endif
428 }
429 
430 
431 /****************************************************************************/
void paintGLGrid()
paints a grid
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:132
bool isActive(const MSTrafficLightLogic *tl) const
Returns whether the given tls program is the currently active for his tls.
static const RGBColor BLUE
Definition: RGBColor.h:181
virtual void buildViewToolBars(GUIGlChildWindow &)
builds the view toolbars
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:582
double getColorValue(int activeScheme) const
gets the color value according to the current scheme index
Definition: GUILane.cpp:928
Locate vehicle - button.
Definition: GUIAppEnum.h:177
GUICompleteSchemeStorage gSchemeStorage
long onCmdCloseLane(FXObject *, FXSelector, void *)
interaction with the simulation
double scale
information about a lane&#39;s width (temporary, used for a single view)
Locate person - button.
Definition: GUIAppEnum.h:179
virtual double getDelay() const
Returns the delay (should be overwritten by subclasses if applicable)
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.
bool setColorScheme(const std::string &name)
set color scheme
GUIGlObjectType
void unlock()
release exclusive access to the simulation state
Definition: GUINet.cpp:526
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:181
void closeTraffic(bool rebuildAllowed=true)
close this lane for traffic
Definition: GUILane.cpp:1128
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
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:144
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
const double SUMO_const_laneWidth
Definition: StdDefs.h:50
GUIMainWindow * myApp
The application.
Locate poi - button.
Definition: GUIAppEnum.h:189
void writeFrame(uint8_t *buffer)
static const RGBColor ORANGE
Definition: RGBColor.h:185
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:169
T MAX2(T a, T b)
Definition: StdDefs.h:74
void lock()
grant exclusive access to the simulation state
Definition: GUINet.cpp:520
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
unsigned char uint8_t
Definition: stdint.h:78
GUIDialog_ViewSettings * myVisualizationChanger
Visualization changer.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:252
bool isGaming() const
return whether the gui is in gaming mode
Definition: GUIMainWindow.h:87
GUIGlID getGlID() const
Returns the numerical id of the object.
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:40
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.
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.
bool isFixed() const
std::vector< MSTrafficLightLogic * > getAllLogics() const
Locate addtional structure - button.
Definition: GUIAppEnum.h:187
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.
static const RGBColor GREEN
Definition: RGBColor.h:180
int addColor(const T &color, const double threshold, const std::string &name="")
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:504
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
double m2p(double meter) const
meter-to-pixels conversion method
long onCmdAddRerouter(FXObject *, FXSelector, void *)
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition: MSNet.h:378
Locate edge - button.
Definition: GUIAppEnum.h:175
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.
static const RGBColor MAGENTA
Definition: RGBColor.h:184
T MIN2(T a, T b)
Definition: StdDefs.h:68
void onGamingClick(Position pos)
handle mouse click in gaming mode
void startTrack(int id)
Starts vehicle tracking.
Locate junction - button.
Definition: GUIAppEnum.h:173
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.
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:240
void buildColorRainbow(GUIColorScheme &scheme, int active, GUIGlObjectType objectType)
recalibrate color scheme according to the current value range
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.
static const RGBColor YELLOW
Definition: RGBColor.h:182
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
static const RGBColor RED
named colors
Definition: RGBColor.h:179
static const RGBColor CYAN
Definition: RGBColor.h:183
GUIVisualizationSettings * myVisualizationSettings
visualization settings
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:89
double getColorValue(int activeScheme) const
gets the color value according to the current scheme index
Definition: GUIEdge.cpp:467
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
static const MSEdgeVector & getAllEdges()
Returns all edges with a numerical id.
Definition: MSEdge.cpp:778
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:126
Locate polygons - button.
Definition: GUIAppEnum.h:191
static const GUIGlID INVALID_ID
Definition: GUIGlObject.h:77
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:138
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.
const std::string & getProgramID() const
Returns this tl-logic&#39;s id.
long long int SUMOTime
Definition: TraCIDefs.h:52
void unblockObject(GUIGlID id)
Marks an object as unblocked.
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:78
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:98
static FXIcon * getIcon(GUIIcon which)
returns a icon previously defined in the enum GUIIcon
void stopTrack()
Stops vehicle tracking.
GUILane * getLaneUnderCursor()
FXPopup * getLocatorPopup()