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>
40 #include <utils/shapes/Polygon.h>
43 #include <traci-server/lib/TraCI.h>
44 #include "TraCIConstants.h"
45 #include "TraCIServerAPI_Polygon.h"
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 bool
52  tcpip::Storage& outputStorage) {
53  // variable & id
54  int variable = inputStorage.readUnsignedByte();
55  std::string id = inputStorage.readString();
56  // check variable
57  if (variable != ID_LIST && variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
58  && variable != ID_COUNT && variable != VAR_PARAMETER) {
60  "Get Polygon Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
61  }
62  // begin response building
63  tcpip::Storage tempMsg;
64  // response-code, variableID, objectID
66  tempMsg.writeUnsignedByte(variable);
67  tempMsg.writeString(id);
68  // process request
69  if (variable == ID_LIST || variable == ID_COUNT) {
70  std::vector<std::string> ids = TraCI_Polygon::getIDList();
71  if (variable == ID_LIST) {
73  tempMsg.writeStringList(ids);
74  } else {
76  tempMsg.writeInt((int) ids.size());
77  }
78  } else {
79  try {
80  switch (variable) {
81  case VAR_TYPE: {
84  }
85  break;
86  case VAR_COLOR: {
89  tempMsg.writeUnsignedByte(tc.r);
90  tempMsg.writeUnsignedByte(tc.g);
91  tempMsg.writeUnsignedByte(tc.b);
92  tempMsg.writeUnsignedByte(tc.a);
93  }
94  break;
95  case VAR_SHAPE: {
98  tempMsg.writeUnsignedByte((int) tp.size());
99  for (int iPoint = 0; iPoint < (int)tp.size(); ++iPoint) {
100  tempMsg.writeDouble(tp[iPoint].x);
101  tempMsg.writeDouble(tp[iPoint].y);
102  }
103  }
104  break;
105  case VAR_FILL: {
106  tempMsg.writeUnsignedByte(TYPE_UBYTE);
107  tempMsg.writeUnsignedByte(TraCI_Polygon::getFilled(id) ? 1 : 0);
108  }
109  break;
110  case VAR_PARAMETER: {
111  std::string paramName = "";
112  if (!server.readTypeCheckingString(inputStorage, paramName)) {
113  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
114  }
116  tempMsg.writeString(TraCI_Polygon::getParameter(id, paramName));
117  }
118  break;
119  }
120  } catch (TraCIException& e) {
121  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, e.what(), outputStorage);
122  }
123  }
124  server.writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_OK, "", outputStorage);
125  server.writeResponseWithLength(outputStorage, tempMsg);
126  return true;
127 }
128 
129 bool
131  tcpip::Storage& outputStorage) {
132  std::string warning = ""; // additional description for response
133  // variable
134  int variable = inputStorage.readUnsignedByte();
135  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
136  && variable != ADD && variable != REMOVE && variable != VAR_PARAMETER) {
138  "Change Polygon State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
139  }
140  // id
141  std::string id = inputStorage.readString();
142  try {
143  // process
144  switch (variable) {
145  case VAR_TYPE: {
146  std::string type;
147  if (!server.readTypeCheckingString(inputStorage, type)) {
148  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
149  }
150  TraCI_Polygon::setType(id, type);
151  }
152  break;
153  case VAR_COLOR: {
154  TraCIColor col;
155  if (!server.readTypeCheckingColor(inputStorage, col)) {
156  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
157  }
158  TraCI_Polygon::setColor(id, col);
159  }
160  break;
161  case VAR_SHAPE: {
162  PositionVector shape;
163  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
164  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The shape must be given using an accoring type.", outputStorage);
165  }
167  }
168  break;
169  case VAR_FILL: {
170  int value = 0;
171  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
172  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "'fill' must be defined using an unsigned byte.", outputStorage);
173  }
174  TraCI_Polygon::setFilled(id, value != 0);
175  }
176  break;
177  case ADD: {
178  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
179  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);
180  }
181  //readt itemNo
182  inputStorage.readInt();
183  std::string type;
184  if (!server.readTypeCheckingString(inputStorage, type)) {
185  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
186  }
187  TraCIColor col;
188  if (!server.readTypeCheckingColor(inputStorage, col)) {
189  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);
190  }
191  int value = 0;
192  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
193  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
194  }
195  bool fill = value != 0;
196  int layer = 0;
197  if (!server.readTypeCheckingInt(inputStorage, layer)) {
198  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
199  }
200  PositionVector shape;
201  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
202  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);
203  }
205 
206  TraCI_Polygon::add(id, tp, col, fill, type, layer);
207 
208  }
209  break;
210  case REMOVE: {
211  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
212  if (!server.readTypeCheckingInt(inputStorage, layer)) {
213  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The layer must be given using an int.", outputStorage);
214  }
215 
216  TraCI_Polygon::remove(id, layer);
217 
218  }
219  break;
220  case VAR_PARAMETER: {
221  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
222  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
223  }
224  //readt itemNo
225  inputStorage.readInt();
226  std::string name;
227  if (!server.readTypeCheckingString(inputStorage, name)) {
228  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
229  }
230  std::string value;
231  if (!server.readTypeCheckingString(inputStorage, value)) {
232  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
233  }
234  TraCI_Polygon::setParameter(id, name, value);
235 
236  }
237  break;
238  default:
239  break;
240  }
241  } catch (TraCIException& e) {
242  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, e.what(), outputStorage);
243  }
244  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_OK, warning, outputStorage);
245  return true;
246 }
247 
248 bool
249 TraCIServerAPI_Polygon::getShape(const std::string& id, PositionVector& shape) {
250  SUMO::Polygon* poly = getPolygon(id);
251  if (poly == 0) {
252  return false;
253  }
254  shape = poly->getShape();
255  return true;
256 }
257 
259 TraCIServerAPI_Polygon::getPolygon(const std::string& id) {
260  return MSNet::getInstance()->getShapeContainer().getPolygons().get(id);
261 }
262 
263 NamedRTree*
265  NamedRTree* t = new NamedRTree();
267  const std::map<std::string, SUMO::Polygon*>& polygons = shapeCont.getPolygons().getMyMap();
268  for (std::map<std::string, SUMO::Polygon*>::const_iterator i = polygons.begin(); i != polygons.end(); ++i) {
269  Boundary b = (*i).second->getShape().getBoxBoundary();
270  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
271  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
272  t->Insert(cmin, cmax, (*i).second);
273  }
274  return t;
275 }
276 
277 #endif
278 
279 
280 /****************************************************************************/
281 
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:90
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:138
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)
const Polygons & getPolygons() const
Returns all polygons.
static std::string getParameter(const std::string &polygonID, const std::string &paramName)
static NamedRTree * getTree()
Returns a tree filled with polygon instances.
#define TYPE_UBYTE
#define RTYPE_OK
#define VAR_TYPE
#define TYPE_POLYGON
unsigned char g
Definition: TraCIDefs.h:79
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:158
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 PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: Polygon.h:87
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)
A 2D- or 3D-polygon.
Definition: Polygon.h:57
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:433
A list of positions.
virtual void writeStringList(const std::vector< std::string > &s)
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:126
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)
#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 TraCIPositionVector getShape(const std::string &polygonID)
static void setType(const std::string &polygonID, const std::string &setType)
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.
static SUMO::Polygon * getPolygon(const std::string &id)
Returns the named polygon.
#define VAR_PARAMETER
#define ID_COUNT
static void setShape(const std::string &polygonID, const TraCIPositionVector &shape)
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:144
#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)