SUMO - Simulation of Urban MObility
GUIViewTraffic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
19 // A view on the simulation; this view is a microscopic one
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #ifdef HAVE_FFMPEG
30 #endif
31 
32 #include <iostream>
33 #include <utility>
34 #include <cmath>
35 #include <limits>
36 #include <guisim/GUINet.h>
37 #include <guisim/GUIEdge.h>
38 #include <guisim/GUILane.h>
39 #include <guisim/GUIVehicle.h>
40 #include <microsim/MSGlobals.h>
41 #include <microsim/MSEdge.h>
42 #include <microsim/MSLane.h>
46 #include <utils/common/RGBColor.h>
48 #include "GUISUMOViewParent.h"
49 #include "GUIViewTraffic.h"
61 #include <utils/gui/div/GLHelper.h>
63 
64 /* -------------------------------------------------------------------------
65  * GUIViewTraffic - FOX callback mapping
66  * ----------------------------------------------------------------------- */
67 FXDEFMAP(GUIViewTraffic) GUIViewTrafficMap[] = {
68  FXMAPFUNC(SEL_COMMAND, MID_CLOSE_LANE, GUIViewTraffic::onCmdCloseLane),
69  FXMAPFUNC(SEL_COMMAND, MID_CLOSE_EDGE, GUIViewTraffic::onCmdCloseEdge),
70  FXMAPFUNC(SEL_COMMAND, MID_ADD_REROUTER, GUIViewTraffic::onCmdAddRerouter),
71 };
72 
73 
74 FXIMPLEMENT_ABSTRACT(GUIViewTraffic, GUISUMOAbstractView, GUIViewTrafficMap, ARRAYNUMBER(GUIViewTrafficMap))
75 
76 
77 // ===========================================================================
78 // member method definitions
79 // ===========================================================================
81  FXComposite* p,
82  GUIMainWindow& app,
83  GUISUMOViewParent* parent,
84  GUINet& net, FXGLVisual* glVis,
85  FXGLCanvas* share) :
86  GUISUMOAbstractView(p, app, parent, net.getVisualisationSpeedUp(), glVis, share),
87  myTrackedID(GUIGlObject::INVALID_ID)
88 #ifdef HAVE_FFMPEG
89  , myCurrentVideo(0)
90 #endif
91 {}
92 
93 
95 }
96 
97 
98 void
100  // build coloring tools
101  {
102  const std::vector<std::string>& names = gSchemeStorage.getNames();
103  for (std::vector<std::string>::const_iterator i = names.begin(); i != names.end(); ++i) {
104  v.getColoringSchemesCombo().appendItem((*i).c_str());
105  if ((*i) == myVisualizationSettings->name) {
106  v.getColoringSchemesCombo().setCurrentItem(v.getColoringSchemesCombo().getNumItems() - 1);
107  }
108  }
109  v.getColoringSchemesCombo().setNumVisible(MAX2(5, (int)names.size() + 1));
110  }
111  // for junctions
112  new FXButton(v.getLocatorPopup(),
113  "\tLocate Junction\tLocate a junction within the network.",
115  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
116  // for edges
117  new FXButton(v.getLocatorPopup(),
118  "\tLocate Street\tLocate a street within the network.",
120  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
121 
122  // for vehicles
123  new FXButton(v.getLocatorPopup(),
124  "\tLocate Vehicle\tLocate a vehicle within the network.",
126  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
127 
128  // for persons
129  if (!MSGlobals::gUseMesoSim) { // there are no persons in mesosim (yet)
130  new FXButton(v.getLocatorPopup(),
131  "\tLocate Vehicle\tLocate a person within the network.",
133  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
134  }
135 
136  // for tls
137  new FXButton(v.getLocatorPopup(),
138  "\tLocate TLS\tLocate a tls within the network.",
140  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
141  // for additional stuff
142  new FXButton(v.getLocatorPopup(),
143  "\tLocate Additional\tLocate an additional structure within the network.",
145  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
146  // for pois
147  new FXButton(v.getLocatorPopup(),
148  "\tLocate PoI\tLocate a PoI within the network.",
150  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
151  // for polygons
152  new FXButton(v.getLocatorPopup(),
153  "\tLocate Polygon\tLocate a Polygon within the network.",
155  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
156 }
157 
158 
159 bool
160 GUIViewTraffic::setColorScheme(const std::string& name) {
161  if (!gSchemeStorage.contains(name)) {
162  return false;
163  }
164  if (myVisualizationChanger != 0) {
165  if (myVisualizationChanger->getCurrentScheme() != name) {
167  }
168  }
169  myVisualizationSettings = &gSchemeStorage.get(name.c_str());
171  update();
172  return true;
173 }
174 
175 
176 void
178  assert(!scheme.isFixed());
179  double minValue = std::numeric_limits<double>::infinity();
180  double maxValue = -std::numeric_limits<double>::infinity();
181  // retrieve range
182  if (objectType == GLO_LANE) {
183  // XXX (see #3409) multi-colors are not currently handled. this is a quick hack
184  if (active == 22) {
185  active = 21; // segment height, fall back to start height
186  } else if (active == 24) {
187  active = 23; // segment incline, fall back to total incline
188  }
189  const MSEdgeVector& edges = MSEdge::getAllEdges();
190  for (MSEdgeVector::const_iterator it = edges.begin(); it != edges.end(); ++it) {
192  const double val = static_cast<GUIEdge*>(*it)->getColorValue(active);
193  minValue = MIN2(minValue, val);
194  maxValue = MAX2(maxValue, val);
195  } else {
196  const std::vector<MSLane*>& lanes = (*it)->getLanes();
197  for (std::vector<MSLane*>::const_iterator it_l = lanes.begin(); it_l != lanes.end(); it_l++) {
198  const double val = static_cast<GUILane*>(*it_l)->getColorValue(active);
199  minValue = MIN2(minValue, val);
200  maxValue = MAX2(maxValue, val);
201  }
202  }
203  }
204  } else if (objectType == GLO_JUNCTION) {
205  if (active == 3) {
206  std::set<const MSJunction*> junctions;
207  for (MSEdge* edge : MSEdge::getAllEdges()) {
208  junctions.insert(edge->getFromJunction());
209  junctions.insert(edge->getToJunction());
210  }
211  for (const MSJunction* junction : junctions) {
212  minValue = MIN2(minValue, junction->getPosition().z());
213  maxValue = MAX2(maxValue, junction->getPosition().z());
214  }
215  }
216  }
217  if (minValue != std::numeric_limits<double>::infinity()) {
218  scheme.clear();
219  // add new thresholds
220  double range = maxValue - minValue;
221  scheme.addColor(RGBColor::RED, (minValue));
222  scheme.addColor(RGBColor::ORANGE, (minValue + range * 1 / 6.0));
223  scheme.addColor(RGBColor::YELLOW, (minValue + range * 2 / 6.0));
224  scheme.addColor(RGBColor::GREEN, (minValue + range * 3 / 6.0));
225  scheme.addColor(RGBColor::CYAN, (minValue + range * 4 / 6.0));
226  scheme.addColor(RGBColor::BLUE, (minValue + range * 5 / 6.0));
227  scheme.addColor(RGBColor::MAGENTA, (maxValue));
228  }
229 }
230 
231 
232 int
233 GUIViewTraffic::doPaintGL(int mode, const Boundary& bound) {
234  // (uncomment the next line to check select mode)
235  //myVisualizationSettings->drawForSelecting = true;
236  // init view settings
237  glRenderMode(mode);
238  glMatrixMode(GL_MODELVIEW);
239  glPushMatrix();
240  glDisable(GL_TEXTURE_2D);
241  glDisable(GL_ALPHA_TEST);
242  glEnable(GL_BLEND);
243  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
244  glEnable(GL_DEPTH_TEST);
245 
246  // draw decals (if not in grabbing mode)
247  if (!myUseToolTips) {
248  drawDecals();
250  paintGLGrid();
251  }
252  }
253 
254  glLineWidth(1);
255  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
256  const float minB[2] = { (float)bound.xmin(), (float)bound.ymin() };
257  const float maxB[2] = { (float)bound.xmax(), (float)bound.ymax() };
259  glEnable(GL_POLYGON_OFFSET_FILL);
260  glEnable(GL_POLYGON_OFFSET_LINE);
261  int hits2 = myGrid->Search(minB, maxB, *myVisualizationSettings);
262  // Draw additional objects
263  if (myAdditionallyDrawn.size() > 0) {
264  glTranslated(0, 0, -.01);
266  for (auto i : myAdditionallyDrawn) {
267  i.first->drawGLAdditional(this, *myVisualizationSettings);
268  }
270  glTranslated(0, 0, .01);
271  }
272  glPopMatrix();
273  /*
274  // draw legends
275  glMatrixMode(GL_MODELVIEW);
276  glLoadIdentity();
277  glTranslated(1.-.2, 1.-.5, 0.);
278  glScaled(.2, .5, 1.);
279  GUIColoringSchemesMap<GUILane> &sm = GUIViewTraffic::getLaneSchemesMap(); //!!!
280  sm.getColorer(myVisualizationSettings->laneEdgeMode)->drawLegend();
281  */
282  return hits2;
283 }
284 
285 
286 void
288  myTrackedID = id;
289 }
290 
291 
292 void
295 }
296 
297 
298 GUIGlID
300  return myTrackedID;
301 }
302 
303 
304 void
307  const std::vector<MSTrafficLightLogic*>& logics = tlsControl.getAllLogics();
308  MSTrafficLightLogic* minTll = 0;
309  double minDist = std::numeric_limits<double>::infinity();
310  for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
311  // get the logic
312  MSTrafficLightLogic* tll = (*i);
313  if (tlsControl.isActive(tll)) {
314  // get the links
315  const MSTrafficLightLogic::LaneVector& lanes = tll->getLanesAt(0);
316  if (lanes.size() > 0) {
317  const Position& endPos = lanes[0]->getShape().back();
318  if (endPos.distanceTo(pos) < minDist) {
319  minDist = endPos.distanceTo(pos);
320  minTll = tll;
321  }
322  }
323  }
324  }
325  if (minTll != 0) {
326  const MSTLLogicControl::TLSLogicVariants& vars = tlsControl.get(minTll->getID());
327  const std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
328  if (logics.size() > 1) {
330  for (int i = 0; i < (int)logics.size() - 1; ++i) {
331  if (minTll->getProgramID() == logics[i]->getProgramID()) {
332  l = (MSSimpleTrafficLightLogic*) logics[i + 1];
333  tlsControl.switchTo(minTll->getID(), l->getProgramID());
334  }
335  }
336  if (l == logics[0]) {
337  tlsControl.switchTo(minTll->getID(), l->getProgramID());
338  }
340  update();
341  }
342  }
343 }
344 
345 
346 SUMOTime
349 }
350 
351 
352 GUILane*
354  if (makeCurrent()) {
355  int id = getObjectUnderCursor();
356  if (id != 0) {
358  if (o != 0) {
359  return dynamic_cast<GUILane*>(o);
360  }
361  }
362  makeNonCurrent();
363  }
364  return 0;
365 }
366 
367 long
368 GUIViewTraffic::onCmdCloseLane(FXObject*, FXSelector, void*) {
369  GUILane* lane = getLaneUnderCursor();
370  if (lane != 0) {
371  lane->closeTraffic();
373  update();
374  }
375  return 1;
376 }
377 
378 
379 long
380 GUIViewTraffic::onCmdCloseEdge(FXObject*, FXSelector, void*) {
381  GUILane* lane = getLaneUnderCursor();
382  if (lane != 0) {
383  dynamic_cast<GUIEdge*>(&lane->getEdge())->closeTraffic(lane);
385  update();
386  }
387  return 1;
388 }
389 
390 
391 long
392 GUIViewTraffic::onCmdAddRerouter(FXObject*, FXSelector, void*) {
393  GUILane* lane = getLaneUnderCursor();
394  if (lane != 0) {
395  dynamic_cast<GUIEdge*>(&lane->getEdge())->addRerouter();
397  update();
398  }
399  return 1;
400 }
401 
402 
403 long
404 GUIViewTraffic::onDoubleClicked(FXObject*, FXSelector, void*) {
405  // leave fullscreen mode
406  if (myApp->isFullScreen()) {
407  myApp->onCmdFullScreen(0, 0, 0);
408  } else {
409  stopTrack();
410  }
411  return 1;
412 }
413 
414 
415 
416 void
417 GUIViewTraffic::saveFrame(const std::string& destFile, FXColor* buf) {
418 #ifdef HAVE_FFMPEG
419  if (myCurrentVideo == 0) {
420  myCurrentVideo = new GUIVideoEncoder(destFile.c_str(), getWidth(), getHeight(), myApp->getDelay());
421  }
422  myCurrentVideo->writeFrame((uint8_t*)buf);
423 #else
424  UNUSED_PARAMETER(destFile);
425  UNUSED_PARAMETER(buf);
426 #endif
427 }
428 
429 
430 void
432 #ifdef HAVE_FFMPEG
433  if (myCurrentVideo != 0) {
434  delete myCurrentVideo;
435  myCurrentVideo = 0;
436  }
437 #endif
438 }
439 
440 
441 void
444 #ifdef HAVE_FFMPEG
445  if (myCurrentVideo != 0) {
446  std::string error = makeSnapshot("");
447  if (error != "" && error != "video") {
448  WRITE_WARNING(error);
449  }
450  }
451 #endif
452 }
453 
454 
455 const std::vector<SUMOTime>
457  return myApp->retrieveBreakpoints();
458 }
459 
460 /****************************************************************************/
Locate junction - button.
Definition: GUIAppEnum.h:172
void paintGLGrid()
paints a grid
static const RGBColor BLUE
Definition: RGBColor.h:174
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:131
virtual void buildViewToolBars(GUIGlChildWindow &)
builds the view toolbars
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:125
GUICompleteSchemeStorage gSchemeStorage
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:617
long onCmdCloseLane(FXObject *, FXSelector, void *)
interaction with the simulation
long long int SUMOTime
Definition: SUMOTime.h:36
double scale
information about a lane&#39;s width (temporary, used for a single view)
Storage for all programs of a single tls.
bool setColorScheme(const std::string &name)
set color scheme
GUIGlObjectType
void unlock()
release exclusive access to the simulation state
Definition: GUINet.cpp:525
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.
const std::vector< std::string > & getNames() const
Returns a list of stored settings names.
void closeTraffic(bool rebuildAllowed=true)
close this lane for traffic
Definition: GUILane.cpp:1178
bool gaming
whether the application is in gaming mode or not
Locate addtional structure - button.
Definition: GUIAppEnum.h:182
The base class for an intersection.
Definition: MSJunction.h:60
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:43
GUIMainWindow * myApp
The application.
virtual long onCmdFullScreen(FXObject *, FXSelector, void *)
Toggle full screen mode.
virtual const std::vector< SUMOTime > retrieveBreakpoints() const
retrieve breakpoints if provided by the application
void writeFrame(uint8_t *buffer)
bool isActive(const MSTrafficLightLogic *tl) const
Returns whether the given tls program is the currently active for his tls.
static const RGBColor ORANGE
Definition: RGBColor.h:178
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:160
T MAX2(T a, T b)
Definition: StdDefs.h:68
void lock()
grant exclusive access to the simulation state
Definition: GUINet.cpp:519
const std::string & getID() const
Returns the id.
Definition: Named.h:69
GUIDialog_ViewSettings * myVisualizationChanger
Visualization changer.
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:33
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:242
double getColorValue(int activeScheme) const
gets the color value according to the current scheme index
Definition: GUILane.cpp:974
A fixed traffic light logic.
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
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:63
A class that stores and controls tls and switching of their programs.
A road/street connecting two junctions (gui-version)
Definition: GUIEdge.h:53
A road/street connecting two junctions.
Definition: MSEdge.h:75
double getColorValue(int activeScheme) const
gets the color value according to the current scheme index
Definition: GUIEdge.cpp:454
SUMOTime duration
The duration of the phase.
std::vector< MSTrafficLightLogic * > getAllLogics() const
static const RGBColor GREEN
Definition: RGBColor.h:173
Locate polygons - button.
Definition: GUIAppEnum.h:186
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.
const std::string & getProgramID() const
Returns this tl-logic&#39;s id.
static GUINet * getGUIInstance()
Returns the pointer to the unique instance of GUINet (singleton).
Definition: GUINet.cpp:503
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
long onCmdAddRerouter(FXObject *, FXSelector, void *)
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition: MSNet.h:382
Locate person - button.
Definition: GUIAppEnum.h:178
std::string makeSnapshot(const std::string &destFile, const int width=-1, const int height=-1)
Takes a snapshots and writes it into the given file.
long onCmdCloseEdge(FXObject *, FXSelector, void *)
FXComboBox & getColoringSchemesCombo()
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
GUIVisualizationSettings & get(const std::string &name)
Returns the named scheme.
Locate vehicle - button.
Definition: GUIAppEnum.h:176
bool isGaming() const
return whether the gui is in gaming mode
Definition: GUIMainWindow.h:80
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:256
static const RGBColor MAGENTA
Definition: RGBColor.h:177
T MIN2(T a, T b)
Definition: StdDefs.h:62
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:119
void onGamingClick(Position pos)
handle mouse click in gaming mode
A simple video encoder from RGBA pics to anything ffmpeg can handle.
void startTrack(int id)
Starts vehicle tracking.
Locate edge - button.
Definition: GUIAppEnum.h:174
virtual void checkSnapshots()
Checks whether it is time for a snapshot.
FXDEFMAP(GUIViewTraffic) GUIViewTrafficMap[]
long onDoubleClicked(FXObject *, FXSelector, void *)
bool isFullScreen()
bool myUseToolTips
use tool tips
void checkSnapshots()
Checks whether it is time for a snapshot.
void buildColorRainbow(GUIColorScheme &scheme, int active, GUIGlObjectType objectType)
recalibrate color scheme according to the current value range
SUMOTime getCurrentTimeStep() const
get the current simulation time
A single child window which contains a view of the simulation area.
void endSnapshot()
Ends a video snapshot.
unsigned int GUIGlID
Definition: GUIGlObject.h:43
FXbool makeCurrent()
A reimplementation due to some internal reasons.
static const RGBColor YELLOW
Definition: RGBColor.h:175
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
static const RGBColor RED
named colors
Definition: RGBColor.h:172
double m2p(double meter) const
meter-to-pixels conversion method
Locate TLS - button.
Definition: GUIAppEnum.h:180
static const RGBColor CYAN
Definition: RGBColor.h:176
GUIVisualizationSettings * myVisualizationSettings
visualization settings
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:82
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:773
bool contains(const std::string &name) const
Returns the information whether a setting with the given name is stored.
#define INVALID_ID
static const GUIGlID INVALID_ID
Definition: GUIGlObject.h:70
const std::vector< SUMOTime > retrieveBreakpoints() const
retrieve breakpoints from the current runThread
std::string getCurrentScheme() const
Returns the name of the currently chosen scheme.
virtual double getDelay() const
Returns the delay (should be overwritten by subclasses if applicable)
bool showGrid
Information whether a grid shall be shown.
void drawDecals()
Draws the stored decals.
GUIGlID getGlID() const
Returns the numerical id of the object.
The parent class for traffic light logics.
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:233
void unblockObject(GUIGlID id)
Marks an object as unblocked.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:71
GUIGlID getObjectUnderCursor()
returns the id of the object under the cursor using GL_SELECT
Locate poi - button.
Definition: GUIAppEnum.h:184
GUIGlID getTrackedID() const
Returns the id of the tracked vehicle (-1 if none)
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:137
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
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:116
static bool gUseMesoSim
Definition: MSGlobals.h:91
std::vector< MSTrafficLightLogic * > getAllLogics() const
Returns a vector which contains all logics.
static FXIcon * getIcon(GUIIcon which)
returns a icon previously defined in the enum GUIIcon
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
void stopTrack()
Stops vehicle tracking.
GUILane * getLaneUnderCursor()
FXPopup * getLocatorPopup()
a junction