SUMO - Simulation of Urban MObility
TraCIServerAPI_GUI.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // APIs for getting/setting GUI values via TraCI
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #ifndef NO_TRACI
33 
34 #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: {
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  }
177  // take screenshot after the current step is finished (showing the same state as sumo-gui and netstate-output)
178  v->addSnapshot(MSNet::getInstance()->getCurrentTimeStep(), filename);
179  }
180  break;
181  case VAR_TRACK_VEHICLE: {
182  std::string id;
183  if (!server.readTypeCheckingString(inputStorage, id)) {
184  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Tracking requires a string vehicle ID.", outputStorage);
185  }
186  if (id == "") {
187  v->stopTrack();
188  } else {
190  if (veh == 0) {
191  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Could not find vehicle '" + id + "'.", outputStorage);
192  }
193  if (v->getTrackedID() != static_cast<GUIVehicle*>(veh)->getGlID()) {
194  v->startTrack(static_cast<GUIVehicle*>(veh)->getGlID());
195  }
196  }
197  }
198  default:
199  break;
200  }
201  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_OK, warning, outputStorage);
202  return true;
203 }
204 
205 
207 TraCIServerAPI_GUI::getNamedView(const std::string& id) {
209  if (mw == 0) {
210  return 0;
211  }
212  GUIGlChildWindow* const c = static_cast<GUIGlChildWindow*>(mw->getViewByID(id));
213  if (c == 0) {
214  return 0;
215  }
216  return c->getView();
217 }
218 
219 
220 #endif
221 
222 
223 /****************************************************************************/
224 
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:137
GUISUMOAbstractView * getView() const
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:131
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:67
GUIVisualizationSettings * getVisualisationSettings() const
get visualitation settings
#define RTYPE_OK
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
void addSnapshot(SUMOTime time, const std::string &file)
Sets the snapshot time to file map.
double x() const
Returns the x-position.
Definition: Position.h:62
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
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:92
#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:47
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
#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:66
static GUIMainWindow * getInstance()
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:306
#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:125
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:69
#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:65
FXMDIChild * getViewByID(const std::string &id) const
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, libsumo::TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
virtual void writeDouble(double)
virtual double getYPos() const =0
Returns the y-offset of the field to show stored in this changer.
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 ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:143
A 3D-position.
Definition: TraCIDefs.h:71
#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:60