SUMO - Simulation of Urban MObility
GNEVariableSpeedSign.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 //
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <string>
31 #include <iostream>
32 #include <utility>
38 #include <utils/common/ToString.h>
39 #include <utils/geom/GeomHelper.h>
46 #include <utils/gui/div/GLHelper.h>
50 
51 #include "GNEVariableSpeedSign.h"
52 #include "GNELane.h"
53 #include "GNEViewNet.h"
54 #include "GNEUndoList.h"
55 #include "GNENet.h"
56 #include "GNEChange_Attribute.h"
58 
59 
60 // ===========================================================================
61 // member method definitions
62 // ===========================================================================
63 
64 GNEVariableSpeedSign::GNEVariableSpeedSign(const std::string& id, GNEViewNet* viewNet, Position pos, std::vector<GNELane*> /* lanes */, const std::string& filename, const std::vector<GNEVariableSpeedSignStep>& steps) :
66  myPosition(pos),
67  myFilename(filename),
68  mySteps(steps),
69  mySaveInFilename(false) {
70  // Update geometry;
72 }
73 
74 
76 }
77 
78 
79 void
81  // Clear shape
82  myShape.clear();
83 
84  // Set block icon position
86 
87  // Set block icon offset
88  myBlockIconOffset = Position(-0.5, -0.5);
89 
90  // Set block icon rotation, and using their rotation for draw logo
92 
93  // Set position
94  myShape.push_back(myPosition);
95  /*
96  // Add shape of childs (To avoid graphics errors)
97  for (childLanes::iterator i = myChildLanes.begin(); i != myChildLanes.end(); i++) {
98  myShape.append(i->lane->getShape());
99  }
100 
101  // Update connections
102  updateConnections();
103  */
104  // Refresh element (neccesary to avoid grabbing problems)
106 }
107 
108 
109 Position
111  return myPosition;
112 }
113 
114 
115 void
117  GNEVariableSpeedSignDialog variableSpeedSignDialog(this);
118 }
119 
120 
121 void
123  // change Position
124  myPosition = newPosition;
125  updateGeometry();
126 }
127 
128 
129 void
131  undoList->p_begin("position of " + toString(getTag()));
132  undoList->p_add(new GNEChange_Attribute(this, SUMO_ATTR_POSITION, toString(myPosition), true, toString(oldPos)));
133  undoList->p_end();
134  // Refresh element
136 }
137 
138 
139 void
140 GNEVariableSpeedSign::writeAdditional(OutputDevice& device, bool /*volatileOptionsEnabled*/) const {
141  // Write parameters
142  device.openTag(getTag());
143  device.writeAttr(SUMO_ATTR_ID, getID());
144  device.writeAttr(SUMO_ATTR_LANES, /*joinToString(getLaneChildIds(), " ").c_str()*/"");
145  device.writeAttr(SUMO_ATTR_X, myPosition.x());
146  device.writeAttr(SUMO_ATTR_Y, myPosition.y());
147  // If filenam isn't empty and save in filename is enabled, save in a different file. In other case, save in the same additional XML
148  if (!myFilename.empty() && mySaveInFilename) {
149  // Write filename attribute
151  // Save values in a different file
152  /*
153  OutputDevice& deviceVSS = OutputDevice::getDevice(currentDirectory + myFilename);
154  deviceVSS.openTag("VSS");
155  for (std::vector<GNEVariableSpeedSignStep>::const_iterator i = myVSSValues.begin(); i != myVSSValues.end(); ++i) {
156  // Open VSS tag
157  deviceVSS.openTag(SUMO_TAG_STEP);
158  // Write TimeSTep
159  deviceVSS.writeAttr(SUMO_ATTR_TIME, i->getTime());
160  // Write speed
161  deviceVSS.writeAttr(SUMO_ATTR_SPEED, i->getSpeed());
162  // Close VSS tag
163  deviceVSS.closeTag();
164  }
165  deviceVSS.close();
166  */
167  } else {
168  for (std::vector<GNEVariableSpeedSignStep>::const_iterator i = mySteps.begin(); i != mySteps.end(); ++i) {
169  // Open VSS tag
170  device.openTag(SUMO_TAG_STEP);
171  // Write TimeSTep
172  device.writeAttr(SUMO_ATTR_TIME, i->getTime());
173  // Write speed
174  device.writeAttr(SUMO_ATTR_SPEED, i->getSpeed());
175  // Close VSS tag
176  device.closeTag();
177  }
178  }
179  if (myBlocked) {
181  }
182  // Close tag
183  device.closeTag();
184 }
185 
186 
187 const std::string&
189  return myFilename;
190 }
191 
192 
193 const std::vector<GNEVariableSpeedSignStep>&
195  return mySteps;
196 }
197 
198 
199 void
200 GNEVariableSpeedSign::setFilename(const std::string& filename) {
201  myFilename = filename;
202 }
203 
204 
205 void
206 GNEVariableSpeedSign::setVariableSpeedSignSteps(const std::vector<GNEVariableSpeedSignStep>& steps) {
207  mySteps = steps;
208 }
209 
210 
211 void
213  mySteps.push_back(step);
214 }
215 
216 
217 const std::string&
219  return myViewNet->getNet()->getMicrosimID();
220 }
221 
222 
223 void
225  // Start drawing adding an gl identificator
226  glPushName(getGlID());
227 
228  // Add a draw matrix for drawing logo
229  glPushMatrix();
230  glTranslated(myShape[0].x(), myShape[0].y(), getType());
231  glColor3d(1, 1, 1);
232  glRotated(180, 0, 0, 1);
233 
234  // Draw icon depending of variable speed sign is or isn't selected
235  if (isAdditionalSelected()) {
237  } else {
239  }
240 
241  // Pop draw icon matrix
242  glPopMatrix();
243 
244  // Show Lock icon depending of the Edit mode
245  drawLockIcon(0.4);
246 
247  // Push matrix to draw every symbol over lane
248  glPushMatrix();
249 
250  // Translate to 0,0
251  glTranslated(0, 0, getType());
252  /*
253  // Obtain exaggeration
254  const double exaggeration = s.addSize.getExaggeration(s);
255 
256  // Iterate over lanes
257  for (childLanes::const_iterator i = myChildLanes.begin(); i != myChildLanes.end(); i++) {
258  // Draw every signal over Lane
259  glPushMatrix();
260  glScaled(exaggeration, exaggeration, 1);
261  glTranslated(i->positionOverLane.x(), i->positionOverLane.y(), 0);
262  glRotated(i->rotationOverLane, 0, 0, 1);
263  glTranslated(0, -1.5, 0);
264 
265  int noPoints = 9;
266  if (s.scale > 25) {
267  noPoints = (int)(9.0 + s.scale / 10.0);
268  if (noPoints > 36) {
269  noPoints = 36;
270  }
271  }
272  glColor3d(1, 0, 0);
273  GLHelper::drawFilledCircle((double) 1.3, noPoints);
274  if (s.scale >= 5) {
275  glTranslated(0, 0, .1);
276  glColor3d(0, 0, 0);
277  GLHelper::drawFilledCircle((double) 1.1, noPoints);
278  // Draw speed
279  double speed = i->lane->getSpeed();
280  // Show as Km/h
281  speed *= 3.6f;
282  if (((int) speed + 1) % 10 == 0) {
283  speed = (double)(((int) speed + 1) / 10 * 10);
284  }
285  // draw the speed string
286  std::string speedToDraw = toString<double>(speed);
287  glColor3d(1, 1, 0);
288  glTranslated(0, 0, .1);
289  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
290  pfSetPosition(0, 0);
291  pfSetScale(1.2f);
292  double w = pfdkGetStringWidth(speedToDraw.c_str());
293  glRotated(180, 0, 1, 0);
294  glTranslated(-w / 2., 0.3, 0);
295  pfDrawString(speedToDraw.c_str());
296  }
297  glPopMatrix();
298  }
299  */
300  // Pop symbol matrix
301  glPopMatrix();
302 
303  // Draw name
304  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
305 
306  // Pop name
307  glPopName();
308 }
309 
310 
311 std::string
313  switch (key) {
314  case SUMO_ATTR_ID:
315  return getAdditionalID();
316  case SUMO_ATTR_LANES:
317  return /*joinToString(getLaneChildIds(), " ")*/ "";
318  case SUMO_ATTR_POSITION:
319  return toString(myPosition);
320  case SUMO_ATTR_FILE:
321  return myFilename;
323  return toString(myBlocked);
324  default:
325  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
326  }
327 }
328 
329 
330 void
331 GNEVariableSpeedSign::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
332  if (value == getAttribute(key)) {
333  return; //avoid needless changes, later logic relies on the fact that attributes have changed
334  }
335  switch (key) {
336  case SUMO_ATTR_ID:
337  case SUMO_ATTR_LANES:
338  case SUMO_ATTR_POSITION:
339  case SUMO_ATTR_FILE:
341  undoList->p_add(new GNEChange_Attribute(this, key, value));
342  updateGeometry();
343  break;
344  default:
345  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
346  }
347 }
348 
349 
350 bool
351 GNEVariableSpeedSign::isValid(SumoXMLAttr key, const std::string& value) {
352  switch (key) {
353  case SUMO_ATTR_ID:
354  if (isValidID(value) && (myViewNet->getNet()->getAdditional(getTag(), value) == NULL)) {
355  return true;
356  } else {
357  return false;
358  }
359  case SUMO_ATTR_POSITION: {
360  bool ok;
361  return (GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false).size() == 1);
362  }
363  case SUMO_ATTR_LANES: {
364  std::vector<std::string> laneIds = GNEAttributeCarrier::parse<std::vector<std::string> > (value);
365  // Empty Lanes aren't valid
366  if (laneIds.empty()) {
367  return false;
368  }
369  // Iterate over parsed lanes
370  for (int i = 0; i < (int)laneIds.size(); i++) {
371  if (myViewNet->getNet()->retrieveLane(laneIds.at(i), false) == NULL) {
372  return false;
373  }
374  }
375  return true;
376  }
377  case SUMO_ATTR_FILE:
378  return isValidFilename(value);
380  return canParse<bool>(value);
381  default:
382  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
383  }
384 }
385 
386 
387 void
388 GNEVariableSpeedSign::setAttribute(SumoXMLAttr key, const std::string& value) {
389  switch (key) {
390  case SUMO_ATTR_ID:
391  setAdditionalID(value);
392  break;
393  case SUMO_ATTR_LANES: {
394  // Declare variables
395  std::vector<std::string> laneIds = GNEAttributeCarrier::parse<std::vector<std::string> > (value);
396  std::vector<GNELane*> lanes;
397  GNELane* lane;
398  // Iterate over parsed lanes and obtain pointer to lanes
399  for (int i = 0; i < (int)laneIds.size(); i++) {
400  lane = myViewNet->getNet()->retrieveLane(laneIds.at(i), false);
401  if (lane) {
402  lanes.push_back(lane);
403  }
404  }
405  // Set new childs
406  //setLaneChilds(lanes);
407  break;
408  }
409  case SUMO_ATTR_POSITION:
410  bool ok;
411  myPosition = GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false)[0];
412  updateGeometry();
413  getViewNet()->update();
414  break;
415  case SUMO_ATTR_FILE:
416  myFilename = value;
417  break;
419  myBlocked = parse<bool>(value);
420  getViewNet()->update();
421  break;
422  default:
423  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
424  }
425 }
426 
427 /****************************************************************************/
static void drawTexturedBox(int which, double size)
Draws a named texture as a box with the given size.
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:261
void drawName(const Position &pos, const double scale, const GUIVisualizationTextSettings &settings, const double angle=0) const
draw name of item
void setBlockIconRotation()
double scale
information about a lane&#39;s width (temporary, used for a single view)
const std::string & getParentName() const
Returns the name of the parent object.
GNEVariableSpeedSign(const std::string &id, GNEViewNet *viewNet, Position pos, std::vector< GNELane * > lanes, const std::string &filename, const std::vector< GNEVariableSpeedSignStep > &vssValues)
Constructor.
void commitGeometryMoving(const Position &oldPos, GNEUndoList *undoList)
commit geometry changes in the attributes of an element after use of moveGeometry(...)
GUIVisualizationTextSettings addName
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
std::string getAttribute(SumoXMLAttr key) const
This functions has to be implemented in allGNEAttributeCarrier.
static GUIGlID getTexture(GUITexture which)
returns a texture previously defined in the enum GUITexture
Position getPositionInView() const
Returns position of additional in view.
double x() const
Returns the x-position.
Definition: Position.h:63
Stores the information about how to visualize structures.
void openAdditionalDialog()
open GNEVariableSpeedSignDialog
bool isAdditionalSelected() const
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:54
void p_begin(const std::string &description)
Begin undo command sub-group. This begins a new group of commands that are treated as a single comman...
Definition: GNEUndoList.cpp:85
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static bool isValidFilename(const std::string &value)
true if value is a valid file value
GUIGlID getGlID() const
Returns the numerical id of the object.
const std::vector< GNEVariableSpeedSignStep > & getSteps() const
get values of variable speed signal
GNEViewNet * myViewNet
The GNEViewNet this additional element belongs.
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
static bool isValidID(const std::string &value)
true if value is a valid sumo ID
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
GNEViewNet * getViewNet() const
Returns a pointer to GNEViewNet in which additional element is located.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
const std::string & getAdditionalID() const
returns the ID of additional
void refreshAdditional(GNEAdditional *additional)
refreshes boundary information of an additional after a geometry update
Definition: GNENet.cpp:998
PositionVector myShape
The shape of the additional element.
void p_end()
End undo command sub-group. If the sub-group is still empty, it will be deleted; otherwise, the sub-group will be added as a new command into parent group. A matching begin() must have been called previously.
Definition: GNEUndoList.cpp:92
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
void writeAdditional(OutputDevice &device, bool volatileOptionsEnabled) const
writte additional element into a xml file
GNELane * retrieveLane(const std::string &id, bool failHard=true)
get lane by id
Definition: GNENet.cpp:958
friend class GNEChange_Attribute
declare friend class
double y() const
Returns the y-position.
Definition: Position.h:68
bool mySaveInFilename
enable or disable save in external filename
block movement of a graphic element
void setVariableSpeedSignSteps(const std::vector< GNEVariableSpeedSignStep > &steps)
set values of variable speed signal
void addStep(const GNEVariableSpeedSignStep &step)
insert a new step in variable speed signal
void updateGeometry()
update pre-computed geometry information
void setAdditionalID(const std::string &id)
set the ID of additional
const std::string getID() const
function to support debugging
void drawLockIcon(double size=0.5) const
draw lock icon
void moveGeometry(const Position &newPosition)
change the position of the element geometry without saving in undoList
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes ...
Position myBlockIconOffset
The offSet of the block icon.
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1680
trigger: the time of the step
const std::string & getFilename() const
get filename of variable speed sign
std::vector< GNEVariableSpeedSignStep > mySteps
values of variable speed signal
bool myBlocked
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
An Element which don&#39;t belongs to GNENet but has influency in the simulation.
Definition: GNEAdditional.h:62
void setFilename(const std::string &filename)
set filename of variable speed sign
~GNEVariableSpeedSign()
Destructor.
GNENet * getNet() const
get the net object
SumoXMLTag getTag() const
get XML Tag assigned to this object
A variable speed sign.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
Position myPosition
position of VSS in view
bool closeTag()
Closes the most recently opened tag.
std::string myFilename
filename of variable speed sign
static PositionVector parseShapeReporting(const std::string &shpdef, const std::string &objecttype, const char *objectid, bool &ok, bool allowEmpty, bool report=true)
Builds a PositionVector from a string representation, reporting occured errors.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Position myBlockIconPosition
position of the block icon
trigger: a step description