SUMO - Simulation of Urban MObility
GUITrafficLightLogicWrapper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A wrapper for tl-logics to allow their visualisation and interaction
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
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 #include <cassert>
39 #include <utils/gui/div/GLHelper.h>
42 #include <microsim/MSLane.h>
49 #include <gui/GUIGlobals.h>
51 #include "GUINet.h"
53 
54 #ifdef CHECK_MEMORY_LEAKS
55 #include <foreign/nvwa/debug_new.h>
56 #endif // CHECK_MEMORY_LEAKS
57 
58 
59 // ===========================================================================
60 // FOX callback mapping
61 // ===========================================================================
68 };
69 
70 // Object implementation
72 
73 
74 // ===========================================================================
75 // method definitions
76 // ===========================================================================
77 /* -------------------------------------------------------------------------
78  * GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu - methods
79  * ----------------------------------------------------------------------- */
81  GUIMainWindow& app, GUISUMOAbstractView& parent,
82  GUIGlObject& o)
83  : GUIGLObjectPopupMenu(app, parent, o) {}
84 
85 
87 
88 
89 
90 long
92  FXObject*, FXSelector, void*) {
93  assert(myObject->getType() == GLO_TLLOGIC);
95  return 1;
96 }
97 
98 
99 long
101  FXObject*, FXSelector, void*) {
102  assert(myObject->getType() == GLO_TLLOGIC);
104  return 1;
105 }
106 
107 
108 long
110  FXObject*, FXSelector /*sel*/, void*) {
111  assert(myObject->getType() == GLO_TLLOGIC);
113  return 1;
114 }
115 
116 
117 long
119  FXObject*, FXSelector sel, void*) {
120  assert(myObject->getType() == GLO_TLLOGIC);
121  static_cast<GUITrafficLightLogicWrapper*>(myObject)->switchTLSLogic(FXSELID(sel) - MID_SWITCH);
122  return 1;
123 }
124 
125 
126 
127 /* -------------------------------------------------------------------------
128  * GUITrafficLightLogicWrapper - methods
129  * ----------------------------------------------------------------------- */
131  MSTLLogicControl& control, MSTrafficLightLogic& tll) :
132  GUIGlObject(GLO_TLLOGIC, tll.getID()),
133  myTLLogicControl(control), myTLLogic(tll) {}
134 
135 
137 
138 
141  GUISUMOAbstractView& parent) {
142  myApp = &app;
143  GUIGLObjectPopupMenu* ret = new GUITrafficLightLogicWrapperPopupMenu(app, parent, *this);
144  buildPopupHeader(ret, app);
146  //
148  std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
149  if (logics.size() > 1) {
150  std::vector<MSTrafficLightLogic*>::const_iterator i;
151  size_t index = 0;
152  for (i = logics.begin(); i != logics.end(); ++i, ++index) {
153  if (!vars.isActive(*i)) {
154  new FXMenuCommand(ret, ("Switch to '" + (*i)->getProgramID() + "'").c_str(),
155  GUIIconSubSys::getIcon(ICON_FLAG_MINUS), ret, (FXSelector)(MID_SWITCH + index));
156  }
157  }
158  new FXMenuSeparator(ret);
159  }
160  new FXMenuCommand(ret, "Switch off", GUIIconSubSys::getIcon(ICON_FLAG_MINUS), ret, MID_SWITCH_OFF);
161  new FXMenuCommand(ret, "Track Phases", 0, ret, MID_TRACKPHASES);
162  new FXMenuCommand(ret, "Show Phases", 0, ret, MID_SHOWPHASES);
163  new FXMenuSeparator(ret);
167  new FXMenuCommand(ret, ("phase: " + toString(tll->getCurrentPhaseIndex())).c_str(), 0, 0, 0);
168  new FXMenuSeparator(ret);
169  buildPositionCopyEntry(ret, false);
170  return ret;
171 }
172 
173 
174 void
178  new FuncBinding_StringParam<MSTLLogicControl, std::pair<SUMOTime, MSPhaseDefinition> >
180  window->create();
181  window->show();
182 }
183 
184 
185 void
189  static_cast<MSSimpleTrafficLightLogic&>(myTLLogic).getPhases());
190  window->setBeginTime(0);
191  window->create();
192  window->show();
193 }
194 
195 
199  return 0;
200 }
201 
202 
203 Boundary
205  Boundary ret;
207  for (MSTrafficLightLogic::LaneVectorVector::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
208  const MSTrafficLightLogic::LaneVector& lanes2 = (*i);
209  for (MSTrafficLightLogic::LaneVector::const_iterator j = lanes2.begin(); j != lanes2.end(); ++j) {
210  ret.add((*j)->getShape()[-1]);
211  }
212  }
213  ret.grow(20);
214  return ret;
215 }
216 
217 
218 void
220  if (to == -1) {
224  } else {
226  std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
227  myTLLogicControl.switchTo(myTLLogic.getID(), logics[to]->getProgramID());
228  }
229 }
230 
231 
232 int
234  return myTLLogic.getLinkIndex(link);
235 }
236 
237 
238 void
240  if (s.gaming) {
241  if (!MSNet::getInstance()->getTLSControl().isActive(&myTLLogic)) {
242  return;
243  };
244  const std::string& curState = myTLLogic.getCurrentPhaseDef().getState();
245  if (curState.find_first_of("gG") == std::string::npos) {
246  // no link is 'green' at the moment. find those that turn green next
248  int curPhaseIdx = myTLLogic.getCurrentPhaseIndex();
249  int phaseIdx = (curPhaseIdx + 1) % phases.size();
250  std::vector<unsigned int> nextGreen;
251  while (phaseIdx != curPhaseIdx) {
252  const std::string& state = phases[phaseIdx]->getState();
253  for (unsigned int linkIdx = 0; linkIdx < state.size(); linkIdx++) {
254  if ((LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MINOR ||
255  (LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MAJOR) {
256  nextGreen.push_back(linkIdx);
257  }
258  }
259  if (nextGreen.size() > 0) {
260  break;
261  }
262  phaseIdx = (phaseIdx + 1) % phases.size();
263  }
264  // highlight nextGreen links
265  for (std::vector<unsigned int>::iterator it_idx = nextGreen.begin(); it_idx != nextGreen.end(); it_idx++) {
266  const MSTrafficLightLogic::LaneVector& lanes = myTLLogic.getLanesAt(*it_idx);
267  for (MSTrafficLightLogic::LaneVector::const_iterator it_lane = lanes.begin(); it_lane != lanes.end(); it_lane++) {
268  glPushMatrix();
269  glColor3d(0, 1, 0);
270  Position pos = (*it_lane)->getShape().back();
271  glTranslated(pos.x(), pos.y(), GLO_MAX);
272  GLHelper::drawFilledCircle((*it_lane)->getWidth() / 2.);
273  glPopMatrix();
274  }
275  }
276  }
277  }
278 }
279 
280 
281 /****************************************************************************/
282 
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
The link has green light, may pass.
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
const std::string & getState() const
Returns the state within this phase.
long onCmdBegin2TrackPhases(FXObject *, FXSelector, void *)
Called if the phases shall be begun to track.
Storage for all programs of a single tls.
void buildNameCopyPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds entries which allow to copy the name / typed name into the clipboard.
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link.
void switchTo(const std::string &id, const std::string &programID)
Switches the named (id) tls to the named (programID) program.
bool gaming
whether the application is in gaming mode or not
The link has green light, has to brake.
Stores the information about how to visualize structures.
void create()
Creates the window (FOX-Toolkit)
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link within the according tls.
void begin2TrackPhases()
Builds a GUITLLogicPhasesTrackerWindow which will receive new phases.
void buildCenterPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to center to the object.
MSTrafficLightLogic * getActive(const std::string &id) const
Returns the active program of a named tls.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:160
MSTLLogicControl & myTLLogicControl
Reference to the according tls.
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
GUIMainWindow * myApp
The main application.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
void buildPositionCopyEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to copy the cursor position if geo projection is used, also builds an entry for copying the geo-position.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
FXDEFMAP(GUIDialog_AppSettings) GUIDialog_AppSettingsMap[]
GUIGLObjectPopupMenu * getPopUpMenu(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own popup-menu.
void showPhases()
Builds a GUITLLogicPhasesTrackerWindow which displays the phase diagram.
A class that stores and controls tls and switching of their programs.
std::vector< MSTrafficLightLogic * > getAllLogics() const
static void drawFilledCircle(SUMOReal width, int steps=8)
Draws a filled circle around (0,0)
Definition: GLHelper.cpp:344
const std::string & getID() const
Returns the id.
Definition: Named.h:65
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
Definition: GUIGlObject.h:180
GUIGlID createTLWrapper(MSTrafficLightLogic *tll)
creates a wrapper for the given logic and returns the GlID
Definition: GUINet.cpp:156
bool isActive(const MSTrafficLightLogic *tl) const
std::pair< SUMOTime, MSPhaseDefinition > getPhaseDef(const std::string &tlid) const
return the complete phase definition for a named traffic lights logic
GUITrafficLightLogicWrapperPopupMenuMap[]
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
const LaneVector & getLanesAt(unsigned int i) const
Returns the list of lanes that are controlled by the signals at the given position.
void switchTLSLogic(int to)
Builds a GUITLLogicPhasesTrackerWindow which displays the phase diagram.
a tl-logic
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
MSTrafficLightLogic & myTLLogic
The wrapped tl-logic.
GUIParameterTableWindow * getParameterWindow(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own parameter window.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:54
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by this tls.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
void add(SUMOReal x, SUMOReal y)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:76
Show TLS phases - popup entry.
Definition: GUIAppEnum.h:233
Boundary & grow(SUMOReal by)
extends the boundary by the given amount
Definition: Boundary.cpp:201
std::vector< MSLane * > LaneVector
Definition of the list of links that participate in this tl-light.
std::vector< LaneVector > LaneVectorVector
Definition of a list that holds lists of links that do have the same attribute.
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
The popup menu of a globject.
void buildSelectionPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to (de)select the object.
This window displays a phase diagram for a chosen tl-logic.
Begin to track phases - menu entry.
Definition: GUIAppEnum.h:237
GUITrafficLightLogicWrapper(MSTLLogicControl &control, MSTrafficLightLogic &tll)
Constructor.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
The parent class for traffic light logics.
void setBeginTime(SUMOTime time)
Sets the time the display shall be shown as beginning at.
empty max
GUITLLogicPhasesTrackerWindow()
protected constructor for FOX
GUIGlObject * myObject
The object that belongs to this popup-menu.
A window containing a gl-object&#39;s parameter.
static FXIcon * getIcon(GUIIcon which)
long onCmdShowPhases(FXObject *, FXSelector, void *)
Called if the phases shall be shown.
void buildPopupHeader(GUIGLObjectPopupMenu *ret, GUIMainWindow &app, bool addSeparator=true)
Builds the header.
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.