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