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.sourceforge.net/
13 // Copyright (C) 2001-2012 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 #ifdef WIN32
35 #include <windows.h>
36 #endif
37 
38 #include <GL/gl.h>
39 
40 #include <cassert>
45 #include <utils/gui/div/GLHelper.h>
48 #include <microsim/MSLane.h>
55 #include <gui/GUIGlobals.h>
57 #include "GUINet.h"
58 
59 #ifdef CHECK_MEMORY_LEAKS
60 #include <foreign/nvwa/debug_new.h>
61 #endif // CHECK_MEMORY_LEAKS
62 
63 
64 // ===========================================================================
65 // FOX callback mapping
66 // ===========================================================================
73 };
74 
75 // Object implementation
77 
78 
79 // ===========================================================================
80 // method definitions
81 // ===========================================================================
82 /* -------------------------------------------------------------------------
83  * GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapperPopupMenu - methods
84  * ----------------------------------------------------------------------- */
86  GUIMainWindow& app, GUISUMOAbstractView& parent,
87  GUIGlObject& o)
88  : GUIGLObjectPopupMenu(app, parent, o) {}
89 
90 
92 
93 
94 
95 long
97  FXObject*, FXSelector, void*) {
98  assert(myObject->getType() == GLO_TLLOGIC);
99  static_cast<GUITrafficLightLogicWrapper*>(myObject)->begin2TrackPhases();
100  return 1;
101 }
102 
103 
104 long
106  FXObject*, FXSelector, void*) {
107  assert(myObject->getType() == GLO_TLLOGIC);
108  static_cast<GUITrafficLightLogicWrapper*>(myObject)->showPhases();
109  return 1;
110 }
111 
112 
113 long
115  FXObject*, FXSelector /*sel*/, void*) {
116  assert(myObject->getType() == GLO_TLLOGIC);
117  static_cast<GUITrafficLightLogicWrapper*>(myObject)->switchTLSLogic(-1);
118  return 1;
119 }
120 
121 
122 long
124  FXObject*, FXSelector sel, void*) {
125  assert(myObject->getType() == GLO_TLLOGIC);
126  static_cast<GUITrafficLightLogicWrapper*>(myObject)->switchTLSLogic(FXSELID(sel) - MID_SWITCH);
127  return 1;
128 }
129 
130 
131 
132 /* -------------------------------------------------------------------------
133  * GUITrafficLightLogicWrapper - methods
134  * ----------------------------------------------------------------------- */
136  MSTLLogicControl& control, MSTrafficLightLogic& tll) :
137  GUIGlObject(GLO_TLLOGIC, tll.getID()),
138  myTLLogicControl(control), myTLLogic(tll) {}
139 
140 
142 
143 
146  GUISUMOAbstractView& parent) {
147  myApp = &app;
148  GUIGLObjectPopupMenu* ret = new GUITrafficLightLogicWrapperPopupMenu(app, parent, *this);
149  buildPopupHeader(ret, app);
151  //
153  std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
154  if (logics.size() > 1) {
155  std::vector<MSTrafficLightLogic*>::const_iterator i;
156  size_t index = 0;
157  for (i = logics.begin(); i != logics.end(); ++i, ++index) {
158  if (!vars.isActive(*i)) {
159  new FXMenuCommand(ret, ("Switch to '" + (*i)->getProgramID() + "'").c_str(),
160  GUIIconSubSys::getIcon(ICON_FLAG_MINUS), ret, (FXSelector)(MID_SWITCH + index));
161  }
162  }
163  new FXMenuSeparator(ret);
164  }
166  new FXMenuCommand(ret, "Track Phases", 0, ret, MID_TRACKPHASES);
167  new FXMenuCommand(ret, "Show Phases", 0, ret, MID_SHOWPHASES);
168  new FXMenuSeparator(ret);
169  //
172  buildPositionCopyEntry(ret, false);
173  return ret;
174 }
175 
176 
177 void
181  new FuncBinding_StringParam<MSTLLogicControl, std::pair<SUMOTime, MSPhaseDefinition> >
183  window->create();
184  window->show();
185 }
186 
187 
188 void
192  static_cast<MSSimpleTrafficLightLogic&>(myTLLogic).getPhases());
193  window->setBeginTime(0);
194  window->create();
195  window->show();
196 }
197 
198 
202  return 0;
203 }
204 
205 
206 Boundary
208  Boundary ret;
210  for (MSTrafficLightLogic::LaneVectorVector::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
211  const MSTrafficLightLogic::LaneVector& lanes2 = (*i);
212  for (MSTrafficLightLogic::LaneVector::const_iterator j = lanes2.begin(); j != lanes2.end(); ++j) {
213  ret.add((*j)->getShape()[-1]);
214  }
215  }
216  ret.grow(20);
217  return ret;
218 }
219 
220 
221 void
223  if (to == -1) {
227  } else {
229  std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
230  myTLLogicControl.switchTo(myTLLogic.getID(), logics[to]->getProgramID());
231  }
232 }
233 
234 
235 int
237  return myTLLogic.getLinkIndex(link);
238 }
239 
240 
241 void
243  if (s.gaming) {
244  if (!MSNet::getInstance()->getTLSControl().isActive(&myTLLogic)) {
245  return;
246  };
247  const std::string& curState = myTLLogic.getCurrentPhaseDef().getState();
248  if (curState.find_first_of("gG") == std::string::npos) {
249  // no link is 'green' at the moment. find those that turn green next
251  unsigned int curPhaseIdx = myTLLogic.getCurrentPhaseIndex();
252  unsigned int phaseIdx = (curPhaseIdx + 1) % phases.size();
253  std::vector<unsigned int> nextGreen;
254  while (phaseIdx != curPhaseIdx) {
255  const std::string& state = phases[phaseIdx]->getState();
256  for (unsigned int linkIdx = 0; linkIdx < state.size(); linkIdx++) {
257  if ((LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MINOR ||
258  (LinkState)state[linkIdx] == LINKSTATE_TL_GREEN_MAJOR) {
259  nextGreen.push_back(linkIdx);
260  }
261  }
262  if (nextGreen.size() > 0) {
263  break;
264  }
265  phaseIdx = (phaseIdx + 1) % phases.size();
266  }
267  // highlight nextGreen links
268  for (std::vector<unsigned int>::iterator it_idx = nextGreen.begin(); it_idx != nextGreen.end(); it_idx++) {
269  const MSTrafficLightLogic::LaneVector& lanes = myTLLogic.getLanesAt(*it_idx);
270  for (MSTrafficLightLogic::LaneVector::const_iterator it_lane = lanes.begin(); it_lane != lanes.end(); it_lane++) {
271  glPushMatrix();
272  glColor3d(0, 1, 0);
273  Position pos = (*it_lane)->getShape().getEnd();
274  glTranslated(pos.x(), pos.y(), GLO_MAX);
275  GLHelper::drawFilledCircle((*it_lane)->getWidth() / 2.);
276  glPopMatrix();
277  }
278  }
279  }
280  }
281 }
282 
283 
284 /****************************************************************************/
285