SUMO - Simulation of Urban MObility
GUITrafficLightLogicWrapper.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 /****************************************************************************/
18 // A wrapper for tl-logics to allow their visualisation and interaction
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <cassert>
28 #include <utils/geom/GeomHelper.h>
33 #include <utils/gui/div/GLHelper.h>
37 #include <microsim/MSLane.h>
44 #include <gui/GUIGlobals.h>
46 #include "GUINet.h"
48 
49 
50 // ===========================================================================
51 // FOX callback mapping
52 // ===========================================================================
59 };
60 
61 // Object implementation
63 
64 
65 // ===========================================================================
66 // method definitions
67 // ===========================================================================
68 /* -------------------------------------------------------------------------
69  * GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu - methods
70  * ----------------------------------------------------------------------- */
72  GUIMainWindow& app, GUISUMOAbstractView& parent,
73  GUIGlObject& o)
74  : GUIGLObjectPopupMenu(app, parent, o) {}
75 
76 
78 
79 
80 
81 long
83  FXObject*, FXSelector, void*) {
84  assert(myObject->getType() == GLO_TLLOGIC);
86  return 1;
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 /*sel*/, 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);
112  static_cast<GUITrafficLightLogicWrapper*>(myObject)->switchTLSLogic(FXSELID(sel) - MID_SWITCH);
113  return 1;
114 }
115 
116 
117 
118 /* -------------------------------------------------------------------------
119  * GUITrafficLightLogicWrapper - methods
120  * ----------------------------------------------------------------------- */
122  MSTLLogicControl& control, MSTrafficLightLogic& tll) :
123  GUIGlObject(GLO_TLLOGIC, tll.getID()),
124  myTLLogicControl(control), myTLLogic(tll) {}
125 
126 
128 
129 
132  GUISUMOAbstractView& parent) {
133  myApp = &app;
134  GUIGLObjectPopupMenu* ret = new GUITrafficLightLogicWrapperPopupMenu(app, parent, *this);
135  buildPopupHeader(ret, app);
137  //
139  std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
140  if (logics.size() > 1) {
141  std::vector<MSTrafficLightLogic*>::const_iterator i;
142  int index = 0;
143  for (i = logics.begin(); i != logics.end(); ++i, ++index) {
144  if (!vars.isActive(*i)) {
145  new FXMenuCommand(ret, ("Switch to '" + (*i)->getProgramID() + "'").c_str(),
146  GUIIconSubSys::getIcon(ICON_FLAG_MINUS), ret, (FXSelector)(MID_SWITCH + index));
147  }
148  }
149  new FXMenuSeparator(ret);
150  }
151  new FXMenuCommand(ret, "Switch off", GUIIconSubSys::getIcon(ICON_FLAG_MINUS), ret, MID_SWITCH_OFF);
152  new FXMenuCommand(ret, "Track Phases", nullptr, ret, MID_TRACKPHASES);
153  new FXMenuCommand(ret, "Show Phases", nullptr, ret, MID_SHOWPHASES);
154  new FXMenuSeparator(ret);
158  new FXMenuCommand(ret, ("phase: " + toString(tll->getCurrentPhaseIndex())).c_str(), nullptr, nullptr, 0);
159  new FXMenuSeparator(ret);
160  buildShowParamsPopupEntry(ret, false);
161  buildPositionCopyEntry(ret, false);
162  return ret;
163 }
164 
165 
166 void
170  new FuncBinding_StringParam<MSTLLogicControl, std::pair<SUMOTime, MSPhaseDefinition> >
172  window->create();
173  window->show();
174 }
175 
176 
177 void
181  static_cast<MSSimpleTrafficLightLogic&>(myTLLogic).getPhases());
182  window->setBeginTime(0);
183  window->create();
184  window->show();
185 }
186 
187 
192  new GUIParameterTableWindow(app, *this, 3 + (int)myTLLogic.getParametersMap().size());
193  ret->mkItem("tlLogic [id]", false, myTLLogic.getID());
194  ret->mkItem("program", false, myTLLogic.getProgramID());
195  // close building
196  ret->closeBuilding(&myTLLogic);
197  return ret;
198 }
199 
200 
201 Boundary
203  Boundary ret;
205  for (MSTrafficLightLogic::LaneVectorVector::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
206  const MSTrafficLightLogic::LaneVector& lanes2 = (*i);
207  for (MSTrafficLightLogic::LaneVector::const_iterator j = lanes2.begin(); j != lanes2.end(); ++j) {
208  ret.add((*j)->getShape()[-1]);
209  }
210  }
211  ret.grow(20);
212  return ret;
213 }
214 
215 
216 void
218  if (to == -1) {
222  } else {
224  std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
225  myTLLogicControl.switchTo(myTLLogic.getID(), logics[to]->getProgramID());
226  }
227 }
228 
229 
230 int
232  return myTLLogic.getLinkIndex(link);
233 }
234 
235 
236 void
238  if (s.gaming) {
239  if (!MSNet::getInstance()->getTLSControl().isActive(&myTLLogic)) {
240  return;
241  };
242  const std::string& curState = myTLLogic.getCurrentPhaseDef().getState();
243  if (curState.find_first_of("gG") == std::string::npos) {
244  // no link is 'green' at the moment. find those that turn green next
246  int curPhaseIdx = myTLLogic.getCurrentPhaseIndex();
247  int phaseIdx = (curPhaseIdx + 1) % phases.size();
248  std::vector<int> nextGreen;
249  while (phaseIdx != curPhaseIdx) {
250  const std::string& state = phases[phaseIdx]->getState();
251  for (int linkIdx = 0; linkIdx < (int)state.size(); linkIdx++) {
252  if ((LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MINOR ||
253  (LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MAJOR) {
254  nextGreen.push_back(linkIdx);
255  }
256  }
257  if (nextGreen.size() > 0) {
258  break;
259  }
260  phaseIdx = (phaseIdx + 1) % phases.size();
261  }
262  // highlight nextGreen links
263  for (std::vector<int>::iterator it_idx = nextGreen.begin(); it_idx != nextGreen.end(); it_idx++) {
264  const MSTrafficLightLogic::LaneVector& lanes = myTLLogic.getLanesAt(*it_idx);
265  for (MSTrafficLightLogic::LaneVector::const_iterator it_lane = lanes.begin(); it_lane != lanes.end(); it_lane++) {
266  glPushMatrix();
267  // split circle in red and yellow
268  Position pos = (*it_lane)->getShape().back();
269  glTranslated(pos.x(), pos.y(), GLO_MAX);
270  double rot = RAD2DEG((*it_lane)->getShape().angleAt2D((int)(*it_lane)->getShape().size() - 2)) - 90;
271  glRotated(rot, 0, 0, 1);
273  GLHelper::drawFilledCircle((*it_lane)->getWidth() / 2., 8, -90, 90);
275  GLHelper::drawFilledCircle((*it_lane)->getWidth() / 2., 8, 90, 270);
276  glPopMatrix();
277  }
278  }
279  }
280  }
281 }
282 
283 
284 /****************************************************************************/
285 
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.
void closeBuilding(const Parameterised *p=0)
Closes the building of the table.
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 createTLWrapper(MSTrafficLightLogic *tll)
creates a wrapper for the given logic
Definition: GUINet.cpp:145
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
Show TLS phases - popup entry.
Definition: GUIAppEnum.h:248
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:62
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by this tls.
void create()
Creates the window (FOX-Toolkit)
void mkItem(const char *name, bool dynamic, ValueSource< T > *src)
Adds a row which obtains its value from a ValueSource.
void begin2TrackPhases()
Builds a GUITLLogicPhasesTrackerWindow which will receive new phases.
double x() const
Returns the x-position.
Definition: Position.h:57
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:165
MSTLLogicControl & myTLLogicControl
Reference to the according tls.
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
#define RAD2DEG(x)
Definition: GeomHelper.h:39
GUIMainWindow * myApp
The main application.
const std::string & getID() const
Returns the id.
Definition: Named.h:78
void buildShowParamsPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to open the parameter window.
static void drawFilledCircle(double width, int steps=8)
Draws a filled circle around (0,0)
Definition: GLHelper.cpp:344
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:42
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.
Begin to track phases - menu entry.
Definition: GUIAppEnum.h:252
A class that stores and controls tls and switching of their programs.
std::vector< MSTrafficLightLogic * > getAllLogics() const
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:49
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:573
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
GUITrafficLightLogicWrapperPopupMenuMap[]
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
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:301
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.
The link has yellow light, may pass.
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link within the according tls.
The link has red light (must brake)
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.
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.
const std::map< std::string, std::string > & getParametersMap() const
Returns the inner key/value map.
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:79
GUIGlObject * myObject
The object that belongs to this popup-menu.
A window containing a gl-object&#39;s parameter.
static const RGBColor & getLinkColor(const LinkState &ls)
map from LinkState to color constants
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.