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