Path: blob/main/src/traci-server/TraCIServerAPI_Polygon.cpp
193898 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2002-2026 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file TraCIServerAPI_Polygon.cpp14/// @author Daniel Krajzewicz15/// @author Laura Bieker16/// @author Michael Behrisch17/// @author Jakob Erdmann18/// @author Christoph Sommer19/// @author Gregor Laemmel20/// @date Sept 200221///22// APIs for getting/setting polygon values via TraCI23/****************************************************************************/24#include <config.h>2526#include <utils/common/StdDefs.h>27#include <microsim/MSNet.h>28#include <utils/shapes/ShapeContainer.h>29#include <libsumo/Polygon.h>30#include <libsumo/Helper.h>31#include <libsumo/StorageHelper.h>32#include <libsumo/TraCIConstants.h>33#include "TraCIServerAPI_Polygon.h"343536// ===========================================================================37// method definitions38// ===========================================================================39bool40TraCIServerAPI_Polygon::processSet(TraCIServer& server, tcpip::Storage& inputStorage,41tcpip::Storage& outputStorage) {42std::string warning = ""; // additional description for response43// variable44int variable = inputStorage.readUnsignedByte();45if (variable != libsumo::VAR_TYPE && variable != libsumo::VAR_COLOR && variable != libsumo::VAR_SHAPE && variable != libsumo::VAR_FILL46&& variable != libsumo::VAR_WIDTH && variable != libsumo::VAR_MOVE_TO47&& variable != libsumo::ADD && variable != libsumo::REMOVE && variable != libsumo::VAR_PARAMETER) {48return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE,49"Change Polygon State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);50}51// id52std::string id = inputStorage.readString();53try {54// process55switch (variable) {56case libsumo::VAR_TYPE:57libsumo::Polygon::setType(id, StoHelp::readTypedString(inputStorage, "The type must be given as a string."));58break;59case libsumo::VAR_COLOR:60libsumo::Polygon::setColor(id, StoHelp::readTypedColor(inputStorage, "The color must be given using an according type."));61break;62case libsumo::VAR_SHAPE:63libsumo::Polygon::setShape(id, StoHelp::readTypedPolygon(inputStorage, "The shape must be given using an according type."));64break;65case libsumo::VAR_FILL:66libsumo::Polygon::setFilled(id, StoHelp::readTypedInt(inputStorage, "'fill' must be defined using an integer.") != 0);67break;68case libsumo::VAR_WIDTH:69libsumo::Polygon::setLineWidth(id, StoHelp::readTypedDouble(inputStorage, "'lineWidth' must be defined using a double."));70break;71case libsumo::ADD: {72const int parameterCount = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for adding a new polygon.");73if (parameterCount != 5 && parameterCount != 6) {74return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding a polygon needs five to six parameters.", outputStorage);75}76const std::string type = StoHelp::readTypedString(inputStorage, "The type must be given as a string.");77const libsumo::TraCIColor col = StoHelp::readTypedColor(inputStorage, "The second polygon parameter must be the color.");78const bool fill = StoHelp::readBool(inputStorage, "The third polygon parameter must be 'fill' encoded as ubyte.");79const int layer = StoHelp::readTypedInt(inputStorage, "The fourth polygon parameter must be the layer encoded as int.");80const libsumo::TraCIPositionVector tp = StoHelp::readTypedPolygon(inputStorage, "The fifth polygon parameter must be the shape.");81double lineWidth = 1.;82if (parameterCount == 6) {83lineWidth = StoHelp::readTypedDouble(inputStorage, "The sixth polygon parameter must be the lineWidth encoded as double.");84}85libsumo::Polygon::add(id, tp, col, fill, type, layer, lineWidth);86}87break;88case libsumo::VAR_ADD_DYNAMICS: {89if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {90return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for adding dynamics to a polygon.", outputStorage);91}92int itemNo = inputStorage.readInt();93if (itemNo != 5) {94return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding polygon dynamics needs four parameters.", outputStorage);95}96const std::string trackedID = StoHelp::readTypedString(inputStorage, "The first parameter for adding polygon dynamics must be ID of the tracked object as a string ('' to disregard tracking).");97const std::vector<double> timeSpan = StoHelp::readTypedDoubleList(inputStorage, "The second parameter for adding polygon dynamics must be the timespan of the animation (length=0 to disregard animation).");98const std::vector<double> alphaSpan = StoHelp::readTypedDoubleList(inputStorage, "The third parameter for adding polygon dynamics must be the alphaSpanStr of the animation (length=0 to disregard alpha animation).");99const bool looped = StoHelp::readBool(inputStorage, "The fourth parameter for adding polygon dynamics must be boolean indicating whether the animation should be looped.");100const bool rotate = StoHelp::readBool(inputStorage, "The fifth parameter for adding polygon dynamics must be boolean indicating whether the tracking polygon should be rotated.");101libsumo::Polygon::addDynamics(id, trackedID, timeSpan, alphaSpan, looped, rotate);102}103break;104case libsumo::REMOVE: {105libsumo::Polygon::remove(id, StoHelp::readTypedInt(inputStorage, "The layer must be given using an int."));106}107break;108case libsumo::VAR_PARAMETER: {109StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");110const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");111const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");112libsumo::Polygon::setParameter(id, name, value);113}114break;115default:116break;117}118} catch (libsumo::TraCIException& e) {119return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, e.what(), outputStorage);120}121server.writeStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);122return true;123}124125126/****************************************************************************/127128129