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 <utils/shapes/Polygon.h>
56 #include "GUISUMOViewParent.h"
57 #include "GUIViewTraffic.h"
70 #include <utils/gui/div/GLHelper.h>
72 
73 /* -------------------------------------------------------------------------
74  * GUIViewTraffic - FOX callback mapping
75  * ----------------------------------------------------------------------- */
76 FXDEFMAP(GUIViewTraffic) GUIViewTrafficMap[] = {
77  FXMAPFUNC(SEL_COMMAND, MID_CLOSE_LANE, GUIViewTraffic::onCmdCloseLane),
78  FXMAPFUNC(SEL_COMMAND, MID_CLOSE_EDGE, GUIViewTraffic::onCmdCloseEdge),
79  FXMAPFUNC(SEL_COMMAND, MID_ADD_REROUTER, GUIViewTraffic::onCmdAddRerouter),
80 };
81 
82 
83 FXIMPLEMENT_ABSTRACT(GUIViewTraffic, GUISUMOAbstractView, GUIViewTrafficMap, ARRAYNUMBER(GUIViewTrafficMap))
84 
85 
86 // ===========================================================================
87 // member method definitions
88 // ===========================================================================
90  FXComposite* p,
91  GUIMainWindow& app,
92  GUISUMOViewParent* parent,
93  GUINet& net, FXGLVisual* glVis,
94  FXGLCanvas* share) :
95  GUISUMOAbstractView(p, app, parent, net.getVisualisationSpeedUp(), glVis, share),
96  myTrackedID(GUIGlObject::INVALID_ID)
97 #ifdef HAVE_FFMPEG
98  , myCurrentVideo(0)
99 #endif
100 {}
101 
102 
104 }
105 
106 
107 void
109  // build coloring tools
110  {
111  const std::vector<std::string>& names = gSchemeStorage.getNames();
112  for (std::vector<std::string>::const_iterator i = names.begin(); i != names.end(); ++i) {
113  v.getColoringSchemesCombo().appendItem((*i).c_str());
114  if ((*i) == myVisualizationSettings->name) {
115  v.getColoringSchemesCombo().setCurrentItem(v.getColoringSchemesCombo().getNumItems() - 1);
116  }
117  }
118  v.getColoringSchemesCombo().setNumVisible(MAX2(5, (int)names.size() + 1));
119  }
120  // for junctions
121  new FXButton(v.getLocatorPopup(),
122  "\tLocate Junction\tLocate a junction within the network.",
124  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
125  // for edges
126  new FXButton(v.getLocatorPopup(),
127  "\tLocate Street\tLocate a street within the network.",
129  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
130 
131  // for vehicles
132  new FXButton(v.getLocatorPopup(),
133  "\tLocate Vehicle\tLocate a vehicle within the network.",
135  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
136 
137  // for persons
138  if (!MSGlobals::gUseMesoSim) { // there are no persons in mesosim (yet)
139  new FXButton(v.getLocatorPopup(),
140  "\tLocate Vehicle\tLocate a person within the network.",
142  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
143  }
144 
145  // for tls
146  new FXButton(v.getLocatorPopup(),
147  "\tLocate TLS\tLocate a tls within the network.",
149  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
150  // for additional stuff
151  new FXButton(v.getLocatorPopup(),
152  "\tLocate Additional\tLocate an additional structure within the network.",
154  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
155  // for pois
156  new FXButton(v.getLocatorPopup(),
157  "\tLocate PoI\tLocate a PoI within the network.",
159  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
160  // for polygons
161  new FXButton(v.getLocatorPopup(),
162  "\tLocate Polygon\tLocate a Polygon within the network.",
164  ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
165 }
166 
167 
168 bool
169 GUIViewTraffic::setColorScheme(const std::string& name) {
170  if (!gSchemeStorage.contains(name)) {
171  return false;
172  }
173  if (myVisualizationChanger != 0) {
174  if (myVisualizationChanger->getCurrentScheme() != name) {
176  }
177  }
178  myVisualizationSettings = &gSchemeStorage.get(name.c_str());
180  update();
181  return true;
182 }
183 
184 
185 int
186 GUIViewTraffic::doPaintGL(int mode, const Boundary& bound) {
187  // init view settings
188  glRenderMode(mode);
189  glMatrixMode(GL_MODELVIEW);
190  glPushMatrix();
191  glDisable(GL_TEXTURE_2D);
192  glDisable(GL_ALPHA_TEST);
193  glDisable(GL_BLEND);
194  glEnable(GL_DEPTH_TEST);
195 
196  // draw decals (if not in grabbing mode)
197  if (!myUseToolTips) {
198  drawDecals();
200  paintGLGrid();
201  }
202  }
203 
204  glLineWidth(1);
205  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
206  const float minB[2] = { (float)bound.xmin(), (float)bound.ymin() };
207  const float maxB[2] = { (float)bound.xmax(), (float)bound.ymax() };
209  glEnable(GL_POLYGON_OFFSET_FILL);
210  glEnable(GL_POLYGON_OFFSET_LINE);
211  int hits2 = myGrid->Search(minB, maxB, *myVisualizationSettings);
212  //
213  if (myAdditionallyDrawn.size() > 0) {
214  glTranslated(0, 0, -.01);
216  for (std::map<const GUIGlObject*, int>::iterator i = myAdditionallyDrawn.begin(); i != myAdditionallyDrawn.end(); ++i) {
217  (i->first)->drawGLAdditional(this, *myVisualizationSettings);
218  }
220  glTranslated(0, 0, .01);
221  }
222  glPopMatrix();
223  /*
224  // draw legends
225  glMatrixMode(GL_MODELVIEW);
226  glLoadIdentity();
227  glTranslated(1.-.2, 1.-.5, 0.);
228  glScaled(.2, .5, 1.);
229  GUIColoringSchemesMap<GUILane> &sm = GUIViewTraffic::getLaneSchemesMap(); //!!!
230  sm.getColorer(myVisualizationSettings->laneEdgeMode)->drawLegend();
231  */
232  return hits2;
233 }
234 
235 
236 void
238  myTrackedID = id;
239 }
240 
241 
242 void
245 }
246 
247 
248 GUIGlID
250  return myTrackedID;
251 }
252 
253 
254 void
257  const std::vector<MSTrafficLightLogic*>& logics = tlsControl.getAllLogics();
258  MSTrafficLightLogic* minTll = 0;
259  double minDist = std::numeric_limits<double>::infinity();
260  for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
261  // get the logic
262  MSTrafficLightLogic* tll = (*i);
263  if (tlsControl.isActive(tll)) {
264  // get the links
265  const MSTrafficLightLogic::LaneVector& lanes = tll->getLanesAt(0);
266  if (lanes.size() > 0) {
267  const Position& endPos = lanes[0]->getShape().back();
268  if (endPos.distanceTo(pos) < minDist) {
269  minDist = endPos.distanceTo(pos);
270  minTll = tll;
271  }
272  }
273  }
274  }
275  if (minTll != 0) {
276  const MSTLLogicControl::TLSLogicVariants& vars = tlsControl.get(minTll->getID());
277  const std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
278  if (logics.size() > 1) {
280  for (int i = 0; i < (int)logics.size() - 1; ++i) {
281  if (minTll->getProgramID() == logics[i]->getProgramID()) {
282  l = (MSSimpleTrafficLightLogic*) logics[i + 1];
283  tlsControl.switchTo(minTll->getID(), l->getProgramID());
284  }
285  }
286  if (l == logics[0]) {
287  tlsControl.switchTo(minTll->getID(), l->getProgramID());
288  }
290  update();
291  }
292  }
293 }
294 
295 
296 SUMOTime
299 }
300 
301 
302 GUILane*
304  if (makeCurrent()) {
305  int id = getObjectUnderCursor();
306  if (id != 0) {
308  if (o != 0) {
309  return dynamic_cast<GUILane*>(o);
310  }
311  }
312  makeNonCurrent();
313  }
314  return 0;
315 }
316 
317 long
318 GUIViewTraffic::onCmdCloseLane(FXObject*, FXSelector, void*) {
319  GUILane* lane = getLaneUnderCursor();
320  if (lane != 0) {
321  lane->closeTraffic();
323  update();
324  }
325  return 1;
326 }
327 
328 
329 long
330 GUIViewTraffic::onCmdCloseEdge(FXObject*, FXSelector, void*) {
331  GUILane* lane = getLaneUnderCursor();
332  if (lane != 0) {
333  dynamic_cast<GUIEdge*>(&lane->getEdge())->closeTraffic(lane);
335  update();
336  }
337  return 1;
338 }
339 
340 
341 long
342 GUIViewTraffic::onCmdAddRerouter(FXObject*, FXSelector, void*) {
343  GUILane* lane = getLaneUnderCursor();
344  if (lane != 0) {
345  dynamic_cast<GUIEdge*>(&lane->getEdge())->addRerouter();
347  update();
348  }
349  return 1;
350 }
351 
352 
353 void
354 GUIViewTraffic::saveFrame(const std::string& destFile, FXColor* buf) {
355 #ifdef HAVE_FFMPEG
356  if (myCurrentVideo == 0) {
357  myCurrentVideo = new GUIVideoEncoder(destFile.c_str(), getWidth(), getHeight(), myApp->getDelay());
358  }
359  myCurrentVideo->writeFrame((uint8_t*)buf);
360 #else
361  UNUSED_PARAMETER(destFile);
362  UNUSED_PARAMETER(buf);
363 #endif
364 }
365 
366 
367 void
369 #ifdef HAVE_FFMPEG
370  if (myCurrentVideo != 0) {
371  delete myCurrentVideo;
372  myCurrentVideo = 0;
373  }
374 #endif
375 }
376 
377 
378 void
381 #ifdef HAVE_FFMPEG
382  if (myCurrentVideo != 0) {
383  std::string error = makeSnapshot("");
384  if (error != "" && error != "video") {
385  WRITE_WARNING(error);
386  }
387  }
388 #endif
389 }
390 
391 
392 /****************************************************************************/
Locate TLS - button.
Definition: GUIAppEnum.h:181
void paintGLGrid()
paints a grid
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:138
virtual void buildViewToolBars(GUIGlChildWindow &)
builds the view toolbars
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:132
GUICompleteSchemeStorage gSchemeStorage
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:582
long onCmdCloseLane(FXObject *, FXSelector, void *)
interaction with the simulation
Locate edge - button.
Definition: GUIAppEnum.h:175
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
void unlock()
release exclusive access to the simulation state
Definition: GUINet.cpp:513
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:1090
bool gaming
whether the application is in gaming mode or not
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:48
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 MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
T MAX2(T a, T b)
Definition: StdDefs.h:70
void lock()
grant exclusive access to the simulation state
Definition: GUINet.cpp:507
const std::string & getID() const
Returns the id.
Definition: Named.h:66
Locate polygons - button.
Definition: GUIAppEnum.h:191
GUIDialog_ViewSettings * myVisualizationChanger
Visualization changer.
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:38
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.
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:70
A class that stores and controls tls and switching of their programs.
A road/street connecting two junctions (gui-version)
Definition: GUIEdge.h:60
SUMOTime duration
The duration of the phase.
std::vector< MSTrafficLightLogic * > getAllLogics() const
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:491
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:383
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:85
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:257
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:126
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.
Locate poi - button.
Definition: GUIAppEnum.h:189
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.
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:50
FXbool makeCurrent()
A reimplementation due to some internal reasons.
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
double m2p(double meter) const
meter-to-pixels conversion method
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
bool contains(const std::string &name) const
Returns the information whether a setting with the given name is stored.
static const GUIGlID INVALID_ID
Definition: GUIGlObject.h:77
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.
Locate vehicle - button.
Definition: GUIAppEnum.h:177
void drawDecals()
Draws the stored decals.
Locate addtional structure - button.
Definition: GUIAppEnum.h:187
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:240
long long int SUMOTime
Definition: TraCIDefs.h:52
Locate junction - button.
Definition: GUIAppEnum.h:173
void unblockObject(GUIGlID id)
Marks an object as unblocked.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
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:144
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
Locate person - button.
Definition: GUIAppEnum.h:179
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
static bool gUseMesoSim
Definition: MSGlobals.h:98
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()