SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GUITriggeredRerouter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Reroutes vehicles passing an edge (gui version)
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifdef _WIN32
34 #include <windows.h>
35 #endif
36 
37 #include <GL/gl.h>
38 
39 #include <string>
42 #include <utils/geom/Line.h>
43 #include <utils/geom/Boundary.h>
44 #include <utils/gui/div/GLHelper.h>
45 #include <utils/common/ToString.h>
46 #include <utils/common/Command.h>
47 #include <microsim/MSNet.h>
48 #include <microsim/MSLane.h>
49 #include <microsim/MSEdge.h>
50 #include <guisim/GUINet.h>
51 #include <guisim/GUIEdge.h>
52 #include "GUITriggeredRerouter.h"
55 #include <gui/GUIGlobals.h>
62 
63 #ifdef CHECK_MEMORY_LEAKS
64 #include <foreign/nvwa/debug_new.h>
65 #endif // CHECK_MEMORY_LEAKS
66 
67 
68 // ===========================================================================
69 // FOX callback mapping
70 // ===========================================================================
71 /* -------------------------------------------------------------------------
72  * GUITriggeredRerouter::GUITriggeredRerouterPopupMenu - mapping
73  * ----------------------------------------------------------------------- */
77 
78 };
79 
80 // Object implementation
82 
83 
84 /* -------------------------------------------------------------------------
85  * GUITriggeredRerouter::GUIManip_TriggeredRerouter - mapping
86  * ----------------------------------------------------------------------- */
87 FXDEFMAP(GUITriggeredRerouter::GUIManip_TriggeredRerouter) GUIManip_TriggeredRerouterMap[] = {
92 };
93 
94 FXIMPLEMENT(GUITriggeredRerouter::GUIManip_TriggeredRerouter, GUIManipulator, GUIManip_TriggeredRerouterMap, ARRAYNUMBER(GUIManip_TriggeredRerouterMap))
95 
96 
97 // ===========================================================================
98 // method definitions
99 // ===========================================================================
100 /* -------------------------------------------------------------------------
101  * GUITriggeredRerouter::GUIManip_TriggeredRerouter - methods
102  * ----------------------------------------------------------------------- */
104  GUIMainWindow& app,
105  const std::string& name, GUITriggeredRerouter& o,
106  int /*xpos*/, int /*ypos*/)
107  : GUIManipulator(app, name, 0, 0), myParent(&app),
108  myChosenValue(0), myChosenTarget(myChosenValue, NULL, MID_OPTION),
109  myUsageProbability(o.getProbability()), myUsageProbabilityTarget(myUsageProbability),
110  myObject(&o) {
111  myChosenTarget.setTarget(this);
112  FXVerticalFrame* f1 =
113  new FXVerticalFrame(this, LAYOUT_FILL_X | LAYOUT_FILL_Y, 0, 0, 0, 0, 0, 0, 0, 0);
114 
115  FXGroupBox* gp = new FXGroupBox(f1, "Change Probability",
116  GROUPBOX_TITLE_LEFT | FRAME_SUNKEN | FRAME_RIDGE,
117  0, 0, 0, 0, 4, 4, 1, 1, 2, 0);
118  {
119  // default
120  FXHorizontalFrame* gf1 =
121  new FXHorizontalFrame(gp, LAYOUT_TOP | LAYOUT_LEFT, 0, 0, 0, 0, 10, 10, 5, 5);
122  new FXRadioButton(gf1, "Default", &myChosenTarget, FXDataTarget::ID_OPTION + 0,
123  ICON_BEFORE_TEXT | LAYOUT_SIDE_TOP,
124  0, 0, 0, 0, 2, 2, 0, 0);
125  }
126  {
127  // free
128  FXHorizontalFrame* gf12 =
129  new FXHorizontalFrame(gp, LAYOUT_TOP | LAYOUT_LEFT, 0, 0, 0, 0, 10, 10, 5, 5);
130  new FXRadioButton(gf12, "User Given: ", &myChosenTarget, FXDataTarget::ID_OPTION + 1,
131  ICON_BEFORE_TEXT | LAYOUT_SIDE_TOP | LAYOUT_CENTER_Y,
132  0, 0, 0, 0, 2, 2, 0, 0);
133  myUsageProbabilityDial =
134  new FXRealSpinDial(gf12, 10, this, MID_USER_DEF,
135  LAYOUT_TOP | FRAME_SUNKEN | FRAME_THICK);
136  myUsageProbabilityDial->setFormatString("%.2f");
137  myUsageProbabilityDial->setIncrements(.1, .1, .1);
138  myUsageProbabilityDial->setRange(0, 1);
139  myUsageProbabilityDial->setValue(myObject->getUserProbability());
140  }
141  {
142  // off
143  FXHorizontalFrame* gf13 =
144  new FXHorizontalFrame(gp, LAYOUT_TOP | LAYOUT_LEFT, 0, 0, 0, 0, 10, 10, 5, 5);
145  new FXRadioButton(gf13, "Off", &myChosenTarget, FXDataTarget::ID_OPTION + 2,
146  ICON_BEFORE_TEXT | LAYOUT_SIDE_TOP,
147  0, 0, 0, 0, 2, 2, 0, 0);
148  }
149  myChosenValue = myObject->inUserMode()
150  ? myObject->getUserProbability() > 0
151  ? 1 : 2
152  : 0;
153  new FXButton(f1, "Close", NULL, this, MID_CLOSE,
154  BUTTON_INITIAL | BUTTON_DEFAULT | FRAME_RAISED | FRAME_THICK | LAYOUT_TOP | LAYOUT_LEFT | LAYOUT_CENTER_X, 0, 0, 0, 0, 30, 30, 4, 4);
155 }
156 
157 
159 
160 
161 long
163  destroy();
164  return 1;
165 }
166 
167 
168 long
170  myUsageProbability = (SUMOReal)(myUsageProbabilityDial->getValue());
171  static_cast<GUITriggeredRerouter*>(myObject)->setUserUsageProbability(myUsageProbability);
172  static_cast<GUITriggeredRerouter*>(myObject)->setUserMode(true);
173  myParent->updateChildren();
174  return 1;
175 }
176 
177 
178 long
180  sender->handle(this,
181  myChosenValue != 1 ? FXSEL(SEL_COMMAND, ID_DISABLE) : FXSEL(SEL_COMMAND, ID_ENABLE),
182  ptr);
183  myParent->updateChildren();
184  return 1;
185 }
186 
187 
188 long
190  static_cast<GUITriggeredRerouter*>(myObject)->setUserUsageProbability(myUsageProbability);
191  switch (myChosenValue) {
192  case 0:
193  static_cast<GUITriggeredRerouter*>(myObject)->setUserMode(false);
194  break;
195  case 1:
196  static_cast<GUITriggeredRerouter*>(myObject)->setUserMode(true);
197  break;
198  case 2:
199  static_cast<GUITriggeredRerouter*>(myObject)->setUserUsageProbability(0);
200  static_cast<GUITriggeredRerouter*>(myObject)->setUserMode(true);
201  break;
202  default:
203  throw 1;
204  }
205  myParent->updateChildren();
206  return 1;
207 }
208 
209 
210 /* -------------------------------------------------------------------------
211  * GUITriggeredRerouter::GUITriggeredRerouterPopupMenu - methods
212  * ----------------------------------------------------------------------- */
214  GUIMainWindow& app, GUISUMOAbstractView& parent,
215  GUIGlObject& o)
216  : GUIGLObjectPopupMenu(app, parent, o) {}
217 
218 
220 
221 
222 long
224  FXSelector,
225  void*) {
226  static_cast<GUITriggeredRerouter*>(myObject)->openManipulator(
227  *myApplication, *myParent);
228  return 1;
229 }
230 
231 
232 /* -------------------------------------------------------------------------
233  * GUITriggeredRerouter - methods
234  * ----------------------------------------------------------------------- */
236  const std::string& id,
237  const std::vector<MSEdge*> &edges,
238  SUMOReal prob, const std::string& aXMLFilename, bool off)
239  : MSTriggeredRerouter(id, edges, prob, aXMLFilename, off),
240  GUIGlObject_AbstractAdd("rerouter", GLO_TRIGGER, id) {
241  size_t k;
242  size_t no = 0;
243  for (k = 0; k < edges.size(); k++) {
244  GUIEdge* gedge = static_cast<GUIEdge*>(edges[k]);
245  no += gedge->getLanes().size();
246  }
247  myFGPositions.reserve(no);
248  myFGRotations.reserve(no);
249  for (k = 0; k < edges.size(); k++) {
250  GUIEdge* gedge = static_cast<GUIEdge*>(edges[k]);
251  const std::vector<MSLane*> &lanes = gedge->getLanes();
252  size_t noLanes = lanes.size();
253  for (size_t i = 0; i < noLanes; ++i) {
254  const PositionVector& v = gedge->getLaneGeometry((size_t) i).getShape();
255  SUMOReal pos = v.length() - (SUMOReal) 6.;
256  myFGPositions.push_back(v.positionAtLengthPosition(pos));
258  Line l(v.getBegin(), v.getEnd());
260  }
261  }
262 }
263 
264 
266 
267 
270  GUISUMOAbstractView& parent) {
271  GUIGLObjectPopupMenu* ret = new GUITriggeredRerouterPopupMenu(app, parent, *this);
272  buildPopupHeader(ret, app);
274  buildShowManipulatorPopupEntry(ret, false);
277  buildPositionCopyEntry(ret, false);
278  return ret;
279 }
280 
281 
285  return 0;
286 }
287 
288 
289 void
291  glPushName(getGlID());
292  for (size_t i = 0; i < myFGPositions.size(); ++i) {
293  const Position& pos = myFGPositions[i];
294  SUMOReal rot = myFGRotations[i];
295  glPushMatrix();
296  glScaled(s.addExaggeration, s.addExaggeration, 1);
297  glTranslated(pos.x(), pos.y(), 0);
298  glRotated(rot, 0, 0, 1);
299  glTranslated(0, 0, getType());
300  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
301 
302  glBegin(GL_TRIANGLES);
303  glColor3d(1, .8f, 0);
304  // base
305  glVertex2d(0 - 1.4, 0);
306  glVertex2d(0 - 1.4, 6);
307  glVertex2d(0 + 1.4, 6);
308  glVertex2d(0 + 1.4, 0);
309  glVertex2d(0 - 1.4, 0);
310  glVertex2d(0 + 1.4, 6);
311  glEnd();
312 
313  glTranslated(0, 0, .1);
314  glColor3d(0, 0, 0);
315  pfSetPosition(0, 0);
316  pfSetScale(3.f);
317  SUMOReal w = pfdkGetStringWidth("U");
318  glRotated(180, 0, 1, 0);
319  glTranslated(-w / 2., 2, 0);
320  pfDrawString("U");
321 
322  glTranslated(w / 2., -2, 0);
324  prob *= 100.;
325  prob = (SUMOReal)((int) prob);
326  std::string str = toString(prob) + "%";
327  pfSetPosition(0, 0);
328  pfSetScale(.7f);
329  w = pfdkGetStringWidth(str.c_str());
330  glTranslated(-w / 2., 4, 0);
331  pfDrawString(str.c_str());
332  glPopMatrix();
333  }
334  if (hasCurrentReroute(MSNet::getInstance()->getCurrentTimeStep()) && getProbability() > 0) {
335  const RerouteInterval& ri =
336  getCurrentReroute(MSNet::getInstance()->getCurrentTimeStep());
337  for (std::vector<MSEdge*>::const_iterator i = ri.closed.begin(); i != ri.closed.end(); ++i) {
338  GUIEdge* gedge = static_cast<GUIEdge*>(*i);
339  const std::vector<MSLane*> &lanes = gedge->getLanes();
340  size_t noLanes = lanes.size();
341  SUMOReal prob = getProbability() * 360;
342  for (size_t j = 0; j < noLanes; ++j) {
343  const PositionVector& v = gedge->getLaneGeometry((size_t) j).getShape();
344  SUMOReal d = 3.;
347 
348  glPushMatrix();
349  glTranslated(pos.x(), pos.y(), 0);
350  glRotated(rot, 0, 0, 1);
351  glTranslated(0, -1.5, 0);
352 
353  int noPoints = 9;
354  if (s.scale > 25) {
355  noPoints = (int)(9.0 + s.scale / 10.0);
356  if (noPoints > 36) {
357  noPoints = 36;
358  }
359  }
360  glTranslated(0, 0, getType());
361  glColor3d(0.7, 0, 0);
362  GLHelper::drawFilledCircle((SUMOReal) 1.3, noPoints);
363  glTranslated(0, 0, .1);
364  glColor3d(1, 0, 0);
365  GLHelper::drawFilledCircle((SUMOReal) 1.3, noPoints, 0, prob);
366  glTranslated(0, 0, .1);
367  glColor3d(1, 1, 1);
368  glRotated(-90, 0, 0, 1);
369  glBegin(GL_TRIANGLES);
370  glVertex2d(0 - .3, -1.);
371  glVertex2d(0 - .3, 1.);
372  glVertex2d(0 + .3, 1.);
373  glVertex2d(0 + .3, -1.);
374  glVertex2d(0 - .3, -1.);
375  glVertex2d(0 + .3, 1.);
376  glEnd();
377  glPopMatrix();
378  }
379  }
380  }
381  glPopName();
382 }
383 
384 
385 Boundary
387  Boundary b(myBoundary);
388  b.grow(20);
389  return b;
390 }
391 
392 
393 
398  new GUIManip_TriggeredRerouter(app, getFullName(), *this, 0, 0);
399  gui->create();
400  gui->show();
401  return gui;
402 }
403 
404 
405 
406 /****************************************************************************/
407