Path: blob/main/tests/complex/traci/gui/polygon_dynamics_gui/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-gui'), "-c", "sumo.sumocfg", "-S", "-Q"], stdout=sys.stdout)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")63pass6465print("# (2) Adding malformed dynamics 1 ...")66try:67traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 4, 3])68except traci.exceptions.TraCIException:69print("Caught TraCIException")70pass7172print("# (3) Adding malformed dynamics 2 ...")73try:74traci.polygon.addDynamics(polygonID, "", [1, 2, 3, 4], [200, 20, 2, 1])75except traci.exceptions.TraCIException:76print("Caught TraCIException")77pass7879print("# (4) Adding malformed dynamics 3 ...")80try:81traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 3], [200, 20, 2])82except traci.exceptions.TraCIException:83print("Caught TraCIException")84pass8586print("# (5) Adding malformed dynamics 4 ...")87try:88traci.polygon.addDynamics(polygonID, "", [0], [200])89except traci.exceptions.TraCIException:90print("Caught TraCIException")91pass9293print("# (6) Adding malformed dynamics 5 ...")94try:95traci.polygon.addDynamics(polygonID, "horiz", [], [], True)96except traci.exceptions.TraCIException:97print("Caught TraCIException")98pass99100examine(polygonID)101102for step in range(6, 9):103print("step", step)104traci.simulationStep()105106# Successfull specifications107108print("# (1) No tracking with alpha animation")109traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 5], [0, 200, 100, 0])110111for step in range(9, 12):112print("step", step)113traci.simulationStep()114examine(polygonID)115116print("polygons", traci.polygon.getIDList())117print("polygon count", traci.polygon.getIDCount())118119print("vehicles", traci.vehicle.getIDList())120print("vehicle count", traci.vehicle.getIDCount())121122for step in range(12, 15):123print("step", step)124traci.simulationStep()125126print("polygons", traci.polygon.getIDList())127print("polygon count", traci.polygon.getIDCount())128129polygonID = "poly1"130print("adding", polygonID)131traci.polygon.add(132polygonID, ((960, 510), (960, 550), (1000, 550), (1000, 510)), (0, 100, 100, 255), True, "test")133134print("polygons", traci.polygon.getIDList())135print("polygon count", traci.polygon.getIDCount())136137print("# (2) No tracking with time line but no animation (removal after time line expired)")138traci.polygon.addDynamics(polygonID, "", [0, 1, 2, 3])139140for step in range(15, 18):141print("step", step)142traci.simulationStep()143144print("polygons", traci.polygon.getIDList())145print("polygon count", traci.polygon.getIDCount())146147polygonID = "poly2"148print("adding", polygonID)149traci.polygon.add(150polygonID, ((960, 510), (960, 550), (1000, 550), (1000, 510)), (150, 0, 100, 255), True, "test")151152vehID = "veh0"153print("adding", vehID)154traci.route.add("trip0", ["2fi", "1o"])155traci.vehicle.add(vehID, "trip0")156157print("vehicles", traci.vehicle.getIDList())158print("vehicle count", traci.vehicle.getIDCount())159160print("# (3) Tracking without time line")161traci.polygon.addDynamics(polygonID, vehID)162163for step in range(18, 21):164print("step", step)165traci.simulationStep()166167print("# (4) Tracking existing vehicle with time line")168polygonID = "poly3"169vehID = "horiz"170traci.polygon.add(171polygonID, ((750, 490), (750, 450), (790, 450), (790, 490)), (0, 0, 200, 255), True, "test")172traci.polygon.addDynamics(polygonID, vehID, [0, 10])173174print("polygons", traci.polygon.getIDList())175print("polygon count", traci.polygon.getIDCount())176177for step in range(21, 25):178print("step", step)179traci.simulationStep()180181print("# (5) replacing tracking dynamics and highlighting vehicle permanently")182polygonID = "poly2"183vehID = "veh0"184traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20],185[50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50])186traci.vehicle.highlight(vehID)187188print("polygons", traci.polygon.getIDList())189print("polygon count", traci.polygon.getIDCount())190191for step in range(25, 35):192print("step", step)193traci.simulationStep()194195# "poly3" should have been removed196print("polygons", traci.polygon.getIDList())197print("polygon count", traci.polygon.getIDCount())198199print("# (6) Tracking with alpha animation")200vehID = "horiz"201polygonID = "poly4"202traci.polygon.add(203polygonID, ((550, 510), (550, 550), (590, 550), (590, 510)), (20, 20, 200, 255), True, "test")204traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20],205[50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50])206207for step in range(35, 40):208print("step", step)209traci.simulationStep()210211print("# (7) Add a second tracking polygon")212vehID = "horiz"213polygonID = "poly5"214traci.polygon.add(215polygonID, ((350, 490), (350, 450), (390, 450), (390, 490)), (200, 20, 20, 255), True, "test")216traci.polygon.addDynamics(polygonID, vehID)217218print("polygons", traci.polygon.getIDList())219print("polygon count", traci.polygon.getIDCount())220221for step in range(40, 45):222print("step", step)223traci.simulationStep()224225print("polygons", traci.polygon.getIDList())226print("polygon count", traci.polygon.getIDCount())227228print("# (8) Parking vehicle and renew dynamics and highlight")229vehID = "veh0"230polygonID = "poly2"231traci.vehicle.setStop(vehID, "2si", 100, 1, 5, 1)232traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20],233[50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50])234traci.vehicle.highlight(vehID, (0, 100, 100, 100))235236for step in range(45, 65):237print("step", step)238traci.simulationStep()239240print("polygons", traci.polygon.getIDList())241print("polygon count", traci.polygon.getIDCount())242243for step in range(65, 110):244print("step", step)245traci.simulationStep()246247print("# (9) transfer tracking dynamics from vehicle 'horiz' to 'veh0' and redefine as looped animation")248vehID = "veh0"249polygonID = "poly5"250traci.polygon.addDynamics(polygonID, vehID, [0, 2, 4], [50, 200, 50], True)251252print("# (10) Highlight vehicle 'horiz' for limited duration")253vehID = "horiz"254traci.vehicle.highlight(vehID, (0, 255, 0), -1, 200, 4)255256print("polygons", traci.polygon.getIDList())257print("polygon count", traci.polygon.getIDCount())258259for pID in traci.polygon.getIDList():260examine(pID)261262print("vehicles", traci.vehicle.getIDList())263print("vehicle count", traci.vehicle.getIDCount())264265for step in range(110, 120):266print("step", step)267traci.simulationStep()268269print("# (11) Tracked vehicle 'veh0' arrives")270for step in range(120, 125):271print("step", step)272traci.simulationStep()273274print("polygons", traci.polygon.getIDList())275print("polygon count", traci.polygon.getIDCount())276277print("vehicles", traci.vehicle.getIDList())278print("vehicle count", traci.vehicle.getIDCount())279280print("# (12) Adding and highlighting POIs (two highlights for the same must specify highlight type)")281282print("pois", traci.poi.getIDList())283print("poi count", traci.poi.getIDCount())284285print("polygons", traci.polygon.getIDList())286print("polygon count", traci.polygon.getIDCount())287288poiID = "myPOI0"289print("adding", poiID)290traci.poi.add(poiID, 350, 510, (255, 255, 255, 255), "test", 0, "img.jpeg")291traci.poi.highlight(poiID, (0, 0, 255), -1, 200, 4)292293poiID = "myPOI1"294print("adding", poiID)295traci.poi.add(poiID, 370, 510, (255, 255, 255, 255), "test", 0, "img.jpeg")296traci.poi.highlight(poiID, (0, 0, 255), -1, 200, 4)297298poiID = "myPOI0"299traci.poi.highlight(poiID, (55, 55, 100), 20, 200, 4)300301print("pois", traci.poi.getIDList())302print("poi count", traci.poi.getIDCount())303304print("polygons", traci.polygon.getIDList())305print("polygon count", traci.polygon.getIDCount())306307for pID in traci.polygon.getIDList():308examine(pID)309310for step in range(125, 130):311print("step", step)312traci.simulationStep()313314print("pois", traci.poi.getIDList())315print("poi count", traci.poi.getIDCount())316317print("polygons", traci.polygon.getIDList())318print("polygon count", traci.polygon.getIDCount())319320for pID in traci.polygon.getIDList():321examine(pID)322323traci.close()324325326