SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GUINet.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A MSNet extended by some values for usage within the gui
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 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <utility>
33 #include <set>
34 #include <vector>
35 #include <map>
42 #include <utils/common/RGBColor.h>
44 #include <microsim/MSNet.h>
45 #include <microsim/MSJunction.h>
47 #include <microsim/MSEdge.h>
49 #include <microsim/MSVehicle.h>
54 #include <microsim/MSRouteLoader.h>
55 #include <guisim/GUIEdge.h>
62 #include <gui/GUIGlobals.h>
63 #include "GUIVehicle.h"
64 #include "GUINet.h"
65 #include "GUIShapeContainer.h"
66 
67 #ifdef HAVE_INTERNAL
68 #include <mesogui/GUIMEVehicleControl.h>
69 #endif
70 
71 #ifdef CHECK_MEMORY_LEAKS
72 #include <foreign/nvwa/debug_new.h>
73 #endif // CHECK_MEMORY_LEAKS
74 
75 
76 // ===========================================================================
77 // definition of static variables used for visualisation of objects' values
78 // ===========================================================================
79 template std::vector< GLObjectValuePassConnector<SUMOReal>* > GLObjectValuePassConnector<SUMOReal>::myContainer;
81 
82 template std::vector< GLObjectValuePassConnector<std::pair<int, class MSPhaseDefinition> >* > GLObjectValuePassConnector<std::pair<int, class MSPhaseDefinition> >::myContainer;
84 
85 
86 // ===========================================================================
87 // member method definitions
88 // ===========================================================================
89 GUINet::GUINet(MSVehicleControl* vc, MSEventControl* beginOfTimestepEvents,
90  MSEventControl* endOfTimestepEvents, MSEventControl* insertionEvents) :
91  MSNet(vc, beginOfTimestepEvents, endOfTimestepEvents, insertionEvents, new GUIShapeContainer(myGrid)),
93  myLastSimDuration(0), /*myLastVisDuration(0),*/ myLastIdleDuration(0),
94  myLastVehicleMovementCount(0), myOverallVehicleCount(0), myOverallSimDuration(0) {
96 }
97 
98 
100  // delete allocated wrappers
101  // of junctions
102  for (std::vector<GUIJunctionWrapper*>::iterator i1 = myJunctionWrapper.begin(); i1 != myJunctionWrapper.end(); i1++) {
103  delete(*i1);
104  }
105  // of additional structures
107  // of tl-logics
108  for (Logics2WrapperMap::iterator i3 = myLogics2Wrapper.begin(); i3 != myLogics2Wrapper.end(); i3++) {
109  delete(*i3).second;
110  }
111  // of detectors
112  for (std::vector<GUIDetectorWrapper*>::iterator i = myDetectorDict.begin(); i != myDetectorDict.end(); ++i) {
113  delete *i;
114  }
115 }
116 
117 
118 const Boundary&
120  return myBoundary;
121 }
122 
123 
126  if (myPersonControl == 0) {
128  }
129  return *myPersonControl;
130 }
131 
132 
133 void
135  // get the list of loaded tl-logics
136  const std::vector<MSTrafficLightLogic*>& logics = getTLSControl().getAllLogics();
137  // allocate storage for the wrappers
138  myTLLogicWrappers.reserve(logics.size());
139  // go through the logics
140  for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
141  createTLWrapper(*i);
142  }
143 }
144 
145 
146 GUIGlID
148  if (myLogics2Wrapper.count(tll) > 0) {
149  return myLogics2Wrapper[tll]->getGlID();
150  }
151  // get the links
152  const MSTrafficLightLogic::LinkVectorVector& links = tll->getLinks();
153  if (links.size() == 0) { // @legacy this should never happen in 0.13.0+ networks
154  return 0;
155  }
156  // build the wrapper
159  // build the association link->wrapper
160  MSTrafficLightLogic::LinkVectorVector::const_iterator j;
161  for (j = links.begin(); j != links.end(); j++) {
162  MSTrafficLightLogic::LinkVector::const_iterator j2;
163  for (j2 = (*j).begin(); j2 != (*j).end(); j2++) {
164  myLinks2Logic[*j2] = tll->getID();
165  }
166  }
168  myLogics2Wrapper[tll] = tllw;
169  return tllw->getGlID();
170 }
171 
172 
173 Position
174 GUINet::getJunctionPosition(const std::string& name) const {
175  // !!! no check for existance!
176  return myJunctions->get(name)->getPosition();
177 }
178 
179 
180 bool
181 GUINet::vehicleExists(const std::string& name) const {
182  return myVehicleControl->getVehicle(name) != 0;
183 }
184 
185 
186 unsigned int
188  if (myLinks2Logic.count(link) == 0) {
189  assert(false);
190  return 0;
191  }
192  MSTrafficLightLogic* tll = myLogics->getActive(myLinks2Logic.find(link)->second);
193  if (myLogics2Wrapper.count(tll) == 0) {
194  // tll may have been added via traci. @see ticket #459
195  return 0;
196  }
197  return myLogics2Wrapper.find(tll)->second->getGlID();
198 }
199 
200 
201 int
203  Links2LogicMap::const_iterator i = myLinks2Logic.find(link);
204  if (i == myLinks2Logic.end()) {
205  return -1;
206  }
207  if (myLogics2Wrapper.find(myLogics->getActive((*i).second)) == myLogics2Wrapper.end()) {
208  return -1;
209  }
210  return myLogics2Wrapper.find(myLogics->getActive((*i).second))->second->getLinkIndex(link);
211 }
212 
213 
214 void
218 }
219 
220 
221 std::vector<GUIGlID>
222 GUINet::getJunctionIDs(bool includeInternal) const {
223  std::vector<GUIGlID> ret;
224  for (std::vector<GUIJunctionWrapper*>::const_iterator i = myJunctionWrapper.begin(); i != myJunctionWrapper.end(); ++i) {
225  if (!(*i)->isInner() || includeInternal) {
226  ret.push_back((*i)->getGlID());
227  }
228  }
229  return ret;
230 }
231 
232 
233 std::vector<GUIGlID>
235  std::vector<GUIGlID> ret;
236  std::vector<std::string> ids;
237  for (std::map<MSTrafficLightLogic*, GUITrafficLightLogicWrapper*>::const_iterator i = myLogics2Wrapper.begin(); i != myLogics2Wrapper.end(); ++i) {
238  std::string sid = (*i).second->getMicrosimID();
239  if (find(ids.begin(), ids.end(), sid) == ids.end()) {
240  ret.push_back((*i).second->getGlID());
241  ids.push_back(sid);
242  }
243  }
244  return ret;
245 }
246 
247 
248 void
250  // initialise detector storage for gui
251  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::const_iterator i = myDetectorControl->myDetectors.begin(); i != myDetectorControl->myDetectors.end(); ++i) {
252  const std::map<std::string, MSDetectorFileOutput*>& dets = myDetectorControl->getTypedDetectors((*i).first).getMyMap();
253  for (std::map<std::string, MSDetectorFileOutput*>::const_iterator j = dets.begin(); j != dets.end(); ++j) {
254  GUIDetectorWrapper* wrapper = (*j).second->buildDetectorGUIRepresentation();
255  if (wrapper != 0) {
256  myDetectorDict.push_back(wrapper);
257  myGrid.addAdditionalGLObject(wrapper);
258  }
259  }
260  }
261  // initialise the tl-map
262  initTLMap();
263  // initialise edge storage for gui
265  // initialise junction storage for gui
266  size_t size = myJunctions->size();
267  myJunctionWrapper.reserve(size);
268  const std::map<std::string, MSJunction*>& junctions = myJunctions->getMyMap();
269  for (std::map<std::string, MSJunction*>::const_iterator i = junctions.begin(); i != junctions.end(); ++i) {
270  myJunctionWrapper.push_back(new GUIJunctionWrapper(*(*i).second));
271  }
272  // build the visualization tree
273  float* cmin = new float[2];
274  float* cmax = new float[2];
275  for (std::vector<GUIEdge*>::iterator i = myEdgeWrapper.begin(); i != myEdgeWrapper.end(); ++i) {
276  GUIEdge* edge = *i;
277  Boundary b;
278  const std::vector<MSLane*>& lanes = edge->getLanes();
279  for (std::vector<MSLane*>::const_iterator j = lanes.begin(); j != lanes.end(); ++j) {
280  b.add((*j)->getShape().getBoxBoundary());
281  }
282  // make sure persons are always drawn and selectable since they depend on their edge being drawn
284  cmin[0] = b.xmin();
285  cmin[1] = b.ymin();
286  cmax[0] = b.xmax();
287  cmax[1] = b.ymax();
288  myGrid.Insert(cmin, cmax, edge);
289  myBoundary.add(b);
290  if (myBoundary.getWidth() > 10e16 || myBoundary.getHeight() > 10e16) {
291  throw ProcessError("Network size exceeds 1 Lightyear. Please reconsider your inputs.\n");
292  }
293  }
294  for (std::vector<GUIJunctionWrapper*>::iterator i = myJunctionWrapper.begin(); i != myJunctionWrapper.end(); ++i) {
295  GUIJunctionWrapper* junction = *i;
296  Boundary b = junction->getBoundary();
297  b.grow(2.);
298  cmin[0] = b.xmin();
299  cmin[1] = b.ymin();
300  cmax[0] = b.xmax();
301  cmax[1] = b.ymax();
302  myGrid.Insert(cmin, cmax, junction);
303  myBoundary.add(b);
304  }
305  delete[] cmin;
306  delete[] cmax;
308 }
309 
310 
311 unsigned int
313  return myLastSimDuration +/*myLastVisDuration+*/myLastIdleDuration;
314 }
315 
316 
317 unsigned int
319  return myLastSimDuration;
320 }
321 
322 /*
323 int
324 GUINet::getVisDuration() const
325 {
326  return myLastVisDuration;
327 }
328 */
329 
330 
331 SUMOReal
333  if (myLastSimDuration == 0) {
334  return -1;
335  }
336  return (SUMOReal) 1000. / (SUMOReal) myLastSimDuration;
337 }
338 
339 
340 SUMOReal
341 GUINet::getUPS() const {
342  if (myLastSimDuration == 0) {
343  return -1;
344  }
346 }
347 
348 
349 SUMOReal
350 GUINet::getMeanRTFactor(int duration) const {
351  if (myOverallSimDuration == 0) {
352  return -1;
353  }
354  return ((SUMOReal)(duration) * (SUMOReal) 1000. / (SUMOReal)myOverallSimDuration);
355 }
356 
357 
358 SUMOReal
360  if (myOverallSimDuration == 0) {
361  return -1;
362  }
364 }
365 
366 
367 unsigned int
369  return myLastIdleDuration;
370 }
371 
372 
373 void
375  myLastSimDuration = val;
376  myOverallSimDuration += val;
379 }
380 
381 /*
382 void
383 GUINet::setVisDuration(int val)
384 {
385  myLastVisDuration = val;
386 }
387 */
388 
389 void
391  myLastIdleDuration = val;
392 }
393 
394 
397  GUISUMOAbstractView& parent) {
398  GUIGLObjectPopupMenu* ret = new GUIGLObjectPopupMenu(app, parent, *this);
399  buildPopupHeader(ret, app);
402  buildPositionCopyEntry(ret, false);
403  return ret;
404 }
405 
406 
411  new GUIParameterTableWindow(app, *this, 15);
412  // add items
413  ret->mkItem("loaded vehicles [#]", true,
415  ret->mkItem("waiting vehicles [#]", true,
417  ret->mkItem("departed vehicles [#]", true,
419  ret->mkItem("running vehicles [#]", true,
421  ret->mkItem("arrived vehicles [#]", true,
423  ret->mkItem("collisions [#]", true,
425  ret->mkItem("teleports [#]", true,
427  ret->mkItem("end time [s]", false, OptionsCont::getOptions().getString("end"));
428  ret->mkItem("begin time [s]", false, OptionsCont::getOptions().getString("begin"));
429 // ret->mkItem("time step [s]", true, new FunctionBinding<GUINet, SUMOTime>(this, &GUINet::getCurrentTimeStep));
430  if (logSimulationDuration()) {
431  ret->mkItem("step duration [ms]", true, new FunctionBinding<GUINet, unsigned int>(this, &GUINet::getWholeDuration));
432  ret->mkItem("simulation duration [ms]", true, new FunctionBinding<GUINet, unsigned int>(this, &GUINet::getSimDuration));
433  /*
434  ret->mkItem("visualisation duration [ms]", true,
435  new CastingFunctionBinding<GUINet, SUMOReal, int>(
436  &(getNet()), &GUINet::getVisDuration));
437  */
438  ret->mkItem("idle duration [ms]", true, new FunctionBinding<GUINet, unsigned int>(this, &GUINet::getIdleDuration));
439  ret->mkItem("duration factor []", true, new FunctionBinding<GUINet, SUMOReal>(this, &GUINet::getRTFactor));
440  /*
441  ret->mkItem("mean duration factor []", true,
442  new FuncBinding_IntParam<GUINet, SUMOReal>(
443  &(getNet()), &GUINet::getMeanRTFactor), 1);
444  */
445  ret->mkItem("ups [#]", true, new FunctionBinding<GUINet, SUMOReal>(this, &GUINet::getUPS));
446  ret->mkItem("mean ups [#]", true, new FunctionBinding<GUINet, SUMOReal>(this, &GUINet::getMeanUPS));
447  }
448  // close building
449  ret->closeBuilding();
450  return ret;
451 }
452 
453 
454 void
456 }
457 
458 Boundary
460  return getBoundary();
461 }
462 
463 
464 GUINet*
466  GUINet* net = dynamic_cast<GUINet*>(MSNet::getInstance());
467  if (net != 0) {
468  return net;
469  }
470  throw ProcessError("A gui-network was not yet constructed.");
471 }
472 
473 
476  return dynamic_cast<GUIVehicleControl*>(myVehicleControl);
477 }
478 
479 #ifdef HAVE_INTERNAL
480 GUIMEVehicleControl*
481 GUINet::getGUIMEVehicleControl() {
482  return dynamic_cast<GUIMEVehicleControl*>(myVehicleControl);
483 }
484 #endif
485 
486 /****************************************************************************/
487