Path: blob/main/tests/complex/traci/polygon/polygon_dynamics/runner.py
169689 views
#!/usr/bin/env python1# -*- coding: utf-8 -*-2# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo3# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.4# This program and the accompanying materials are made available under the5# terms of the Eclipse Public License 2.0 which is available at6# https://www.eclipse.org/legal/epl-2.0/7# This Source Code may also be made available under the following Secondary8# Licenses when the conditions for such availability set forth in the Eclipse9# Public License 2.0 are satisfied: GNU General Public License, version 210# or later which is available at11# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html12# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1314# @file runner.py15# @author Michael Behrisch16# @author Daniel Krajzewicz17# @date 2011-03-04181920from __future__ import print_function21from __future__ import absolute_import22import os23import sys2425if "SUMO_HOME" in os.environ:26sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))27import traci # noqa28import sumolib # noqa293031def examine(polygonID):32print("# examining", polygonID)33print("shape", traci.polygon.getShape(polygonID))34print("type", traci.polygon.getType(polygonID))35print("color", traci.polygon.getColor(polygonID))36print("filled", traci.polygon.getFilled(polygonID))373839traci.start([sumolib.checkBinary('sumo'), "-c", "sumo.sumocfg"])40# traci.init(port=12345) # debug41for step in range(3):42print("step", step)43traci.simulationStep()44polygonID = "poly0"45print("adding", polygonID)46traci.polygon.add(47polygonID, ((960, 510), (960, 550), (1000, 550), (1000, 510)), (100, 200, 0, 255), True, "test")4849print("polygons", traci.polygon.getIDList())50print("polygon count", traci.polygon.getIDCount())51examine(polygonID)5253for step in range(3, 6):54print("step", step)55traci.simulationStep()5657# Failing specification tests58print("# (1) Adding underspecified dynamics...")59try:60traci.polygon.addDynamics(polygonID)61except traci.exceptions.TraCIException:62print("Caught TraCIException")6364print("# (2) Adding malformed dynamics 1 ...")65try:66traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 4, 3])67except traci.exceptions.TraCIException:68print("Caught TraCIException")6970print("# (3) Adding malformed dynamics 2 ...")71try:72traci.polygon.addDynamics(polygonID, "", [1, 2, 3, 4], [200, 20, 2, 1])73except traci.exceptions.TraCIException:74print("Caught TraCIException")7576print("# (4) Adding malformed dynamics 3 ...")77try:78traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 3], [200, 20, 2])79except traci.exceptions.TraCIException:80print("Caught TraCIException")8182print("# (5) Adding malformed dynamics 4 ...")83try:84traci.polygon.addDynamics(polygonID, "", [0], [200])85except traci.exceptions.TraCIException:86print("Caught TraCIException")8788print("# (6) Adding malformed dynamics 5 ...")89try:90traci.polygon.addDynamics(polygonID, "horiz", [], [], True)91except traci.exceptions.TraCIException:92print("Caught TraCIException")9394examine(polygonID)9596for step in range(6, 9):97print("step", step)98traci.simulationStep()99100# Successful specifications101102print("# (1) No tracking with alpha animation")103traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 5], [0, 200, 100, 0])104105for step in range(9, 12):106print("step", step)107traci.simulationStep()108examine(polygonID)109110print("polygons", traci.polygon.getIDList())111print("polygon count", traci.polygon.getIDCount())112113print("vehicles", traci.vehicle.getIDList())114print("vehicle count", traci.vehicle.getIDCount())115116for step in range(12, 15):117print("step", step)118traci.simulationStep()119120print("polygons", traci.polygon.getIDList())121print("polygon count", traci.polygon.getIDCount())122123polygonID = "poly1"124print("adding", polygonID)125traci.polygon.add(126polygonID, ((960, 510), (960, 550), (1000, 550), (1000, 510)), (0, 100, 100, 255), True, "test")127128print("polygons", traci.polygon.getIDList())129print("polygon count", traci.polygon.getIDCount())130131print("# (2) No tracking with time line but no animation (removal after time line expired)")132traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 3])133134for step in range(15, 18):135print("step", step)136traci.simulationStep()137138print("polygons", traci.polygon.getIDList())139print("polygon count", traci.polygon.getIDCount())140141polygonID = "poly2"142print("adding", polygonID)143traci.polygon.add(144polygonID, ((960, 510), (960, 550), (1000, 550), (1000, 510)), (150, 0, 100, 255), True, "test")145146vehID = "veh0"147print("adding", vehID)148traci.route.add("trip0", ["2fi", "1o"])149traci.vehicle.add(vehID, "trip0")150151print("vehicles", traci.vehicle.getIDList())152print("vehicle count", traci.vehicle.getIDCount())153154print("# (3) Tracking without time line")155traci.polygon.addDynamics(polygonID, vehID)156157for step in range(18, 21):158print("step", step)159traci.simulationStep()160161print("# (4) Tracking existing vehicle with time line")162polygonID = "poly3"163vehID = "horiz"164traci.polygon.add(165polygonID, ((750, 490), (750, 450), (790, 450), (790, 490)), (0, 0, 200, 255), True, "test")166traci.polygon.addDynamics(polygonID, vehID, [0, 10])167168print("polygons", traci.polygon.getIDList())169print("polygon count", traci.polygon.getIDCount())170171for step in range(21, 25):172print("step", step)173traci.simulationStep()174175print("# (5) replacing tracking dynamics and highlighting vehicle permanently")176polygonID = "poly2"177vehID = "veh0"178traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20], [17950, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50])180traci.vehicle.highlight(vehID)181182print("polygons", traci.polygon.getIDList())183print("polygon count", traci.polygon.getIDCount())184185for step in range(25, 35):186print("step", step)187traci.simulationStep()188189# "poly3" should have been removed190print("polygons", traci.polygon.getIDList())191print("polygon count", traci.polygon.getIDCount())192193print("# (6) Tracking with alpha animation")194vehID = "horiz"195polygonID = "poly4"196traci.polygon.add(197polygonID, ((550, 510), (550, 550), (590, 550), (590, 510)), (20, 20, 200, 255), True, "test")198traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20], [19950, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50])200201for step in range(35, 40):202print("step", step)203traci.simulationStep()204205print("# (7) Add a second tracking polygon")206vehID = "horiz"207polygonID = "poly5"208traci.polygon.add(209polygonID, ((350, 490), (350, 450), (390, 450), (390, 490)), (200, 20, 20, 255), True, "test")210traci.polygon.addDynamics(polygonID, vehID)211212print("polygons", traci.polygon.getIDList())213print("polygon count", traci.polygon.getIDCount())214215for step in range(40, 45):216print("step", step)217traci.simulationStep()218219print("polygons", traci.polygon.getIDList())220print("polygon count", traci.polygon.getIDCount())221222print("# (8) Parking vehicle and renew dynamics and highlight")223vehID = "veh0"224polygonID = "poly2"225traci.vehicle.setStop(vehID, "2si", 100, 1, 5, 1)226traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20], [22750, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50])228traci.vehicle.highlight(vehID, (0, 100, 100, 100))229230for step in range(45, 65):231print("step", step)232traci.simulationStep()233234print("polygons", traci.polygon.getIDList())235print("polygon count", traci.polygon.getIDCount())236237for step in range(65, 110):238print("step", step)239traci.simulationStep()240241print("# (9) transfer tracking dynamics from vehicle 'horiz' to 'veh0' and redefine as looped animation")242vehID = "veh0"243polygonID = "poly5"244traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4], [50, 200, 50], True)245246print("# (10) Highlight vehicle 'horiz' for limited duration")247vehID = "horiz"248traci.vehicle.highlight(vehID, (0, 255, 0), -1, 200, 4)249250print("polygons", traci.polygon.getIDList())251print("polygon count", traci.polygon.getIDCount())252253for pID in traci.polygon.getIDList():254examine(pID)255256print("vehicles", traci.vehicle.getIDList())257print("vehicle count", traci.vehicle.getIDCount())258259for step in range(110, 120):260print("step", step)261traci.simulationStep()262263print("# (11) Tracked vehicle 'veh0' arrives")264for step in range(120, 125):265print("step", step)266traci.simulationStep()267268print("polygons", traci.polygon.getIDList())269print("polygon count", traci.polygon.getIDCount())270271print("vehicles", traci.vehicle.getIDList())272print("vehicle count", traci.vehicle.getIDCount())273274print("# (12) Adding and highlighting POIs (two highlights for the same must specify highlight type)")275276print("pois", traci.poi.getIDList())277print("poi count", traci.poi.getIDCount())278279print("polygons", traci.polygon.getIDList())280print("polygon count", traci.polygon.getIDCount())281282poiID = "myPOI0"283print("adding", poiID)284traci.poi.add(poiID, 350, 510, (255, 255, 255, 255), "test", 0, "img.jpeg")285traci.poi.highlight(poiID, (0, 0, 255), -1, 200, 4)286287poiID = "myPOI1"288print("adding", poiID)289traci.poi.add(poiID, 370, 510, (255, 255, 255, 255), "test", 0, "img.jpeg")290traci.poi.highlight(poiID, (0, 0, 255), -1, 200, 4)291292poiID = "myPOI0"293traci.poi.highlight(poiID, (55, 55, 100), 20, 200, 4, 1)294295print("pois", traci.poi.getIDList())296print("poi count", traci.poi.getIDCount())297298print("polygons", traci.polygon.getIDList())299print("polygon count", traci.polygon.getIDCount())300301for pID in traci.polygon.getIDList():302examine(pID)303304for step in range(125, 130):305print("step", step)306traci.simulationStep()307308print("pois", traci.poi.getIDList())309print("poi count", traci.poi.getIDCount())310311print("polygons", traci.polygon.getIDList())312print("polygon count", traci.polygon.getIDCount())313314for pID in traci.polygon.getIDList():315examine(pID)316317traci.close()318319320