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-2017 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 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 bool
53  tcpip::Storage& outputStorage) {
54  // variable & id
55  int variable = inputStorage.readUnsignedByte();
56  std::string id = inputStorage.readString();
57  // check variable
58  if (variable != ID_LIST && variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET
59  && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY) {
60  return server.writeErrorStatusCmd(CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
61  }
62  // begin response building
63  tcpip::Storage tempMsg;
64  // response-code, variableID, objectID
66  tempMsg.writeUnsignedByte(variable);
67  tempMsg.writeString(id);
68  // process request
69  if (variable == ID_LIST) {
70  std::vector<std::string> ids = GUIMainWindow::getInstance()->getViewIDs();
72  tempMsg.writeStringList(ids);
73  } else {
75  if (v == 0) {
76  return server.writeErrorStatusCmd(CMD_GET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
77  }
78  switch (variable) {
79  case VAR_VIEW_ZOOM:
81  tempMsg.writeDouble(v->getChanger().getZoom());
82  break;
83  case VAR_VIEW_OFFSET:
85  tempMsg.writeDouble(v->getChanger().getXPos());
86  tempMsg.writeDouble(v->getChanger().getYPos());
87  break;
88  case VAR_VIEW_SCHEMA: {
91  break;
92  }
93  case VAR_VIEW_BOUNDARY: {
96  tempMsg.writeDouble(b.xmin());
97  tempMsg.writeDouble(b.ymin());
98  tempMsg.writeDouble(b.xmax());
99  tempMsg.writeDouble(b.ymax());
100  break;
101  }
102  default:
103  break;
104  }
105  }
106  server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_OK, "", outputStorage);
107  server.writeResponseWithLength(outputStorage, tempMsg);
108  return true;
109 }
110 
111 
112 bool
114  tcpip::Storage& outputStorage) {
115  std::string warning = ""; // additional description for response
116  // variable
117  int variable = inputStorage.readUnsignedByte();
118  if (variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY
119  && variable != VAR_SCREENSHOT && variable != VAR_TRACK_VEHICLE
120  ) {
121  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
122  }
123  // id
124  std::string id = inputStorage.readString();
126  if (v == 0) {
127  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
128  }
129  // process
130  switch (variable) {
131  case VAR_VIEW_ZOOM: {
132  Position off, p;
133  double zoom = 1;
134  if (!server.readTypeCheckingDouble(inputStorage, zoom)) {
135  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The zoom must be given as a double.", outputStorage);
136  }
137  off.set(v->getChanger().getXPos(), v->getChanger().getYPos(), v->getChanger().zoom2ZPos(zoom));
138  p.set(off.x(), off.y(), 0);
139  v->setViewportFromTo(off, p);
140  }
141  break;
142  case VAR_VIEW_OFFSET: {
143  TraCIPosition tp;
144  if (!server.readTypeCheckingPosition2D(inputStorage, tp)) {
145  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The view port must be given as a position.", outputStorage);
146  }
147 
148  Position off, p;
149  off.set(tp.x, tp.y, v->getChanger().getZPos());
150  p.set(tp.x, tp.y, 0);
151  v->setViewportFromTo(off, p);
152  }
153  break;
154  case VAR_VIEW_SCHEMA: {
155  std::string schema;
156  if (!server.readTypeCheckingString(inputStorage, schema)) {
157  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The scheme must be specified by a string.", outputStorage);
158  }
159  if (!v->setColorScheme(schema)) {
160  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The scheme is not known.", outputStorage);
161  }
162  }
163  break;
164  case VAR_VIEW_BOUNDARY: {
165  Boundary b;
166  if (!server.readTypeCheckingBoundary(inputStorage, b)) {
167  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The boundary must be specified by a bounding box.", outputStorage);
168  }
169  v->centerTo(b);
170  break;
171  }
172  case VAR_SCREENSHOT: {
173  std::string filename;
174  if (!server.readTypeCheckingString(inputStorage, filename)) {
175  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Making a snapshot requires a file name.", outputStorage);
176  }
178  }
179  break;
180  case VAR_TRACK_VEHICLE: {
181  std::string id;
182  if (!server.readTypeCheckingString(inputStorage, id)) {
183  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Tracking requires a string vehicle ID.", outputStorage);
184  }
185  if (id == "") {
186  v->stopTrack();
187  } else {
189  if (veh == 0) {
190  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Could not find vehicle '" + id + "'.", outputStorage);
191  }
192  if (v->getTrackedID() != static_cast<GUIVehicle*>(veh)->getGlID()) {
193  v->startTrack(static_cast<GUIVehicle*>(veh)->getGlID());
194  }
195  }
196  }
197  default:
198  break;
199  }
200  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_OK, warning, outputStorage);
201  return true;
202 }
203 
204 
206 TraCIServerAPI_GUI::getNamedView(const std::string& id) {
208  if (mw == 0) {
209  return 0;
210  }
211  GUIGlChildWindow* const c = static_cast<GUIGlChildWindow*>(mw->getViewByID(id));
212  if (c == 0) {
213  return 0;
214  }
215  return c->getView();
216 }
217 
218 
219 #endif
220 
221 
222 /****************************************************************************/
223 
double x
Definition: TraCIDefs.h:72
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:138
GUISUMOAbstractView * getView() const
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:132
virtual void setViewportFromTo(const Position &lookFrom, const Position &lookAt)
applies the given viewport settings
#define POSITION_2D
virtual double zoom2ZPos(double zoom) const =0
Returns the camera height at which the given zoom level is reached.
double y() const
Returns the y-position.
Definition: Position.h:68
GUIVisualizationSettings * getVisualisationSettings() const
get visualitation settings
#define RTYPE_OK
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
double x() const
Returns the x-position.
Definition: Position.h:63
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
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)
virtual double getZPos() const =0
Returns the camera height corresponding to the current zoom factor.
void set(double x, double y)
set positions x and y
Definition: Position.h:93
#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.
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)
std::vector< std::string > getViewIDs() const
#define TYPE_STRING
virtual int readUnsignedByte()
bool readTypeCheckingBoundary(tcpip::Storage &inputStorage, Boundary &into)
Reads the value type and a 2D bounding box, verifying the type.
virtual GUIGlID getTrackedID() const
get tracked id
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.
Representation of a vehicle.
Definition: SUMOVehicle.h:67
A 3D-position.
Definition: TraCIDefs.h:71
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:310
#define TYPE_BOUNDINGBOX
virtual void centerTo(GUIGlID id, bool applyZoom, double zoomDist=20)
centers to the chosen artifact
#define VAR_VIEW_SCHEMA
virtual void writeStringList(const std::vector< std::string > &s)
GUIPerspectiveChanger & getChanger() const
get changer
static GUISUMOAbstractView * getNamedView(const std::string &id)
Returns the named view.
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:126
virtual std::string readString()
#define CMD_GET_GUI_VARIABLE
virtual double getZoom() const =0
Returns the zoom factor computed stored in this changer.
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
#define RESPONSE_GET_GUI_VARIABLE
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
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:66
FXMDIChild * getViewByID(const std::string &id) const
virtual void writeDouble(double)
virtual double getYPos() const =0
Returns the y-offset of the field to show stored in this changer.
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
Boundary getVisibleBoundary() const
get visible boundary
double y
Definition: TraCIDefs.h:72
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:144
#define ID_LIST
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xac: Get GUI Variable)
virtual double getXPos() const =0
Returns the x-offset of the field to show stored in this changer.
#define VAR_VIEW_OFFSET
A MSVehicle extended by some values for usage within the gui.
Definition: GUIVehicle.h:61