SUMO - Simulation of Urban MObility
GNEVariableSpeedSignal.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
10 // Copyright (C) 2001-2013 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 "GNEVariableSpeedSignal.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 GNEVariableSpeedSignal::GNEVariableSpeedSignal(const std::string& id, GNEViewNet* viewNet, Position pos, std::vector<GNELane*> lanes, const std::string& filename, const std::map<SUMOTime, SUMOReal>& VSSValues, bool blocked) :
68  GNEAdditionalSet(id, viewNet, pos, SUMO_TAG_VSS, blocked, std::vector<GNEAdditional * >(), std::vector<GNEEdge * >(), lanes),
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
108 }
109 
110 
111 Position
113  return myPosition;
114 }
115 
116 
117 void
119  GNEVariableSpeedSignalDialog variableSpeedSignalDialog(this);
120 }
121 
122 
123 void
125  // if item isn't blocked
126  if (myBlocked == false) {
127  // change Position
128  undoList->p_add(new GNEChange_Attribute(this, SUMO_ATTR_POSITION, toString(Position(posx, posy, 0))));
129  }
130 }
131 
132 
133 void
134 GNEVariableSpeedSignal::writeAdditional(OutputDevice& device, const std::string& currentDirectory) {
135  // Write parameters
136  device.openTag(getTag());
137  device.writeAttr(SUMO_ATTR_ID, getID());
138  device.writeAttr(SUMO_ATTR_LANES, joinToString(getLaneChildIds(), " ").c_str());
139  device.writeAttr(SUMO_ATTR_X, myPosition.x());
140  device.writeAttr(SUMO_ATTR_Y, myPosition.y());
141  // 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
142  if (!myFilename.empty() && mySaveInFilename == true) {
143  // Write filename attribute
145  // Save values in a different file
146  OutputDevice& deviceVSS = OutputDevice::getDevice(currentDirectory + myFilename);
147  deviceVSS.openTag("VSS");
148  for (std::map<SUMOTime, SUMOReal>::const_iterator i = myVSSValues.begin(); i != myVSSValues.end(); ++i) {
149  // Open VSS tag
150  deviceVSS.openTag(SUMO_TAG_STEP);
151  // Write TimeSTep
152  deviceVSS.writeAttr(SUMO_ATTR_TIME, i->first);
153  // Write speed
154  deviceVSS.writeAttr(SUMO_ATTR_SPEED, i->second);
155  // Close VSS tag
156  deviceVSS.closeTag();
157  }
158  deviceVSS.close();
159  } else {
160  for (std::map<SUMOTime, SUMOReal>::const_iterator i = myVSSValues.begin(); i != myVSSValues.end(); ++i) {
161  // Open VSS tag
162  device.openTag(SUMO_TAG_STEP);
163  // Write TimeSTep
164  device.writeAttr(SUMO_ATTR_TIME, i->first);
165  // Write speed
166  device.writeAttr(SUMO_ATTR_SPEED, i->second);
167  // Close VSS tag
168  device.closeTag();
169  }
170  }
171  // Close tag
172  device.closeTag();
173 }
174 
175 
176 std::string
178  return myFilename;
179 }
180 
181 
182 std::map<SUMOTime, SUMOReal>
184  return myVSSValues;
185 }
186 
187 
188 void
189 GNEVariableSpeedSignal::setFilename(std::string filename) {
190  myFilename = filename;
191 }
192 
193 
194 void
195 GNEVariableSpeedSignal::setVariableSpeedSignalSteps(const std::map<SUMOTime, SUMOReal>& vssValues) {
196  myVSSValues = vssValues;
197 }
198 
199 
200 bool
202  if (myVSSValues.find(time) == myVSSValues.end()) {
203  myVSSValues[time] = speed;
204  return true;
205  } else {
206  return false;
207  }
208 }
209 
210 
211 const std::string&
213  return myViewNet->getNet()->getMicrosimID();
214 }
215 
216 
217 void
219  // Start drawing adding an gl identificator
220  glPushName(getGlID());
221 
222  // Add a draw matrix for drawing logo
223  glPushMatrix();
224  glTranslated(myShape[0].x(), myShape[0].y(), getType());
225  glColor3d(1, 1, 1);
226  glRotated(180, 0, 0, 1);
227 
228  // Draw icon depending of rerouter is or isn't selected
229  if (isAdditionalSelected()) {
231  } else {
233  }
234 
235  // Pop draw icon matrix
236  glPopMatrix();
237 
238  // Show Lock icon depending of the Edit mode
239  drawLockIcon(0.4);
240 
241  // Push matrix to draw every symbol over lane
242  glPushMatrix();
243 
244  // Traslate to 0,0
245  glTranslated(0, 0, getType());
246 
247  // Obtain exaggeration
248  const SUMOReal exaggeration = s.addSize.getExaggeration(s);
249 
250  // Iterate over lanes
251  for (childLanes::const_iterator i = myChildLanes.begin(); i != myChildLanes.end(); i++) {
252  // Draw every signal over Lane
253  glPushMatrix();
254  glScaled(exaggeration, exaggeration, 1);
255  glTranslated(i->positionOverLane.x(), i->positionOverLane.y(), 0);
256  glRotated(i->rotationOverLane, 0, 0, 1);
257  glTranslated(0, -1.5, 0);
258 
259  int noPoints = 9;
260  if (s.scale > 25) {
261  noPoints = (int)(9.0 + s.scale / 10.0);
262  if (noPoints > 36) {
263  noPoints = 36;
264  }
265  }
266  glColor3d(1, 0, 0);
267  GLHelper::drawFilledCircle((SUMOReal) 1.3, noPoints);
268  if (s.scale >= 5) {
269  glTranslated(0, 0, .1);
270  glColor3d(0, 0, 0);
271  GLHelper::drawFilledCircle((SUMOReal) 1.1, noPoints);
272  // Draw speed
273  SUMOReal speed = i->lane->getSpeed();
274  // Show as Km/h
275  speed *= 3.6f;
276  if (((int) speed + 1) % 10 == 0) {
277  speed = (SUMOReal)(((int) speed + 1) / 10 * 10);
278  }
279  // draw the speed string
280  std::string speedToDraw = toString<SUMOReal>(speed);
281  glColor3d(1, 1, 0);
282  glTranslated(0, 0, .1);
283  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
284  pfSetPosition(0, 0);
285  pfSetScale(1.2f);
286  SUMOReal w = pfdkGetStringWidth(speedToDraw.c_str());
287  glRotated(180, 0, 1, 0);
288  glTranslated(-w / 2., 0.3, 0);
289  pfDrawString(speedToDraw.c_str());
290  }
291  glPopMatrix();
292  }
293 
294  // Pop symbol matrix
295  glPopMatrix();
296 
297  // Draw connections
298  drawConnections();
299 
300  // Draw name
301  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
302 
303  // Pop name
304  glPopName();
305 }
306 
307 
308 std::string
310  switch (key) {
311  case SUMO_ATTR_ID:
312  return getAdditionalID();
313  case SUMO_ATTR_LANES:
314  return joinToString(getLaneChildIds(), " ");
315  case SUMO_ATTR_POSITION:
316  return toString(myPosition);
317  case SUMO_ATTR_FILE:
318  return myFilename;
319  default:
320  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
321  }
322 }
323 
324 
325 void
326 GNEVariableSpeedSignal::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
327  if (value == getAttribute(key)) {
328  return; //avoid needless changes, later logic relies on the fact that attributes have changed
329  }
330  switch (key) {
331  case SUMO_ATTR_ID:
332  case SUMO_ATTR_LANES:
333  case SUMO_ATTR_POSITION:
334  case SUMO_ATTR_FILE:
335  undoList->p_add(new GNEChange_Attribute(this, key, value));
336  updateGeometry();
337  break;
338  default:
339  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
340  }
341 }
342 
343 
344 bool
345 GNEVariableSpeedSignal::isValid(SumoXMLAttr key, const std::string& value) {
346  switch (key) {
347  case SUMO_ATTR_ID:
348  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
349  return true;
350  } else {
351  return false;
352  }
353  case SUMO_ATTR_POSITION:
354  bool ok;
355  return GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false).size() == 1;
356  case SUMO_ATTR_LANES: {
357  std::vector<std::string> laneIds;
358  SUMOSAXAttributes::parseStringVector(value, laneIds);
359  // Empty Lanes aren't valid
360  if (laneIds.empty()) {
361  return false;
362  }
363  // Iterate over parsed lanes
364  for (int i = 0; i < (int)laneIds.size(); i++) {
365  if (myViewNet->getNet()->retrieveLane(laneIds.at(i), false) == NULL) {
366  return false;
367  }
368  }
369  return true;
370  }
371  case SUMO_ATTR_FILE:
372  return isValidFileValue(value);
373  default:
374  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
375  }
376 }
377 
378 
379 void
380 GNEVariableSpeedSignal::setAttribute(SumoXMLAttr key, const std::string& value) {
381  switch (key) {
382  case SUMO_ATTR_ID:
383  setAdditionalID(value);
384  break;
385  case SUMO_ATTR_LANES: {
386  // Declare variables
387  std::vector<std::string> laneIds;
388  std::vector<GNELane*> lanes;
389  GNELane* lane;
390  SUMOSAXAttributes::parseStringVector(value, laneIds);
391  // Iterate over parsed lanes and obtain pointer to lanes
392  for (int i = 0; i < (int)laneIds.size(); i++) {
393  lane = myViewNet->getNet()->retrieveLane(laneIds.at(i), false);
394  if (lane) {
395  lanes.push_back(lane);
396  }
397  }
398  // Set new childs
399  setLaneChilds(lanes);
400  break;
401  }
402  case SUMO_ATTR_POSITION:
403  bool ok;
404  myPosition = GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false)[0];
405  updateGeometry();
406  getViewNet()->update();
407  break;
408  case SUMO_ATTR_FILE:
409  myFilename = value;
410  break;
411  default:
412  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
413  }
414 }
415 
416 /****************************************************************************/
SUMOReal getExaggeration(const GUIVisualizationSettings &s, SUMOReal factor=20) const
return the drawing size including exaggeration and constantSize values
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:257
int pfDrawString(const char *c)
Definition: polyfonts.c:1074
void close()
Closes the device and removes it from the dictionary.
std::string getFilename() const
get filename of rerouter
void setLaneChilds(std::vector< GNELane * > lanes)
set lane childs
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
long long int SUMOTime
Definition: SUMOTime.h:43
GUIVisualizationTextSettings addName
void setFilename(std::string filename)
set filename of rerouter
Stores the information about how to visualize structures.
bool isAdditionalSelected() const
childLanes myChildLanes
list of child lanes and their positions and rotation
void pfSetPosition(SUMOReal x, SUMOReal y)
Definition: polyfonts.c:480
Position getPositionInView() const
Returns position of Variable Speed Signal in view.
void updateGeometry()
update pre-computed geometry information
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:54
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
void setVariableSpeedSignalSteps(const std::map< SUMOTime, SUMOReal > &vssValues)
set values of variable speed signal
An Element wich group additionalSet elements.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
GUIGlID getGlID() const
Returns the numerical id of the object.
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
static void drawFilledCircle(SUMOReal width, int steps=8)
Draws a filled circle around (0,0)
Definition: GLHelper.cpp:344
void writeAdditional(OutputDevice &device, const std::string &currentDirectory)
writte additional element into a xml file
GUIVisualizationSizeSettings addSize
std::string getAttribute(SumoXMLAttr key) const
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.
void updateConnections()
update connections.
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
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.
const std::string & getAdditionalID() const
returns the ID of additional
PositionVector myShape
The shape of the additional element.
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:658
void setBlockIconRotation(GNELane *lane=NULL)
std::map< SUMOTime, SUMOReal > myVSSValues
values of variable speed signal
friend class GNEChange_Attribute
declare friend class
void drawLockIcon(SUMOReal size=0.5) const
draw lock icon
std::map< SUMOTime, SUMOReal > getVariableSpeedSignalSteps() const
get values of variable speed signal
bool isValid(SumoXMLAttr key, const std::string &value)
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 moveAdditional(SUMOReal posx, SUMOReal posy, GNEUndoList *undoList)
change the position of the rerouter geometry
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
void drawConnections() const
draw connections.
Position myBlockIconOffset
The offSet of the block icon.
A road/street connecting two junctions (netedit-version)
Definition: GNEEdge.h:54
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1093
std::string myFilename
filename of rerouter
GNEVariableSpeedSignal(const std::string &id, GNEViewNet *viewNet, Position pos, std::vector< GNELane * > lanes, const std::string &filename, const std::map< SUMOTime, SUMOReal > &VSSValues, bool blocked)
Constructor.
bool myBlocked
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
void pfSetScale(SUMOReal s)
Definition: polyfonts.c:465
An Element which don&#39;t belongs to GNENet but has influency in the simulation.
Definition: GNEAdditional.h:63
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
RGBColor myBaseColor
base color (Default green)
const std::string & getParentName() const
Returns the name of the parent object (if any)
GNENet * getNet() const
get the net object
Definition: GNEViewNet.cpp:845
SUMOReal pfdkGetStringWidth(const char *c)
Definition: polyfonts.c:1113
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:188
SumoXMLTag getTag() const
get 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:213
void openAdditionalDialog()
open GNEVariableSpeedSignalDialog
std::vector< std::string > getLaneChildIds() const
get ids of lane childs
static GUIGlID getGif(GUITexture which)
returns a texture Gif previously defined in the enum GUITexture
bool insertStep(const SUMOTime time, const SUMOReal speed)
insert a new step in variable speed signal
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.
void append(const PositionVector &v, SUMOReal sameThreshold=2.0)
Position myBlockIconPosition
position of the block icon
bool mySaveInFilename
enable or disable save in external filename