SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIServerAPI_Polygon.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // APIs for getting/setting polygon values via TraCI
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifndef NO_TRACI
34 
35 #include <utils/common/StdDefs.h>
36 #include <microsim/MSNet.h>
37 #include <utils/shapes/Polygon.h>
39 #include "TraCIConstants.h"
40 #include "TraCIServerAPI_Polygon.h"
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // used namespaces
49 // ===========================================================================
50 using namespace traci;
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 bool
58  tcpip::Storage& outputStorage) {
59  // variable & id
60  int variable = inputStorage.readUnsignedByte();
61  std::string id = inputStorage.readString();
62  // check variable
63  if (variable != ID_LIST && variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
64  && variable != ID_COUNT) {
65  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Get Polygon Variable: unsupported variable specified", outputStorage);
66  }
67  // begin response building
68  tcpip::Storage tempMsg;
69  // response-code, variableID, objectID
71  tempMsg.writeUnsignedByte(variable);
72  tempMsg.writeString(id);
73  // process request
74  if (variable == ID_LIST || variable == ID_COUNT) {
75  std::vector<std::string> ids;
77  shapeCont.getPolygons().insertIDs(ids);
78  if (variable == ID_LIST) {
80  tempMsg.writeStringList(ids);
81  } else {
83  tempMsg.writeInt((int) ids.size());
84  }
85  } else {
86  Polygon* p = getPolygon(id);
87  if (p == 0) {
88  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Polygon '" + id + "' is not known", outputStorage);
89  }
90  switch (variable) {
91  case VAR_TYPE:
93  tempMsg.writeString(p->getType());
94  break;
95  case VAR_COLOR:
97  tempMsg.writeUnsignedByte(p->getColor().red());
98  tempMsg.writeUnsignedByte(p->getColor().green());
99  tempMsg.writeUnsignedByte(p->getColor().blue());
100  tempMsg.writeUnsignedByte(p->getColor().alpha());
101  break;
102  case VAR_SHAPE:
104  tempMsg.writeUnsignedByte(MIN2(static_cast<int>(255), static_cast<int>(p->getShape().size())));
105  for (unsigned int iPoint = 0; iPoint < MIN2(static_cast<size_t>(255), p->getShape().size()); ++iPoint) {
106  tempMsg.writeDouble(p->getShape()[iPoint].x());
107  tempMsg.writeDouble(p->getShape()[iPoint].y());
108  }
109  break;
110  case VAR_FILL:
111  tempMsg.writeUnsignedByte(TYPE_UBYTE);
112  tempMsg.writeUnsignedByte(p->getFill() ? 1 : 0);
113  break;
114  default:
115  break;
116  }
117  }
118  server.writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_OK, "", outputStorage);
119  server.writeResponseWithLength(outputStorage, tempMsg);
120  return true;
121 }
122 
123 
124 bool
126  tcpip::Storage& outputStorage) {
127  std::string warning = ""; // additional description for response
128  // variable
129  int variable = inputStorage.readUnsignedByte();
130  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
131  && variable != ADD && variable != REMOVE) {
132  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Change Polygon State: unsupported variable specified", outputStorage);
133  }
134  // id
135  std::string id = inputStorage.readString();
136  Polygon* p = 0;
138  if (variable != ADD && variable != REMOVE) {
139  p = getPolygon(id);
140  if (p == 0) {
141  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Polygon '" + id + "' is not known", outputStorage);
142  }
143  }
144  // process
145  switch (variable) {
146  case VAR_TYPE: {
147  std::string type;
148  if (!server.readTypeCheckingString(inputStorage, type)) {
149  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
150  }
151  p->setType(type);
152  }
153  break;
154  case VAR_COLOR: {
155  RGBColor col;
156  if (!server.readTypeCheckingColor(inputStorage, col)) {
157  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
158  }
159  p->setColor(col);
160  }
161  break;
162  case VAR_SHAPE: {
163  PositionVector shape;
164  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
165  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The shape must be given using an accoring type.", outputStorage);
166  }
167  shapeCont.reshapePolygon(id, shape);
168  }
169  break;
170  case VAR_FILL: {
171  int value = 0;
172  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
173  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "'fill' must be defined using an unsigned byte.", outputStorage);
174  }
175  p->setFill(value != 0);
176  }
177  break;
178  case ADD: {
179  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
180  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);
181  }
182  //readt itemNo
183  inputStorage.readInt();
184  std::string type;
185  if (!server.readTypeCheckingString(inputStorage, type)) {
186  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
187  }
188  RGBColor col;
189  if (!server.readTypeCheckingColor(inputStorage, col)) {
190  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);
191  }
192  int value = 0;
193  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
194  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
195  }
196  bool fill = value != 0;
197  int layer = 0;
198  if (!server.readTypeCheckingInt(inputStorage, layer)) {
199  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
200  }
201  PositionVector shape;
202  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
203  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);
204  }
205  //
206  if (!shapeCont.addPolygon(id, type, col, (SUMOReal)layer,
208  delete p;
209  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Could not add polygon.", outputStorage);
210  }
211  }
212  break;
213  case REMOVE: {
214  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
215  if (!server.readTypeCheckingInt(inputStorage, layer)) {
216  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The layer must be given using an int.", outputStorage);
217  }
218  if (!shapeCont.removePolygon(id)) {
219  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Could not remove polygon '" + id + "'", outputStorage);
220  }
221  }
222  break;
223  default:
224  break;
225  }
226  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_OK, warning, outputStorage);
227  return true;
228 }
229 
230 
231 bool
232 TraCIServerAPI_Polygon::getShape(const std::string& id, PositionVector& shape) {
233  Polygon* poly = getPolygon(id);
234  if (poly == 0) {
235  return false;
236  }
237  shape.push_back(poly->getShape());
238  return true;
239 }
240 
241 
242 Polygon*
243 TraCIServerAPI_Polygon::getPolygon(const std::string& id) {
245 }
246 
247 
248 TraCIRTree*
250  TraCIRTree* t = new TraCIRTree();
252  const std::map<std::string, Polygon*>& polygons = shapeCont.getPolygons().getMyMap();
253  for (std::map<std::string, Polygon*>::const_iterator i = polygons.begin(); i != polygons.end(); ++i) {
254  Boundary b = (*i).second->getShape().getBoxBoundary();
255  t->addObject((*i).second, b);
256  }
257  return t;
258 }
259 
260 
261 #endif
262 
263 
264 /****************************************************************************/
265 
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:150
#define TYPE_COMPOUND
static Polygon * getPolygon(const std::string &id)
Returns the named polygon.
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: Polygon.h:75
#define TYPE_UBYTE
#define RTYPE_OK
#define VAR_TYPE
#define TYPE_POLYGON
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
#define VAR_COLOR
static bool processSet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc8: Change Polygon State)
#define RESPONSE_GET_POLYGON_VARIABLE
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:150
#define TYPE_COLOR
#define TYPE_STRINGLIST
Storage for geometrical objects.
#define CMD_GET_POLYGON_VARIABLE
virtual void writeUnsignedByte(int)
#define VAR_SHAPE
const Polygons & getPolygons() const
Returns all polygons.
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:48
static const SUMOReal DEFAULT_ANGLE
Definition: Shape.h:149
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:76
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
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:349
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)
void addObject(Named *o, Boundary &b)
Adds an additional object (detector/shape/trigger) for visualisation.
Definition: TraCIRTree.h:115
T MIN2(T a, T b)
Definition: StdDefs.h:57
virtual std::string readString()
#define ADD
#define REMOVE
const RGBColor & getColor() const
Returns the color of the Shape.
Definition: Shape.h:78
virtual void reshapePolygon(const std::string &id, const PositionVector &shape)
Assigns a shape to the named polygon.
const IDMap & getMyMap() const
void push_back(const PositionVector &p)
Appends all positions from the given vector.
A RT-tree for efficient storing of SUMO&#39;s GL-objects.
Definition: TraCIRTree.h:59
#define CMD_SET_POLYGON_VARIABLE
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
virtual void writeString(const std::string &s)
void setType(const std::string &type)
Sets a new type.
Definition: Shape.h:112
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)
#define VAR_FILL
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:83
#define SUMOReal
Definition: config.h:221
#define ID_COUNT
bool readTypeCheckingUnsignedByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and an unsigned byte, verifying the type.
const std::string & getType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:70
bool readTypeCheckingColor(tcpip::Storage &inputStorage, RGBColor &into)
Reads the value type and a color, verifying the type.
#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:94
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
bool getFill() const
Returns whether the polygon is filled.
Definition: Polygon.h:82
void setColor(const RGBColor &col)
Sets a new color.
Definition: Shape.h:120
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
static TraCIRTree * getTree()
Returns a tree filled with inductive loop instances.
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
static bool getShape(const std::string &id, PositionVector &shape)
Returns the named polygons&#39;s shape.
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa8: Get Polygon Variable)