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.sourceforge.net/
12 // Copyright (C) 2001-2012 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>
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  std::string warning = ""; // additional description for response
60  // variable & id
61  int variable = inputStorage.readUnsignedByte();
62  std::string id = inputStorage.readString();
63  // check variable
64  if (variable != ID_LIST && variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
65  && variable != ID_COUNT) {
66  server.writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_ERR, "Get Polygon Variable: unsupported variable specified", outputStorage);
67  return false;
68  }
69  // begin response building
70  tcpip::Storage tempMsg;
71  // response-code, variableID, objectID
73  tempMsg.writeUnsignedByte(variable);
74  tempMsg.writeString(id);
75  // process request
76  if (variable == ID_LIST || variable == ID_COUNT) {
77  std::vector<std::string> ids;
79  for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer(); ++i) {
80  shapeCont.getPolygonCont(i).insertIDs(ids);
81  }
82  if (variable == ID_LIST) {
84  tempMsg.writeStringList(ids);
85  } else {
87  tempMsg.writeInt((int) ids.size());
88  }
89  } else {
90  Polygon* p = 0;
92  for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer() && p == 0; ++i) {
93  p = shapeCont.getPolygonCont(i).get(id);
94  }
95  if (p == 0) {
96  server.writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_ERR, "Polygon '" + id + "' is not known", outputStorage);
97  return false;
98  }
99  switch (variable) {
100  case VAR_TYPE:
102  tempMsg.writeString(p->getType());
103  break;
104  case VAR_COLOR:
105  tempMsg.writeUnsignedByte(TYPE_COLOR);
106  tempMsg.writeUnsignedByte(static_cast<int>(p->getColor().red() * 255. + .5));
107  tempMsg.writeUnsignedByte(static_cast<int>(p->getColor().green() * 255. + .5));
108  tempMsg.writeUnsignedByte(static_cast<int>(p->getColor().blue() * 255. + .5));
109  tempMsg.writeUnsignedByte(255);
110  break;
111  case VAR_SHAPE:
113  tempMsg.writeUnsignedByte(MIN2(static_cast<int>(255), static_cast<int>(p->getShape().size())));
114  for (unsigned int iPoint = 0; iPoint < MIN2(static_cast<size_t>(255), p->getShape().size()); ++iPoint) {
115  tempMsg.writeDouble(p->getShape()[iPoint].x());
116  tempMsg.writeDouble(p->getShape()[iPoint].y());
117  }
118  break;
119  case VAR_FILL:
120  tempMsg.writeUnsignedByte(TYPE_UBYTE);
121  tempMsg.writeUnsignedByte(p->fill() ? 1 : 0);
122  break;
123  default:
124  break;
125  }
126  }
127  server.writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_OK, warning, outputStorage);
128  server.writeResponseWithLength(outputStorage, tempMsg);
129  return true;
130 }
131 
132 
133 bool
135  tcpip::Storage& outputStorage) {
136  std::string warning = ""; // additional description for response
137  // variable
138  int variable = inputStorage.readUnsignedByte();
139  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
140  && variable != ADD && variable != REMOVE) {
141  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "Change Polygon State: unsupported variable specified", outputStorage);
142  return false;
143  }
144  // id
145  std::string id = inputStorage.readString();
146  Polygon* p = 0;
147  int layer = 0;
149  if (variable != ADD && variable != REMOVE) {
150  for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer() && p == 0; ++i) {
151  p = shapeCont.getPolygonCont(i).get(id);
152  layer = i;
153  }
154  if (p == 0) {
155  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "Polygon '" + id + "' is not known", outputStorage);
156  return false;
157  }
158  }
159  // process
160  int valueDataType = inputStorage.readUnsignedByte();
161  switch (variable) {
162  case VAR_TYPE: {
163  if (valueDataType != TYPE_STRING) {
164  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The type must be given as a string.", outputStorage);
165  return false;
166  }
167  std::string type = inputStorage.readString();
168  p->setType(type);
169  }
170  break;
171  case VAR_COLOR: {
172  if (valueDataType != TYPE_COLOR) {
173  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The color must be given using an accoring type.", outputStorage);
174  return false;
175  }
176  SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
177  SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
178  SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
179  //read SUMOReal a
180  inputStorage.readUnsignedByte();
181  p->setColor(RGBColor(r, g, b));
182  }
183  break;
184  case VAR_SHAPE: {
185  if (valueDataType != TYPE_POLYGON) {
186  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The shape must be given using an accoring type.", outputStorage);
187  return false;
188  }
189  unsigned int noEntries = inputStorage.readUnsignedByte();
190  PositionVector shape;
191  for (unsigned int i = 0; i < noEntries; ++i) {
192  SUMOReal x = inputStorage.readDouble();
193  SUMOReal y = inputStorage.readDouble();
194  shape.push_back(Position(x, y));
195  }
196  shapeCont.reshapePolygon(layer, id, shape);
197  }
198  break;
199  case VAR_FILL: {
200  if (valueDataType != TYPE_UBYTE) {
201  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "'fill' must be defined using an unsigned byte.", outputStorage);
202  return false;
203  }
204  bool fill = inputStorage.readUnsignedByte() != 0;
205  p->setFill(fill);
206  }
207  break;
208  case ADD: {
209  if (valueDataType != TYPE_COMPOUND) {
210  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "A compound object is needed for setting a new polygon.", outputStorage);
211  return false;
212  }
213  //readt itemNo
214  inputStorage.readInt();
215  // type
216  if (inputStorage.readUnsignedByte() != TYPE_STRING) {
217  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The first polygon parameter must be the type encoded as a string.", outputStorage);
218  return false;
219  }
220  std::string type = inputStorage.readString();
221  // color
222  if (inputStorage.readUnsignedByte() != TYPE_COLOR) {
223  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The second polygon parameter must be the color.", outputStorage);
224  return false;
225  }
226  SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
227  SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
228  SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
229  //read SUMOReal a
230  inputStorage.readUnsignedByte();
231  // fill
232  if (inputStorage.readUnsignedByte() != TYPE_UBYTE) {
233  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
234  return false;
235  }
236  bool fill = inputStorage.readUnsignedByte() != 0;
237  // layer
238  if (inputStorage.readUnsignedByte() != TYPE_INTEGER) {
239  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
240  return false;
241  }
242  layer = inputStorage.readInt();
243  // shape
244  if (inputStorage.readUnsignedByte() != TYPE_POLYGON) {
245  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The fifth polygon parameter must be the shape.", outputStorage);
246  return false;
247  }
248  unsigned int noEntries = inputStorage.readUnsignedByte();
249  PositionVector shape;
250  for (unsigned int i = 0; i < noEntries; ++i) {
251  SUMOReal x = inputStorage.readDouble();
252  SUMOReal y = inputStorage.readDouble();
253  shape.push_back(Position(x, y));
254  }
255  //
256  if (!shapeCont.addPolygon(id, layer, type, RGBColor(r, g, b), fill, shape)) {
257  delete p;
258  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "Could not add polygon.", outputStorage);
259  return false;
260  }
261  }
262  break;
263  case REMOVE: {
264  if (valueDataType != TYPE_INTEGER) {
265  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The layer must be given using an int.", outputStorage);
266  return false;
267  }
268  layer = inputStorage.readInt();
269  if (!shapeCont.removePolygon(layer, id)) {
270  bool removed = false;
271  for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer(); ++i) {
272  removed |= shapeCont.removePolygon(i, id);
273  }
274  if (!removed) {
275  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "Could not remove PoI '" + id + "'", outputStorage);
276  return false;
277  }
278  }
279  }
280  break;
281  default:
282  break;
283  }
284  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_OK, warning, outputStorage);
285  return true;
286 }
287 
288 #endif
289 
290 
291 /****************************************************************************/
292