Eclipse 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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
21 // A wrapper for tl-logics to allow their visualisation and interaction
22 /****************************************************************************/
23 #include <config.h>
24 
25 #include <cassert>
26 #include <utils/geom/GeomHelper.h>
31 #include <utils/gui/div/GLHelper.h>
35 #include <microsim/MSLane.h>
45 #include <gui/GUIGlobals.h>
48 
50 #include "GUINet.h"
51 
52 // ===========================================================================
53 // FOX callback mapping
54 // ===========================================================================
62 };
63 
64 // Object implementation
66 
67 
68 // ===========================================================================
69 // method definitions
70 // ===========================================================================
71 /* -------------------------------------------------------------------------
72  * GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu - methods
73  * ----------------------------------------------------------------------- */
75  GUIMainWindow& app, GUISUMOAbstractView& parent,
76  GUIGlObject& o)
77  : GUIGLObjectPopupMenu(app, parent, o) {}
78 
79 
81 
82 
83 
84 long
86  FXObject*, FXSelector, void*) {
87  assert(myObject->getType() == GLO_TLLOGIC);
88  static_cast<GUITrafficLightLogicWrapper*>(myObject)->begin2TrackPhases();
89  return 1;
90 }
91 
92 
93 long
95  FXObject*, FXSelector, void*) {
96  assert(myObject->getType() == GLO_TLLOGIC);
97  static_cast<GUITrafficLightLogicWrapper*>(myObject)->showPhases();
98  return 1;
99 }
100 
101 long
103  FXObject*, FXSelector, void*) {
104  assert(myObject->getType() == GLO_TLLOGIC);
105  GUITrafficLightLogicWrapper* w = static_cast<GUITrafficLightLogicWrapper*>(myObject);
107  if (act == nullptr) {
109  assert(db != 0);
110  db->setShowDetectors(!db->showDetectors());
111  } else {
112  act->setShowDetectors(!act->showDetectors());
113  }
114  return 1;
115 }
116 
117 long
119  FXObject*, FXSelector /*sel*/, void*) {
120  assert(myObject->getType() == GLO_TLLOGIC);
121  static_cast<GUITrafficLightLogicWrapper*>(myObject)->switchTLSLogic(-1);
122  return 1;
123 }
124 
125 
126 long
128  FXObject*, FXSelector sel, void*) {
129  assert(myObject->getType() == GLO_TLLOGIC);
130  static_cast<GUITrafficLightLogicWrapper*>(myObject)->switchTLSLogic(FXSELID(sel) - MID_SWITCH);
131  return 1;
132 }
133 
134 
135 
136 /* -------------------------------------------------------------------------
137  * GUITrafficLightLogicWrapper - methods
138  * ----------------------------------------------------------------------- */
140  MSTLLogicControl& control, MSTrafficLightLogic& tll) :
141  GUIGlObject(GLO_TLLOGIC, tll.getID()),
142  myTLLogicControl(control), myTLLogic(tll) {}
143 
144 
146 
147 
150  GUISUMOAbstractView& parent) {
151  myApp = &app;
152  GUIGLObjectPopupMenu* ret = new GUITrafficLightLogicWrapperPopupMenu(app, parent, *this);
153  buildPopupHeader(ret, app);
155  //
157  std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
158  if (logics.size() > 1) {
159  std::vector<MSTrafficLightLogic*>::const_iterator i;
160  int index = 0;
161  for (i = logics.begin(); i != logics.end(); ++i, ++index) {
162  if (!vars.isActive(*i)) {
163  GUIDesigns::buildFXMenuCommand(ret, ("Switch to '" + (*i)->getProgramID() + "'").c_str(),
164  GUIIconSubSys::getIcon(GUIIcon::FLAG_MINUS), ret, (FXSelector)(MID_SWITCH + index));
165  }
166  }
167  new FXMenuSeparator(ret);
168  }
170  GUIDesigns::buildFXMenuCommand(ret, "Track Phases", nullptr, ret, MID_TRACKPHASES);
171  GUIDesigns::buildFXMenuCommand(ret, "Show Phases", nullptr, ret, MID_SHOWPHASES);
173  if (act != nullptr) {
174  GUIDesigns::buildFXMenuCommand(ret, act->showDetectors() ? "Hide Detectors" : "Show Detectors", nullptr, ret, MID_SHOW_DETECTORS);
175  }
177  if (db != nullptr) {
178  GUIDesigns::buildFXMenuCommand(ret, db->showDetectors() ? "Hide Detectors" : "Show Detectors", nullptr, ret, MID_SHOW_DETECTORS);
179  }
180  new FXMenuSeparator(ret);
184  GUIDesigns::buildFXMenuCommand(ret, "phase: " + toString(tll->getCurrentPhaseIndex()), nullptr, nullptr, 0);
185  const std::string& name = tll->getCurrentPhaseDef().getName();
186  if (name != "") {
187  GUIDesigns::buildFXMenuCommand(ret, "phase name: " + name, nullptr, nullptr, 0);
188  }
189  new FXMenuSeparator(ret);
190  buildShowParamsPopupEntry(ret, false);
191  buildPositionCopyEntry(ret, false);
192  return ret;
193 }
194 
195 
196 void
200  new FuncBinding_StringParam<MSTLLogicControl, std::pair<SUMOTime, MSPhaseDefinition> >
202  window->create();
203  window->show();
204 }
205 
206 
207 void
211  static_cast<MSSimpleTrafficLightLogic&>(myTLLogic).getPhases());
212  window->setBeginTime(0);
213  window->create();
214  window->show();
215 }
216 
217 
221  GUIParameterTableWindow* ret = new GUIParameterTableWindow(app, *this);
222  ret->mkItem("tlLogic [id]", false, myTLLogic.getID());
223  ret->mkItem("type", false, toString(myTLLogic.getLogicType()));
224  ret->mkItem("program", false, myTLLogic.getProgramID());
231  MSRailSignal* rs = dynamic_cast<MSRailSignal*>(&myTLLogic);
232  if (rs != nullptr) {
237  }
238  // close building
239  ret->closeBuilding(&myTLLogic);
240  return ret;
241 }
242 
243 
244 Boundary
246  Boundary ret;
248  for (MSTrafficLightLogic::LaneVectorVector::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
249  const MSTrafficLightLogic::LaneVector& lanes2 = (*i);
250  for (MSTrafficLightLogic::LaneVector::const_iterator j = lanes2.begin(); j != lanes2.end(); ++j) {
251  ret.add((*j)->getShape()[-1]);
252  }
253  }
254  ret.grow(20);
255  return ret;
256 }
257 
258 const std::string
260  return myTLLogic.getParameter("name", "");
261 }
262 
263 
264 void
266  if (to == -1) {
269  } else {
271  std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
272  myTLLogicControl.switchTo(myTLLogic.getID(), logics[to]->getProgramID());
273  }
274 }
275 
276 
277 int
279  return myTLLogic.getLinkIndex(link);
280 }
281 
282 
283 void
285  if (s.gaming) {
286  if (!MSNet::getInstance()->getTLSControl().isActive(&myTLLogic)) {
287  return;
288  };
289  const std::string& curState = myTLLogic.getCurrentPhaseDef().getState();
290  if (curState.find_first_of("gG") == std::string::npos) {
291  // no link is 'green' at the moment. find those that turn green next
293  int curPhaseIdx = myTLLogic.getCurrentPhaseIndex();
294  int phaseIdx = (curPhaseIdx + 1) % phases.size();
295  std::vector<int> nextGreen;
296  while (phaseIdx != curPhaseIdx) {
297  const std::string& state = phases[phaseIdx]->getState();
298  for (int linkIdx = 0; linkIdx < (int)state.size(); linkIdx++) {
299  if ((LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MINOR ||
300  (LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MAJOR) {
301  nextGreen.push_back(linkIdx);
302  }
303  }
304  if (nextGreen.size() > 0) {
305  break;
306  }
307  phaseIdx = (phaseIdx + 1) % phases.size();
308  }
309  // highlight nextGreen links
310  for (std::vector<int>::iterator it_idx = nextGreen.begin(); it_idx != nextGreen.end(); it_idx++) {
311  const MSTrafficLightLogic::LaneVector& lanes = myTLLogic.getLanesAt(*it_idx);
312  for (MSTrafficLightLogic::LaneVector::const_iterator it_lane = lanes.begin(); it_lane != lanes.end(); it_lane++) {
313  glPushMatrix();
314  // split circle in red and yellow
315  Position pos = (*it_lane)->getShape().back();
316  glTranslated(pos.x(), pos.y(), GLO_MAX);
317  double rot = RAD2DEG((*it_lane)->getShape().angleAt2D((int)(*it_lane)->getShape().size() - 2)) - 90;
318  glRotated(rot, 0, 0, 1);
320  GLHelper::drawFilledCircle((*it_lane)->getWidth() / 2., 8, -90, 90);
322  GLHelper::drawFilledCircle((*it_lane)->getWidth() / 2., 8, 90, 270);
323  glPopMatrix();
324  }
325  }
326  }
327  }
328 }
329 
333 }
334 
335 int
338 }
339 
340 std::string
343 }
344 
345 int
347  return (int)STEPS2TIME(getActiveTLLogic()->getCurrentPhaseDef().duration);
348 }
349 
350 int
352  return (int)STEPS2TIME(getActiveTLLogic()->getCurrentPhaseDef().minDuration);
353 }
354 
355 int
357  return (int)STEPS2TIME(getActiveTLLogic()->getCurrentPhaseDef().maxDuration);
358 }
359 
360 int
362  return (int)(SIMTIME - STEPS2TIME(getActiveTLLogic()->getCurrentPhaseDef().myLastSwitch));
363 }
364 
365 
366 /****************************************************************************/
@ MID_SWITCH_OFF
Show TLS phases - popup entry.
Definition: GUIAppEnum.h:419
@ MID_SHOWPHASES
Definition: GUIAppEnum.h:421
@ MID_TRACKPHASES
Begin to track phases - menu entry.
Definition: GUIAppEnum.h:423
@ MID_SHOW_DETECTORS
show/hide actuation detectors
Definition: GUIAppEnum.h:425
@ MID_SWITCH
Definition: GUIAppEnum.h:420
FXDEFMAP(GUIDialog_AppSettings) GUIDialog_AppSettingsMap[]
@ GLO_MAX
empty max
@ GLO_TLLOGIC
a tl-logic
GUITrafficLightLogicWrapperPopupMenuMap[]
#define RAD2DEG(x)
Definition: GeomHelper.h:36
#define STEPS2TIME(x)
Definition: SUMOTime.h:53
#define SIMTIME
Definition: SUMOTime.h:60
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic,...
@ LINKSTATE_TL_YELLOW_MAJOR
The link has yellow light, may pass.
@ LINKSTATE_TL_GREEN_MAJOR
The link has green light, may pass.
@ LINKSTATE_TL_RED
The link has red light (must brake)
@ LINKSTATE_TL_GREEN_MINOR
The link has green light, has to brake.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
void add(double x, double y, double z=0)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:77
Boundary & grow(double by)
extends the boundary by the given amount
Definition: Boundary.cpp:299
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:446
static void drawFilledCircle(double width, int steps=8)
Draws a filled circle around (0,0)
Definition: GLHelper.cpp:347
static FXMenuCommand * buildFXMenuCommand(FXComposite *p, const std::string &text, FXIcon *icon, FXObject *tgt, FXSelector sel)
build menu command
Definition: GUIDesigns.cpp:40
The popup menu of a globject.
void buildPositionCopyEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to copy the cursor position if geo projection is used,...
void buildShowParamsPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to open the parameter window.
void buildCenterPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to center to the object.
void buildNameCopyPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds entries which allow to copy the name / typed name into the clipboard.
void buildPopupHeader(GUIGLObjectPopupMenu *ret, GUIMainWindow &app, bool addSeparator=true)
Builds the header.
void buildSelectionPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to (de)select the object.
static FXIcon * getIcon(const GUIIcon which)
returns a icon previously defined in the enum GUIIcon
void createTLWrapper(MSTrafficLightLogic *tll) override
creates a wrapper for the given logic
Definition: GUINet.cpp:154
static GUINet * getGUIInstance()
Returns the pointer to the unique instance of GUINet (singleton).
Definition: GUINet.cpp:526
A window containing a gl-object's parameter.
void mkItem(const char *name, bool dynamic, ValueSource< T > *src)
Adds a row which obtains its value from a ValueSource.
void closeBuilding(const Parameterised *p=0)
Closes the building of the table.
This window displays a phase diagram for a chosen tl-logic.
void setBeginTime(SUMOTime time)
Sets the time the display shall be shown as beginning at.
void create()
Creates the window (FOX-Toolkit)
long onCmdBegin2TrackPhases(FXObject *, FXSelector, void *)
Called if the phases shall be begun to track.
long onCmdShowPhases(FXObject *, FXSelector, void *)
Called if the phases shall be shown.
MSTLLogicControl & myTLLogicControl
Reference to the according tls.
MSTrafficLightLogic & getTLLogic() const
MSTrafficLightLogic & myTLLogic
The wrapped tl-logic.
GUIMainWindow * myApp
The main application.
void begin2TrackPhases()
Builds a GUITLLogicPhasesTrackerWindow which will receive new phases.
GUIParameterTableWindow * getParameterWindow(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own parameter window.
GUIGLObjectPopupMenu * getPopUpMenu(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own popup-menu.
GUITrafficLightLogicWrapper(MSTLLogicControl &control, MSTrafficLightLogic &tll)
Constructor.
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
const std::string getOptionalName() const
Returns the value for generic parameter 'name' or ''.
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.
MSTrafficLightLogic * getActiveTLLogic() const
void switchTLSLogic(int to)
Builds a GUITLLogicPhasesTrackerWindow which displays the phase diagram.
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link within the according tls.
void showPhases()
Builds a GUITLLogicPhasesTrackerWindow which displays the phase diagram.
Stores the information about how to visualize structures.
bool gaming
whether the application is in gaming mode or not
static const RGBColor & getLinkColor(const LinkState &ls)
map from LinkState to color constants
An actuated (adaptive) traffic light logic.
An actuated traffic light logic based on time delay of approaching vehicles.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
const std::string & getState() const
Returns the state within this phase.
const std::string & getName() const
A signal for rails.
Definition: MSRailSignal.h:46
std::string getBlockingVehicleIDs() const
std::string getPriorityVehicleIDs() const
std::string getConstraintInfo() const
std::string getRivalVehicleIDs() const
A fixed traffic light logic.
Storage for all programs of a single tls.
std::vector< MSTrafficLightLogic * > getAllLogics() const
bool isActive(const MSTrafficLightLogic *tl) const
A class that stores and controls tls and switching of their programs.
std::pair< SUMOTime, MSPhaseDefinition > getPhaseDef(const std::string &tlid) const
return the complete phase definition for a named traffic lights logic
void switchTo(const std::string &id, const std::string &programID)
Switches the named (id) tls to the named (programID) program.
MSTrafficLightLogic * getActive(const std::string &id) const
Returns the active program of a named tls.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
The parent class for traffic light logics.
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
std::vector< LaneVector > LaneVectorVector
Definition of a list that holds lists of lanes that do have the same attribute.
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link.
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by this tls.
TrafficLightType getLogicType() const
Returns the type of the logic.
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
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.
const std::string & getProgramID() const
Returns this tl-logic's id.
const std::string & getID() const
Returns the id.
Definition: Named.h:73
const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
double x() const
Returns the x-position.
Definition: Position.h:54
double y() const
Returns the y-position.
Definition: Position.h:59