LCOV - code coverage report
Current view: top level - src/traci-server - TraCIServerAPI_Polygon.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 82.6 % 109 90
Test Date: 2024-12-21 15:45:41 Functions: 100.0 % 2 2

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

Generated by: LCOV version 2.0-1