Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
TraCIServerAPI_Polygon.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2002-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
23// APIs for getting/setting polygon values via TraCI
24/****************************************************************************/
25#include <config.h>
26
28#include <microsim/MSNet.h>
30#include <libsumo/Polygon.h>
31#include <libsumo/Helper.h>
35
36
37// ===========================================================================
38// method definitions
39// ===========================================================================
40bool
42 tcpip::Storage& outputStorage) {
43 std::string warning = ""; // additional description for response
44 // variable
45 int variable = inputStorage.readUnsignedByte();
46 if (variable != libsumo::VAR_TYPE && variable != libsumo::VAR_COLOR && variable != libsumo::VAR_SHAPE && variable != libsumo::VAR_FILL
47 && variable != libsumo::VAR_WIDTH && variable != libsumo::VAR_MOVE_TO
48 && variable != libsumo::ADD && variable != libsumo::REMOVE && variable != libsumo::VAR_PARAMETER) {
50 "Change Polygon State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
51 }
52 // id
53 std::string id = inputStorage.readString();
54 try {
55 // process
56 switch (variable) {
58 libsumo::Polygon::setType(id, StoHelp::readTypedString(inputStorage, "The type must be given as a string."));
59 break;
61 libsumo::Polygon::setColor(id, StoHelp::readTypedColor(inputStorage, "The color must be given using an according type."));
62 break;
64 libsumo::Polygon::setShape(id, StoHelp::readTypedPolygon(inputStorage, "The shape must be given using an according type."));
65 break;
67 libsumo::Polygon::setFilled(id, StoHelp::readTypedInt(inputStorage, "'fill' must be defined using an integer.") != 0);
68 break;
70 libsumo::Polygon::setLineWidth(id, StoHelp::readTypedDouble(inputStorage, "'lineWidth' must be defined using a double."));
71 break;
73 libsumo::Polygon::setHeight(id, StoHelp::readTypedDouble(inputStorage, "'height' must be defined using a double."));
74 break;
75 case libsumo::ADD: {
76 const int parameterCount = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for adding a new polygon.");
77 if (parameterCount != 5 && parameterCount != 6) {
78 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding a polygon needs five to six parameters.", outputStorage);
79 }
80 const std::string type = StoHelp::readTypedString(inputStorage, "The type must be given as a string.");
81 const libsumo::TraCIColor col = StoHelp::readTypedColor(inputStorage, "The second polygon parameter must be the color.");
82 const bool fill = StoHelp::readBool(inputStorage, "The third polygon parameter must be 'fill' encoded as ubyte.");
83 const int layer = StoHelp::readTypedInt(inputStorage, "The fourth polygon parameter must be the layer encoded as int.");
84 const libsumo::TraCIPositionVector tp = StoHelp::readTypedPolygon(inputStorage, "The fifth polygon parameter must be the shape.");
85 double lineWidth = 1.;
86 if (parameterCount == 6) {
87 lineWidth = StoHelp::readTypedDouble(inputStorage, "The sixth polygon parameter must be the lineWidth encoded as double.");
88 }
89 libsumo::Polygon::add(id, tp, col, fill, type, layer, lineWidth);
90 }
91 break;
93 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
94 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for adding dynamics to a polygon.", outputStorage);
95 }
96 int itemNo = inputStorage.readInt();
97 if (itemNo != 5) {
98 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding polygon dynamics needs four parameters.", outputStorage);
99 }
100 const 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).");
101 const 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).");
102 const 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).");
103 const bool looped = StoHelp::readBool(inputStorage, "The fourth parameter for adding polygon dynamics must be boolean indicating whether the animation should be looped.");
104 const bool rotate = StoHelp::readBool(inputStorage, "The fifth parameter for adding polygon dynamics must be boolean indicating whether the tracking polygon should be rotated.");
105 libsumo::Polygon::addDynamics(id, trackedID, timeSpan, alphaSpan, looped, rotate);
106 }
107 break;
108 case libsumo::REMOVE: {
109 libsumo::Polygon::remove(id, StoHelp::readTypedInt(inputStorage, "The layer must be given using an int."));
110 }
111 break;
113 StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
114 const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
115 const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
116 libsumo::Polygon::setParameter(id, name, value);
117 }
118 break;
119 default:
120 break;
121 }
122 } catch (libsumo::TraCIException& e) {
123 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, e.what(), outputStorage);
124 }
126 return true;
127}
128
129
130/****************************************************************************/
std::string toHex(const T i, std::streamsize numDigits=0)
Definition ToString.h:81
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc8: Change Polygon State)
TraCI server used to control sumo by a remote TraCI client.
Definition TraCIServer.h:59
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
static int readCompound(tcpip::Storage &ret, int expectedSize=-1, const std::string &error="")
static bool readBool(tcpip::Storage &ret, const std::string &error="")
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
static std::vector< double > readTypedDoubleList(tcpip::Storage &ret, const std::string &error="")
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
static const libsumo::TraCIPositionVector readTypedPolygon(tcpip::Storage &ret, const std::string &error="")
static const libsumo::TraCIColor readTypedColor(tcpip::Storage &ret, const std::string &error="")
static double readTypedDouble(tcpip::Storage &ret, const std::string &error="")
An error which allows to continue.
Definition TraCIDefs.h:145
virtual std::string readString()
Definition storage.cpp:180
virtual int readUnsignedByte()
Definition storage.cpp:155
virtual int readInt()
Definition storage.cpp:311
TRACI_CONST int VAR_TYPE
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int VAR_MOVE_TO
TRACI_CONST int CMD_SET_POLYGON_VARIABLE
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_WIDTH
TRACI_CONST int CMD_SET_VEHICLE_VARIABLE
TRACI_CONST int VAR_SHAPE
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int REMOVE
TRACI_CONST int VAR_ADD_DYNAMICS
TRACI_CONST int VAR_HEIGHT
TRACI_CONST int VAR_FILL
TRACI_CONST int RTYPE_OK
TRACI_CONST int ADD
A list of positions.
Definition TraCIDefs.h:240