SUMO - Simulation of Urban MObility
GNEVariableSpeedSign.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2001-2016 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 #ifdef CHECK_MEMORY_LEAKS
60 #include <foreign/nvwa/debug_new.h>
61 #endif
62 
63 // ===========================================================================
64 // member method definitions
65 // ===========================================================================
66 
67 GNEVariableSpeedSign::GNEVariableSpeedSign(const std::string& id, GNEViewNet* viewNet, Position pos, std::vector<GNELane*> /* lanes */, const std::string& filename, const std::map<SUMOReal, SUMOReal>& vssValues) :
69  myFilename(filename),
70  myVSSValues(vssValues),
71  mySaveInFilename(false) {
72  // Update geometry;
74  // Set colors
75  myBaseColor = RGBColor(76, 170, 50, 255);
76  myBaseColorSelected = RGBColor(161, 255, 135, 255);
77 }
78 
79 
81 }
82 
83 
84 void
86  // Clear shape
87  myShape.clear();
88 
89  // Set block icon position
91 
92  // Set block icon offset
93  myBlockIconOffset = Position(-0.5, -0.5);
94 
95  // Set block icon rotation, and using their rotation for draw logo
97 
98  // Set position
99  myShape.push_back(myPosition);
100  /*
101  // Add shape of childs (To avoid graphics errors)
102  for (childLanes::iterator i = myChildLanes.begin(); i != myChildLanes.end(); i++) {
103  myShape.append(i->lane->getShape());
104  }
105 
106  // Update connections
107  updateConnections();
108  */
109  // Refresh element (neccesary to avoid grabbing problems)
111 }
112 
113 
114 Position
116  return myPosition;
117 }
118 
119 
120 void
122  GNEVariableSpeedSignDialog variableSpeedSignDialog(this);
123 }
124 
125 
126 void
128  // change Position
129  myPosition = Position(offsetx, offsety);
130  updateGeometry();
131 }
132 
133 
134 void
136  undoList->p_begin("position of " + toString(getTag()));
137  undoList->p_add(new GNEChange_Attribute(this, SUMO_ATTR_POSITION, toString(myPosition), true, toString(Position(oldPosx, oldPosy))));
138  undoList->p_end();
139  // Refresh element
141 }
142 
143 
144 void
146  // Write parameters
147  device.openTag(getTag());
148  device.writeAttr(SUMO_ATTR_ID, getID());
149  device.writeAttr(SUMO_ATTR_LANES, /*joinToString(getLaneChildIds(), " ").c_str()*/"");
150  device.writeAttr(SUMO_ATTR_X, myPosition.x());
151  device.writeAttr(SUMO_ATTR_Y, myPosition.y());
152  // 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
153  if (!myFilename.empty() && mySaveInFilename == true) {
154  // Write filename attribute
156  // Save values in a different file
157  /*
158  OutputDevice& deviceVSS = OutputDevice::getDevice(currentDirectory + myFilename);
159  deviceVSS.openTag("VSS");
160  for (std::map<SUMOReal, SUMOReal>::const_iterator i = myVSSValues.begin(); i != myVSSValues.end(); ++i) {
161  // Open VSS tag
162  deviceVSS.openTag(SUMO_TAG_STEP);
163  // Write TimeSTep
164  deviceVSS.writeAttr(SUMO_ATTR_TIME, i->first);
165  // Write speed
166  deviceVSS.writeAttr(SUMO_ATTR_SPEED, i->second);
167  // Close VSS tag
168  deviceVSS.closeTag();
169  }
170  deviceVSS.close();
171  */
172  } else {
173  for (std::map<SUMOReal, SUMOReal>::const_iterator i = myVSSValues.begin(); i != myVSSValues.end(); ++i) {
174  // Open VSS tag
175  device.openTag(SUMO_TAG_STEP);
176  // Write TimeSTep
177  device.writeAttr(SUMO_ATTR_TIME, i->first);
178  // Write speed
179  device.writeAttr(SUMO_ATTR_SPEED, i->second);
180  // Close VSS tag
181  device.closeTag();
182  }
183  }
184  if (myBlocked) {
186  }
187  // Close tag
188  device.closeTag();
189 }
190 
191 
192 std::string
194  return myFilename;
195 }
196 
197 
198 std::map<SUMOReal, SUMOReal>
200  return myVSSValues;
201 }
202 
203 
204 void
205 GNEVariableSpeedSign::setFilename(std::string filename) {
206  myFilename = filename;
207 }
208 
209 
210 void
211 GNEVariableSpeedSign::setVariableSpeedSignSteps(const std::map<SUMOReal, SUMOReal>& vssValues) {
212  myVSSValues = vssValues;
213 }
214 
215 
216 bool
218  if (myVSSValues.find(time) == myVSSValues.end()) {
219  myVSSValues[time] = speed;
220  return true;
221  } else {
222  return false;
223  }
224 }
225 
226 
227 const std::string&
229  return myViewNet->getNet()->getMicrosimID();
230 }
231 
232 
233 void
235  // Start drawing adding an gl identificator
236  glPushName(getGlID());
237 
238  // Add a draw matrix for drawing logo
239  glPushMatrix();
240  glTranslated(myShape[0].x(), myShape[0].y(), getType());
241  glColor3d(1, 1, 1);
242  glRotated(180, 0, 0, 1);
243 
244  // Draw icon depending of rerouter is or isn't selected
245  if (isAdditionalSelected()) {
247  } else {
249  }
250 
251  // Pop draw icon matrix
252  glPopMatrix();
253 
254  // Show Lock icon depending of the Edit mode
255  drawLockIcon(0.4);
256 
257  // Push matrix to draw every symbol over lane
258  glPushMatrix();
259 
260  // Translate to 0,0
261  glTranslated(0, 0, getType());
262  /*
263  // Obtain exaggeration
264  const SUMOReal exaggeration = s.addSize.getExaggeration(s);
265 
266  // Iterate over lanes
267  for (childLanes::const_iterator i = myChildLanes.begin(); i != myChildLanes.end(); i++) {
268  // Draw every signal over Lane
269  glPushMatrix();
270  glScaled(exaggeration, exaggeration, 1);
271  glTranslated(i->positionOverLane.x(), i->positionOverLane.y(), 0);
272  glRotated(i->rotationOverLane, 0, 0, 1);
273  glTranslated(0, -1.5, 0);
274 
275  int noPoints = 9;
276  if (s.scale > 25) {
277  noPoints = (int)(9.0 + s.scale / 10.0);
278  if (noPoints > 36) {
279  noPoints = 36;
280  }
281  }
282  glColor3d(1, 0, 0);
283  GLHelper::drawFilledCircle((SUMOReal) 1.3, noPoints);
284  if (s.scale >= 5) {
285  glTranslated(0, 0, .1);
286  glColor3d(0, 0, 0);
287  GLHelper::drawFilledCircle((SUMOReal) 1.1, noPoints);
288  // Draw speed
289  SUMOReal speed = i->lane->getSpeed();
290  // Show as Km/h
291  speed *= 3.6f;
292  if (((int) speed + 1) % 10 == 0) {
293  speed = (SUMOReal)(((int) speed + 1) / 10 * 10);
294  }
295  // draw the speed string
296  std::string speedToDraw = toString<SUMOReal>(speed);
297  glColor3d(1, 1, 0);
298  glTranslated(0, 0, .1);
299  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
300  pfSetPosition(0, 0);
301  pfSetScale(1.2f);
302  SUMOReal w = pfdkGetStringWidth(speedToDraw.c_str());
303  glRotated(180, 0, 1, 0);
304  glTranslated(-w / 2., 0.3, 0);
305  pfDrawString(speedToDraw.c_str());
306  }
307  glPopMatrix();
308  }
309  */
310  // Pop symbol matrix
311  glPopMatrix();
312 
313  // Draw name
314  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
315 
316  // Pop name
317  glPopName();
318 }
319 
320 
321 std::string
323  switch (key) {
324  case SUMO_ATTR_ID:
325  return getAdditionalID();
326  case SUMO_ATTR_LANES:
327  return /*joinToString(getLaneChildIds(), " ")*/ "";
328  case SUMO_ATTR_POSITION:
329  return toString(myPosition);
330  case SUMO_ATTR_FILE:
331  return myFilename;
333  return toString(myBlocked);
334  default:
335  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
336  }
337 }
338 
339 
340 void
341 GNEVariableSpeedSign::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
342  if (value == getAttribute(key)) {
343  return; //avoid needless changes, later logic relies on the fact that attributes have changed
344  }
345  switch (key) {
346  case SUMO_ATTR_ID:
347  case SUMO_ATTR_LANES:
348  case SUMO_ATTR_POSITION:
349  case SUMO_ATTR_FILE:
351  undoList->p_add(new GNEChange_Attribute(this, key, value));
352  updateGeometry();
353  break;
354  default:
355  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
356  }
357 }
358 
359 
360 bool
361 GNEVariableSpeedSign::isValid(SumoXMLAttr key, const std::string& value) {
362  switch (key) {
363  case SUMO_ATTR_ID:
364  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
365  return true;
366  } else {
367  return false;
368  }
369  case SUMO_ATTR_POSITION:
370  bool ok;
371  return GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false).size() == 1;
372  case SUMO_ATTR_LANES: {
373  std::vector<std::string> laneIds;
374  SUMOSAXAttributes::parseStringVector(value, laneIds);
375  // Empty Lanes aren't valid
376  if (laneIds.empty()) {
377  return false;
378  }
379  // Iterate over parsed lanes
380  for (int i = 0; i < (int)laneIds.size(); i++) {
381  if (myViewNet->getNet()->retrieveLane(laneIds.at(i), false) == NULL) {
382  return false;
383  }
384  }
385  return true;
386  }
387  case SUMO_ATTR_FILE:
388  return isValidFileValue(value);
390  return canParse<bool>(value);
391  default:
392  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
393  }
394 }
395 
396 
397 void
398 GNEVariableSpeedSign::setAttribute(SumoXMLAttr key, const std::string& value) {
399  switch (key) {
400  case SUMO_ATTR_ID:
401  setAdditionalID(value);
402  break;
403  case SUMO_ATTR_LANES: {
404  // Declare variables
405  std::vector<std::string> laneIds;
406  std::vector<GNELane*> lanes;
407  GNELane* lane;
408  SUMOSAXAttributes::parseStringVector(value, laneIds);
409  // Iterate over parsed lanes and obtain pointer to lanes
410  for (int i = 0; i < (int)laneIds.size(); i++) {
411  lane = myViewNet->getNet()->retrieveLane(laneIds.at(i), false);
412  if (lane) {
413  lanes.push_back(lane);
414  }
415  }
416  // Set new childs
417  //setLaneChilds(lanes);
418  break;
419  }
420  case SUMO_ATTR_POSITION:
421  bool ok;
422  myPosition = GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false)[0];
423  updateGeometry();
424  getViewNet()->update();
425  break;
426  case SUMO_ATTR_FILE:
427  myFilename = value;
428  break;
430  myBlocked = parse<bool>(value);
431  getViewNet()->update();
432  break;
433  default:
434  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
435  }
436 }
437 
438 /****************************************************************************/
bool insertStep(const SUMOReal time, const SUMOReal speed)
insert a new step in variable speed signal
void moveAdditionalGeometry(SUMOReal offsetx, SUMOReal offsety)
change the position of the rerouter geometry
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:256
std::map< SUMOReal, SUMOReal > getVariableSpeedSignSteps() const
get values of variable speed signal
const std::string & getParentName() const
Returns the name of the parent object (if any)
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
GNEVariableSpeedSign(const std::string &id, GNEViewNet *viewNet, Position pos, std::vector< GNELane * > lanes, const std::string &filename, const std::map< SUMOReal, SUMOReal > &vssValues)
Constructor.
std::map< SUMOReal, SUMOReal > myVSSValues
values of variable speed signal
static GUIGlID getTexture(GUITexture which)
returns a texture previously defined in the enum GUITexture
Position getPositionInView() const
Returns position of Variable Speed Signal in view.
Stores the information about how to visualize structures.
void openAdditionalDialog()
open GNEVariableSpeedSignDialog
bool isAdditionalSelected() const
std::string getFilename() const
get filename of rerouter
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:86
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
void setFilename(std::string filename)
set filename of rerouter
GUIGlID getGlID() const
Returns the numerical id of the object.
void writeAdditional(OutputDevice &device) const
writte additional element into a xml file
SUMOReal scale
information about a lane&#39;s width (temporary, used for a single view)
GNEViewNet * myViewNet
The GNEViewNet this additional element belongs.
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
RGBColor myBaseColorSelected
base color selected (Default blue)
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
void setVariableSpeedSignSteps(const std::map< SUMOReal, SUMOReal > &vssValues)
set values of variable speed signal
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
static bool isValidFileValue(const std::string &value)
true if value is a valid file value
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:55
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:814
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:93
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
GNELane * retrieveLane(const std::string &id, bool failHard=true)
get lane by id
Definition: GNENet.cpp:774
void setBlockIconRotation(GNELane *lane=NULL)
set Rotation of block Icon
friend class GNEChange_Attribute
declare friend class
bool mySaveInFilename
enable or disable save in external filename
block movement of a graphic element
void drawLockIcon(SUMOReal size=0.5) const
draw lock icon
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 drawName(const Position &pos, const SUMOReal scale, const GUIVisualizationTextSettings &settings, const SUMOReal angle=0) const
draw name of item
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.
void commmitAdditionalGeometryMoved(SUMOReal oldPosx, SUMOReal oldPosy, GNEUndoList *undoList)
updated geometry changes in the attributes of additional
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1265
trigger: the time of the step
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
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
RGBColor myBaseColor
base color (Default green)
~GNEVariableSpeedSign()
Destructor.
GNENet * getNet() const
get the net object
Definition: GNEViewNet.cpp:989
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
The position in which this additional element is located.
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:208
std::string myFilename
filename of rerouter
static void drawTexturedBox(int which, SUMOReal size)
Draws a named texture as a box with the given size.
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