Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/gui/polygon_dynamics_gui/runner.py
169689 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
4
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file runner.py
16
# @author Michael Behrisch
17
# @author Daniel Krajzewicz
18
# @date 2011-03-04
19
20
21
from __future__ import print_function
22
from __future__ import absolute_import
23
import os
24
import sys
25
26
if "SUMO_HOME" in os.environ:
27
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
28
import traci # noqa
29
import sumolib # noqa
30
31
32
def examine(polygonID):
33
print("# examining", polygonID)
34
print("shape", traci.polygon.getShape(polygonID))
35
print("type", traci.polygon.getType(polygonID))
36
print("color", traci.polygon.getColor(polygonID))
37
print("filled", traci.polygon.getFilled(polygonID))
38
39
40
traci.start([sumolib.checkBinary('sumo-gui'), "-c", "sumo.sumocfg", "-S", "-Q"], stdout=sys.stdout)
41
# traci.init(port=12345) # debug
42
for step in range(3):
43
print("step", step)
44
traci.simulationStep()
45
polygonID = "poly0"
46
print("adding", polygonID)
47
traci.polygon.add(
48
polygonID, ((960, 510), (960, 550), (1000, 550), (1000, 510)), (100, 200, 0, 255), True, "test")
49
50
print("polygons", traci.polygon.getIDList())
51
print("polygon count", traci.polygon.getIDCount())
52
examine(polygonID)
53
54
for step in range(3, 6):
55
print("step", step)
56
traci.simulationStep()
57
58
# Failing specification tests
59
print("# (1) Adding underspecified dynamics...")
60
try:
61
traci.polygon.addDynamics(polygonID)
62
except traci.exceptions.TraCIException:
63
print("Caught TraCIException")
64
pass
65
66
print("# (2) Adding malformed dynamics 1 ...")
67
try:
68
traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 4, 3])
69
except traci.exceptions.TraCIException:
70
print("Caught TraCIException")
71
pass
72
73
print("# (3) Adding malformed dynamics 2 ...")
74
try:
75
traci.polygon.addDynamics(polygonID, "", [1, 2, 3, 4], [200, 20, 2, 1])
76
except traci.exceptions.TraCIException:
77
print("Caught TraCIException")
78
pass
79
80
print("# (4) Adding malformed dynamics 3 ...")
81
try:
82
traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 3], [200, 20, 2])
83
except traci.exceptions.TraCIException:
84
print("Caught TraCIException")
85
pass
86
87
print("# (5) Adding malformed dynamics 4 ...")
88
try:
89
traci.polygon.addDynamics(polygonID, "", [0], [200])
90
except traci.exceptions.TraCIException:
91
print("Caught TraCIException")
92
pass
93
94
print("# (6) Adding malformed dynamics 5 ...")
95
try:
96
traci.polygon.addDynamics(polygonID, "horiz", [], [], True)
97
except traci.exceptions.TraCIException:
98
print("Caught TraCIException")
99
pass
100
101
examine(polygonID)
102
103
for step in range(6, 9):
104
print("step", step)
105
traci.simulationStep()
106
107
# Successfull specifications
108
109
print("# (1) No tracking with alpha animation")
110
traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 5], [0, 200, 100, 0])
111
112
for step in range(9, 12):
113
print("step", step)
114
traci.simulationStep()
115
examine(polygonID)
116
117
print("polygons", traci.polygon.getIDList())
118
print("polygon count", traci.polygon.getIDCount())
119
120
print("vehicles", traci.vehicle.getIDList())
121
print("vehicle count", traci.vehicle.getIDCount())
122
123
for step in range(12, 15):
124
print("step", step)
125
traci.simulationStep()
126
127
print("polygons", traci.polygon.getIDList())
128
print("polygon count", traci.polygon.getIDCount())
129
130
polygonID = "poly1"
131
print("adding", polygonID)
132
traci.polygon.add(
133
polygonID, ((960, 510), (960, 550), (1000, 550), (1000, 510)), (0, 100, 100, 255), True, "test")
134
135
print("polygons", traci.polygon.getIDList())
136
print("polygon count", traci.polygon.getIDCount())
137
138
print("# (2) No tracking with time line but no animation (removal after time line expired)")
139
traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 3])
140
141
for step in range(15, 18):
142
print("step", step)
143
traci.simulationStep()
144
145
print("polygons", traci.polygon.getIDList())
146
print("polygon count", traci.polygon.getIDCount())
147
148
polygonID = "poly2"
149
print("adding", polygonID)
150
traci.polygon.add(
151
polygonID, ((960, 510), (960, 550), (1000, 550), (1000, 510)), (150, 0, 100, 255), True, "test")
152
153
vehID = "veh0"
154
print("adding", vehID)
155
traci.route.add("trip0", ["2fi", "1o"])
156
traci.vehicle.add(vehID, "trip0")
157
158
print("vehicles", traci.vehicle.getIDList())
159
print("vehicle count", traci.vehicle.getIDCount())
160
161
print("# (3) Tracking without time line")
162
traci.polygon.addDynamics(polygonID, vehID)
163
164
for step in range(18, 21):
165
print("step", step)
166
traci.simulationStep()
167
168
print("# (4) Tracking existing vehicle with time line")
169
polygonID = "poly3"
170
vehID = "horiz"
171
traci.polygon.add(
172
polygonID, ((750, 490), (750, 450), (790, 450), (790, 490)), (0, 0, 200, 255), True, "test")
173
traci.polygon.addDynamics(polygonID, vehID, [0, 10])
174
175
print("polygons", traci.polygon.getIDList())
176
print("polygon count", traci.polygon.getIDCount())
177
178
for step in range(21, 25):
179
print("step", step)
180
traci.simulationStep()
181
182
print("# (5) replacing tracking dynamics and highlighting vehicle permanently")
183
polygonID = "poly2"
184
vehID = "veh0"
185
traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20],
186
[50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50])
187
traci.vehicle.highlight(vehID)
188
189
print("polygons", traci.polygon.getIDList())
190
print("polygon count", traci.polygon.getIDCount())
191
192
for step in range(25, 35):
193
print("step", step)
194
traci.simulationStep()
195
196
# "poly3" should have been removed
197
print("polygons", traci.polygon.getIDList())
198
print("polygon count", traci.polygon.getIDCount())
199
200
print("# (6) Tracking with alpha animation")
201
vehID = "horiz"
202
polygonID = "poly4"
203
traci.polygon.add(
204
polygonID, ((550, 510), (550, 550), (590, 550), (590, 510)), (20, 20, 200, 255), True, "test")
205
traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20],
206
[50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50])
207
208
for step in range(35, 40):
209
print("step", step)
210
traci.simulationStep()
211
212
print("# (7) Add a second tracking polygon")
213
vehID = "horiz"
214
polygonID = "poly5"
215
traci.polygon.add(
216
polygonID, ((350, 490), (350, 450), (390, 450), (390, 490)), (200, 20, 20, 255), True, "test")
217
traci.polygon.addDynamics(polygonID, vehID)
218
219
print("polygons", traci.polygon.getIDList())
220
print("polygon count", traci.polygon.getIDCount())
221
222
for step in range(40, 45):
223
print("step", step)
224
traci.simulationStep()
225
226
print("polygons", traci.polygon.getIDList())
227
print("polygon count", traci.polygon.getIDCount())
228
229
print("# (8) Parking vehicle and renew dynamics and highlight")
230
vehID = "veh0"
231
polygonID = "poly2"
232
traci.vehicle.setStop(vehID, "2si", 100, 1, 5, 1)
233
traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20],
234
[50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50])
235
traci.vehicle.highlight(vehID, (0, 100, 100, 100))
236
237
for step in range(45, 65):
238
print("step", step)
239
traci.simulationStep()
240
241
print("polygons", traci.polygon.getIDList())
242
print("polygon count", traci.polygon.getIDCount())
243
244
for step in range(65, 110):
245
print("step", step)
246
traci.simulationStep()
247
248
print("# (9) transfer tracking dynamics from vehicle 'horiz' to 'veh0' and redefine as looped animation")
249
vehID = "veh0"
250
polygonID = "poly5"
251
traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4], [50, 200, 50], True)
252
253
print("# (10) Highlight vehicle 'horiz' for limited duration")
254
vehID = "horiz"
255
traci.vehicle.highlight(vehID, (0, 255, 0), -1, 200, 4)
256
257
print("polygons", traci.polygon.getIDList())
258
print("polygon count", traci.polygon.getIDCount())
259
260
for pID in traci.polygon.getIDList():
261
examine(pID)
262
263
print("vehicles", traci.vehicle.getIDList())
264
print("vehicle count", traci.vehicle.getIDCount())
265
266
for step in range(110, 120):
267
print("step", step)
268
traci.simulationStep()
269
270
print("# (11) Tracked vehicle 'veh0' arrives")
271
for step in range(120, 125):
272
print("step", step)
273
traci.simulationStep()
274
275
print("polygons", traci.polygon.getIDList())
276
print("polygon count", traci.polygon.getIDCount())
277
278
print("vehicles", traci.vehicle.getIDList())
279
print("vehicle count", traci.vehicle.getIDCount())
280
281
print("# (12) Adding and highlighting POIs (two highlights for the same must specify highlight type)")
282
283
print("pois", traci.poi.getIDList())
284
print("poi count", traci.poi.getIDCount())
285
286
print("polygons", traci.polygon.getIDList())
287
print("polygon count", traci.polygon.getIDCount())
288
289
poiID = "myPOI0"
290
print("adding", poiID)
291
traci.poi.add(poiID, 350, 510, (255, 255, 255, 255), "test", 0, "img.jpeg")
292
traci.poi.highlight(poiID, (0, 0, 255), -1, 200, 4)
293
294
poiID = "myPOI1"
295
print("adding", poiID)
296
traci.poi.add(poiID, 370, 510, (255, 255, 255, 255), "test", 0, "img.jpeg")
297
traci.poi.highlight(poiID, (0, 0, 255), -1, 200, 4)
298
299
poiID = "myPOI0"
300
traci.poi.highlight(poiID, (55, 55, 100), 20, 200, 4)
301
302
print("pois", traci.poi.getIDList())
303
print("poi count", traci.poi.getIDCount())
304
305
print("polygons", traci.polygon.getIDList())
306
print("polygon count", traci.polygon.getIDCount())
307
308
for pID in traci.polygon.getIDList():
309
examine(pID)
310
311
for step in range(125, 130):
312
print("step", step)
313
traci.simulationStep()
314
315
print("pois", traci.poi.getIDList())
316
print("poi count", traci.poi.getIDCount())
317
318
print("polygons", traci.polygon.getIDList())
319
print("polygon count", traci.polygon.getIDCount())
320
321
for pID in traci.polygon.getIDList():
322
examine(pID)
323
324
traci.close()
325
326