SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  // Refresh element (neccesary to avoid grabbing problems)
111 }
112 
113 
114 Position
116  return myPosition;
117 }
118 
119 
120 void
122  GNEVariableSpeedSignalDialog variableSpeedSignalDialog(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
145 GNEVariableSpeedSignal::writeAdditional(OutputDevice& device, const std::string& currentDirectory) {
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  OutputDevice& deviceVSS = OutputDevice::getDevice(currentDirectory + myFilename);
158  deviceVSS.openTag("VSS");
159  for (std::map<SUMOTime, SUMOReal>::const_iterator i = myVSSValues.begin(); i != myVSSValues.end(); ++i) {
160  // Open VSS tag
161  deviceVSS.openTag(SUMO_TAG_STEP);
162  // Write TimeSTep
163  deviceVSS.writeAttr(SUMO_ATTR_TIME, i->first);
164  // Write speed
165  deviceVSS.writeAttr(SUMO_ATTR_SPEED, i->second);
166  // Close VSS tag
167  deviceVSS.closeTag();
168  }
169  deviceVSS.close();
170  } else {
171  for (std::map<SUMOTime, SUMOReal>::const_iterator i = myVSSValues.begin(); i != myVSSValues.end(); ++i) {
172  // Open VSS tag
173  device.openTag(SUMO_TAG_STEP);
174  // Write TimeSTep
175  device.writeAttr(SUMO_ATTR_TIME, i->first);
176  // Write speed
177  device.writeAttr(SUMO_ATTR_SPEED, i->second);
178  // Close VSS tag
179  device.closeTag();
180  }
181  }
182  if (myBlocked) {
184  }
185  // Close tag
186  device.closeTag();
187 }
188 
189 
190 std::string
192  return myFilename;
193 }
194 
195 
196 std::map<SUMOTime, SUMOReal>
198  return myVSSValues;
199 }
200 
201 
202 void
203 GNEVariableSpeedSignal::setFilename(std::string filename) {
204  myFilename = filename;
205 }
206 
207 
208 void
209 GNEVariableSpeedSignal::setVariableSpeedSignalSteps(const std::map<SUMOTime, SUMOReal>& vssValues) {
210  myVSSValues = vssValues;
211 }
212 
213 
214 bool
216  if (myVSSValues.find(time) == myVSSValues.end()) {
217  myVSSValues[time] = speed;
218  return true;
219  } else {
220  return false;
221  }
222 }
223 
224 
225 const std::string&
227  return myViewNet->getNet()->getMicrosimID();
228 }
229 
230 
231 void
233  // Start drawing adding an gl identificator
234  glPushName(getGlID());
235 
236  // Add a draw matrix for drawing logo
237  glPushMatrix();
238  glTranslated(myShape[0].x(), myShape[0].y(), getType());
239  glColor3d(1, 1, 1);
240  glRotated(180, 0, 0, 1);
241 
242  // Draw icon depending of rerouter is or isn't selected
243  if (isAdditionalSelected()) {
245  } else {
247  }
248 
249  // Pop draw icon matrix
250  glPopMatrix();
251 
252  // Show Lock icon depending of the Edit mode
253  drawLockIcon(0.4);
254 
255  // Push matrix to draw every symbol over lane
256  glPushMatrix();
257 
258  // Traslate to 0,0
259  glTranslated(0, 0, getType());
260 
261  // Obtain exaggeration
262  const SUMOReal exaggeration = s.addSize.getExaggeration(s);
263 
264  // Iterate over lanes
265  for (childLanes::const_iterator i = myChildLanes.begin(); i != myChildLanes.end(); i++) {
266  // Draw every signal over Lane
267  glPushMatrix();
268  glScaled(exaggeration, exaggeration, 1);
269  glTranslated(i->positionOverLane.x(), i->positionOverLane.y(), 0);
270  glRotated(i->rotationOverLane, 0, 0, 1);
271  glTranslated(0, -1.5, 0);
272 
273  int noPoints = 9;
274  if (s.scale > 25) {
275  noPoints = (int)(9.0 + s.scale / 10.0);
276  if (noPoints > 36) {
277  noPoints = 36;
278  }
279  }
280  glColor3d(1, 0, 0);
281  GLHelper::drawFilledCircle((SUMOReal) 1.3, noPoints);
282  if (s.scale >= 5) {
283  glTranslated(0, 0, .1);
284  glColor3d(0, 0, 0);
285  GLHelper::drawFilledCircle((SUMOReal) 1.1, noPoints);
286  // Draw speed
287  SUMOReal speed = i->lane->getSpeed();
288  // Show as Km/h
289  speed *= 3.6f;
290  if (((int) speed + 1) % 10 == 0) {
291  speed = (SUMOReal)(((int) speed + 1) / 10 * 10);
292  }
293  // draw the speed string
294  std::string speedToDraw = toString<SUMOReal>(speed);
295  glColor3d(1, 1, 0);
296  glTranslated(0, 0, .1);
297  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
298  pfSetPosition(0, 0);
299  pfSetScale(1.2f);
300  SUMOReal w = pfdkGetStringWidth(speedToDraw.c_str());
301  glRotated(180, 0, 1, 0);
302  glTranslated(-w / 2., 0.3, 0);
303  pfDrawString(speedToDraw.c_str());
304  }
305  glPopMatrix();
306  }
307 
308  // Pop symbol matrix
309  glPopMatrix();
310 
311  // Draw connections
312  drawConnections();
313 
314  // Draw name
315  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
316 
317  // Pop name
318  glPopName();
319 }
320 
321 
322 std::string
324  switch (key) {
325  case SUMO_ATTR_ID:
326  return getAdditionalID();
327  case SUMO_ATTR_LANES:
328  return joinToString(getLaneChildIds(), " ");
329  case SUMO_ATTR_POSITION:
330  return toString(myPosition);
331  case SUMO_ATTR_FILE:
332  return myFilename;
334  return toString(myBlocked);
335  default:
336  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
337  }
338 }
339 
340 
341 void
342 GNEVariableSpeedSignal::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
343  if (value == getAttribute(key)) {
344  return; //avoid needless changes, later logic relies on the fact that attributes have changed
345  }
346  switch (key) {
347  case SUMO_ATTR_ID:
348  case SUMO_ATTR_LANES:
349  case SUMO_ATTR_POSITION:
350  case SUMO_ATTR_FILE:
352  undoList->p_add(new GNEChange_Attribute(this, key, value));
353  updateGeometry();
354  break;
355  default:
356  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
357  }
358 }
359 
360 
361 bool
362 GNEVariableSpeedSignal::isValid(SumoXMLAttr key, const std::string& value) {
363  switch (key) {
364  case SUMO_ATTR_ID:
365  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
366  return true;
367  } else {
368  return false;
369  }
370  case SUMO_ATTR_POSITION:
371  bool ok;
372  return GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false).size() == 1;
373  case SUMO_ATTR_LANES: {
374  std::vector<std::string> laneIds;
375  SUMOSAXAttributes::parseStringVector(value, laneIds);
376  // Empty Lanes aren't valid
377  if (laneIds.empty()) {
378  return false;
379  }
380  // Iterate over parsed lanes
381  for (int i = 0; i < (int)laneIds.size(); i++) {
382  if (myViewNet->getNet()->retrieveLane(laneIds.at(i), false) == NULL) {
383  return false;
384  }
385  }
386  return true;
387  }
388  case SUMO_ATTR_FILE:
389  return isValidFileValue(value);
391  return canParse<bool>(value);
392  default:
393  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
394  }
395 }
396 
397 
398 void
399 GNEVariableSpeedSignal::setAttribute(SumoXMLAttr key, const std::string& value) {
400  switch (key) {
401  case SUMO_ATTR_ID:
402  setAdditionalID(value);
403  break;
404  case SUMO_ATTR_LANES: {
405  // Declare variables
406  std::vector<std::string> laneIds;
407  std::vector<GNELane*> lanes;
408  GNELane* lane;
409  SUMOSAXAttributes::parseStringVector(value, laneIds);
410  // Iterate over parsed lanes and obtain pointer to lanes
411  for (int i = 0; i < (int)laneIds.size(); i++) {
412  lane = myViewNet->getNet()->retrieveLane(laneIds.at(i), false);
413  if (lane) {
414  lanes.push_back(lane);
415  }
416  }
417  // Set new childs
418  setLaneChilds(lanes);
419  break;
420  }
421  case SUMO_ATTR_POSITION:
422  bool ok;
423  myPosition = GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false)[0];
424  updateGeometry();
425  getViewNet()->update();
426  break;
427  case SUMO_ATTR_FILE:
428  myFilename = value;
429  break;
431  myBlocked = parse<bool>(value);
432  getViewNet()->update();
433  break;
434  default:
435  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
436  }
437 }
438 
439 /****************************************************************************/
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)
method for setting the attribute and letting the object perform additional changes ...
long long int SUMOTime
Definition: SUMOTime.h:43
GUIVisualizationTextSettings addName
void moveAdditionalGeometry(SUMOReal offsetx, SUMOReal offsety)
change the position of the rerouter geometry
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:55
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
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
void setVariableSpeedSignalSteps(const std::map< SUMOTime, SUMOReal > &vssValues)
set values of variable speed signal
void commmitAdditionalGeometryMoved(SUMOReal oldPosx, SUMOReal oldPosy, GNEUndoList *undoList)
updated geometry changes in the attributes of additional
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'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
void refreshAdditional(GNEAdditional *additional)
refreshes boundary information of an additional after a geometry update
Definition: GNENet.cpp:784
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:744
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)
method for checking if the key and their conrrespond attribute are valids
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
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:55
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1250
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'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:912
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:214
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