SUMO - Simulation of Urban MObility
TraCIServerAPI_Polygon.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // APIs for getting/setting polygon values via TraCI
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2002-2016 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #ifndef NO_TRACI
36 
37 #include <utils/common/StdDefs.h>
38 #include <microsim/MSNet.h>
39 #include <utils/shapes/Polygon.h>
41 #include "TraCIConstants.h"
42 #include "TraCIServerAPI_Polygon.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 bool
54  tcpip::Storage& outputStorage) {
55  // variable & id
56  int variable = inputStorage.readUnsignedByte();
57  std::string id = inputStorage.readString();
58  // check variable
59  if (variable != ID_LIST && variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
60  && variable != ID_COUNT && variable != VAR_PARAMETER) {
61  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Get Polygon Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
62  }
63  // begin response building
64  tcpip::Storage tempMsg;
65  // response-code, variableID, objectID
67  tempMsg.writeUnsignedByte(variable);
68  tempMsg.writeString(id);
69  // process request
70  if (variable == ID_LIST || variable == ID_COUNT) {
71  std::vector<std::string> ids;
73  shapeCont.getPolygons().insertIDs(ids);
74  if (variable == ID_LIST) {
76  tempMsg.writeStringList(ids);
77  } else {
79  tempMsg.writeInt((int) ids.size());
80  }
81  } else {
82  Polygon* p = getPolygon(id);
83  if (p == 0) {
84  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Polygon '" + id + "' is not known", outputStorage);
85  }
86  switch (variable) {
87  case VAR_TYPE:
89  tempMsg.writeString(p->getType());
90  break;
91  case VAR_COLOR:
93  tempMsg.writeUnsignedByte(p->getColor().red());
94  tempMsg.writeUnsignedByte(p->getColor().green());
95  tempMsg.writeUnsignedByte(p->getColor().blue());
96  tempMsg.writeUnsignedByte(p->getColor().alpha());
97  break;
98  case VAR_SHAPE:
100  tempMsg.writeUnsignedByte(MIN2(static_cast<int>(255), static_cast<int>(p->getShape().size())));
101  for (unsigned int iPoint = 0; iPoint < MIN2(static_cast<size_t>(255), p->getShape().size()); ++iPoint) {
102  tempMsg.writeDouble(p->getShape()[iPoint].x());
103  tempMsg.writeDouble(p->getShape()[iPoint].y());
104  }
105  break;
106  case VAR_FILL:
107  tempMsg.writeUnsignedByte(TYPE_UBYTE);
108  tempMsg.writeUnsignedByte(p->getFill() ? 1 : 0);
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(p->getParameter(paramName, ""));
117  }
118  break;
119  default:
120  break;
121  }
122  }
123  server.writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_OK, "", outputStorage);
124  server.writeResponseWithLength(outputStorage, tempMsg);
125  return true;
126 }
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) {
137  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Change Polygon State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
138  }
139  // id
140  std::string id = inputStorage.readString();
141  Polygon* p = 0;
143  if (variable != ADD && variable != REMOVE) {
144  p = getPolygon(id);
145  if (p == 0) {
146  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Polygon '" + id + "' is not known", outputStorage);
147  }
148  }
149  // process
150  switch (variable) {
151  case VAR_TYPE: {
152  std::string type;
153  if (!server.readTypeCheckingString(inputStorage, type)) {
154  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
155  }
156  p->setType(type);
157  }
158  break;
159  case VAR_COLOR: {
160  RGBColor col;
161  if (!server.readTypeCheckingColor(inputStorage, col)) {
162  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
163  }
164  p->setColor(col);
165  }
166  break;
167  case VAR_SHAPE: {
168  PositionVector shape;
169  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
170  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The shape must be given using an accoring type.", outputStorage);
171  }
172  shapeCont.reshapePolygon(id, shape);
173  }
174  break;
175  case VAR_FILL: {
176  int value = 0;
177  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
178  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "'fill' must be defined using an unsigned byte.", outputStorage);
179  }
180  p->setFill(value != 0);
181  }
182  break;
183  case ADD: {
184  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
185  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);
186  }
187  //readt itemNo
188  inputStorage.readInt();
189  std::string type;
190  if (!server.readTypeCheckingString(inputStorage, type)) {
191  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
192  }
193  RGBColor col;
194  if (!server.readTypeCheckingColor(inputStorage, col)) {
195  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);
196  }
197  int value = 0;
198  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
199  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
200  }
201  bool fill = value != 0;
202  int layer = 0;
203  if (!server.readTypeCheckingInt(inputStorage, layer)) {
204  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
205  }
206  PositionVector shape;
207  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
208  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);
209  }
210  //
211  if (!shapeCont.addPolygon(id, type, col, (SUMOReal)layer,
213  delete p;
214  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Could not add polygon.", outputStorage);
215  }
216  }
217  break;
218  case REMOVE: {
219  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
220  if (!server.readTypeCheckingInt(inputStorage, layer)) {
221  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The layer must be given using an int.", outputStorage);
222  }
223  if (!shapeCont.removePolygon(id)) {
224  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Could not remove polygon '" + id + "'", outputStorage);
225  }
226  }
227  break;
228  case VAR_PARAMETER: {
229  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
230  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
231  }
232  //readt itemNo
233  inputStorage.readInt();
234  std::string name;
235  if (!server.readTypeCheckingString(inputStorage, name)) {
236  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
237  }
238  std::string value;
239  if (!server.readTypeCheckingString(inputStorage, value)) {
240  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
241  }
242  p->addParameter(name, value);
243  }
244  break;
245  default:
246  break;
247  }
248  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_OK, warning, outputStorage);
249  return true;
250 }
251 
252 
253 bool
254 TraCIServerAPI_Polygon::getShape(const std::string& id, PositionVector& shape) {
255  Polygon* poly = getPolygon(id);
256  if (poly == 0) {
257  return false;
258  }
259  shape = poly->getShape();
260  return true;
261 }
262 
263 
264 Polygon*
265 TraCIServerAPI_Polygon::getPolygon(const std::string& id) {
267 }
268 
269 
270 NamedRTree*
272  NamedRTree* t = new NamedRTree();
274  const std::map<std::string, Polygon*>& polygons = shapeCont.getPolygons().getMyMap();
275  for (std::map<std::string, Polygon*>::const_iterator i = polygons.begin(); i != polygons.end(); ++i) {
276  Boundary b = (*i).second->getShape().getBoxBoundary();
277  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
278  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
279  t->Insert(cmin, cmax, (*i).second);
280  }
281  return t;
282 }
283 
284 
285 #endif
286 
287 
288 /****************************************************************************/
289 
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:90
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:152
#define TYPE_COMPOUND
static Polygon * getPolygon(const std::string &id)
Returns the named polygon.
bool readTypeCheckingColor(tcpip::Storage &inputStorage, RGBColor &into)
Reads the value type and a color, verifying the type.
static NamedRTree * getTree()
Returns a tree filled with polygon instances.
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:124
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: Polygon.h:82
#define TYPE_UBYTE
#define RTYPE_OK
#define VAR_TYPE
#define TYPE_POLYGON
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:112
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:160
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define TYPE_COLOR
#define TYPE_STRINGLIST
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)
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
#define VAR_SHAPE
const Polygons & getPolygons() const
Returns all polygons.
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:118
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
virtual void writeInt(int)
#define TYPE_STRING
virtual int readUnsignedByte()
T get(const std::string &id) const
Retrieves an item.
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:91
A 2D- or 3D-polygon.
Definition: Polygon.h:52
static const SUMOReal DEFAULT_ANGLE
Definition: Shape.h:151
virtual int readInt()
virtual bool removePolygon(const std::string &id)
Removes a polygon from the container.
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:419
A list of positions.
void insertIDs(std::vector< std::string > &into) const
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:99
virtual void writeStringList(const std::vector< std::string > &s)
T MIN2(T a, T b)
Definition: StdDefs.h:69
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:73
const RGBColor & getColor() const
Returns the color of the Shape.
Definition: Shape.h:79
virtual void reshapePolygon(const std::string &id, const PositionVector &shape)
Assigns a shape to the named polygon.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
const IDMap & getMyMap() const
void addParameter(const std::string &key, const std::string &value)
Adds a parameter.
#define CMD_SET_POLYGON_VARIABLE
virtual void writeString(const std::string &s)
void setType(const std::string &type)
Sets a new type.
Definition: Shape.h:113
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:64
virtual bool addPolygon(const std::string &id, const std::string &type, const RGBColor &color, SUMOReal layer, SUMOReal angle, const std::string &imgFile, const PositionVector &shape, bool fill)
Builds a polygon using the given values and adds it to the container.
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)
#define VAR_FILL
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:83
#define SUMOReal
Definition: config.h:213
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:130
#define VAR_PARAMETER
#define ID_COUNT
const std::string & getType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:71
#define TYPE_INTEGER
#define ID_LIST
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:75
void setFill(bool fill)
Sets whether the polygon shall be filled.
Definition: Polygon.h:103
bool getFill() const
Returns whether the polygon is filled.
Definition: Polygon.h:90
void setColor(const RGBColor &col)
Sets a new color.
Definition: Shape.h:121
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)