SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIServerAPI_GUI.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // APIs for getting/setting GUI 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 <fx.h>
42 #include <guisim/GUINet.h>
43 #include <guisim/GUIVehicle.h>
44 #include "TraCIServerAPI_GUI.h"
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // used namespaces
53 // ===========================================================================
54 using namespace traci;
55 
56 
57 // ===========================================================================
58 // method definitions
59 // ===========================================================================
60 bool
62  tcpip::Storage& outputStorage) {
63  // variable & id
64  int variable = inputStorage.readUnsignedByte();
65  std::string id = inputStorage.readString();
66  // check variable
67  if (variable != ID_LIST && variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET
68  && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY) {
69  return server.writeErrorStatusCmd(CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable specified", outputStorage);
70  }
71  // begin response building
72  tcpip::Storage tempMsg;
73  // response-code, variableID, objectID
75  tempMsg.writeUnsignedByte(variable);
76  tempMsg.writeString(id);
77  // process request
78  if (variable == ID_LIST) {
79  std::vector<std::string> ids = getMainWindow()->getViewIDs();
81  tempMsg.writeStringList(ids);
82  } else {
83  GUISUMOAbstractView* v = getNamedView(id);
84  if (v == 0) {
85  return server.writeErrorStatusCmd(CMD_GET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
86  }
87  switch (variable) {
88  case VAR_VIEW_ZOOM:
90  tempMsg.writeDouble(v->getChanger().getZoom());
91  break;
92  case VAR_VIEW_OFFSET:
94  tempMsg.writeDouble(v->getChanger().getXPos());
95  tempMsg.writeDouble(v->getChanger().getYPos());
96  break;
97  case VAR_VIEW_SCHEMA: {
98  FXComboBox& c = v->getColoringSchemesCombo();
100  tempMsg.writeString((std::string)c.getItem(c.getCurrentItem()).text());
101  break;
102  }
103  case VAR_VIEW_BOUNDARY: {
105  Boundary b = v->getVisibleBoundary();
106  tempMsg.writeDouble(b.xmin());
107  tempMsg.writeDouble(b.ymin());
108  tempMsg.writeDouble(b.xmax());
109  tempMsg.writeDouble(b.ymax());
110  break;
111  }
112  default:
113  break;
114  }
115  }
116  server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_OK, "", outputStorage);
117  server.writeResponseWithLength(outputStorage, tempMsg);
118  return true;
119 }
120 
121 
122 bool
124  tcpip::Storage& outputStorage) {
125  std::string warning = ""; // additional description for response
126  // variable
127  int variable = inputStorage.readUnsignedByte();
128  if (variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY
129  && variable != VAR_SCREENSHOT && variable != VAR_TRACK_VEHICLE
130  ) {
131  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable specified", outputStorage);
132  }
133  // id
134  std::string id = inputStorage.readString();
135  GUISUMOAbstractView* v = getNamedView(id);
136  if (v == 0) {
137  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
138  }
139  // process
140  switch (variable) {
141  case VAR_VIEW_ZOOM: {
142  Position off, p;
143  double zoom = 1;
144  if (!server.readTypeCheckingDouble(inputStorage, zoom)) {
145  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The zoom must be given as a double.", outputStorage);
146  }
147  off.set(v->getChanger().getXPos(), v->getChanger().getYPos(), zoom);
148  v->setViewport(off, p);
149  }
150  break;
151  case VAR_VIEW_OFFSET: {
152  Position off, p;
153  if (!server.readTypeCheckingPosition2D(inputStorage, off)) {
154  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The view port must be given as a position.", outputStorage);
155  }
156  off.set(off.x(), off.y(), v->getChanger().getZoom());
157  v->setViewport(off, p);
158  }
159  break;
160  case VAR_VIEW_SCHEMA: {
161  std::string schema;
162  if (!server.readTypeCheckingString(inputStorage, schema)) {
163  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The scheme must be specified by a string.", outputStorage);
164  }
165  if (!v->setColorScheme(schema)) {
166  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The scheme is not known.", outputStorage);
167  }
168  }
169  break;
170  case VAR_VIEW_BOUNDARY: {
171  Boundary b;
172  if (!server.readTypeCheckingBoundary(inputStorage, b)) {
173  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The boundary must be specified by a bounding box.", outputStorage);
174  }
175  v->centerTo(b);
176  break;
177  }
178  case VAR_SCREENSHOT: {
179  std::string filename;
180  if (!server.readTypeCheckingString(inputStorage, filename)) {
181  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Making a snapshot requires a file name.", outputStorage);
182  }
183  std::string error = v->makeSnapshot(filename);
184  if (error != "") {
185  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, error, outputStorage);
186  }
187  }
188  break;
189  case VAR_TRACK_VEHICLE: {
190  std::string id;
191  if (!server.readTypeCheckingString(inputStorage, id)) {
192  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Tracking requires a string vehicle ID.", outputStorage);
193  }
194  if (id == "") {
195  v->stopTrack();
196  } else {
198  if (veh == 0) {
199  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Could not find vehicle '" + id + "'.", outputStorage);
200  }
201  if (!static_cast<GUIVehicle*>(veh)->hasActiveAddVisualisation(v, GUIVehicle::VO_TRACKED)) {
202  v->startTrack(static_cast<GUIVehicle*>(veh)->getGlID());
203  static_cast<GUIVehicle*>(veh)->addActiveAddVisualisation(v, GUIVehicle::VO_TRACKED);
204  }
205  }
206  }
207  default:
208  break;
209  }
210  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_OK, warning, outputStorage);
211  return true;
212 }
213 
214 
217  FXWindow* w = FXApp::instance()->getRootWindow()->getFirst();
218  while (w != 0 && dynamic_cast<GUIMainWindow*>(w) == 0) {
219  w = w->getNext();
220  }
221  if (w == 0) {
222  // main window not found
223  return 0;
224  }
225  return dynamic_cast<GUIMainWindow*>(w);
226 }
227 
228 
230 TraCIServerAPI_GUI::getNamedView(const std::string& id) {
231  GUIMainWindow* mw = static_cast<GUIMainWindow*>(getMainWindow());
232  if (mw == 0) {
233  return 0;
234  }
235  GUIGlChildWindow* c = static_cast<GUIGlChildWindow*>(mw->getViewByID(id));
236  if (c == 0) {
237  return 0;
238  }
239  return c->getView();
240 }
241 
242 
243 #endif
244 
245 
246 /****************************************************************************/
247 
GUISUMOAbstractView * getView() const
virtual void startTrack(int)
virtual void setViewport(const Position &lookFrom, const Position &lookAt)
applies the given viewport settings
static bool processSet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xcc: Change GUI State)
virtual void centerTo(GUIGlID id, bool applyZoom, SUMOReal zoomDist=20)
centers to the chosen artifact
#define POSITION_2D
virtual SUMOReal getZoom() const =0
Returns the zoom factor computed stored in this changer.
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:124
#define RTYPE_OK
track vehicle
Definition: GUIVehicle.h:256
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:112
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:150
#define TYPE_STRINGLIST
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xac: Get GUI Variable)
static GUIMainWindow * getMainWindow()
Returns the main window.
virtual void writeUnsignedByte(int)
#define CMD_SET_GUI_VARIABLE
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:118
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
#define TYPE_STRING
virtual int readUnsignedByte()
FXMDIChild * getViewByID(const std::string &id) const
bool readTypeCheckingBoundary(tcpip::Storage &inputStorage, Boundary &into)
Reads the value type and a 2D bounding box, verifying the type.
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:76
GUIPerspectiveChanger & getChanger() const
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_SCREENSHOT
#define VAR_VIEW_BOUNDARY
#define VAR_TRACK_VEHICLE
Boundary getVisibleBoundary() const
Representation of a vehicle.
Definition: SUMOVehicle.h:63
virtual SUMOReal getXPos() const =0
Returns the x-offset of the field to show stored in this changer.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:248
#define TYPE_BOUNDINGBOX
#define VAR_VIEW_SCHEMA
virtual void writeStringList(const std::vector< std::string > &s)
FXComboBox & getColoringSchemesCombo()
static GUISUMOAbstractView * getNamedView(const std::string &id)
Returns the named view.
virtual std::string readString()
#define CMD_GET_GUI_VARIABLE
std::string makeSnapshot(const std::string &destFile)
Takes a snapshots and writes it into the given file.
#define RESPONSE_GET_GUI_VARIABLE
#define VAR_VIEW_ZOOM
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
virtual void writeString(const std::string &s)
#define TYPE_DOUBLE
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
void set(SUMOReal x, SUMOReal y)
Definition: Position.h:78
virtual bool setColorScheme(const std::string &)
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
virtual void writeDouble(double)
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:130
virtual SUMOReal getYPos() const =0
Returns the y-offset of the field to show stored in this changer.
#define ID_LIST
#define VAR_VIEW_OFFSET
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, Position &into)
Reads the value type and a 2D position, verifying the type.
A MSVehicle extended by some values for usage within the gui.
Definition: GUIVehicle.h:74