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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
21 // A view on the simulation; this view is a microscopic one
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #ifdef HAVE_FFMPEG
36 #endif
37 
38 #include <iostream>
39 #include <utility>
40 #include <cmath>
41 #include <limits>
42 #include <guisim/GUINet.h>
43 #include <guisim/GUIEdge.h>
44 #include <guisim/GUILane.h>
45 #include <guisim/GUIVehicle.h>
46 #include <microsim/MSGlobals.h>
47 #include <microsim/MSEdge.h>
48 #include <microsim/MSLane.h>
52 #include <utils/common/RGBColor.h>
54 #include "GUISUMOViewParent.h"
55 #include "GUIViewTraffic.h"
67 #include <utils/gui/div/GLHelper.h>
69 
70 /* -------------------------------------------------------------------------
71  * GUIViewTraffic - FOX callback mapping
72  * ----------------------------------------------------------------------- */
73 FXDEFMAP(GUIViewTraffic) GUIViewTrafficMap[] = {
74  FXMAPFUNC(SEL_COMMAND, MID_CLOSE_LANE, GUIViewTraffic::onCmdCloseLane),
75  FXMAPFUNC(SEL_COMMAND, MID_CLOSE_EDGE, GUIViewTraffic::onCmdCloseEdge),
76  FXMAPFUNC(SEL_COMMAND, MID_ADD_REROUTER, GUIViewTraffic::onCmdAddRerouter),
77 };
78 
79 
80 FXIMPLEMENT_ABSTRACT(GUIViewTraffic, GUISUMOAbstractView, GUIViewTrafficMap, ARRAYNUMBER(GUIViewTrafficMap))
81 
82 
83 // ===========================================================================
84 // member method definitions
85 // ===========================================================================
87  FXComposite* p,
88  GUIMainWindow& app,
89  GUISUMOViewParent* parent,
90  GUINet& net, FXGLVisual* glVis,
91  FXGLCanvas* share) :
92  GUISUMOAbstractView(p, app, parent, net.getVisualisationSpeedUp(), glVis, share),
93  myTrackedID(GUIGlObject::INVALID_ID)
94 #ifdef HAVE_FFMPEG
95  , myCurrentVideo(0)
96 #endif
97 {}
98 
99 
101 }
102 
103 
104 void
106  // build coloring tools
107  {
108  const std::vector<std::string>& names = gSchemeStorage.getNames();
109  for (std::vector<std::string>::const_iterator i = names.begin(); i != names.end(); ++i) {
110  v.getColoringSchemesCombo().appendItem((*i).c_str());
111  if ((*i) == myVisualizationSettings->name) {
112  v.getColoringSchemesCombo().setCurrentItem(v.getColoringSchemesCombo().getNumItems() - 1);
113  }
114  }
115  v.getColoringSchemesCombo().setNumVisible(MAX2(5, (int)names.size() + 1));
116  }
117  // for junctions
118  new FXButton(v.getLocatorPopup(),
119  "\tLocate Junction\tLocate a junction within the network.",
121  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
122  // for edges
123  new FXButton(v.getLocatorPopup(),
124  "\tLocate Street\tLocate a street within the network.",
126  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
127 
128  // for vehicles
129  new FXButton(v.getLocatorPopup(),
130  "\tLocate Vehicle\tLocate a vehicle within the network.",
132  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
133 
134  // for persons
135  if (!MSGlobals::gUseMesoSim) { // there are no persons in mesosim (yet)
136  new FXButton(v.getLocatorPopup(),
137  "\tLocate Vehicle\tLocate a person within the network.",
139  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
140  }
141 
142  // for tls
143  new FXButton(v.getLocatorPopup(),
144  "\tLocate TLS\tLocate a tls within the network.",
146  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
147  // for additional stuff
148  new FXButton(v.getLocatorPopup(),
149  "\tLocate Additional\tLocate an additional structure within the network.",
151  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
152  // for pois
153  new FXButton(v.getLocatorPopup(),
154  "\tLocate PoI\tLocate a PoI within the network.",
156  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
157  // for polygons
158  new FXButton(v.getLocatorPopup(),
159  "\tLocate Polygon\tLocate a Polygon within the network.",
161  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
162 }
163 
164 
165 bool
166 GUIViewTraffic::setColorScheme(const std::string& name) {
167  if (!gSchemeStorage.contains(name)) {
168  return false;
169  }
170  if (myVisualizationChanger != 0) {
171  if (myVisualizationChanger->getCurrentScheme() != name) {
173  }
174  }
175  myVisualizationSettings = &gSchemeStorage.get(name.c_str());
177  update();
178  return true;
179 }
180 
181 
182 void
184  assert(!scheme.isFixed());
185  double minValue = std::numeric_limits<double>::infinity();
186  double maxValue = -std::numeric_limits<double>::infinity();
187  // retrieve range
188  if (objectType == GLO_LANE) {
189  // XXX (see #3409) multi-colors are not currently handled. this is a quick hack
190  if (active == 22) {
191  active = 21; // segment height, fall back to start height
192  } else if (active == 24) {
193  active = 23; // segment incline, fall back to total incline
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  } else if (objectType == GLO_JUNCTION) {
211  if (active == 3) {
212  std::set<const MSJunction*> junctions;
213  for (MSEdge* edge : MSEdge::getAllEdges()) {
214  junctions.insert(edge->getFromJunction());
215  junctions.insert(edge->getToJunction());
216  }
217  for (const MSJunction* junction : junctions) {
218  minValue = MIN2(minValue, junction->getPosition().z());
219  maxValue = MAX2(maxValue, junction->getPosition().z());
220  }
221  }
222  }
223  if (minValue != std::numeric_limits<double>::infinity()) {
224  scheme.clear();
225  // add new thresholds
226  double range = maxValue - minValue;
227  scheme.addColor(RGBColor::RED, (minValue));
228  scheme.addColor(RGBColor::ORANGE, (minValue + range * 1 / 6.0));
229  scheme.addColor(RGBColor::YELLOW, (minValue + range * 2 / 6.0));
230  scheme.addColor(RGBColor::GREEN, (minValue + range * 3 / 6.0));
231  scheme.addColor(RGBColor::CYAN, (minValue + range * 4 / 6.0));
232  scheme.addColor(RGBColor::BLUE, (minValue + range * 5 / 6.0));
233  scheme.addColor(RGBColor::MAGENTA, (maxValue));
234  }
235 }
236 
237 
238 int
239 GUIViewTraffic::doPaintGL(int mode, const Boundary& bound) {
240  // init view settings
241  glRenderMode(mode);
242  glMatrixMode(GL_MODELVIEW);
243  glPushMatrix();
244  glDisable(GL_TEXTURE_2D);
245  glDisable(GL_ALPHA_TEST);
246  glEnable(GL_BLEND);
247  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
248  glEnable(GL_DEPTH_TEST);
249 
250  // draw decals (if not in grabbing mode)
251  if (!myUseToolTips) {
252  drawDecals();
254  paintGLGrid();
255  }
256  }
257 
258  glLineWidth(1);
259  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
260  const float minB[2] = { (float)bound.xmin(), (float)bound.ymin() };
261  const float maxB[2] = { (float)bound.xmax(), (float)bound.ymax() };
263  glEnable(GL_POLYGON_OFFSET_FILL);
264  glEnable(GL_POLYGON_OFFSET_LINE);
265  int hits2 = myGrid->Search(minB, maxB, *myVisualizationSettings);
266  //
267  if (myAdditionallyDrawn.size() > 0) {
268  glTranslated(0, 0, -.01);
270  for (std::map<const GUIGlObject*, int>::iterator i = myAdditionallyDrawn.begin(); i != myAdditionallyDrawn.end(); ++i) {
271  (i->first)->drawGLAdditional(this, *myVisualizationSettings);
272  }
274  glTranslated(0, 0, .01);
275  }
276  glPopMatrix();
277  /*
278  // draw legends
279  glMatrixMode(GL_MODELVIEW);
280  glLoadIdentity();
281  glTranslated(1.-.2, 1.-.5, 0.);
282  glScaled(.2, .5, 1.);
283  GUIColoringSchemesMap<GUILane> &sm = GUIViewTraffic::getLaneSchemesMap(); //!!!
284  sm.getColorer(myVisualizationSettings->laneEdgeMode)->drawLegend();
285  */
286  return hits2;
287 }
288 
289 
290 void
292  myTrackedID = id;
293 }
294 
295 
296 void
299 }
300 
301 
302 GUIGlID
304  return myTrackedID;
305 }
306 
307 
308 void
311  const std::vector<MSTrafficLightLogic*>& logics = tlsControl.getAllLogics();
312  MSTrafficLightLogic* minTll = 0;
313  double minDist = std::numeric_limits<double>::infinity();
314  for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
315  // get the logic
316  MSTrafficLightLogic* tll = (*i);
317  if (tlsControl.isActive(tll)) {
318  // get the links
319  const MSTrafficLightLogic::LaneVector& lanes = tll->getLanesAt(0);
320  if (lanes.size() > 0) {
321  const Position& endPos = lanes[0]->getShape().back();
322  if (endPos.distanceTo(pos) < minDist) {
323  minDist = endPos.distanceTo(pos);
324  minTll = tll;
325  }
326  }
327  }
328  }
329  if (minTll != 0) {
330  const MSTLLogicControl::TLSLogicVariants& vars = tlsControl.get(minTll->getID());
331  const std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
332  if (logics.size() > 1) {
334  for (int i = 0; i < (int)logics.size() - 1; ++i) {
335  if (minTll->getProgramID() == logics[i]->getProgramID()) {
336  l = (MSSimpleTrafficLightLogic*) logics[i + 1];
337  tlsControl.switchTo(minTll->getID(), l->getProgramID());
338  }
339  }
340  if (l == logics[0]) {
341  tlsControl.switchTo(minTll->getID(), l->getProgramID());
342  }
344  update();
345  }
346  }
347 }
348 
349 
350 SUMOTime
353 }
354 
355 
356 GUILane*
358  if (makeCurrent()) {
359  int id = getObjectUnderCursor();
360  if (id != 0) {
362  if (o != 0) {
363  return dynamic_cast<GUILane*>(o);
364  }
365  }
366  makeNonCurrent();
367  }
368  return 0;
369 }
370 
371 long
372 GUIViewTraffic::onCmdCloseLane(FXObject*, FXSelector, void*) {
373  GUILane* lane = getLaneUnderCursor();
374  if (lane != 0) {
375  lane->closeTraffic();
377  update();
378  }
379  return 1;
380 }
381 
382 
383 long
384 GUIViewTraffic::onCmdCloseEdge(FXObject*, FXSelector, void*) {
385  GUILane* lane = getLaneUnderCursor();
386  if (lane != 0) {
387  dynamic_cast<GUIEdge*>(&lane->getEdge())->closeTraffic(lane);
389  update();
390  }
391  return 1;
392 }
393 
394 
395 long
396 GUIViewTraffic::onCmdAddRerouter(FXObject*, FXSelector, void*) {
397  GUILane* lane = getLaneUnderCursor();
398  if (lane != 0) {
399  dynamic_cast<GUIEdge*>(&lane->getEdge())->addRerouter();
401  update();
402  }
403  return 1;
404 }
405 
406 
407 void
408 GUIViewTraffic::saveFrame(const std::string& destFile, FXColor* buf) {
409 #ifdef HAVE_FFMPEG
410  if (myCurrentVideo == 0) {
411  myCurrentVideo = new GUIVideoEncoder(destFile.c_str(), getWidth(), getHeight(), myApp->getDelay());
412  }
413  myCurrentVideo->writeFrame((uint8_t*)buf);
414 #else
415  UNUSED_PARAMETER(destFile);
416  UNUSED_PARAMETER(buf);
417 #endif
418 }
419 
420 
421 void
423 #ifdef HAVE_FFMPEG
424  if (myCurrentVideo != 0) {
425  delete myCurrentVideo;
426  myCurrentVideo = 0;
427  }
428 #endif
429 }
430 
431 
432 void
435 #ifdef HAVE_FFMPEG
436  if (myCurrentVideo != 0) {
437  std::string error = makeSnapshot("");
438  if (error != "" && error != "video") {
439  WRITE_WARNING(error);
440  }
441  }
442 #endif
443 }
444 
445 
446 /****************************************************************************/
void paintGLGrid()
paints a grid
static const RGBColor BLUE
Definition: RGBColor.h:180
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:137
Locate poi - button.
Definition: GUIAppEnum.h:184
virtual void buildViewToolBars(GUIGlChildWindow &)
builds the view toolbars
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:131
GUICompleteSchemeStorage gSchemeStorage
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:607
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:178
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:528
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:1140
bool gaming
whether the application is in gaming mode or not
The base class for an intersection.
Definition: MSJunction.h:64
Locate junction - button.
Definition: GUIAppEnum.h:172
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:49
GUIMainWindow * myApp
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:184
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
T MAX2(T a, T b)
Definition: StdDefs.h:73
void lock()
grant exclusive access to the simulation state
Definition: GUINet.cpp:522
const std::string & getID() const
Returns the id.
Definition: Named.h:65
GUIDialog_ViewSettings * myVisualizationChanger
Visualization changer.
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:47
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
double getColorValue(int activeScheme) const
gets the color value according to the current scheme index
Definition: GUILane.cpp:942
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:69
A class that stores and controls tls and switching of their programs.
A road/street connecting two junctions (gui-version)
Definition: GUIEdge.h:59
A road/street connecting two junctions.
Definition: MSEdge.h:80
double getColorValue(int activeScheme) const
gets the color value according to the current scheme index
Definition: GUIEdge.cpp:465
SUMOTime duration
The duration of the phase.
std::vector< MSTrafficLightLogic * > getAllLogics() const
static const RGBColor GREEN
Definition: RGBColor.h:179
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:506
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
long onCmdAddRerouter(FXObject *, FXSelector, void *)
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition: MSNet.h:379
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.
bool isGaming() const
return whether the gui is in gaming mode
Definition: GUIMainWindow.h:86
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
static const RGBColor MAGENTA
Definition: RGBColor.h:183
T MIN2(T a, T b)
Definition: StdDefs.h:67
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:125
void onGamingClick(Position pos)
handle mouse click in gaming mode
void startTrack(int id)
Starts vehicle tracking.
virtual void checkSnapshots()
Checks whether it is time for a snapshot.
FXDEFMAP(GUIViewTraffic) GUIViewTrafficMap[]
Locate polygons - button.
Definition: GUIAppEnum.h:186
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.
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:49
FXbool makeCurrent()
A reimplementation due to some internal reasons.
static const RGBColor YELLOW
Definition: RGBColor.h:181
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
static const RGBColor RED
named colors
Definition: RGBColor.h:178
double m2p(double meter) const
meter-to-pixels conversion method
static const RGBColor CYAN
Definition: RGBColor.h:182
GUIVisualizationSettings * myVisualizationSettings
visualization settings
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:88
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:777
bool contains(const std::string &name) const
Returns the information whether a setting with the given name is stored.
Locate vehicle - button.
Definition: GUIAppEnum.h:176
Locate addtional structure - button.
Definition: GUIAppEnum.h:182
#define INVALID_ID
static const GUIGlID INVALID_ID
Definition: GUIGlObject.h:76
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:239
Locate edge - button.
Definition: GUIAppEnum.h:174
long long int SUMOTime
Definition: TraCIDefs.h:51
Locate TLS - button.
Definition: GUIAppEnum.h:180
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:77
GUIGlID getObjectUnderCursor()
returns the id of the object under the cursor using GL_SELECT
GUIGlID getTrackedID() const
Returns the id of the tracked vehicle (-1 if none)
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:143
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:119
static bool gUseMesoSim
Definition: MSGlobals.h:97
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