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-2017 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 
55 // ===========================================================================
56 // FOX callback mapping
57 // ===========================================================================
64 };
65 
66 // Object implementation
68 
69 
70 // ===========================================================================
71 // method definitions
72 // ===========================================================================
73 /* -------------------------------------------------------------------------
74  * GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu - methods
75  * ----------------------------------------------------------------------- */
77  GUIMainWindow& app, GUISUMOAbstractView& parent,
78  GUIGlObject& o)
79  : GUIGLObjectPopupMenu(app, parent, o) {}
80 
81 
83 
84 
85 
86 long
88  FXObject*, FXSelector, void*) {
89  assert(myObject->getType() == GLO_TLLOGIC);
91  return 1;
92 }
93 
94 
95 long
97  FXObject*, FXSelector, void*) {
98  assert(myObject->getType() == GLO_TLLOGIC);
100  return 1;
101 }
102 
103 
104 long
106  FXObject*, FXSelector /*sel*/, void*) {
107  assert(myObject->getType() == GLO_TLLOGIC);
109  return 1;
110 }
111 
112 
113 long
115  FXObject*, FXSelector sel, void*) {
116  assert(myObject->getType() == GLO_TLLOGIC);
117  static_cast<GUITrafficLightLogicWrapper*>(myObject)->switchTLSLogic(FXSELID(sel) - MID_SWITCH);
118  return 1;
119 }
120 
121 
122 
123 /* -------------------------------------------------------------------------
124  * GUITrafficLightLogicWrapper - methods
125  * ----------------------------------------------------------------------- */
127  MSTLLogicControl& control, MSTrafficLightLogic& tll) :
128  GUIGlObject(GLO_TLLOGIC, tll.getID()),
129  myTLLogicControl(control), myTLLogic(tll) {}
130 
131 
133 
134 
137  GUISUMOAbstractView& parent) {
138  myApp = &app;
139  GUIGLObjectPopupMenu* ret = new GUITrafficLightLogicWrapperPopupMenu(app, parent, *this);
140  buildPopupHeader(ret, app);
142  //
144  std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
145  if (logics.size() > 1) {
146  std::vector<MSTrafficLightLogic*>::const_iterator i;
147  int index = 0;
148  for (i = logics.begin(); i != logics.end(); ++i, ++index) {
149  if (!vars.isActive(*i)) {
150  new FXMenuCommand(ret, ("Switch to '" + (*i)->getProgramID() + "'").c_str(),
151  GUIIconSubSys::getIcon(ICON_FLAG_MINUS), ret, (FXSelector)(MID_SWITCH + index));
152  }
153  }
154  new FXMenuSeparator(ret);
155  }
156  new FXMenuCommand(ret, "Switch off", GUIIconSubSys::getIcon(ICON_FLAG_MINUS), ret, MID_SWITCH_OFF);
157  new FXMenuCommand(ret, "Track Phases", 0, ret, MID_TRACKPHASES);
158  new FXMenuCommand(ret, "Show Phases", 0, ret, MID_SHOWPHASES);
159  new FXMenuSeparator(ret);
163  new FXMenuCommand(ret, ("phase: " + toString(tll->getCurrentPhaseIndex())).c_str(), 0, 0, 0);
164  new FXMenuSeparator(ret);
165  buildPositionCopyEntry(ret, false);
166  return ret;
167 }
168 
169 
170 void
174  new FuncBinding_StringParam<MSTLLogicControl, std::pair<SUMOTime, MSPhaseDefinition> >
176  window->create();
177  window->show();
178 }
179 
180 
181 void
185  static_cast<MSSimpleTrafficLightLogic&>(myTLLogic).getPhases());
186  window->setBeginTime(0);
187  window->create();
188  window->show();
189 }
190 
191 
195  return 0;
196 }
197 
198 
199 Boundary
201  Boundary ret;
203  for (MSTrafficLightLogic::LaneVectorVector::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
204  const MSTrafficLightLogic::LaneVector& lanes2 = (*i);
205  for (MSTrafficLightLogic::LaneVector::const_iterator j = lanes2.begin(); j != lanes2.end(); ++j) {
206  ret.add((*j)->getShape()[-1]);
207  }
208  }
209  ret.grow(20);
210  return ret;
211 }
212 
213 
214 void
216  if (to == -1) {
220  } else {
222  std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
223  myTLLogicControl.switchTo(myTLLogic.getID(), logics[to]->getProgramID());
224  }
225 }
226 
227 
228 int
230  return myTLLogic.getLinkIndex(link);
231 }
232 
233 
234 void
236  if (s.gaming) {
237  if (!MSNet::getInstance()->getTLSControl().isActive(&myTLLogic)) {
238  return;
239  };
240  const std::string& curState = myTLLogic.getCurrentPhaseDef().getState();
241  if (curState.find_first_of("gG") == std::string::npos) {
242  // no link is 'green' at the moment. find those that turn green next
244  int curPhaseIdx = myTLLogic.getCurrentPhaseIndex();
245  int phaseIdx = (curPhaseIdx + 1) % phases.size();
246  std::vector<int> nextGreen;
247  while (phaseIdx != curPhaseIdx) {
248  const std::string& state = phases[phaseIdx]->getState();
249  for (int linkIdx = 0; linkIdx < (int)state.size(); linkIdx++) {
250  if ((LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MINOR ||
251  (LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MAJOR) {
252  nextGreen.push_back(linkIdx);
253  }
254  }
255  if (nextGreen.size() > 0) {
256  break;
257  }
258  phaseIdx = (phaseIdx + 1) % phases.size();
259  }
260  // highlight nextGreen links
261  for (std::vector<int>::iterator it_idx = nextGreen.begin(); it_idx != nextGreen.end(); it_idx++) {
262  const MSTrafficLightLogic::LaneVector& lanes = myTLLogic.getLanesAt(*it_idx);
263  for (MSTrafficLightLogic::LaneVector::const_iterator it_lane = lanes.begin(); it_lane != lanes.end(); it_lane++) {
264  glPushMatrix();
265  glColor3d(0, 1, 0);
266  Position pos = (*it_lane)->getShape().back();
267  glTranslated(pos.x(), pos.y(), GLO_MAX);
268  GLHelper::drawFilledCircle((*it_lane)->getWidth() / 2.);
269  glPopMatrix();
270  }
271  }
272  }
273  }
274 }
275 
276 
277 /****************************************************************************/
278 
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.
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.
double y() const
Returns the y-position.
Definition: Position.h:68
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by this tls.
void create()
Creates the window (FOX-Toolkit)
void begin2TrackPhases()
Builds a GUITLLogicPhasesTrackerWindow which will receive new phases.
double x() const
Returns the x-position.
Definition: Position.h:63
void buildCenterPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to center to the object.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
MSTLLogicControl & myTLLogicControl
Reference to the according tls.
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
GUIMainWindow * myApp
The main application.
const std::string & getID() const
Returns the id.
Definition: Named.h:66
static void drawFilledCircle(double width, int steps=8)
Draws a filled circle around (0,0)
Definition: GLHelper.cpp:340
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.
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link.
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.
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
A class that stores and controls tls and switching of their programs.
std::vector< MSTrafficLightLogic * > getAllLogics() const
GUIGlID createTLWrapper(MSTrafficLightLogic *tll)
creates a wrapper for the given logic and returns the GlID
Definition: GUINet.cpp:151
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
std::pair< SUMOTime, MSPhaseDefinition > getPhaseDef(const std::string &tlid) const
return the complete phase definition for a named traffic lights logic
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
GUITrafficLightLogicWrapperPopupMenuMap[]
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
MSTrafficLightLogic * getActive(const std::string &id) const
Returns the active program of a named tls.
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.
Boundary & grow(double by)
extends the boundary by the given amount
Definition: Boundary.cpp:234
GUIParameterTableWindow * getParameterWindow(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own parameter window.
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.
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
std::vector< LaneVector > LaneVectorVector
Definition of a list that holds lists of lanes that do have the same attribute.
Show TLS phases - popup entry.
Definition: GUIAppEnum.h:247
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link within the according tls.
bool isActive(const MSTrafficLightLogic *tl) const
The popup menu of a globject.
void buildSelectionPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to (de)select the object.
Begin to track phases - menu entry.
Definition: GUIAppEnum.h:251
This window displays a phase diagram for a chosen tl-logic.
GUITrafficLightLogicWrapper(MSTLLogicControl &control, MSTrafficLightLogic &tll)
Constructor.
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.
The parent class for traffic light logics.
void setBeginTime(SUMOTime time)
Sets the time the display shall be shown as beginning at.
empty max
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
GUITLLogicPhasesTrackerWindow()
protected constructor for FOX
void add(double x, double y, double z=0)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:86
GUIGlObject * myObject
The object that belongs to this popup-menu.
A window containing a gl-object&#39;s parameter.
static FXIcon * getIcon(GUIIcon which)
returns a icon previously defined in the enum GUIIcon
long onCmdShowPhases(FXObject *, FXSelector, void *)
Called if the phases shall be shown.
void buildPopupHeader(GUIGLObjectPopupMenu *ret, GUIMainWindow &app, bool addSeparator=true)
Builds the header.