Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2002-2025 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 : /****************************************************************************/
14 : /// @file TraCIServerAPI_Polygon.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Laura Bieker
17 : /// @author Michael Behrisch
18 : /// @author Jakob Erdmann
19 : /// @author Christoph Sommer
20 : /// @author Gregor Laemmel
21 : /// @date Sept 2002
22 : ///
23 : // APIs for getting/setting polygon values via TraCI
24 : /****************************************************************************/
25 : #include <config.h>
26 :
27 : #include <utils/common/StdDefs.h>
28 : #include <microsim/MSNet.h>
29 : #include <utils/shapes/ShapeContainer.h>
30 : #include <libsumo/Polygon.h>
31 : #include <libsumo/Helper.h>
32 : #include <libsumo/StorageHelper.h>
33 : #include <libsumo/TraCIConstants.h>
34 : #include "TraCIServerAPI_Polygon.h"
35 :
36 :
37 : // ===========================================================================
38 : // method definitions
39 : // ===========================================================================
40 : bool
41 4760 : TraCIServerAPI_Polygon::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
42 : tcpip::Storage& outputStorage) {
43 4760 : const int variable = inputStorage.readUnsignedByte();
44 4760 : const std::string id = inputStorage.readString();
45 4760 : server.initWrapper(libsumo::RESPONSE_GET_POLYGON_VARIABLE, variable, id);
46 : try {
47 4760 : if (!libsumo::Polygon::handleVariable(id, variable, &server, &inputStorage)) {
48 15 : return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, "Get Polygon Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
49 : }
50 2 : } catch (libsumo::TraCIException& e) {
51 2 : return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, e.what(), outputStorage);
52 2 : }
53 4753 : server.writeStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
54 4753 : server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
55 : return true;
56 : }
57 :
58 : bool
59 263 : TraCIServerAPI_Polygon::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
60 : tcpip::Storage& outputStorage) {
61 263 : std::string warning = ""; // additional description for response
62 : // variable
63 263 : int variable = inputStorage.readUnsignedByte();
64 263 : if (variable != libsumo::VAR_TYPE && variable != libsumo::VAR_COLOR && variable != libsumo::VAR_SHAPE && variable != libsumo::VAR_FILL
65 235 : && variable != libsumo::VAR_WIDTH && variable != libsumo::VAR_MOVE_TO
66 126 : && variable != libsumo::ADD && variable != libsumo::REMOVE && variable != libsumo::VAR_PARAMETER) {
67 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE,
68 0 : "Change Polygon State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
69 : }
70 : // id
71 263 : std::string id = inputStorage.readString();
72 : try {
73 : // process
74 263 : switch (variable) {
75 : case libsumo::VAR_TYPE: {
76 : std::string type;
77 6 : if (!server.readTypeCheckingString(inputStorage, type)) {
78 3 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
79 : }
80 5 : libsumo::Polygon::setType(id, type);
81 : }
82 : break;
83 : case libsumo::VAR_COLOR: {
84 : libsumo::TraCIColor col;
85 9 : if (!server.readTypeCheckingColor(inputStorage, col)) {
86 48 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
87 : }
88 8 : libsumo::Polygon::setColor(id, col);
89 : }
90 : break;
91 7 : case libsumo::VAR_SHAPE: {
92 7 : PositionVector shape;
93 7 : if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
94 1 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The shape must be given using an according type.", outputStorage);
95 : }
96 6 : libsumo::Polygon::setShape(id, libsumo::Helper::makeTraCIPositionVector(shape));
97 7 : }
98 : break;
99 : case libsumo::VAR_FILL: {
100 6 : const int value = StoHelp::readTypedInt(inputStorage, "'fill' must be defined using an integer.");
101 10 : libsumo::Polygon::setFilled(id, value != 0);
102 : }
103 4 : break;
104 4 : case libsumo::VAR_WIDTH: {
105 4 : double value = 0;
106 4 : if (!server.readTypeCheckingDouble(inputStorage, value)) {
107 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "'lineWidth' must be defined using an double.", outputStorage);
108 : }
109 8 : libsumo::Polygon::setLineWidth(id, value);
110 : }
111 4 : break;
112 98 : case libsumo::ADD: {
113 98 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
114 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);
115 : }
116 98 : int itemNo = inputStorage.readInt();
117 98 : if (itemNo != 5 && itemNo != 6) {
118 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding a polygon needs five to six parameters.", outputStorage);
119 : }
120 : std::string type;
121 98 : if (!server.readTypeCheckingString(inputStorage, type)) {
122 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
123 : }
124 : libsumo::TraCIColor col;
125 98 : if (!server.readTypeCheckingColor(inputStorage, col)) {
126 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);
127 : }
128 98 : int value = 0;
129 98 : if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
130 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
131 : }
132 98 : bool fill = value != 0;
133 101 : const int layer = StoHelp::readTypedInt(inputStorage, "The fourth polygon parameter must be the layer encoded as int.");
134 98 : PositionVector shape;
135 98 : if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
136 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);
137 : }
138 95 : double lineWidth = 1;
139 95 : if (itemNo == 6) {
140 94 : if (!server.readTypeCheckingDouble(inputStorage, lineWidth)) {
141 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The sixth polygon parameter must be the lineWidth encoded as double.", outputStorage);
142 : }
143 : }
144 95 : libsumo::TraCIPositionVector tp = libsumo::Helper::makeTraCIPositionVector(shape);
145 95 : libsumo::Polygon::add(id, tp, col, fill, type, layer, lineWidth);
146 98 : }
147 : break;
148 105 : case libsumo::VAR_ADD_DYNAMICS : {
149 : // Add dynamics to polygon.
150 105 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
151 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for adding dynamics to a polygon.", outputStorage);
152 : }
153 105 : int itemNo = inputStorage.readInt();
154 105 : if (itemNo != 5) {
155 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding polygon dynamics needs four parameters.", outputStorage);
156 : }
157 :
158 : std::string trackedID;
159 105 : if (!server.readTypeCheckingString(inputStorage, trackedID)) {
160 36 : return 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);
161 : }
162 :
163 : std::vector<double> timeSpan;
164 105 : if (!server.readTypeCheckingDoubleList(inputStorage, timeSpan)) {
165 0 : return 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);
166 : }
167 :
168 : std::vector<double> alphaSpan;
169 105 : if (!server.readTypeCheckingDoubleList(inputStorage, alphaSpan)) {
170 0 : return 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);
171 : }
172 :
173 : int looped;
174 105 : if (!server.readTypeCheckingUnsignedByte(inputStorage, looped)) {
175 0 : return 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);
176 : }
177 :
178 : int rotate;
179 105 : if (!server.readTypeCheckingUnsignedByte(inputStorage, rotate)) {
180 0 : return 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);
181 : }
182 :
183 105 : libsumo::Polygon::addDynamics(id, trackedID, timeSpan, alphaSpan, looped != 0, rotate != 0);
184 141 : }
185 : break;
186 : case libsumo::REMOVE: {
187 : // !!! layer not used yet (shouldn't the id be enough?)
188 24 : libsumo::Polygon::remove(id, StoHelp::readTypedInt(inputStorage, "The layer must be given using an int."));
189 : }
190 22 : break;
191 4 : case libsumo::VAR_PARAMETER: {
192 4 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
193 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
194 : }
195 : //readt itemNo
196 4 : inputStorage.readInt();
197 : std::string name;
198 4 : if (!server.readTypeCheckingString(inputStorage, name)) {
199 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
200 : }
201 : std::string value;
202 4 : if (!server.readTypeCheckingString(inputStorage, value)) {
203 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
204 : }
205 4 : libsumo::Polygon::setParameter(id, name, value);
206 :
207 : }
208 : break;
209 : default:
210 : break;
211 : }
212 46 : } catch (libsumo::TraCIException& e) {
213 46 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, e.what(), outputStorage);
214 46 : }
215 214 : server.writeStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
216 : return true;
217 : }
218 :
219 :
220 : /****************************************************************************/
|