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>
61 #include <gui/GUIGlobals.h>
62 #include "GUIVehicle.h"
63 #include "GUINet.h"
64 #include "GUIShapeContainer.h"
65 
66 #ifdef CHECK_MEMORY_LEAKS
67 #include <foreign/nvwa/debug_new.h>
68 #endif // CHECK_MEMORY_LEAKS
69 
70 
71 // ===========================================================================
72 // definition of static variables used for visualisation of objects' values
73 // ===========================================================================
74 template std::vector< GLObjectValuePassConnector<SUMOReal>* > GLObjectValuePassConnector<SUMOReal>::myContainer;
76 
77 template std::vector< GLObjectValuePassConnector<std::pair<int, class MSPhaseDefinition> >* > GLObjectValuePassConnector<std::pair<int, class MSPhaseDefinition> >::myContainer;
79 
80 
81 // ===========================================================================
82 // member method definitions
83 // ===========================================================================
84 GUINet::GUINet(MSVehicleControl* vc, MSEventControl* beginOfTimestepEvents,
85  MSEventControl* endOfTimestepEvents, MSEventControl* insertionEvents) :
86  MSNet(vc, beginOfTimestepEvents, endOfTimestepEvents, insertionEvents, new GUIShapeContainer(myGrid)),
88  myLastSimDuration(0), /*myLastVisDuration(0),*/ myLastIdleDuration(0),
89  myLastVehicleMovementCount(0), myOverallVehicleCount(0), myOverallSimDuration(0) {
91 }
92 
93 
95  // delete allocated wrappers
96  // of junctions
97  for (std::vector<GUIJunctionWrapper*>::iterator i1 = myJunctionWrapper.begin(); i1 != myJunctionWrapper.end(); i1++) {
98  delete(*i1);
99  }
100  // of additional structures
102  // of tl-logics
103  for (Logics2WrapperMap::iterator i3 = myLogics2Wrapper.begin(); i3 != myLogics2Wrapper.end(); i3++) {
104  delete(*i3).second;
105  }
106  // of detectors
107  for (std::vector<GUIDetectorWrapper*>::iterator i = myDetectorDict.begin(); i != myDetectorDict.end(); ++i) {
108  delete *i;
109  }
110 }
111 
112 
113 const Boundary&
115  return myBoundary;
116 }
117 
118 
119 
120 void
122  // get the list of loaded tl-logics
123  const std::vector<MSTrafficLightLogic*> &logics = getTLSControl().getAllLogics();
124  // allocate storage for the wrappers
125  myTLLogicWrappers.reserve(logics.size());
126  // go through the logics
127  for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
128  createTLWrapper(*i);
129  }
130 }
131 
132 
133 GUIGlID
135  if (myLogics2Wrapper.count(tll) > 0) {
136  return myLogics2Wrapper[tll]->getGlID();
137  }
138  // get the links
139  const MSTrafficLightLogic::LinkVectorVector& links = tll->getLinks();
140  if (links.size() == 0) { // @legacy this should never happen in 0.13.0+ networks
141  return 0;
142  }
143  // build the wrapper
146  // build the association link->wrapper
147  MSTrafficLightLogic::LinkVectorVector::const_iterator j;
148  for (j = links.begin(); j != links.end(); j++) {
149  MSTrafficLightLogic::LinkVector::const_iterator j2;
150  for (j2 = (*j).begin(); j2 != (*j).end(); j2++) {
151  myLinks2Logic[*j2] = tll->getID();
152  }
153  }
155  myLogics2Wrapper[tll] = tllw;
156  return tllw->getGlID();
157 }
158 
159 
160 Position
161 GUINet::getJunctionPosition(const std::string& name) const {
162  // !!! no check for existance!
163  return myJunctions->get(name)->getPosition();
164 }
165 
166 
167 bool
168 GUINet::vehicleExists(const std::string& name) const {
169  return myVehicleControl->getVehicle(name) != 0;
170 }
171 
172 
173 Boundary
174 GUINet::getEdgeBoundary(const std::string& name) const {
175  GUIEdge* edge = static_cast<GUIEdge*>(MSEdge::dictionary(name));
176  return edge->getBoundary();
177 }
178 
179 
180 unsigned int
182  if (myLinks2Logic.count(link) == 0) {
183  assert(false);
184  return 0;
185  }
186  MSTrafficLightLogic* tll = myLogics->getActive(myLinks2Logic.find(link)->second);
187  if (myLogics2Wrapper.count(tll) == 0) {
188  // tll may have been added via traci. @see ticket #459
189  return 0;
190  }
191  return myLogics2Wrapper.find(tll)->second->getGlID();
192 }
193 
194 
195 int
197  Links2LogicMap::const_iterator i = myLinks2Logic.find(link);
198  if (i == myLinks2Logic.end()) {
199  return -1;
200  }
201  if (myLogics2Wrapper.find(myLogics->getActive((*i).second)) == myLogics2Wrapper.end()) {
202  return -1;
203  }
204  return myLogics2Wrapper.find(myLogics->getActive((*i).second))->second->getLinkIndex(link);
205 }
206 
207 
208 void
212 }
213 
214 
215 std::vector<GUIGlID>
216 GUINet::getJunctionIDs(bool includeInternal) const {
217  std::vector<GUIGlID> ret;
218  for (std::vector<GUIJunctionWrapper*>::const_iterator i = myJunctionWrapper.begin(); i != myJunctionWrapper.end(); ++i) {
219  if (!(*i)->isInner() || includeInternal) {
220  ret.push_back((*i)->getGlID());
221  }
222  }
223  return ret;
224 }
225 
226 
227 std::vector<GUIGlID>
229  std::vector<GUIGlID> ret;
230  std::vector<std::string> ids;
231  for (std::map<MSTrafficLightLogic*, GUITrafficLightLogicWrapper*>::const_iterator i = myLogics2Wrapper.begin(); i != myLogics2Wrapper.end(); ++i) {
232  std::string sid = (*i).second->getMicrosimID();
233  if (find(ids.begin(), ids.end(), sid) == ids.end()) {
234  ret.push_back((*i).second->getGlID());
235  ids.push_back(sid);
236  }
237  }
238  return ret;
239 }
240 
241 
242 void
244  // initialise detector storage for gui
245  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::const_iterator i = myDetectorControl->myDetectors.begin(); i != myDetectorControl->myDetectors.end(); ++i) {
246  const std::map<std::string, MSDetectorFileOutput*> &dets = myDetectorControl->getTypedDetectors((*i).first).getMyMap();
247  for (std::map<std::string, MSDetectorFileOutput*>::const_iterator j = dets.begin(); j != dets.end(); ++j) {
248  GUIDetectorWrapper* wrapper = (*j).second->buildDetectorGUIRepresentation();
249  if (wrapper != 0) {
250  myDetectorDict.push_back(wrapper);
251  myGrid.addAdditionalGLObject(wrapper);
252  }
253  }
254  }
255  // initialise the tl-map
256  initTLMap();
257  // initialise edge storage for gui
259  // initialise junction storage for gui
260  size_t size = myJunctions->size();
261  myJunctionWrapper.reserve(size);
262  const std::map<std::string, MSJunction*> &junctions = myJunctions->getMyMap();
263  for (std::map<std::string, MSJunction*>::const_iterator i = junctions.begin(); i != junctions.end(); ++i) {
264  myJunctionWrapper.push_back(new GUIJunctionWrapper(*(*i).second));
265  }
266  // build the visualization tree
267  float* cmin = new float[2];
268  float* cmax = new float[2];
269  for (std::vector<GUIEdge*>::iterator i = myEdgeWrapper.begin(); i != myEdgeWrapper.end(); ++i) {
270  GUIEdge* edge = *i;
271  Boundary b;
272  const std::vector<MSLane*> &lanes = edge->getLanes();
273  for (std::vector<MSLane*>::const_iterator j = lanes.begin(); j != lanes.end(); ++j) {
274  b.add((*j)->getShape().getBoxBoundary());
275  }
276  b.grow(2.);
277  cmin[0] = b.xmin();
278  cmin[1] = b.ymin();
279  cmax[0] = b.xmax();
280  cmax[1] = b.ymax();
281  myGrid.Insert(cmin, cmax, edge);
282  myBoundary.add(b);
283  if (myBoundary.getWidth() > 10e16 || myBoundary.getHeight() > 10e16) {
284  throw ProcessError("Network size exceeds 1 Lightyear. Please reconsider your inputs.\n");
285  }
286  }
287  for (std::vector<GUIJunctionWrapper*>::iterator i = myJunctionWrapper.begin(); i != myJunctionWrapper.end(); ++i) {
288  GUIJunctionWrapper* junction = *i;
289  Boundary b = junction->getBoundary();
290  b.grow(2.);
291  cmin[0] = b.xmin();
292  cmin[1] = b.ymin();
293  cmax[0] = b.xmax();
294  cmax[1] = b.ymax();
295  myGrid.Insert(cmin, cmax, junction);
296  myBoundary.add(b);
297  }
298  delete[] cmin;
299  delete[] cmax;
301 }
302 
303 
304 unsigned int
306  return myLastSimDuration +/*myLastVisDuration+*/myLastIdleDuration;
307 }
308 
309 
310 unsigned int
312  return myLastSimDuration;
313 }
314 
315 /*
316 int
317 GUINet::getVisDuration() const
318 {
319  return myLastVisDuration;
320 }
321 */
322 
323 
324 SUMOReal
326  if (myLastSimDuration == 0) {
327  return -1;
328  }
329  return (SUMOReal) 1000. / (SUMOReal) myLastSimDuration;
330 }
331 
332 
333 SUMOReal
334 GUINet::getUPS() const {
335  if (myLastSimDuration == 0) {
336  return -1;
337  }
339 }
340 
341 
342 SUMOReal
343 GUINet::getMeanRTFactor(int duration) const {
344  if (myOverallSimDuration == 0) {
345  return -1;
346  }
347  return ((SUMOReal)(duration) * (SUMOReal) 1000. / (SUMOReal)myOverallSimDuration);
348 }
349 
350 
351 SUMOReal
353  if (myOverallSimDuration == 0) {
354  return -1;
355  }
357 }
358 
359 
360 unsigned int
362  return myLastIdleDuration;
363 }
364 
365 
366 void
368  myLastSimDuration = val;
369  myOverallSimDuration += val;
372 }
373 
374 /*
375 void
376 GUINet::setVisDuration(int val)
377 {
378  myLastVisDuration = val;
379 }
380 */
381 
382 void
384  myLastIdleDuration = val;
385 }
386 
387 
390  GUISUMOAbstractView& parent) {
391  GUIGLObjectPopupMenu* ret = new GUIGLObjectPopupMenu(app, parent, *this);
392  buildPopupHeader(ret, app);
395  buildPositionCopyEntry(ret, false);
396  return ret;
397 }
398 
399 
404  new GUIParameterTableWindow(app, *this, 13);
405  // add items
406  ret->mkItem("loaded vehicles [#]", true,
408  ret->mkItem("waiting vehicles [#]", true,
410  ret->mkItem("departed vehicles [#]", true,
412  ret->mkItem("running vehicles [#]", true,
414  ret->mkItem("arrived vehicles [#]", true,
416  ret->mkItem("end time [s]", false, OptionsCont::getOptions().getString("end"));
417  ret->mkItem("begin time [s]", false, OptionsCont::getOptions().getString("begin"));
418 // ret->mkItem("time step [s]", true, new FunctionBinding<GUINet, SUMOTime>(this, &GUINet::getCurrentTimeStep));
419  if (logSimulationDuration()) {
420  ret->mkItem("step duration [ms]", true, new FunctionBinding<GUINet, unsigned int>(this, &GUINet::getWholeDuration));
421  ret->mkItem("simulation duration [ms]", true, new FunctionBinding<GUINet, unsigned int>(this, &GUINet::getSimDuration));
422  /*
423  ret->mkItem("visualisation duration [ms]", true,
424  new CastingFunctionBinding<GUINet, SUMOReal, int>(
425  &(getNet()), &GUINet::getVisDuration));
426  */
427  ret->mkItem("idle duration [ms]", true, new FunctionBinding<GUINet, unsigned int>(this, &GUINet::getIdleDuration));
428  ret->mkItem("duration factor []", true, new FunctionBinding<GUINet, SUMOReal>(this, &GUINet::getRTFactor));
429  /*
430  ret->mkItem("mean duration factor []", true,
431  new FuncBinding_IntParam<GUINet, SUMOReal>(
432  &(getNet()), &GUINet::getMeanRTFactor), 1);
433  */
434  ret->mkItem("ups [#]", true, new FunctionBinding<GUINet, SUMOReal>(this, &GUINet::getUPS));
435  ret->mkItem("mean ups [#]", true, new FunctionBinding<GUINet, SUMOReal>(this, &GUINet::getMeanUPS));
436  }
437  // close building
438  ret->closeBuilding();
439  return ret;
440 }
441 
442 
443 void
445 }
446 
447 Boundary
449  return getBoundary();
450 }
451 
452 
453 GUINet*
455  GUINet* net = dynamic_cast<GUINet*>(MSNet::getInstance());
456  if (net != 0) {
457  return net;
458  }
459  throw ProcessError("A gui-network was not yet constructed.");
460 }
461 
462 /****************************************************************************/
463