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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // APIs for getting/setting GUI values via TraCI
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <fx.h>
34 #include <guisim/GUINet.h>
35 #include <guisim/GUIVehicle.h>
36 #include <guisim/GUIBaseVehicle.h>
37 #include "TraCIServerAPI_GUI.h"
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
43 bool
45  tcpip::Storage& outputStorage) {
46  // variable & id
47  int variable = inputStorage.readUnsignedByte();
48  std::string id = inputStorage.readString();
49  // check variable
50  if (variable != TRACI_ID_LIST && variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET
51  && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY && variable != VAR_HAS_VIEW) {
52  return server.writeErrorStatusCmd(CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
53  }
54  // begin response building
55  tcpip::Storage tempMsg;
56  // response-code, variableID, objectID
58  tempMsg.writeUnsignedByte(variable);
59  tempMsg.writeString(id);
60  // process request
61  if (variable == TRACI_ID_LIST) {
62  std::vector<std::string> ids = GUIMainWindow::getInstance()->getViewIDs();
64  tempMsg.writeStringList(ids);
65  } else {
67  if (v == nullptr && variable != VAR_HAS_VIEW) {
68  return server.writeErrorStatusCmd(CMD_GET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
69  }
70  switch (variable) {
71  case VAR_VIEW_ZOOM:
73  tempMsg.writeDouble(v->getChanger().getZoom());
74  break;
75  case VAR_VIEW_OFFSET:
77  tempMsg.writeDouble(v->getChanger().getXPos());
78  tempMsg.writeDouble(v->getChanger().getYPos());
79  break;
80  case VAR_VIEW_SCHEMA:
83  break;
84  case VAR_VIEW_BOUNDARY: {
87  tempMsg.writeByte(2);
88  tempMsg.writeDouble(b.xmin());
89  tempMsg.writeDouble(b.ymin());
90  tempMsg.writeDouble(b.xmax());
91  tempMsg.writeDouble(b.ymax());
92  break;
93  }
94  case VAR_HAS_VIEW:
96  tempMsg.writeInt(v != nullptr ? 1 : 0);
97  break;
98  default:
99  break;
100  }
101  }
102  server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_OK, "", outputStorage);
103  server.writeResponseWithLength(outputStorage, tempMsg);
104  return true;
105 }
106 
107 
108 bool
110  tcpip::Storage& outputStorage) {
111  std::string warning = ""; // additional description for response
112  // variable
113  int variable = inputStorage.readUnsignedByte();
114  if (variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY
115  && variable != VAR_SCREENSHOT && variable != VAR_TRACK_VEHICLE
116  ) {
117  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
118  }
119  // id
120  std::string id = inputStorage.readString();
122  if (v == nullptr) {
123  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
124  }
125  // process
126  switch (variable) {
127  case VAR_VIEW_ZOOM: {
128  Position off, p;
129  double zoom = 1;
130  if (!server.readTypeCheckingDouble(inputStorage, zoom)) {
131  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The zoom must be given as a double.", outputStorage);
132  }
133  off.set(v->getChanger().getXPos(), v->getChanger().getYPos(), v->getChanger().zoom2ZPos(zoom));
134  p.set(off.x(), off.y(), 0);
135  v->setViewportFromToRot(off, p, v->getChanger().getRotation());
136  }
137  break;
138  case VAR_VIEW_OFFSET: {
140  if (!server.readTypeCheckingPosition2D(inputStorage, tp)) {
141  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The view port must be given as a position.", outputStorage);
142  }
143 
144  Position off, p;
145  off.set(tp.x, tp.y, v->getChanger().getZPos());
146  p.set(tp.x, tp.y, 0);
147  v->setViewportFromToRot(off, p, v->getChanger().getRotation());
148  }
149  break;
150  case VAR_VIEW_SCHEMA: {
151  std::string schema;
152  if (!server.readTypeCheckingString(inputStorage, schema)) {
153  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The scheme must be specified by a string.", outputStorage);
154  }
155  if (!v->setColorScheme(schema)) {
156  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The scheme is not known.", outputStorage);
157  }
158  }
159  break;
160  case VAR_VIEW_BOUNDARY: {
161  PositionVector p;
162  if (!server.readTypeCheckingPolygon(inputStorage, p)) {
163  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The boundary must be specified by a bounding box.", outputStorage);
164  }
165  v->centerTo(Boundary(p[0].x(), p[0].y(), p[1].x(), p[1].y()));
166  break;
167  }
168  case VAR_SCREENSHOT: {
169  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
170  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Screenshot requires a compound object.", outputStorage);
171  }
172  int parameterCount = inputStorage.readInt();
173  if (parameterCount != 3) {
174  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Screenshot requires three values as parameter.", outputStorage);
175  }
176  std::string filename;
177  if (!server.readTypeCheckingString(inputStorage, filename)) {
178  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The first variable must be a file name.", outputStorage);
179  }
180  int width = 0, height = 0;
181  if (!server.readTypeCheckingInt(inputStorage, width)) {
182  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The second variable must be the width given as int.", outputStorage);
183  }
184  if (!server.readTypeCheckingInt(inputStorage, height)) {
185  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The third variable must be the height given as int.", outputStorage);
186  }
187  // take screenshot after the current step is finished (showing the same state as sumo-gui and netstate-output)
188  v->addSnapshot(MSNet::getInstance()->getCurrentTimeStep(), filename, width, height);
189  }
190  break;
191  case VAR_TRACK_VEHICLE: {
192  std::string id;
193  if (!server.readTypeCheckingString(inputStorage, id)) {
194  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Tracking requires a string vehicle ID.", outputStorage);
195  }
196  if (id == "") {
197  v->stopTrack();
198  } else {
200  if (veh == nullptr) {
201  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Could not find vehicle '" + id + "'.", outputStorage);
202  }
203  if (v->getTrackedID() != static_cast<GUIVehicle*>(veh)->getGlID()) {
204  v->startTrack(static_cast<GUIVehicle*>(veh)->getGlID());
205  }
206  }
207  }
208  default:
209  break;
210  }
211  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_OK, warning, outputStorage);
212  return true;
213 }
214 
215 
217 TraCIServerAPI_GUI::getNamedView(const std::string& id) {
219  if (mw == nullptr) {
220  return nullptr;
221  }
222  GUIGlChildWindow* const c = mw->getViewByID(id);
223  if (c == nullptr) {
224  return nullptr;
225  }
226  return c->getView();
227 }
228 
229 
230 /****************************************************************************/
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:131
GUISUMOAbstractView * getView() const
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:125
#define TYPE_COMPOUND
#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:62
GUIVisualizationSettings * getVisualisationSettings() const
get visualitation settings
#define RTYPE_OK
#define TYPE_POLYGON
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
double x() const
Returns the x-position.
Definition: Position.h:57
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:165
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 double getRotation() const =0
Returns the rotation of the canvas stored in this changer.
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, 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:87
#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:42
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xcc: Change GUI State)
virtual void writeInt(int)
std::vector< std::string > getViewIDs() const
#define TYPE_STRING
virtual int readUnsignedByte()
virtual GUIGlID getTrackedID() const
get tracked id
virtual void startTrack(int)
star track
#define VAR_SCREENSHOT
#define VAR_VIEW_BOUNDARY
#define TRACI_ID_LIST
#define VAR_TRACK_VEHICLE
std::string name
The name of this setting.
Representation of a vehicle.
Definition: SUMOVehicle.h:60
virtual int readInt()
static GUIMainWindow * getInstance()
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
A list of positions.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:316
virtual void writeByte(int)
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:119
virtual std::string readString()
#define CMD_GET_GUI_VARIABLE
virtual double getZoom() const =0
Returns the zoom factor computed stored in this changer.
virtual void setViewportFromToRot(const Position &lookFrom, const Position &lookAt, double rotation)
applies the given viewport settings
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:62
#define RESPONSE_GET_GUI_VARIABLE
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
#define VAR_HAS_VIEW
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:59
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 addSnapshot(SUMOTime time, const std::string &file, const int width=-1, const int height=-1)
Sets the snapshot time to file map.
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
GUIGlChildWindow * getViewByID(const std::string &id) const
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:137
A 3D-position.
Definition: TraCIDefs.h:107
#define TYPE_INTEGER
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:54