Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/guisim/GUINet.h
169667 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file GUINet.h
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @date Sept 2002
19
///
20
// A MSNet extended by some values for usage within the gui
21
/****************************************************************************/
22
#pragma once
23
#include <config.h>
24
25
#include <string>
26
#include <utility>
27
#include <microsim/MSNet.h>
28
#include <microsim/devices/MSDevice_Tripinfo.h>
29
#include <utils/geom/Boundary.h>
30
#include <utils/geom/Position.h>
31
#include <utils/xml/SUMOSAXHandler.h>
32
#include <utils/xml/SAXWeightsHandler.h>
33
#include <foreign/rtree/SUMORTree.h>
34
#include <foreign/rtree/LayeredRTree.h>
35
#include <utils/geom/PositionVector.h>
36
#include <utils/gui/globjects/GUIGlObjectStorage.h>
37
#include <utils/gui/globjects/GUIGLObjectPopupMenu.h>
38
#include <utils/gui/globjects/GUIGlObject.h>
39
#include <utils/gui/globjects/GUIGlObject_AbstractAdd.h>
40
41
42
// ===========================================================================
43
// class declarations
44
// ===========================================================================
45
class MSEdgeControl;
46
class MSJunctionControl;
47
class MSTLLogicControl;
48
class MSTrafficLightLogic;
49
class MSLink;
50
class GUIJunctionWrapper;
51
class GUIDetectorWrapper;
52
class GUICalibrator;
53
class GUITrafficLightLogicWrapper;
54
class RGBColor;
55
class GUIEdge;
56
class OutputDevice;
57
class GUIVehicle;
58
class GUIVehicleControl;
59
class MSVehicleControl;
60
class GUIMEVehicleControl;
61
class Command;
62
63
64
// ===========================================================================
65
// class definitions
66
// ===========================================================================
67
/**
68
* @class GUINet
69
* @brief A MSNet extended by some values for usage within the gui
70
*
71
* This gui version of the network allows the retrieval of some more
72
* information than the normal network version does. Due to this, not only
73
* these retrieval, but also some further initialisation methods must have
74
* been implemented. Nonethenless, this class has almost the same functions
75
* as the MSNet-class.
76
*
77
* Some microsimulation items are wrapped in certain classes to allow their
78
* drawing and their enumerated access. This enumeration is realised by
79
* inserting the wrapped items into vectors and is needed to fasten the
80
* network's drawing as only visible items are being drawn.
81
*/
82
class GUINet : public MSNet, public GUIGlObject {
83
84
friend class GUITrafficLightLogicWrapper; // see createTLWrapper
85
86
public:
87
/** @brief Constructor
88
* @param[in] vc The vehicle control to use
89
* @param[in] beginOfTimestepEvents The event control to use for simulation step begin events
90
* @param[in] endOfTimestepEvents The event control to use for simulation step end events
91
* @param[in] insertionEvents The event control to use for insertion events
92
* @exception ProcessError If a network was already constructed
93
*/
94
GUINet(MSVehicleControl* vc, MSEventControl* beginOfTimestepEvents,
95
MSEventControl* endOfTimestepEvents,
96
MSEventControl* insertionEvents);
97
98
99
/// @brief Destructor
100
~GUINet();
101
102
103
/**
104
* @brief Returns whether this is a GUI Net
105
*/
106
bool isGUINet() const override {
107
return true;
108
}
109
110
111
/// @name inherited from GUIGlObject
112
//@{
113
114
/** @brief Returns an own popup-menu
115
*
116
* @param[in] app The application needed to build the popup-menu
117
* @param[in] parent The parent window needed to build the popup-menu
118
* @return The built popup-menu
119
* @see GUIGlObject::getPopUpMenu
120
*/
121
GUIGLObjectPopupMenu* getPopUpMenu(GUIMainWindow& app, GUISUMOAbstractView& parent) override;
122
123
/** @brief Returns an own parameter window
124
*
125
* @param[in] app The application needed to build the parameter window
126
* @param[in] parent The parent window needed to build the parameter window
127
* @return The built parameter window
128
* @see GUIGlObject::getParameterWindow
129
*/
130
GUIParameterTableWindow* getParameterWindow(GUIMainWindow& app, GUISUMOAbstractView& parent) override;
131
132
/** @brief Returns the boundary to which the view shall be centered in order to show the object
133
*
134
* @return The boundary the object is within
135
* @see GUIGlObject::getCenteringBoundary
136
*/
137
Boundary getCenteringBoundary() const override;
138
139
/** @brief Draws the object
140
* @param[in] s The settings for the current view (may influence drawing)
141
* @see GUIGlObject::drawGL
142
*/
143
void drawGL(const GUIVisualizationSettings& s) const override;
144
//@}
145
146
147
/// returns the bounder of the network
148
const Boundary& getBoundary() const;
149
150
/// returns the position of a junction
151
Position getJunctionPosition(const std::string& name) const;
152
153
/// returns the information whether the vehicle still exists
154
bool vehicleExists(const std::string& name) const;
155
156
/// Some further steps needed for gui processing
157
void guiSimulationStep();
158
159
/** @brief Performs a single simulation step (locking the simulation)
160
*/
161
void simulationStep();
162
163
/// @name functions for performance measurements
164
/// @{
165
166
/** @brief Returns the duration of the last step (sim+visualisation+idle) (in ms)
167
* @return How long it took to compute and display the last step
168
*/
169
int getWholeDuration() const;
170
171
172
/** @brief Returns the duration of the last step's simulation part (in ms)
173
* @return How long it took to compute the last step
174
*/
175
int getSimDuration() const;
176
177
178
/// Returns the simulation speed as a factor to real time
179
double getRTFactor() const;
180
181
/// Returns the update per seconds rate
182
double getUPS() const;
183
184
/// Returns the simulation speed as a factor to real time
185
double getMeanRTFactor(int duration) const;
186
187
/// Returns the update per seconds rate
188
double getMeanUPS() const;
189
190
// Returns the duration of the last step's visualisation part (in ms)
191
//int getVisDuration() const;
192
193
/// Returns the duration of the last step's idle part (in ms)
194
int getIdleDuration() const;
195
196
/// Sets the duration of the last step's simulation part
197
void setSimDuration(int val);
198
199
// Sets the duration of the last step's visualisation part
200
//void setVisDuration(int val);
201
202
/// Sets the duration of the last step's idle part
203
void setIdleDuration(int val);
204
//}
205
206
double getAvgRouteLength() const {
207
return MSDevice_Tripinfo::getAvgRouteLength();
208
}
209
double getAvgDuration() const {
210
return MSDevice_Tripinfo::getAvgDuration();
211
}
212
double getAvgWaitingTime() const {
213
return MSDevice_Tripinfo::getAvgWaitingTime();
214
}
215
double getAvgTimeLoss() const {
216
return MSDevice_Tripinfo::getAvgTimeLoss();
217
}
218
double getAvgDepartDelay() const {
219
return MSDevice_Tripinfo::getAvgDepartDelay();
220
}
221
double getAvgTripSpeed() const {
222
return MSDevice_Tripinfo::getAvgDuration() != 0 ? MSDevice_Tripinfo::getAvgRouteLength() / MSDevice_Tripinfo::getAvgDuration() : 0;
223
}
224
double getAvgWalkRouteLength() const {
225
return MSDevice_Tripinfo::getAvgWalkRouteLength();
226
}
227
double getAvgWalkDuration() const {
228
return MSDevice_Tripinfo::getAvgWalkDuration();
229
}
230
double getAvgWalkTimeLoss() const {
231
return MSDevice_Tripinfo::getAvgWalkTimeLoss();
232
}
233
234
/** @brief Returns the person control
235
*
236
* If the person control does not exist, yet, it is created.
237
*
238
* @return The person control
239
* @see MSPersonControl
240
* @see myPersonControl
241
*/
242
MSTransportableControl& getPersonControl() override;
243
244
245
/** @brief Returns the container control
246
*
247
* If the container control does not exist, yet, it is created.
248
*
249
* @return The container control
250
* @see MSContainerControl
251
* @see myContainerControl
252
*/
253
MSTransportableControl& getContainerControl() override;
254
255
256
/** Returns the gl-id of the traffic light that controls the given link
257
* valid only if the link is controlled by a tls */
258
int getLinkTLID(const MSLink* const link) const;
259
260
/** Returns the index of the link within the junction that controls the given link;
261
* Returns -1 if the link is not controlled by a tls */
262
int getLinkTLIndex(const MSLink* const link) const;
263
264
265
/// @name locator-methods
266
//@{
267
268
/* @brief Returns the gl-ids of all junctions within the net
269
* @param[in] includeInternal Whether to include ids of internal junctions
270
*/
271
std::vector<GUIGlID> getJunctionIDs(bool includeInternal) const;
272
273
/// Returns the gl-ids of all traffic light logics within the net
274
std::vector<GUIGlID> getTLSIDs() const;
275
//@}
276
277
278
/// Initialises gui wrappers
279
void initGUIStructures();
280
281
282
/** @brief Returns the RTree used for visualisation speed-up
283
* @return The visualisation speed-up
284
*/
285
SUMORTree& getVisualisationSpeedUp() {
286
return myGrid;
287
}
288
289
290
/** @brief Returns the RTree used for visualisation speed-up
291
* @return The visualisation speed-up
292
*/
293
const SUMORTree& getVisualisationSpeedUp(bool secondary = false) const {
294
return secondary ? myGrid2 : myGrid;
295
}
296
297
/// @brief add object into rtree
298
void registerRenderedObject(GUIGlObject* o);
299
300
/** @brief Returns the vehicle control
301
* @return The vehicle control
302
* @see MSVehicleControl
303
* @see myVehicleControl
304
*/
305
GUIVehicleControl* getGUIVehicleControl();
306
307
/** @brief Returns the vehicle control
308
* @return The vehicle control
309
* @see MSVehicleControl
310
* @see myVehicleControl
311
*/
312
GUIMEVehicleControl* getGUIMEVehicleControl();
313
314
/// @brief retrieve loaded edged weight for the given attribute and the current simulation time
315
double getEdgeData(const MSEdge* edge, const std::string& attr);
316
317
/// @brief retrieve live lane/edge weight for the given meanData id and attribute
318
double getMeanData(const MSLane* lane, const std::string& id, const std::string& attr);
319
320
/// @brief load edgeData from file
321
bool loadEdgeData(const std::string& file);
322
323
/// @brief return list of loaded edgeData attributes
324
std::vector<std::string> getEdgeDataAttrs() const;
325
326
/// @brief return list of loaded edgeData ids (being computed in the current simulation)
327
std::vector<std::string> getMeanDataIDs() const;
328
329
/// @brief return list of available attributes for the given meanData id
330
std::vector<std::string> getMeanDataAttrs(const std::string& meanDataID) const;
331
332
#ifdef HAVE_OSG
333
void updateColor(const GUIVisualizationSettings& s);
334
#endif
335
336
/// @brief grant exclusive access to the simulation state
337
void lock();
338
339
/// @brief release exclusive access to the simulation state
340
void unlock();
341
342
/** @brief Returns the pointer to the unique instance of GUINet (singleton).
343
* @return Pointer to the unique GUINet-instance
344
* @exception ProcessError If a network was not yet constructed
345
*/
346
static GUINet* getGUIInstance();
347
348
/// @brief creates a wrapper for the given logic
349
void createTLWrapper(MSTrafficLightLogic* tll) override;
350
351
/// @brief return wheter the given logic (or rather it's wrapper) is selected in the GUI
352
bool isSelected(const MSTrafficLightLogic* tll) const override;
353
354
/// @brief update view after simulation.loadState
355
void updateGUI() const override;
356
357
/// @brief register custom hotkey action
358
void addHotkey(int key, Command* press, Command* release = nullptr);
359
360
/// @brief flush outputs once the simulation has reached its end
361
void flushOutputsAtEnd();
362
363
virtual bool skipFinalReset() const override {
364
return mySkipFinalReset;
365
}
366
367
private:
368
/// @brief Initialises the tl-logic map and wrappers
369
void initTLMap();
370
371
friend class GUIOSGBuilder;
372
373
protected:
374
/// @brief The visualization speed-up
375
LayeredRTree myGrid;
376
377
/// @brief The visualization speed-up for secondary shapes
378
SUMORTree myGrid2;
379
380
/// @brief The networks boundary
381
Boundary myBoundary;
382
383
/// @brief Wrapped MS-edges
384
std::vector<GUIEdge*> myEdgeWrapper;
385
386
/// @brief Wrapped MS-junctions
387
std::vector<GUIJunctionWrapper*> myJunctionWrapper;
388
389
/// @brief A detector dictionary
390
std::vector<GUIDetectorWrapper*> myDetectorWrapper;
391
392
/// @brief A calibrator dictionary
393
std::vector<GUICalibrator*> myCalibratorWrapper;
394
395
/// @brief Definition of a link-to-logic-id map
396
typedef std::map<const MSLink*, std::string> Links2LogicMap;
397
/// @brief The link-to-logic-id map
398
Links2LogicMap myLinks2Logic;
399
400
401
/// @brief Definition of a traffic light-to-wrapper map
402
typedef std::map<MSTrafficLightLogic*, GUITrafficLightLogicWrapper*> Logics2WrapperMap;
403
/// @brief The traffic light-to-wrapper map
404
Logics2WrapperMap myLogics2Wrapper;
405
406
407
/// @brief The step durations (simulation, /*visualisation, */idle)
408
int myLastSimDuration, /*myLastVisDuration, */myLastIdleDuration;
409
410
long myLastVehicleMovementCount, myOverallVehicleCount;
411
long myOverallSimDuration;
412
413
/// @brief loaded edge data for visualization
414
std::map<std::string, MSEdgeWeightsStorage*> myLoadedEdgeData;
415
416
bool mySkipFinalReset = false;
417
418
/// @brief class for discovering edge attributes
419
class DiscoverAttributes : public SUMOSAXHandler {
420
public:
421
DiscoverAttributes(const std::string& file):
422
SUMOSAXHandler(file), firstIntervalBegin(SUMOTime_MAX), lastIntervalEnd(0), numIntervals(0) {};
423
~DiscoverAttributes() {};
424
void myStartElement(int element, const SUMOSAXAttributes& attrs);
425
std::vector<std::string> getEdgeAttrs();
426
SUMOTime firstIntervalBegin;
427
SUMOTime lastIntervalEnd;
428
int numIntervals;
429
private:
430
std::set<std::string> edgeAttrs;
431
};
432
433
class EdgeFloatTimeLineRetriever_GUI : public SAXWeightsHandler::EdgeFloatTimeLineRetriever {
434
public:
435
/// @brief Constructor
436
EdgeFloatTimeLineRetriever_GUI(MSEdgeWeightsStorage* weightStorage) : myWeightStorage(weightStorage) {}
437
438
/// @brief Destructor
439
~EdgeFloatTimeLineRetriever_GUI() { }
440
441
/** @brief Adds an effort for a given edge and time period
442
*
443
* @param[in] id The id of the object to add a weight for
444
* @param[in] val The effort
445
* @param[in] beg The begin of the interval the weight is valid for
446
* @param[in] end The end of the interval the weight is valid for
447
* @see SAXWeightsHandler::EdgeFloatTimeLineRetriever::addEdgeWeight
448
*/
449
void addEdgeWeight(const std::string& id, double val, double beg, double end) const;
450
void addEdgeRelWeight(const std::string& from, const std::string& to, double val, double beg, double end) const;
451
452
private:
453
/// @brief The storage that edges shall be added to
454
MSEdgeWeightsStorage* myWeightStorage;
455
456
};
457
458
private:
459
/// The mutex used to avoid concurrent updates of the vehicle buffer
460
mutable FXMutex myLock;
461
462
};
463
464