Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GUITrafficLightLogicWrapper.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2025 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>
36#include <microsim/MSLane.h>
48#include <gui/GUIGlobals.h>
51
53#include "GUINet.h"
54
55// ===========================================================================
56// FOX callback mapping
57// ===========================================================================
66
67// Object implementation
69
70
71// ===========================================================================
72// method definitions
73// ===========================================================================
74/* -------------------------------------------------------------------------
75 * GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu - methods
76 * ----------------------------------------------------------------------- */
80
81
83
84
85
86long
88 FXObject*, FXSelector, void*) {
89 assert(myObject->getType() == GLO_TLLOGIC);
90 static_cast<GUITrafficLightLogicWrapper*>(myObject)->begin2TrackPhases();
91 return 1;
92}
93
94
95long
97 FXObject*, FXSelector, void*) {
98 assert(myObject->getType() == GLO_TLLOGIC);
99 static_cast<GUITrafficLightLogicWrapper*>(myObject)->showPhases();
100 return 1;
101}
102
103long
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) {
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
126long
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
136long
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 * ----------------------------------------------------------------------- */
154
155
157
158
161 myApp = &app;
163 buildPopupHeader(ret, app);
166 std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
167 if (logics.size() > 1) {
168 std::vector<MSTrafficLightLogic*>::const_iterator i;
169 int index = 0;
170 for (i = logics.begin(); i != logics.end(); ++i, ++index) {
171 if (!vars.isActive(*i) && dynamic_cast<MSOffTrafficLightLogic*>(*i) == nullptr) {
172 GUIDesigns::buildFXMenuCommand(ret, TLF("Switch to '%'", (*i)->getProgramID()),
173 GUIIconSubSys::getIcon(GUIIcon::FLAG_MINUS), ret, (FXSelector)(MID_SWITCH + index));
174 }
175 }
176 new FXMenuSeparator(ret);
177 }
178 MSOffTrafficLightLogic* offLogic = dynamic_cast<MSOffTrafficLightLogic*>(vars.getActive());
179 if (offLogic == nullptr) {
181 }
182 GUIDesigns::buildFXMenuCommand(ret, TL("Track Phases"), nullptr, ret, MID_TRACKPHASES);
183 GUIDesigns::buildFXMenuCommand(ret, TL("Show Phases"), nullptr, ret, MID_SHOWPHASES);
185 if (act != nullptr) {
186 GUIDesigns::buildFXMenuCommand(ret, act->showDetectors() ? TL("Hide Detectors") : TL("Show Detectors"), nullptr, ret, MID_SHOW_DETECTORS);
187 }
189 if (db != nullptr) {
190 GUIDesigns::buildFXMenuCommand(ret, db->showDetectors() ? TL("Hide Detectors") : TL("Show Detectors"), nullptr, ret, MID_SHOW_DETECTORS);
191 }
192 NEMALogic* nema = dynamic_cast<NEMALogic*>(&myTLLogic);
193 if (nema != nullptr) {
194 GUIDesigns::buildFXMenuCommand(ret, nema->showDetectors() ? TL("Hide Detectors") : TL("Show Detectors"), nullptr, ret, MID_SHOW_DETECTORS);
195 }
196 new FXMenuSeparator(ret);
200 GUIDesigns::buildFXMenuCommand(ret, TLF("Phase: %", toString(tll->getCurrentPhaseIndex())), nullptr, nullptr, 0);
201 const std::string& name = tll->getCurrentPhaseDef().getName();
202 if (name != "") {
203 GUIDesigns::buildFXMenuCommand(ret, TLF("Phase name: %", name), nullptr, nullptr, 0);
204 }
205 new FXMenuSeparator(ret);
206 buildShowParamsPopupEntry(ret, false);
207 buildPositionCopyEntry(ret, app);
208 return ret;
209}
210
211
212void
214 if (app != nullptr) {
215 myApp = app;
216 }
217 assert(myApp != nullptr);
220 new FuncBinding_StringParam<MSTLLogicControl, std::pair<SUMOTime, MSPhaseDefinition> >
222 window->create();
223 window->show();
224}
225
226
227void
231 static_cast<MSSimpleTrafficLightLogic&>(myTLLogic).getPhases());
232 window->setBeginTime(0);
233 window->create();
234 window->show();
235}
236
237
242 ret->mkItem(TL("tlLogic [id]"), false, myTLLogic.getID());
243 ret->mkItem(TL("type"), false, toString(myTLLogic.getLogicType()));
244 ret->mkItem(TL("program"), false, myTLLogic.getProgramID());
255 MSRailSignal* rs = dynamic_cast<MSRailSignal*>(&myTLLogic);
256 if (rs != nullptr) {
259 ret->mkItem(TL("blocking driveways"), true, new FunctionBindingString<MSRailSignal>(rs, &MSRailSignal::getBlockingDriveWayIDs));
263 }
264 // close building
266 return ret;
267}
268
269
272 Boundary ret;
274 for (MSTrafficLightLogic::LaneVectorVector::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
275 const MSTrafficLightLogic::LaneVector& lanes2 = (*i);
276 for (MSTrafficLightLogic::LaneVector::const_iterator j = lanes2.begin(); j != lanes2.end(); ++j) {
277 ret.add((*j)->getShape()[-1]);
278 }
279 }
280 ret.grow(20);
281 return ret;
282}
283
284const std::string
288
289
290void
292 if (to == -1) {
295 } else {
297 std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
298 myTLLogicControl.switchTo(myTLLogic.getID(), logics[to]->getProgramID());
299 }
300}
301
302
303int
305 return myTLLogic.getLinkIndex(link);
306}
307
308
309void
311 if (s.gaming) {
312 if (!MSNet::getInstance()->getTLSControl().isActive(&myTLLogic) || myTLLogic.getPhases().size() == 0) {
313 return;
314 }
315 const std::string& curState = myTLLogic.getCurrentPhaseDef().getState();
316 if (curState.find_first_of("gG") == std::string::npos) {
317 // no link is 'green' at the moment. find those that turn green next
319 int curPhaseIdx = myTLLogic.getCurrentPhaseIndex();
320 int phaseIdx = (curPhaseIdx + 1) % phases.size();
321 std::vector<int> nextGreen;
322 while (phaseIdx != curPhaseIdx) {
323 const std::string& state = phases[phaseIdx]->getState();
324 for (int linkIdx = 0; linkIdx < (int)state.size(); linkIdx++) {
325 if ((LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MINOR ||
326 (LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MAJOR) {
327 nextGreen.push_back(linkIdx);
328 }
329 }
330 if (nextGreen.size() > 0) {
331 break;
332 }
333 phaseIdx = (phaseIdx + 1) % phases.size();
334 }
335 // highlight nextGreen links
336 for (const int idx : nextGreen) {
337 for (const MSLane* const lane : myTLLogic.getLanesAt(idx)) {
339 // split circle in red and yellow
340 const Position& pos = lane->getShape().back();
341 glTranslated(pos.x(), pos.y(), GLO_MAX);
342 double rot = RAD2DEG(lane->getShape().angleAt2D((int)lane->getShape().size() - 2)) - 90;
343 glRotated(rot, 0, 0, 1);
345 GLHelper::drawFilledCircle(lane->getWidth() / 2., 8, -90, 90);
346 if (!isRailway(lane->getPermissions())) {
347 // no yellow half-cirlce in railway game
349 GLHelper::drawFilledCircle(lane->getWidth() / 2., 8, 90, 270);
350 }
352 }
353 }
354 }
355 }
356}
357
362
363int
367
368std::string
372
373int
375 return (int)STEPS2TIME(getActiveTLLogic()->getCurrentPhaseDef().duration);
376}
377
378int
382
383int
387
388int
390 const SUMOTime earliestEnd = getActiveTLLogic()->getEarliestEnd();
391 return earliestEnd == MSPhaseDefinition::UNSPECIFIED_DURATION ? -1 : (int)STEPS2TIME(earliestEnd);
392}
393
394int
396 const SUMOTime latestEnd = getActiveTLLogic()->getLatestEnd();
397 return latestEnd == MSPhaseDefinition::UNSPECIFIED_DURATION ? -1 : (int)STEPS2TIME(latestEnd);
398}
399
400int
402 return (int)STEPS2TIME(getActiveTLLogic()->getDefaultCycleTime());
403}
404
405int
409
410int
412 return (int)STEPS2TIME(getActiveTLLogic()->getSpentDuration());
413}
414
415
416/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
@ MID_SWITCH_OFF
Show TLS phases - popup entry.
Definition GUIAppEnum.h:479
@ MID_SHOWPHASES
Definition GUIAppEnum.h:481
@ MID_TRACKPHASES
Begin to track phases - menu entry.
Definition GUIAppEnum.h:483
@ MID_SHOW_DETECTORS
show/hide actuation detectors
Definition GUIAppEnum.h:485
@ MID_SWITCH
Definition GUIAppEnum.h:480
FXDEFMAP(GUIDialog_AppSettings) GUIDialog_AppSettingsMap[]
@ GLO_MAX
empty max
@ GLO_TLLOGIC
a tl-logic
GUIIcon
An enumeration of icons used by the gui applications.
Definition GUIIcons.h:33
GUITrafficLightLogicWrapperPopupMenuMap[]
#define RAD2DEG(x)
Definition GeomHelper.h:36
#define TL(string)
Definition MsgHandler.h:304
#define TLF(string,...)
Definition MsgHandler.h:306
#define STEPS2TIME(x)
Definition SUMOTime.h:55
bool isRailway(SVCPermissions permissions)
Returns whether an edge with the given permissions is a (exclusive) railway edge.
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:75
Boundary & grow(double by)
extends the boundary by the given amount
Definition Boundary.cpp:340
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition GLHelper.cpp:649
static void drawFilledCircle(const double widradiusth, const int steps=8)
Draws a filled circle around (0,0)
Definition GLHelper.cpp:564
static void popMatrix()
pop matrix
Definition GLHelper.cpp:131
static void pushMatrix()
push matrix
Definition GLHelper.cpp:118
static FXMenuCommand * buildFXMenuCommand(FXComposite *p, const std::string &text, FXIcon *icon, FXObject *tgt, FXSelector sel, const bool disable=false)
build menu command
The popup menu of a globject.
void buildPositionCopyEntry(GUIGLObjectPopupMenu *ret, const GUIMainWindow &app, bool addSeparator=true) const
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:157
static GUINet * getGUIInstance()
Returns the pointer to the unique instance of GUINet (singleton).
Definition GUINet.cpp:581
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 & myTLLogic
The wrapped tl-logic.
MSTrafficLightLogic & getTLLogic() const
GUIMainWindow * myApp
The main application.
void begin2TrackPhases(GUIMainWindow *app=nullptr)
Builds a GUITLLogicPhasesTrackerWindow which will receive new phases.
const std::string getOptionalName() const override
Returns the value for generic parameter 'name' or ''.
GUIParameterTableWindow * getParameterWindow(GUIMainWindow &app, GUISUMOAbstractView &parent) override
Returns an own parameter window.
GUITrafficLightLogicWrapper(MSTLLogicControl &control, MSTrafficLightLogic &tll)
Constructor.
GUIGLObjectPopupMenu * getPopUpMenu(GUIMainWindow &app, GUISUMOAbstractView &parent) override
Returns an own popup-menu.
void drawGL(const GUIVisualizationSettings &s) const override
Draws the object.
Boundary getCenteringBoundary() const override
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.
Representation of a lane in the micro simulation.
Definition MSLane.h:84
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:187
A traffic lights logic which represents a tls in an off-mode.
static const SUMOTime UNSPECIFIED_DURATION
const std::string & getName() const
const std::string & getState() const
Returns the state within this phase.
A signal for rails.
std::string getBlockingVehicleIDs() const
std::string getRequestedDriveWay() const
std::string getPriorityVehicleIDs() const
std::string getBlockingDriveWayIDs() 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
MSTrafficLightLogic * getActive() 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.
std::vector< LaneVector > LaneVectorVector
Definition of a list that holds lists of lanes that do have the same attribute.
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
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.
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
virtual SUMOTime getLatestEnd(int step=-1) const
const std::string & getProgramID() const
Returns this tl-logic's id.
TrafficLightType getLogicType() const
Returns the type of the logic.
virtual SUMOTime getEarliestEnd(int step=-1) const
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
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.
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:52
double y() const
Returns the y-position.
Definition Position.h:57