Path: blob/main/src/traci-server/TraCIServerAPI_Polygon.cpp
169665 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2002-2025 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::processGet(TraCIServer& server, tcpip::Storage& inputStorage,41tcpip::Storage& outputStorage) {42const int variable = inputStorage.readUnsignedByte();43const std::string id = inputStorage.readString();44server.initWrapper(libsumo::RESPONSE_GET_POLYGON_VARIABLE, variable, id);45try {46if (!libsumo::Polygon::handleVariable(id, variable, &server, &inputStorage)) {47return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, "Get Polygon Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);48}49} catch (libsumo::TraCIException& e) {50return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, e.what(), outputStorage);51}52server.writeStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);53server.writeResponseWithLength(outputStorage, server.getWrapperStorage());54return true;55}5657bool58TraCIServerAPI_Polygon::processSet(TraCIServer& server, tcpip::Storage& inputStorage,59tcpip::Storage& outputStorage) {60std::string warning = ""; // additional description for response61// variable62int variable = inputStorage.readUnsignedByte();63if (variable != libsumo::VAR_TYPE && variable != libsumo::VAR_COLOR && variable != libsumo::VAR_SHAPE && variable != libsumo::VAR_FILL64&& variable != libsumo::VAR_WIDTH && variable != libsumo::VAR_MOVE_TO65&& variable != libsumo::ADD && variable != libsumo::REMOVE && variable != libsumo::VAR_PARAMETER) {66return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE,67"Change Polygon State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);68}69// id70std::string id = inputStorage.readString();71try {72// process73switch (variable) {74case libsumo::VAR_TYPE: {75std::string type;76if (!server.readTypeCheckingString(inputStorage, type)) {77return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);78}79libsumo::Polygon::setType(id, type);80}81break;82case libsumo::VAR_COLOR: {83libsumo::TraCIColor col;84if (!server.readTypeCheckingColor(inputStorage, col)) {85return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);86}87libsumo::Polygon::setColor(id, col);88}89break;90case libsumo::VAR_SHAPE: {91PositionVector shape;92if (!server.readTypeCheckingPolygon(inputStorage, shape)) {93return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The shape must be given using an according type.", outputStorage);94}95libsumo::Polygon::setShape(id, libsumo::Helper::makeTraCIPositionVector(shape));96}97break;98case libsumo::VAR_FILL: {99const int value = StoHelp::readTypedInt(inputStorage, "'fill' must be defined using an integer.");100libsumo::Polygon::setFilled(id, value != 0);101}102break;103case libsumo::VAR_WIDTH: {104double value = 0;105if (!server.readTypeCheckingDouble(inputStorage, value)) {106return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "'lineWidth' must be defined using an double.", outputStorage);107}108libsumo::Polygon::setLineWidth(id, value);109}110break;111case libsumo::ADD: {112if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {113return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);114}115int itemNo = inputStorage.readInt();116if (itemNo != 5 && itemNo != 6) {117return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding a polygon needs five to six parameters.", outputStorage);118}119std::string type;120if (!server.readTypeCheckingString(inputStorage, type)) {121return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);122}123libsumo::TraCIColor col;124if (!server.readTypeCheckingColor(inputStorage, col)) {125return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);126}127int value = 0;128if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {129return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);130}131bool fill = value != 0;132const int layer = StoHelp::readTypedInt(inputStorage, "The fourth polygon parameter must be the layer encoded as int.");133PositionVector shape;134if (!server.readTypeCheckingPolygon(inputStorage, shape)) {135return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);136}137double lineWidth = 1;138if (itemNo == 6) {139if (!server.readTypeCheckingDouble(inputStorage, lineWidth)) {140return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The sixth polygon parameter must be the lineWidth encoded as double.", outputStorage);141}142}143libsumo::TraCIPositionVector tp = libsumo::Helper::makeTraCIPositionVector(shape);144libsumo::Polygon::add(id, tp, col, fill, type, layer, lineWidth);145}146break;147case libsumo::VAR_ADD_DYNAMICS : {148// Add dynamics to polygon.149if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {150return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for adding dynamics to a polygon.", outputStorage);151}152int itemNo = inputStorage.readInt();153if (itemNo != 5) {154return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding polygon dynamics needs four parameters.", outputStorage);155}156157std::string trackedID;158if (!server.readTypeCheckingString(inputStorage, trackedID)) {159return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The first parameter for adding polygon dynamics must be ID of the tracked object as a string ('' to disregard tracking).", outputStorage);160}161162std::vector<double> timeSpan;163if (!server.readTypeCheckingDoubleList(inputStorage, timeSpan)) {164return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The second parameter for adding polygon dynamics must be the timespan of the animation (length=0 to disregard animation).", outputStorage);165}166167std::vector<double> alphaSpan;168if (!server.readTypeCheckingDoubleList(inputStorage, alphaSpan)) {169return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The third parameter for adding polygon dynamics must be the alphaSpanStr of the animation (length=0 to disregard alpha animation).", outputStorage);170}171172int looped;173if (!server.readTypeCheckingUnsignedByte(inputStorage, looped)) {174return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fourth parameter for adding polygon dynamics must be boolean indicating whether the animation should be looped.", outputStorage);175}176177int rotate;178if (!server.readTypeCheckingUnsignedByte(inputStorage, rotate)) {179return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fifth parameter for adding polygon dynamics must be boolean indicating whether the tracking polygon should be rotated.", outputStorage);180}181182libsumo::Polygon::addDynamics(id, trackedID, timeSpan, alphaSpan, looped != 0, rotate != 0);183}184break;185case libsumo::REMOVE: {186// !!! layer not used yet (shouldn't the id be enough?)187libsumo::Polygon::remove(id, StoHelp::readTypedInt(inputStorage, "The layer must be given using an int."));188}189break;190case libsumo::VAR_PARAMETER: {191if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {192return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);193}194//readt itemNo195inputStorage.readInt();196std::string name;197if (!server.readTypeCheckingString(inputStorage, name)) {198return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);199}200std::string value;201if (!server.readTypeCheckingString(inputStorage, value)) {202return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);203}204libsumo::Polygon::setParameter(id, name, value);205206}207break;208default:209break;210}211} catch (libsumo::TraCIException& e) {212return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, e.what(), outputStorage);213}214server.writeStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);215return true;216}217218219/****************************************************************************/220221222