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-2022 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>
47 #include <gui/GUIGlobals.h>
50 
52 #include "GUINet.h"
53 
54 // ===========================================================================
55 // FOX callback mapping
56 // ===========================================================================
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);
90  static_cast<GUITrafficLightLogicWrapper*>(myObject)->begin2TrackPhases();
91  return 1;
92 }
93 
94 
95 long
97  FXObject*, FXSelector, void*) {
98  assert(myObject->getType() == GLO_TLLOGIC);
99  static_cast<GUITrafficLightLogicWrapper*>(myObject)->showPhases();
100  return 1;
101 }
102 
103 long
105  FXObject*, FXSelector, void*) {
106  assert(myObject->getType() == GLO_TLLOGIC);
107  GUITrafficLightLogicWrapper* w = static_cast<GUITrafficLightLogicWrapper*>(myObject);
109  if (act != nullptr) {
110  act->setShowDetectors(!act->showDetectors());
111  } else {
113  if (db != nullptr) {
114  db->setShowDetectors(!db->showDetectors());
115  } else {
116  NEMALogic* nema = dynamic_cast<NEMALogic*>(&w->getTLLogic());
117  if (nema != nullptr) {
118  nema->setShowDetectors(!nema->showDetectors());
119  }
120  }
121  }
122  myParent->update();
123  return 1;
124 }
125 
126 long
128  FXObject*, FXSelector /*sel*/, void*) {
129  assert(myObject->getType() == GLO_TLLOGIC);
130  static_cast<GUITrafficLightLogicWrapper*>(myObject)->switchTLSLogic(-1);
131  myParent->update();
132  return 1;
133 }
134 
135 
136 long
138  FXObject*, FXSelector sel, void*) {
139  assert(myObject->getType() == GLO_TLLOGIC);
140  static_cast<GUITrafficLightLogicWrapper*>(myObject)->switchTLSLogic(FXSELID(sel) - MID_SWITCH);
141  myParent->update();
142  return 1;
143 }
144 
145 
146 
147 /* -------------------------------------------------------------------------
148  * GUITrafficLightLogicWrapper - methods
149  * ----------------------------------------------------------------------- */
151  MSTLLogicControl& control, MSTrafficLightLogic& tll) :
152  GUIGlObject(GLO_TLLOGIC, tll.getID()),
153  myTLLogicControl(control), myTLLogic(tll) {}
154 
155 
157 
158 
161  GUISUMOAbstractView& parent) {
162  myApp = &app;
163  GUIGLObjectPopupMenu* ret = new GUITrafficLightLogicWrapperPopupMenu(app, parent, *this);
164  buildPopupHeader(ret, app);
166  //
168  std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
169  if (logics.size() > 1) {
170  std::vector<MSTrafficLightLogic*>::const_iterator i;
171  int index = 0;
172  for (i = logics.begin(); i != logics.end(); ++i, ++index) {
173  if (!vars.isActive(*i) && dynamic_cast<MSOffTrafficLightLogic*>(*i) == nullptr) {
174  GUIDesigns::buildFXMenuCommand(ret, ("Switch to '" + (*i)->getProgramID() + "'").c_str(),
175  GUIIconSubSys::getIcon(GUIIcon::FLAG_MINUS), ret, (FXSelector)(MID_SWITCH + index));
176  }
177  }
178  new FXMenuSeparator(ret);
179  }
180  MSOffTrafficLightLogic* offLogic = dynamic_cast<MSOffTrafficLightLogic*>(&myTLLogic);
181  if (offLogic == nullptr) {
183  }
184  GUIDesigns::buildFXMenuCommand(ret, "Track Phases", nullptr, ret, MID_TRACKPHASES);
185  GUIDesigns::buildFXMenuCommand(ret, "Show Phases", nullptr, ret, MID_SHOWPHASES);
187  if (act != nullptr) {
188  GUIDesigns::buildFXMenuCommand(ret, act->showDetectors() ? "Hide Detectors" : "Show Detectors", nullptr, ret, MID_SHOW_DETECTORS);
189  }
191  if (db != nullptr) {
192  GUIDesigns::buildFXMenuCommand(ret, db->showDetectors() ? "Hide Detectors" : "Show Detectors", nullptr, ret, MID_SHOW_DETECTORS);
193  }
194  NEMALogic* nema = dynamic_cast<NEMALogic*>(&myTLLogic);
195  if (nema != nullptr) {
196  GUIDesigns::buildFXMenuCommand(ret, nema->showDetectors() ? "Hide Detectors" : "Show Detectors", nullptr, ret, MID_SHOW_DETECTORS);
197  }
198  new FXMenuSeparator(ret);
202  GUIDesigns::buildFXMenuCommand(ret, "phase: " + toString(tll->getCurrentPhaseIndex()), nullptr, nullptr, 0);
203  const std::string& name = tll->getCurrentPhaseDef().getName();
204  if (name != "") {
205  GUIDesigns::buildFXMenuCommand(ret, "phase name: " + name, nullptr, nullptr, 0);
206  }
207  new FXMenuSeparator(ret);
208  buildShowParamsPopupEntry(ret, false);
209  buildPositionCopyEntry(ret, false);
210  return ret;
211 }
212 
213 
214 void
218  new FuncBinding_StringParam<MSTLLogicControl, std::pair<SUMOTime, MSPhaseDefinition> >
220  window->create();
221  window->show();
222 }
223 
224 
225 void
229  static_cast<MSSimpleTrafficLightLogic&>(myTLLogic).getPhases());
230  window->setBeginTime(0);
231  window->create();
232  window->show();
233 }
234 
235 
239  GUIParameterTableWindow* ret = new GUIParameterTableWindow(app, *this);
240  ret->mkItem("tlLogic [id]", false, myTLLogic.getID());
241  ret->mkItem("type", false, toString(myTLLogic.getLogicType()));
242  ret->mkItem("program", false, myTLLogic.getProgramID());
253  MSRailSignal* rs = dynamic_cast<MSRailSignal*>(&myTLLogic);
254  if (rs != nullptr) {
259  }
260  // close building
261  ret->closeBuilding(&myTLLogic);
262  return ret;
263 }
264 
265 double
267  return 1;
268 }
269 
270 
271 Boundary
273  Boundary ret;
275  for (MSTrafficLightLogic::LaneVectorVector::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
276  const MSTrafficLightLogic::LaneVector& lanes2 = (*i);
277  for (MSTrafficLightLogic::LaneVector::const_iterator j = lanes2.begin(); j != lanes2.end(); ++j) {
278  ret.add((*j)->getShape()[-1]);
279  }
280  }
281  ret.grow(20);
282  return ret;
283 }
284 
285 const std::string
287  return myTLLogic.getParameter("name", "");
288 }
289 
290 
291 void
293  if (to == -1) {
296  } else {
298  std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
299  myTLLogicControl.switchTo(myTLLogic.getID(), logics[to]->getProgramID());
300  }
301 }
302 
303 
304 int
306  return myTLLogic.getLinkIndex(link);
307 }
308 
309 
310 void
312  if (s.gaming) {
313  if (!MSNet::getInstance()->getTLSControl().isActive(&myTLLogic)) {
314  return;
315  };
316  const std::string& curState = myTLLogic.getCurrentPhaseDef().getState();
317  if (curState.find_first_of("gG") == std::string::npos) {
318  // no link is 'green' at the moment. find those that turn green next
320  int curPhaseIdx = myTLLogic.getCurrentPhaseIndex();
321  int phaseIdx = (curPhaseIdx + 1) % phases.size();
322  std::vector<int> nextGreen;
323  while (phaseIdx != curPhaseIdx) {
324  const std::string& state = phases[phaseIdx]->getState();
325  for (int linkIdx = 0; linkIdx < (int)state.size(); linkIdx++) {
326  if ((LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MINOR ||
327  (LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MAJOR) {
328  nextGreen.push_back(linkIdx);
329  }
330  }
331  if (nextGreen.size() > 0) {
332  break;
333  }
334  phaseIdx = (phaseIdx + 1) % phases.size();
335  }
336  // highlight nextGreen links
337  for (std::vector<int>::iterator it_idx = nextGreen.begin(); it_idx != nextGreen.end(); it_idx++) {
338  const MSTrafficLightLogic::LaneVector& lanes = myTLLogic.getLanesAt(*it_idx);
339  for (MSTrafficLightLogic::LaneVector::const_iterator it_lane = lanes.begin(); it_lane != lanes.end(); it_lane++) {
341  // split circle in red and yellow
342  Position pos = (*it_lane)->getShape().back();
343  glTranslated(pos.x(), pos.y(), GLO_MAX);
344  double rot = RAD2DEG((*it_lane)->getShape().angleAt2D((int)(*it_lane)->getShape().size() - 2)) - 90;
345  glRotated(rot, 0, 0, 1);
347  GLHelper::drawFilledCircle((*it_lane)->getWidth() / 2., 8, -90, 90);
349  GLHelper::drawFilledCircle((*it_lane)->getWidth() / 2., 8, 90, 270);
351  }
352  }
353  }
354  }
355 }
356 
360 }
361 
362 int
365 }
366 
367 std::string
370 }
371 
372 int
374  return (int)STEPS2TIME(getActiveTLLogic()->getCurrentPhaseDef().duration);
375 }
376 
377 int
379  return (int)STEPS2TIME(getActiveTLLogic()->getCurrentPhaseDef().minDuration);
380 }
381 
382 int
384  return (int)STEPS2TIME(getActiveTLLogic()->getCurrentPhaseDef().maxDuration);
385 }
386 
387 int
389  const SUMOTime earliestEnd = getActiveTLLogic()->getCurrentPhaseDef().earliestEnd;
390  return earliestEnd == MSPhaseDefinition::UNSPECIFIED_DURATION ? -1 : (int)STEPS2TIME(earliestEnd);
391 }
392 
393 int
395  const SUMOTime latestEnd = getActiveTLLogic()->getCurrentPhaseDef().latestEnd;
396  return latestEnd == MSPhaseDefinition::UNSPECIFIED_DURATION ? -1 : (int)STEPS2TIME(latestEnd);
397 }
398 
399 int
402 }
403 
404 int
406  return (int)STEPS2TIME(getActiveTLLogic()->getTimeInCycle());
407 }
408 
409 int
411  return (int)(SIMTIME - STEPS2TIME(getActiveTLLogic()->getCurrentPhaseDef().myLastSwitch));
412 }
413 
414 
415 /****************************************************************************/
@ MID_SWITCH_OFF
Show TLS phases - popup entry.
Definition: GUIAppEnum.h:431
@ MID_SHOWPHASES
Definition: GUIAppEnum.h:433
@ MID_TRACKPHASES
Begin to track phases - menu entry.
Definition: GUIAppEnum.h:435
@ MID_SHOW_DETECTORS
show/hide actuation detectors
Definition: GUIAppEnum.h:437
@ MID_SWITCH
Definition: GUIAppEnum.h:432
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
long long int SUMOTime
Definition: SUMOTime.h:32
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:46
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:507
static void drawFilledCircle(double width, int steps=8)
Draws a filled circle around (0,0)
Definition: GLHelper.cpp:431
static void popMatrix()
pop matrix
Definition: GLHelper.cpp:123
static void pushMatrix()
push matrix
Definition: GLHelper.cpp:114
static FXMenuCommand * buildFXMenuCommand(FXComposite *p, const std::string &text, FXIcon *icon, FXObject *tgt, FXSelector sel)
build menu command
Definition: GUIDesigns.cpp:42
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:153
static GUINet * getGUIInstance()
Returns the pointer to the unique instance of GUINet (singleton).
Definition: GUINet.cpp:533
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 ''.
double getExaggeration(const GUIVisualizationSettings &s) const
return exaggeration asociated with this GLObject
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, bool realistic=false)
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:174
A traffic lights logic which represents a tls in an off-mode.
const std::string & getState() const
Returns the state within this phase.
static const SUMOTime UNSPECIFIED_DURATION
SUMOTime latestEnd
The maximum time within the cycle for switching (for coordinated actuation)
const std::string & getName() const
SUMOTime earliestEnd
The minimum time within the cycle for switching (for coordinated actuation)
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.
A NEMA (adaptive) traffic light logic based on E2Detector.
bool showDetectors() const
void setShowDetectors(bool show)
const std::string & getID() const
Returns the id.
Definition: Named.h:74
virtual 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:37
double x() const
Returns the x-position.
Definition: Position.h:55
double y() const
Returns the y-position.
Definition: Position.h:60