SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
13 // Copyright (C) 2001-2014 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 #ifdef CHECK_MEMORY_LEAKS
55 #include <foreign/nvwa/debug_new.h>
56 #endif // CHECK_MEMORY_LEAKS
57 
58 
59 // ===========================================================================
60 // FOX callback mapping
61 // ===========================================================================
68 };
69 
70 // Object implementation
72 
73 
74 // ===========================================================================
75 // method definitions
76 // ===========================================================================
77 /* -------------------------------------------------------------------------
78  * GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu - methods
79  * ----------------------------------------------------------------------- */
81  GUIMainWindow& app, GUISUMOAbstractView& parent,
82  GUIGlObject& o)
83  : GUIGLObjectPopupMenu(app, parent, o) {}
84 
85 
87 
88 
89 
90 long
92  FXObject*, FXSelector, void*) {
93  assert(myObject->getType() == GLO_TLLOGIC);
94  static_cast<GUITrafficLightLogicWrapper*>(myObject)->begin2TrackPhases();
95  return 1;
96 }
97 
98 
99 long
101  FXObject*, FXSelector, void*) {
102  assert(myObject->getType() == GLO_TLLOGIC);
103  static_cast<GUITrafficLightLogicWrapper*>(myObject)->showPhases();
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(-1);
113  return 1;
114 }
115 
116 
117 long
119  FXObject*, FXSelector sel, void*) {
120  assert(myObject->getType() == GLO_TLLOGIC);
121  static_cast<GUITrafficLightLogicWrapper*>(myObject)->switchTLSLogic(FXSELID(sel) - MID_SWITCH);
122  return 1;
123 }
124 
125 
126 
127 /* -------------------------------------------------------------------------
128  * GUITrafficLightLogicWrapper - methods
129  * ----------------------------------------------------------------------- */
131  MSTLLogicControl& control, MSTrafficLightLogic& tll) :
132  GUIGlObject(GLO_TLLOGIC, tll.getID()),
133  myTLLogicControl(control), myTLLogic(tll) {}
134 
135 
137 
138 
141  GUISUMOAbstractView& parent) {
142  myApp = &app;
143  GUIGLObjectPopupMenu* ret = new GUITrafficLightLogicWrapperPopupMenu(app, parent, *this);
144  buildPopupHeader(ret, app);
146  //
148  std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
149  if (logics.size() > 1) {
150  std::vector<MSTrafficLightLogic*>::const_iterator i;
151  size_t index = 0;
152  for (i = logics.begin(); i != logics.end(); ++i, ++index) {
153  if (!vars.isActive(*i)) {
154  new FXMenuCommand(ret, ("Switch to '" + (*i)->getProgramID() + "'").c_str(),
155  GUIIconSubSys::getIcon(ICON_FLAG_MINUS), ret, (FXSelector)(MID_SWITCH + index));
156  }
157  }
158  new FXMenuSeparator(ret);
159  }
161  new FXMenuCommand(ret, "Track Phases", 0, ret, MID_TRACKPHASES);
162  new FXMenuCommand(ret, "Show Phases", 0, ret, MID_SHOWPHASES);
163  new FXMenuSeparator(ret);
164  //
167  buildPositionCopyEntry(ret, false);
168  return ret;
169 }
170 
171 
172 void
176  new FuncBinding_StringParam<MSTLLogicControl, std::pair<SUMOTime, MSPhaseDefinition> >
178  window->create();
179  window->show();
180 }
181 
182 
183 void
187  static_cast<MSSimpleTrafficLightLogic&>(myTLLogic).getPhases());
188  window->setBeginTime(0);
189  window->create();
190  window->show();
191 }
192 
193 
197  return 0;
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  unsigned int curPhaseIdx = myTLLogic.getCurrentPhaseIndex();
247  unsigned int phaseIdx = (curPhaseIdx + 1) % phases.size();
248  std::vector<unsigned int> nextGreen;
249  while (phaseIdx != curPhaseIdx) {
250  const std::string& state = phases[phaseIdx]->getState();
251  for (unsigned int linkIdx = 0; linkIdx < 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<unsigned 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  glColor3d(0, 1, 0);
268  Position pos = (*it_lane)->getShape().back();
269  glTranslated(pos.x(), pos.y(), GLO_MAX);
270  GLHelper::drawFilledCircle((*it_lane)->getWidth() / 2.);
271  glPopMatrix();
272  }
273  }
274  }
275  }
276 }
277 
278 
279 /****************************************************************************/
280