Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/polygon/polygon_dynamics/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'), "-c", "sumo.sumocfg"])
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
65
print("# (2) Adding malformed dynamics 1 ...")
66
try:
67
traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 4, 3])
68
except traci.exceptions.TraCIException:
69
print("Caught TraCIException")
70
71
print("# (3) Adding malformed dynamics 2 ...")
72
try:
73
traci.polygon.addDynamics(polygonID, "", [1, 2, 3, 4], [200, 20, 2, 1])
74
except traci.exceptions.TraCIException:
75
print("Caught TraCIException")
76
77
print("# (4) Adding malformed dynamics 3 ...")
78
try:
79
traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 3], [200, 20, 2])
80
except traci.exceptions.TraCIException:
81
print("Caught TraCIException")
82
83
print("# (5) Adding malformed dynamics 4 ...")
84
try:
85
traci.polygon.addDynamics(polygonID, "", [0], [200])
86
except traci.exceptions.TraCIException:
87
print("Caught TraCIException")
88
89
print("# (6) Adding malformed dynamics 5 ...")
90
try:
91
traci.polygon.addDynamics(polygonID, "horiz", [], [], True)
92
except traci.exceptions.TraCIException:
93
print("Caught TraCIException")
94
95
examine(polygonID)
96
97
for step in range(6, 9):
98
print("step", step)
99
traci.simulationStep()
100
101
# Successful specifications
102
103
print("# (1) No tracking with alpha animation")
104
traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 5], [0, 200, 100, 0])
105
106
for step in range(9, 12):
107
print("step", step)
108
traci.simulationStep()
109
examine(polygonID)
110
111
print("polygons", traci.polygon.getIDList())
112
print("polygon count", traci.polygon.getIDCount())
113
114
print("vehicles", traci.vehicle.getIDList())
115
print("vehicle count", traci.vehicle.getIDCount())
116
117
for step in range(12, 15):
118
print("step", step)
119
traci.simulationStep()
120
121
print("polygons", traci.polygon.getIDList())
122
print("polygon count", traci.polygon.getIDCount())
123
124
polygonID = "poly1"
125
print("adding", polygonID)
126
traci.polygon.add(
127
polygonID, ((960, 510), (960, 550), (1000, 550), (1000, 510)), (0, 100, 100, 255), True, "test")
128
129
print("polygons", traci.polygon.getIDList())
130
print("polygon count", traci.polygon.getIDCount())
131
132
print("# (2) No tracking with time line but no animation (removal after time line expired)")
133
traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 3])
134
135
for step in range(15, 18):
136
print("step", step)
137
traci.simulationStep()
138
139
print("polygons", traci.polygon.getIDList())
140
print("polygon count", traci.polygon.getIDCount())
141
142
polygonID = "poly2"
143
print("adding", polygonID)
144
traci.polygon.add(
145
polygonID, ((960, 510), (960, 550), (1000, 550), (1000, 510)), (150, 0, 100, 255), True, "test")
146
147
vehID = "veh0"
148
print("adding", vehID)
149
traci.route.add("trip0", ["2fi", "1o"])
150
traci.vehicle.add(vehID, "trip0")
151
152
print("vehicles", traci.vehicle.getIDList())
153
print("vehicle count", traci.vehicle.getIDCount())
154
155
print("# (3) Tracking without time line")
156
traci.polygon.addDynamics(polygonID, vehID)
157
158
for step in range(18, 21):
159
print("step", step)
160
traci.simulationStep()
161
162
print("# (4) Tracking existing vehicle with time line")
163
polygonID = "poly3"
164
vehID = "horiz"
165
traci.polygon.add(
166
polygonID, ((750, 490), (750, 450), (790, 450), (790, 490)), (0, 0, 200, 255), True, "test")
167
traci.polygon.addDynamics(polygonID, vehID, [0, 10])
168
169
print("polygons", traci.polygon.getIDList())
170
print("polygon count", traci.polygon.getIDCount())
171
172
for step in range(21, 25):
173
print("step", step)
174
traci.simulationStep()
175
176
print("# (5) replacing tracking dynamics and highlighting vehicle permanently")
177
polygonID = "poly2"
178
vehID = "veh0"
179
traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20], [
180
50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50])
181
traci.vehicle.highlight(vehID)
182
183
print("polygons", traci.polygon.getIDList())
184
print("polygon count", traci.polygon.getIDCount())
185
186
for step in range(25, 35):
187
print("step", step)
188
traci.simulationStep()
189
190
# "poly3" should have been removed
191
print("polygons", traci.polygon.getIDList())
192
print("polygon count", traci.polygon.getIDCount())
193
194
print("# (6) Tracking with alpha animation")
195
vehID = "horiz"
196
polygonID = "poly4"
197
traci.polygon.add(
198
polygonID, ((550, 510), (550, 550), (590, 550), (590, 510)), (20, 20, 200, 255), True, "test")
199
traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20], [
200
50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50])
201
202
for step in range(35, 40):
203
print("step", step)
204
traci.simulationStep()
205
206
print("# (7) Add a second tracking polygon")
207
vehID = "horiz"
208
polygonID = "poly5"
209
traci.polygon.add(
210
polygonID, ((350, 490), (350, 450), (390, 450), (390, 490)), (200, 20, 20, 255), True, "test")
211
traci.polygon.addDynamics(polygonID, vehID)
212
213
print("polygons", traci.polygon.getIDList())
214
print("polygon count", traci.polygon.getIDCount())
215
216
for step in range(40, 45):
217
print("step", step)
218
traci.simulationStep()
219
220
print("polygons", traci.polygon.getIDList())
221
print("polygon count", traci.polygon.getIDCount())
222
223
print("# (8) Parking vehicle and renew dynamics and highlight")
224
vehID = "veh0"
225
polygonID = "poly2"
226
traci.vehicle.setStop(vehID, "2si", 100, 1, 5, 1)
227
traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20], [
228
50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50])
229
traci.vehicle.highlight(vehID, (0, 100, 100, 100))
230
231
for step in range(45, 65):
232
print("step", step)
233
traci.simulationStep()
234
235
print("polygons", traci.polygon.getIDList())
236
print("polygon count", traci.polygon.getIDCount())
237
238
for step in range(65, 110):
239
print("step", step)
240
traci.simulationStep()
241
242
print("# (9) transfer tracking dynamics from vehicle 'horiz' to 'veh0' and redefine as looped animation")
243
vehID = "veh0"
244
polygonID = "poly5"
245
traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4], [50, 200, 50], True)
246
247
print("# (10) Highlight vehicle 'horiz' for limited duration")
248
vehID = "horiz"
249
traci.vehicle.highlight(vehID, (0, 255, 0), -1, 200, 4)
250
251
print("polygons", traci.polygon.getIDList())
252
print("polygon count", traci.polygon.getIDCount())
253
254
for pID in traci.polygon.getIDList():
255
examine(pID)
256
257
print("vehicles", traci.vehicle.getIDList())
258
print("vehicle count", traci.vehicle.getIDCount())
259
260
for step in range(110, 120):
261
print("step", step)
262
traci.simulationStep()
263
264
print("# (11) Tracked vehicle 'veh0' arrives")
265
for step in range(120, 125):
266
print("step", step)
267
traci.simulationStep()
268
269
print("polygons", traci.polygon.getIDList())
270
print("polygon count", traci.polygon.getIDCount())
271
272
print("vehicles", traci.vehicle.getIDList())
273
print("vehicle count", traci.vehicle.getIDCount())
274
275
print("# (12) Adding and highlighting POIs (two highlights for the same must specify highlight type)")
276
277
print("pois", traci.poi.getIDList())
278
print("poi count", traci.poi.getIDCount())
279
280
print("polygons", traci.polygon.getIDList())
281
print("polygon count", traci.polygon.getIDCount())
282
283
poiID = "myPOI0"
284
print("adding", poiID)
285
traci.poi.add(poiID, 350, 510, (255, 255, 255, 255), "test", 0, "img.jpeg")
286
traci.poi.highlight(poiID, (0, 0, 255), -1, 200, 4)
287
288
poiID = "myPOI1"
289
print("adding", poiID)
290
traci.poi.add(poiID, 370, 510, (255, 255, 255, 255), "test", 0, "img.jpeg")
291
traci.poi.highlight(poiID, (0, 0, 255), -1, 200, 4)
292
293
poiID = "myPOI0"
294
traci.poi.highlight(poiID, (55, 55, 100), 20, 200, 4, 1)
295
296
print("pois", traci.poi.getIDList())
297
print("poi count", traci.poi.getIDCount())
298
299
print("polygons", traci.polygon.getIDList())
300
print("polygon count", traci.polygon.getIDCount())
301
302
for pID in traci.polygon.getIDList():
303
examine(pID)
304
305
for step in range(125, 130):
306
print("step", step)
307
traci.simulationStep()
308
309
print("pois", traci.poi.getIDList())
310
print("poi count", traci.poi.getIDCount())
311
312
print("polygons", traci.polygon.getIDList())
313
print("polygon count", traci.polygon.getIDCount())
314
315
for pID in traci.polygon.getIDList():
316
examine(pID)
317
318
traci.close()
319
320