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 /****************************************************************************/
11 // APIs for getting/setting polygon values via TraCI
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
14 // Copyright (C) 2002-2014 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) {
61  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Get Polygon Variable: unsupported variable 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  default:
111  break;
112  }
113  }
114  server.writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_OK, "", outputStorage);
115  server.writeResponseWithLength(outputStorage, tempMsg);
116  return true;
117 }
118 
119 
120 bool
122  tcpip::Storage& outputStorage) {
123  std::string warning = ""; // additional description for response
124  // variable
125  int variable = inputStorage.readUnsignedByte();
126  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
127  && variable != ADD && variable != REMOVE) {
128  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Change Polygon State: unsupported variable specified", outputStorage);
129  }
130  // id
131  std::string id = inputStorage.readString();
132  Polygon* p = 0;
134  if (variable != ADD && variable != REMOVE) {
135  p = getPolygon(id);
136  if (p == 0) {
137  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Polygon '" + id + "' is not known", outputStorage);
138  }
139  }
140  // process
141  switch (variable) {
142  case VAR_TYPE: {
143  std::string type;
144  if (!server.readTypeCheckingString(inputStorage, type)) {
145  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
146  }
147  p->setType(type);
148  }
149  break;
150  case VAR_COLOR: {
151  RGBColor col;
152  if (!server.readTypeCheckingColor(inputStorage, col)) {
153  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
154  }
155  p->setColor(col);
156  }
157  break;
158  case VAR_SHAPE: {
159  PositionVector shape;
160  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
161  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The shape must be given using an accoring type.", outputStorage);
162  }
163  shapeCont.reshapePolygon(id, shape);
164  }
165  break;
166  case VAR_FILL: {
167  int value = 0;
168  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
169  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "'fill' must be defined using an unsigned byte.", outputStorage);
170  }
171  p->setFill(value != 0);
172  }
173  break;
174  case ADD: {
175  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
176  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);
177  }
178  //readt itemNo
179  inputStorage.readInt();
180  std::string type;
181  if (!server.readTypeCheckingString(inputStorage, type)) {
182  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
183  }
184  RGBColor col;
185  if (!server.readTypeCheckingColor(inputStorage, col)) {
186  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);
187  }
188  int value = 0;
189  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
190  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
191  }
192  bool fill = value != 0;
193  int layer = 0;
194  if (!server.readTypeCheckingInt(inputStorage, layer)) {
195  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
196  }
197  PositionVector shape;
198  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
199  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);
200  }
201  //
202  if (!shapeCont.addPolygon(id, type, col, (SUMOReal)layer,
204  delete p;
205  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Could not add polygon.", outputStorage);
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  if (!shapeCont.removePolygon(id)) {
215  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "Could not remove polygon '" + id + "'", outputStorage);
216  }
217  }
218  break;
219  default:
220  break;
221  }
222  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_OK, warning, outputStorage);
223  return true;
224 }
225 
226 
227 bool
228 TraCIServerAPI_Polygon::getShape(const std::string& id, PositionVector& shape) {
229  Polygon* poly = getPolygon(id);
230  if (poly == 0) {
231  return false;
232  }
233  shape.push_back(poly->getShape());
234  return true;
235 }
236 
237 
238 Polygon*
239 TraCIServerAPI_Polygon::getPolygon(const std::string& id) {
241 }
242 
243 
244 NamedRTree*
246  NamedRTree* t = new NamedRTree();
248  const std::map<std::string, Polygon*>& polygons = shapeCont.getPolygons().getMyMap();
249  for (std::map<std::string, Polygon*>::const_iterator i = polygons.begin(); i != polygons.end(); ++i) {
250  Boundary b = (*i).second->getShape().getBoxBoundary();
251  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
252  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
253  t->Insert(cmin, cmax, (*i).second);
254  }
255  return t;
256 }
257 
258 
259 #endif
260 
261 
262 /****************************************************************************/
263