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 // 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) {
59  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Get Polygon Variable: unsupported variable 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;
71  shapeCont.getPolygons().insertIDs(ids);
72  if (variable == ID_LIST) {
74  tempMsg.writeStringList(ids);
75  } else {
77  tempMsg.writeInt((int) ids.size());
78  }
79  } else {
80  Polygon* p = getPolygon(id);
81  if (p == 0) {
82  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Polygon '" + id + "' is not known", outputStorage);
83  }
84  switch (variable) {
85  case VAR_TYPE:
87  tempMsg.writeString(p->getType());
88  break;
89  case VAR_COLOR:
91  tempMsg.writeUnsignedByte(p->getColor().red());
92  tempMsg.writeUnsignedByte(p->getColor().green());
93  tempMsg.writeUnsignedByte(p->getColor().blue());
94  tempMsg.writeUnsignedByte(p->getColor().alpha());
95  break;
96  case VAR_SHAPE:
98  tempMsg.writeUnsignedByte(MIN2(static_cast<int>(255), static_cast<int>(p->getShape().size())));
99  for (unsigned int iPoint = 0; iPoint < MIN2(static_cast<size_t>(255), p->getShape().size()); ++iPoint) {
100  tempMsg.writeDouble(p->getShape()[iPoint].x());
101  tempMsg.writeDouble(p->getShape()[iPoint].y());
102  }
103  break;
104  case VAR_FILL:
105  tempMsg.writeUnsignedByte(TYPE_UBYTE);
106  tempMsg.writeUnsignedByte(p->getFill() ? 1 : 0);
107  break;
108  default:
109  break;
110  }
111  }
112  server.writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_OK, "", outputStorage);
113  server.writeResponseWithLength(outputStorage, tempMsg);
114  return true;
115 }
116 
117 
118 bool
120  tcpip::Storage& outputStorage) {
121  std::string warning = ""; // additional description for response
122  // variable
123  int variable = inputStorage.readUnsignedByte();
124  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
125  && variable != ADD && variable != REMOVE) {
126  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Change Polygon State: unsupported variable specified", outputStorage);
127  }
128  // id
129  std::string id = inputStorage.readString();
130  Polygon* p = 0;
132  if (variable != ADD && variable != REMOVE) {
133  p = getPolygon(id);
134  if (p == 0) {
135  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Polygon '" + id + "' is not known", outputStorage);
136  }
137  }
138  // process
139  switch (variable) {
140  case VAR_TYPE: {
141  std::string type;
142  if (!server.readTypeCheckingString(inputStorage, type)) {
143  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
144  }
145  p->setType(type);
146  }
147  break;
148  case VAR_COLOR: {
149  RGBColor col;
150  if (!server.readTypeCheckingColor(inputStorage, col)) {
151  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
152  }
153  p->setColor(col);
154  }
155  break;
156  case VAR_SHAPE: {
157  PositionVector shape;
158  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
159  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The shape must be given using an accoring type.", outputStorage);
160  }
161  shapeCont.reshapePolygon(id, shape);
162  }
163  break;
164  case VAR_FILL: {
165  int value = 0;
166  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
167  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "'fill' must be defined using an unsigned byte.", outputStorage);
168  }
169  p->setFill(value != 0);
170  }
171  break;
172  case ADD: {
173  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
174  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);
175  }
176  //readt itemNo
177  inputStorage.readInt();
178  std::string type;
179  if (!server.readTypeCheckingString(inputStorage, type)) {
180  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
181  }
182  RGBColor col;
183  if (!server.readTypeCheckingColor(inputStorage, col)) {
184  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);
185  }
186  int value = 0;
187  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
188  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
189  }
190  bool fill = value != 0;
191  int layer = 0;
192  if (!server.readTypeCheckingInt(inputStorage, layer)) {
193  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
194  }
195  PositionVector shape;
196  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
197  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);
198  }
199  //
200  if (!shapeCont.addPolygon(id, type, col, (SUMOReal)layer,
202  delete p;
203  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Could not add polygon.", outputStorage);
204  }
205  }
206  break;
207  case REMOVE: {
208  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
209  if (!server.readTypeCheckingInt(inputStorage, layer)) {
210  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The layer must be given using an int.", outputStorage);
211  }
212  if (!shapeCont.removePolygon(id)) {
213  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Could not remove polygon '" + id + "'", outputStorage);
214  }
215  }
216  break;
217  default:
218  break;
219  }
220  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_OK, warning, outputStorage);
221  return true;
222 }
223 
224 
225 bool
226 TraCIServerAPI_Polygon::getShape(const std::string& id, PositionVector& shape) {
227  Polygon* poly = getPolygon(id);
228  if (poly == 0) {
229  return false;
230  }
231  shape.push_back(poly->getShape());
232  return true;
233 }
234 
235 
236 Polygon*
237 TraCIServerAPI_Polygon::getPolygon(const std::string& id) {
239 }
240 
241 
242 NamedRTree*
244  NamedRTree* t = new NamedRTree();
246  const std::map<std::string, Polygon*>& polygons = shapeCont.getPolygons().getMyMap();
247  for (std::map<std::string, Polygon*>::const_iterator i = polygons.begin(); i != polygons.end(); ++i) {
248  Boundary b = (*i).second->getShape().getBoxBoundary();
249  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
250  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
251  t->Insert(cmin, cmax, (*i).second);
252  }
253  return t;
254 }
255 
256 
257 #endif
258 
259 
260 /****************************************************************************/
261 
void Insert(const float a_min[2], const float a_max[2], Named *a_data)
Insert entry.
Definition: NamedRTree.h:79
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:151
#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:78
#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:60
#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:150
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)
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:48
static const SUMOReal DEFAULT_ANGLE
Definition: Shape.h:150
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:354
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:57
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
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.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
const IDMap & getMyMap() const
void push_back(const PositionVector &p)
Appends all positions from the given vector.
#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: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)
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:215
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 ID_COUNT
const std::string & getType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:70
#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:99
bool getFill() const
Returns whether the polygon is filled.
Definition: Polygon.h:86
void setColor(const RGBColor &col)
Sets a new color.
Definition: Shape.h:120
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)