SUMO - Simulation of Urban MObility
TraCIServerAPI_Polygon.cpp
Go to the documentation of this file.
1 /****************************************************************************/
12 // APIs for getting/setting polygon values via TraCI
13 /****************************************************************************/
14 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
15 // Copyright (C) 2002-2017 DLR (http://www.dlr.de/) and contributors
16 /****************************************************************************/
17 //
18 // This file is part of SUMO.
19 // SUMO is free software: you can redistribute it and/or modify
20 // it under the terms of the GNU General Public License as published by
21 // the Free Software Foundation, either version 3 of the License, or
22 // (at your option) any later version.
23 //
24 /****************************************************************************/
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #ifndef NO_TRACI
37 
38 #include <utils/common/StdDefs.h>
39 #include <microsim/MSNet.h>
42 #include <traci-server/lib/TraCI.h>
43 #include "TraCIConstants.h"
44 #include "TraCIServerAPI_Polygon.h"
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
49 bool
51  tcpip::Storage& outputStorage) {
52  // variable & id
53  int variable = inputStorage.readUnsignedByte();
54  std::string id = inputStorage.readString();
55  // check variable
56  if (variable != ID_LIST && variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
57  && variable != ID_COUNT && variable != VAR_PARAMETER) {
59  "Get Polygon Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
60  }
61  // begin response building
62  tcpip::Storage tempMsg;
63  // response-code, variableID, objectID
65  tempMsg.writeUnsignedByte(variable);
66  tempMsg.writeString(id);
67  // process request
68  if (variable == ID_LIST || variable == ID_COUNT) {
69  std::vector<std::string> ids = TraCI_Polygon::getIDList();
70  if (variable == ID_LIST) {
72  tempMsg.writeStringList(ids);
73  } else {
75  tempMsg.writeInt((int) ids.size());
76  }
77  } else {
78  try {
79  switch (variable) {
80  case VAR_TYPE: {
83  }
84  break;
85  case VAR_COLOR: {
88  tempMsg.writeUnsignedByte(tc.r);
89  tempMsg.writeUnsignedByte(tc.g);
90  tempMsg.writeUnsignedByte(tc.b);
91  tempMsg.writeUnsignedByte(tc.a);
92  }
93  break;
94  case VAR_SHAPE: {
97  tempMsg.writeUnsignedByte((int) tp.size());
98  for (int iPoint = 0; iPoint < (int)tp.size(); ++iPoint) {
99  tempMsg.writeDouble(tp[iPoint].x);
100  tempMsg.writeDouble(tp[iPoint].y);
101  }
102  }
103  break;
104  case VAR_FILL: {
105  tempMsg.writeUnsignedByte(TYPE_UBYTE);
106  tempMsg.writeUnsignedByte(TraCI_Polygon::getFilled(id) ? 1 : 0);
107  }
108  break;
109  case VAR_PARAMETER: {
110  std::string paramName = "";
111  if (!server.readTypeCheckingString(inputStorage, paramName)) {
112  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
113  }
115  tempMsg.writeString(TraCI_Polygon::getParameter(id, paramName));
116  }
117  break;
118  }
119  } catch (TraCIException& e) {
120  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, e.what(), outputStorage);
121  }
122  }
123  server.writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_OK, "", outputStorage);
124  server.writeResponseWithLength(outputStorage, tempMsg);
125  return true;
126 }
127 
128 bool
130  tcpip::Storage& outputStorage) {
131  std::string warning = ""; // additional description for response
132  // variable
133  int variable = inputStorage.readUnsignedByte();
134  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
135  && variable != ADD && variable != REMOVE && variable != VAR_PARAMETER) {
137  "Change Polygon State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
138  }
139  // id
140  std::string id = inputStorage.readString();
141  try {
142  // process
143  switch (variable) {
144  case VAR_TYPE: {
145  std::string type;
146  if (!server.readTypeCheckingString(inputStorage, type)) {
147  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
148  }
149  TraCI_Polygon::setType(id, type);
150  }
151  break;
152  case VAR_COLOR: {
153  TraCIColor col;
154  if (!server.readTypeCheckingColor(inputStorage, col)) {
155  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
156  }
157  TraCI_Polygon::setColor(id, col);
158  }
159  break;
160  case VAR_SHAPE: {
161  PositionVector shape;
162  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
163  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The shape must be given using an accoring type.", outputStorage);
164  }
166  }
167  break;
168  case VAR_FILL: {
169  int value = 0;
170  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
171  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "'fill' must be defined using an unsigned byte.", outputStorage);
172  }
173  TraCI_Polygon::setFilled(id, value != 0);
174  }
175  break;
176  case ADD: {
177  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
178  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);
179  }
180  //readt itemNo
181  inputStorage.readInt();
182  std::string type;
183  if (!server.readTypeCheckingString(inputStorage, type)) {
184  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
185  }
186  TraCIColor col;
187  if (!server.readTypeCheckingColor(inputStorage, col)) {
188  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);
189  }
190  int value = 0;
191  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
192  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
193  }
194  bool fill = value != 0;
195  int layer = 0;
196  if (!server.readTypeCheckingInt(inputStorage, layer)) {
197  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
198  }
199  PositionVector shape;
200  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
201  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);
202  }
204 
205  TraCI_Polygon::add(id, tp, col, fill, type, layer);
206 
207  }
208  break;
209  case REMOVE: {
210  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
211  if (!server.readTypeCheckingInt(inputStorage, layer)) {
212  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The layer must be given using an int.", outputStorage);
213  }
214 
215  TraCI_Polygon::remove(id, layer);
216 
217  }
218  break;
219  case VAR_PARAMETER: {
220  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
221  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
222  }
223  //readt itemNo
224  inputStorage.readInt();
225  std::string name;
226  if (!server.readTypeCheckingString(inputStorage, name)) {
227  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
228  }
229  std::string value;
230  if (!server.readTypeCheckingString(inputStorage, value)) {
231  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
232  }
233  TraCI_Polygon::setParameter(id, name, value);
234 
235  }
236  break;
237  default:
238  break;
239  }
240  } catch (TraCIException& e) {
241  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, e.what(), outputStorage);
242  }
243  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_OK, warning, outputStorage);
244  return true;
245 }
246 
247 bool
248 TraCIServerAPI_Polygon::getShape(const std::string& id, PositionVector& shape) {
249  SUMOPolygon* poly = getPolygon(id);
250  if (poly == 0) {
251  return false;
252  }
253  shape = poly->getShape();
254  return true;
255 }
256 
258 TraCIServerAPI_Polygon::getPolygon(const std::string& id) {
260 }
261 
262 NamedRTree*
264  NamedRTree* t = new NamedRTree();
266  const std::map<std::string, SUMOPolygon*>& polygons = shapeCont.getPolygons().getMyMap();
267  for (std::map<std::string, SUMOPolygon*>::const_iterator i = polygons.begin(); i != polygons.end(); ++i) {
268  Boundary b = (*i).second->getShape().getBoxBoundary();
269  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
270  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
271  t->Insert(cmin, cmax, (*i).second);
272  }
273  return t;
274 }
275 
276 #endif
277 
278 
279 /****************************************************************************/
280 
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:90
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:132
bool readTypeCheckingColor(tcpip::Storage &inputStorage, TraCIColor &into)
Reads the value type and a color, verifying the type.
static TraCIColor getColor(const std::string &polygonID)
#define TYPE_COMPOUND
static void add(const std::string &polygonID, const TraCIPositionVector &shape, const TraCIColor &c, bool fill, const std::string &type, int layer)
static std::string getParameter(const std::string &polygonID, const std::string &paramName)
static NamedRTree * getTree()
Returns a tree filled with polygon instances.
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:144
#define TYPE_UBYTE
#define RTYPE_OK
#define VAR_TYPE
#define TYPE_POLYGON
unsigned char g
Definition: TraCIDefs.h:79
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: SUMOPolygon.h:84
A RT-tree for efficient storing of SUMO&#39;s Named objects.
Definition: NamedRTree.h:72
#define VAR_COLOR
#define RESPONSE_GET_POLYGON_VARIABLE
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:169
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define TYPE_COLOR
#define TYPE_STRINGLIST
static void remove(const std::string &polygonID, int layer=0)
unsigned char r
Definition: TraCIDefs.h:79
Storage for geometrical objects.
#define CMD_GET_POLYGON_VARIABLE
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
virtual void writeUnsignedByte(int)
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
unsigned char a
Definition: TraCIDefs.h:79
#define VAR_SHAPE
const Polygons & getPolygons() const
Returns all polygons.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
static void setParameter(std::string &name, std::string &value, std::string &string)
virtual void writeInt(int)
static void setColor(const std::string &polygonID, const TraCIColor &c)
#define TYPE_STRING
virtual int readUnsignedByte()
static bool getFilled(const std::string &polygonID)
static std::string getType(const std::string &polygonID)
T get(const std::string &id) const
Retrieves an item.
static void setFilled(std::string polygonID, bool filled)
static std::vector< std::string > getIDList()
virtual int readInt()
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:428
A list of positions.
virtual void writeStringList(const std::vector< std::string > &s)
virtual std::string readString()
#define ADD
bool readTypeCheckingUnsignedByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and an unsigned byte, verifying the type.
#define REMOVE
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
const IDMap & getMyMap() const
Returns a reference to the internal map.
#define CMD_SET_POLYGON_VARIABLE
virtual void writeString(const std::string &s)
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:66
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: TraCI.cpp:108
static SUMOPolygon * getPolygon(const std::string &id)
Returns the named polygon.
static TraCIPositionVector getShape(const std::string &polygonID)
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:126
static void setType(const std::string &polygonID, const std::string &setType)
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:138
virtual void writeDouble(double)
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa8: Get Polygon Variable)
unsigned char b
Definition: TraCIDefs.h:79
#define VAR_FILL
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
#define VAR_PARAMETER
#define ID_COUNT
static void setShape(const std::string &polygonID, const TraCIPositionVector &shape)
#define TYPE_INTEGER
#define ID_LIST
A list of positions.
static bool getShape(const std::string &id, PositionVector &shape)
Returns the named polygons&#39;s shape.
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc8: Change Polygon State)