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 
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  myFilename(filename),
67  mySteps(steps),
68  mySaveInFilename(false) {
69  // Update geometry;
71  // Set colors
72  myBaseColor = RGBColor(76, 170, 50, 255);
73  myBaseColorSelected = RGBColor(161, 255, 135, 255);
74 }
75 
76 
78 }
79 
80 
81 void
83  // Clear shape
84  myShape.clear();
85 
86  // Set block icon position
88 
89  // Set block icon offset
90  myBlockIconOffset = Position(-0.5, -0.5);
91 
92  // Set block icon rotation, and using their rotation for draw logo
94 
95  // Set position
96  myShape.push_back(myPosition);
97  /*
98  // Add shape of childs (To avoid graphics errors)
99  for (childLanes::iterator i = myChildLanes.begin(); i != myChildLanes.end(); i++) {
100  myShape.append(i->lane->getShape());
101  }
102 
103  // Update connections
104  updateConnections();
105  */
106  // Refresh element (neccesary to avoid grabbing problems)
108 }
109 
110 
111 Position
113  return myPosition;
114 }
115 
116 
117 void
119  GNEVariableSpeedSignDialog variableSpeedSignDialog(this);
120 }
121 
122 
123 void
124 GNEVariableSpeedSign::moveAdditionalGeometry(double offsetx, double offsety) {
125  // change Position
126  myPosition = Position(offsetx, offsety);
127  updateGeometry();
128 }
129 
130 
131 void
132 GNEVariableSpeedSign::commmitAdditionalGeometryMoved(double oldPosx, double oldPosy, GNEUndoList* undoList) {
133  undoList->p_begin("position of " + toString(getTag()));
134  undoList->p_add(new GNEChange_Attribute(this, SUMO_ATTR_POSITION, toString(myPosition), true, toString(Position(oldPosx, oldPosy))));
135  undoList->p_end();
136  // Refresh element
138 }
139 
140 
141 void
143  // Write parameters
144  device.openTag(getTag());
145  device.writeAttr(SUMO_ATTR_ID, getID());
146  device.writeAttr(SUMO_ATTR_LANES, /*joinToString(getLaneChildIds(), " ").c_str()*/"");
147  device.writeAttr(SUMO_ATTR_X, myPosition.x());
148  device.writeAttr(SUMO_ATTR_Y, myPosition.y());
149  // 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
150  if (!myFilename.empty() && mySaveInFilename == true) {
151  // Write filename attribute
153  // Save values in a different file
154  /*
155  OutputDevice& deviceVSS = OutputDevice::getDevice(currentDirectory + myFilename);
156  deviceVSS.openTag("VSS");
157  for (std::vector<GNEVariableSpeedSignStep>::const_iterator i = myVSSValues.begin(); i != myVSSValues.end(); ++i) {
158  // Open VSS tag
159  deviceVSS.openTag(SUMO_TAG_STEP);
160  // Write TimeSTep
161  deviceVSS.writeAttr(SUMO_ATTR_TIME, i->getTime());
162  // Write speed
163  deviceVSS.writeAttr(SUMO_ATTR_SPEED, i->getSpeed());
164  // Close VSS tag
165  deviceVSS.closeTag();
166  }
167  deviceVSS.close();
168  */
169  } else {
170  for (std::vector<GNEVariableSpeedSignStep>::const_iterator i = mySteps.begin(); i != mySteps.end(); ++i) {
171  // Open VSS tag
172  device.openTag(SUMO_TAG_STEP);
173  // Write TimeSTep
174  device.writeAttr(SUMO_ATTR_TIME, i->getTime());
175  // Write speed
176  device.writeAttr(SUMO_ATTR_SPEED, i->getSpeed());
177  // Close VSS tag
178  device.closeTag();
179  }
180  }
181  if (myBlocked) {
183  }
184  // Close tag
185  device.closeTag();
186 }
187 
188 
189 const std::string&
191  return myFilename;
192 }
193 
194 
195 const std::vector<GNEVariableSpeedSignStep>&
197  return mySteps;
198 }
199 
200 
201 void
202 GNEVariableSpeedSign::setFilename(const std::string& filename) {
203  myFilename = filename;
204 }
205 
206 
207 void
208 GNEVariableSpeedSign::setVariableSpeedSignSteps(const std::vector<GNEVariableSpeedSignStep>& steps) {
209  mySteps = steps;
210 }
211 
212 
213 void
215  mySteps.push_back(step);
216 }
217 
218 
219 const std::string&
221  return myViewNet->getNet()->getMicrosimID();
222 }
223 
224 
225 void
227  // Start drawing adding an gl identificator
228  glPushName(getGlID());
229 
230  // Add a draw matrix for drawing logo
231  glPushMatrix();
232  glTranslated(myShape[0].x(), myShape[0].y(), getType());
233  glColor3d(1, 1, 1);
234  glRotated(180, 0, 0, 1);
235 
236  // Draw icon depending of variable speed sign is or isn't selected
237  if (isAdditionalSelected()) {
239  } else {
241  }
242 
243  // Pop draw icon matrix
244  glPopMatrix();
245 
246  // Show Lock icon depending of the Edit mode
247  drawLockIcon(0.4);
248 
249  // Push matrix to draw every symbol over lane
250  glPushMatrix();
251 
252  // Translate to 0,0
253  glTranslated(0, 0, getType());
254  /*
255  // Obtain exaggeration
256  const double exaggeration = s.addSize.getExaggeration(s);
257 
258  // Iterate over lanes
259  for (childLanes::const_iterator i = myChildLanes.begin(); i != myChildLanes.end(); i++) {
260  // Draw every signal over Lane
261  glPushMatrix();
262  glScaled(exaggeration, exaggeration, 1);
263  glTranslated(i->positionOverLane.x(), i->positionOverLane.y(), 0);
264  glRotated(i->rotationOverLane, 0, 0, 1);
265  glTranslated(0, -1.5, 0);
266 
267  int noPoints = 9;
268  if (s.scale > 25) {
269  noPoints = (int)(9.0 + s.scale / 10.0);
270  if (noPoints > 36) {
271  noPoints = 36;
272  }
273  }
274  glColor3d(1, 0, 0);
275  GLHelper::drawFilledCircle((double) 1.3, noPoints);
276  if (s.scale >= 5) {
277  glTranslated(0, 0, .1);
278  glColor3d(0, 0, 0);
279  GLHelper::drawFilledCircle((double) 1.1, noPoints);
280  // Draw speed
281  double speed = i->lane->getSpeed();
282  // Show as Km/h
283  speed *= 3.6f;
284  if (((int) speed + 1) % 10 == 0) {
285  speed = (double)(((int) speed + 1) / 10 * 10);
286  }
287  // draw the speed string
288  std::string speedToDraw = toString<double>(speed);
289  glColor3d(1, 1, 0);
290  glTranslated(0, 0, .1);
291  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
292  pfSetPosition(0, 0);
293  pfSetScale(1.2f);
294  double w = pfdkGetStringWidth(speedToDraw.c_str());
295  glRotated(180, 0, 1, 0);
296  glTranslated(-w / 2., 0.3, 0);
297  pfDrawString(speedToDraw.c_str());
298  }
299  glPopMatrix();
300  }
301  */
302  // Pop symbol matrix
303  glPopMatrix();
304 
305  // Draw name
306  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
307 
308  // Pop name
309  glPopName();
310 }
311 
312 
313 std::string
315  switch (key) {
316  case SUMO_ATTR_ID:
317  return getAdditionalID();
318  case SUMO_ATTR_LANES:
319  return /*joinToString(getLaneChildIds(), " ")*/ "";
320  case SUMO_ATTR_POSITION:
321  return toString(myPosition);
322  case SUMO_ATTR_FILE:
323  return myFilename;
325  return toString(myBlocked);
326  default:
327  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
328  }
329 }
330 
331 
332 void
333 GNEVariableSpeedSign::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
334  if (value == getAttribute(key)) {
335  return; //avoid needless changes, later logic relies on the fact that attributes have changed
336  }
337  switch (key) {
338  case SUMO_ATTR_ID:
339  case SUMO_ATTR_LANES:
340  case SUMO_ATTR_POSITION:
341  case SUMO_ATTR_FILE:
343  undoList->p_add(new GNEChange_Attribute(this, key, value));
344  updateGeometry();
345  break;
346  default:
347  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
348  }
349 }
350 
351 
352 bool
353 GNEVariableSpeedSign::isValid(SumoXMLAttr key, const std::string& value) {
354  switch (key) {
355  case SUMO_ATTR_ID:
356  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
357  return true;
358  } else {
359  return false;
360  }
361  case SUMO_ATTR_POSITION:
362  bool ok;
363  return (GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false).size() == 1);
364  case SUMO_ATTR_LANES: {
365  std::vector<std::string> laneIds = GNEAttributeCarrier::parse<std::vector<std::string> > (value);
366  // Empty Lanes aren't valid
367  if (laneIds.empty()) {
368  return false;
369  }
370  // Iterate over parsed lanes
371  for (int i = 0; i < (int)laneIds.size(); i++) {
372  if (myViewNet->getNet()->retrieveLane(laneIds.at(i), false) == NULL) {
373  return false;
374  }
375  }
376  return true;
377  }
378  case SUMO_ATTR_FILE:
379  return isValidFilename(value);
381  return canParse<bool>(value);
382  default:
383  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
384  }
385 }
386 
387 
388 void
389 GNEVariableSpeedSign::setAttribute(SumoXMLAttr key, const std::string& value) {
390  switch (key) {
391  case SUMO_ATTR_ID:
392  setAdditionalID(value);
393  break;
394  case SUMO_ATTR_LANES: {
395  // Declare variables
396  std::vector<std::string> laneIds = GNEAttributeCarrier::parse<std::vector<std::string> > (value);
397  std::vector<GNELane*> lanes;
398  GNELane* lane;
399  // Iterate over parsed lanes and obtain pointer to lanes
400  for (int i = 0; i < (int)laneIds.size(); i++) {
401  lane = myViewNet->getNet()->retrieveLane(laneIds.at(i), false);
402  if (lane) {
403  lanes.push_back(lane);
404  }
405  }
406  // Set new childs
407  //setLaneChilds(lanes);
408  break;
409  }
410  case SUMO_ATTR_POSITION:
411  bool ok;
412  myPosition = GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false)[0];
413  updateGeometry();
414  getViewNet()->update();
415  break;
416  case SUMO_ATTR_FILE:
417  myFilename = value;
418  break;
420  myBlocked = parse<bool>(value);
421  getViewNet()->update();
422  break;
423  default:
424  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
425  }
426 }
427 
428 /****************************************************************************/
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:256
void drawName(const Position &pos, const double scale, const GUIVisualizationTextSettings &settings, const double angle=0) const
draw name of item
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.
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
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.
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:82
void commmitAdditionalGeometryMoved(double oldPosx, double oldPosy, GNEUndoList *undoList)
updated geometry changes in the attributes of additional
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.
void writeAdditional(OutputDevice &device) const
writte additional element into a xml file
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.
RGBColor myBaseColorSelected
base color selected (Default blue)
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
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:825
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:89
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:785
void setBlockIconRotation(GNELane *lane=NULL)
set Rotation of block Icon
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 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:1331
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
RGBColor myBaseColor
base color (Default green)
~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
The position in which this additional element is located.
bool closeTag()
Closes the most recently opened tag.
std::string myFilename
filename of variable speed sign
void moveAdditionalGeometry(double offsetx, double offsety)
change the position of the variable speed sign geometry
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