SUMO - Simulation of Urban MObility
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.dlr.de/
12 // Copyright (C) 2001-2016 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 <guisim/GUIBaseVehicle.h>
45 #include "GUIEvent_Screenshot.h"
46 #include "TraCIServerAPI_GUI.h"
47 
48 #ifdef CHECK_MEMORY_LEAKS
49 #include <foreign/nvwa/debug_new.h>
50 #endif // CHECK_MEMORY_LEAKS
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_VIEW_ZOOM && variable != VAR_VIEW_OFFSET
64  && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY) {
65  return server.writeErrorStatusCmd(CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable " + toHex(variable, 2) + " 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) {
75  std::vector<std::string> ids = GUIMainWindow::getInstance()->getViewIDs();
77  tempMsg.writeStringList(ids);
78  } else {
80  if (v == 0) {
81  return server.writeErrorStatusCmd(CMD_GET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
82  }
83  switch (variable) {
84  case VAR_VIEW_ZOOM:
86  tempMsg.writeDouble(v->getChanger().getZoom());
87  break;
88  case VAR_VIEW_OFFSET:
90  tempMsg.writeDouble(v->getChanger().getXPos());
91  tempMsg.writeDouble(v->getChanger().getYPos());
92  break;
93  case VAR_VIEW_SCHEMA: {
96  break;
97  }
98  case VAR_VIEW_BOUNDARY: {
100  Boundary b = v->getVisibleBoundary();
101  tempMsg.writeDouble(b.xmin());
102  tempMsg.writeDouble(b.ymin());
103  tempMsg.writeDouble(b.xmax());
104  tempMsg.writeDouble(b.ymax());
105  break;
106  }
107  default:
108  break;
109  }
110  }
111  server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_OK, "", outputStorage);
112  server.writeResponseWithLength(outputStorage, tempMsg);
113  return true;
114 }
115 
116 
117 bool
119  tcpip::Storage& outputStorage) {
120  std::string warning = ""; // additional description for response
121  // variable
122  int variable = inputStorage.readUnsignedByte();
123  if (variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY
124  && variable != VAR_SCREENSHOT && variable != VAR_TRACK_VEHICLE
125  ) {
126  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
127  }
128  // id
129  std::string id = inputStorage.readString();
131  if (v == 0) {
132  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
133  }
134  // process
135  switch (variable) {
136  case VAR_VIEW_ZOOM: {
137  Position off, p;
138  double zoom = 1;
139  if (!server.readTypeCheckingDouble(inputStorage, zoom)) {
140  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The zoom must be given as a double.", outputStorage);
141  }
142  off.set(v->getChanger().getXPos(), v->getChanger().getYPos(), v->getChanger().zoom2ZPos(zoom));
143  p.set(off.x(), off.y(), 0);
144  v->setViewportFromTo(off, p);
145  }
146  break;
147  case VAR_VIEW_OFFSET: {
148  Position off, p;
149  if (!server.readTypeCheckingPosition2D(inputStorage, off)) {
150  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The view port must be given as a position.", outputStorage);
151  }
152  off.set(off.x(), off.y(), v->getChanger().getZPos());
153  p.set(off.x(), off.y(), 0);
154  v->setViewportFromTo(off, p);
155  }
156  break;
157  case VAR_VIEW_SCHEMA: {
158  std::string schema;
159  if (!server.readTypeCheckingString(inputStorage, schema)) {
160  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The scheme must be specified by a string.", outputStorage);
161  }
162  if (!v->setColorScheme(schema)) {
163  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The scheme is not known.", outputStorage);
164  }
165  }
166  break;
167  case VAR_VIEW_BOUNDARY: {
168  Boundary b;
169  if (!server.readTypeCheckingBoundary(inputStorage, b)) {
170  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The boundary must be specified by a bounding box.", outputStorage);
171  }
172  v->centerTo(b);
173  break;
174  }
175  case VAR_SCREENSHOT: {
176  std::string filename;
177  if (!server.readTypeCheckingString(inputStorage, filename)) {
178  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Making a snapshot requires a file name.", outputStorage);
179  }
181  }
182  break;
183  case VAR_TRACK_VEHICLE: {
184  std::string id;
185  if (!server.readTypeCheckingString(inputStorage, id)) {
186  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Tracking requires a string vehicle ID.", outputStorage);
187  }
188  if (id == "") {
189  v->stopTrack();
190  } else {
192  if (veh == 0) {
193  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Could not find vehicle '" + id + "'.", outputStorage);
194  }
195  if (!static_cast<GUIVehicle*>(veh)->hasActiveAddVisualisation(v, GUIBaseVehicle::VO_TRACKED)) {
196  v->startTrack(static_cast<GUIVehicle*>(veh)->getGlID());
197  static_cast<GUIVehicle*>(veh)->addActiveAddVisualisation(v, GUIBaseVehicle::VO_TRACKED);
198  }
199  }
200  }
201  default:
202  break;
203  }
204  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_OK, warning, outputStorage);
205  return true;
206 }
207 
208 
210 TraCIServerAPI_GUI::getNamedView(const std::string& id) {
212  if (mw == 0) {
213  return 0;
214  }
215  GUIGlChildWindow* const c = static_cast<GUIGlChildWindow*>(mw->getViewByID(id));
216  if (c == 0) {
217  return 0;
218  }
219  return c->getView();
220 }
221 
222 
223 #endif
224 
225 
226 /****************************************************************************/
227 
GUISUMOAbstractView * getView() const
virtual void setViewportFromTo(const Position &lookFrom, const Position &lookAt)
applies the given viewport settings
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
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:112
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define TYPE_STRINGLIST
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
virtual void writeUnsignedByte(int)
#define CMD_SET_GUI_VARIABLE
virtual void stopTrack()
stop track
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
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
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xcc: Change GUI State)
#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.
virtual void startTrack(int)
star track
virtual void sendBlockingEvent(GUIEvent *event)
Sends an event from the application thread to the GUI and waits until it is handled.
#define VAR_SCREENSHOT
#define VAR_VIEW_BOUNDARY
#define VAR_TRACK_VEHICLE
std::string name
The name of this setting.
Boundary getVisibleBoundary() const
get visible boundary
Representation of a vehicle.
Definition: SUMOVehicle.h:66
virtual SUMOReal getXPos() const =0
Returns the x-offset of the field to show stored in this changer.
static GUIMainWindow * getInstance()
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:307
#define TYPE_BOUNDINGBOX
#define VAR_VIEW_SCHEMA
virtual void writeStringList(const std::vector< std::string > &s)
static GUISUMOAbstractView * getNamedView(const std::string &id)
Returns the named view.
virtual std::string readString()
#define CMD_GET_GUI_VARIABLE
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:73
#define RESPONSE_GET_GUI_VARIABLE
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
virtual SUMOReal getZPos() const =0
Returns the camera height corresponding to the current zoom factor.
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, Position &into)
Reads the value type and a 2D position, verifying the type.
virtual bool setColorScheme(const std::string &)
set color scheme
#define VAR_VIEW_ZOOM
virtual void writeString(const std::string &s)
#define TYPE_DOUBLE
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:65
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
void set(SUMOReal x, SUMOReal y)
Definition: Position.h:78
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
virtual void writeDouble(double)
GUIPerspectiveChanger & getChanger() const
get changer
GUIVisualizationSettings * getVisualisationSettings() const
get visualitation settings
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
virtual SUMOReal getYPos() const =0
Returns the y-offset of the field to show stored in this changer.
std::vector< std::string > getViewIDs() const
virtual SUMOReal zoom2ZPos(SUMOReal zoom) const =0
Returns the camera height at which the given zoom level is reached.
#define ID_LIST
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xac: Get GUI Variable)
#define VAR_VIEW_OFFSET
A MSVehicle extended by some values for usage within the gui.
Definition: GUIVehicle.h:61