SUMO - Simulation of Urban MObility
GNEVariableSpeedSign.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 //
18 /****************************************************************************/
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #ifdef _MSC_VER
24 #include <windows_config.h>
25 #else
26 #include <config.h>
27 #endif
28 
29 #include <string>
30 #include <iostream>
31 #include <utility>
36 #include <utils/common/ToString.h>
37 #include <utils/geom/GeomHelper.h>
43 #include <utils/gui/div/GLHelper.h>
47 
48 #include "GNEVariableSpeedSign.h"
49 #include "GNELane.h"
50 #include "GNEViewNet.h"
51 #include "GNEUndoList.h"
52 #include "GNENet.h"
53 #include "GNEChange_Attribute.h"
56 
57 
58 // ===========================================================================
59 // member method definitions
60 // ===========================================================================
61 
62 GNEVariableSpeedSign::GNEVariableSpeedSign(const std::string& id, GNEViewNet* viewNet, Position pos, std::vector<GNELane*> lanes, const std::string& filename) :
63  GNEAdditional(id, viewNet, SUMO_TAG_VSS, ICON_VARIABLESPEEDSIGN, true, lanes),
64  myPosition(pos),
65  myFilename(filename),
66  mySaveInFilename(false) {
67 }
68 
69 
71 }
72 
73 
74 void
76  // Clear shape
77  myShape.clear();
78 
79  // Set block icon position
81 
82  // Set block icon offset
83  myBlockIconOffset = Position(-0.5, -0.5);
84 
85  // Set block icon rotation, and using their rotation for draw logo
87 
88  // Set position
89  myShape.push_back(myPosition);
90 
91  // clear mySymbolsPositionAndRotation
93 
94  // update child connections
96 
97 
98  // Refresh element (neccesary to avoid grabbing problems)
100 }
101 
102 
103 Position
105  return myPosition;
106 }
107 
108 
109 void
111  // Open VSS dialog
113 }
114 
115 
116 void
117 GNEVariableSpeedSign::moveGeometry(const Position& oldPos, const Position& offset) {
118  // restore old position, apply offset and update Geometry
119  myPosition = oldPos;
120  myPosition.add(offset);
121  updateGeometry();
122 }
123 
124 
125 void
127  undoList->p_begin("position of " + toString(getTag()));
128  undoList->p_add(new GNEChange_Attribute(this, SUMO_ATTR_POSITION, toString(myPosition), true, toString(oldPos)));
129  undoList->p_end();
130 }
131 
132 
133 void
135  // Write parameters
136  device.openTag(getTag());
137  device.writeAttr(SUMO_ATTR_ID, getID());
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) {
143  // Write filename attribute
145  // Save values in a different file
147  deviceVSS.openTag("VSS");
148  // write steps
149  for (auto i : mySteps) {
150  i->writeStep(device);
151  }
152  deviceVSS.close();
153  } else {
154  // write steps
155  for (auto i : mySteps) {
156  i->writeStep(device);
157  }
158  }
159  // Close tag
160  device.closeTag();
161 }
162 
163 
164 void
166  auto it = std::find(mySteps.begin(), mySteps.end(), step);
167  if (it == mySteps.end()) {
168  mySteps.push_back(step);
169  // sort steps always after a adding/restoring
171  } else {
172  throw ProcessError("Variable Speed Sign Step already exist");
173  }
174 }
175 
176 
177 void
179  auto it = std::find(mySteps.begin(), mySteps.end(), step);
180  if (it != mySteps.end()) {
181  mySteps.erase(it);
182  // sort steps always after a adding/restoring
184  } else {
185  throw ProcessError("Variable Speed Sign Step doesn't exist");
186  }
187 }
188 
189 
190 const std::vector<GNEVariableSpeedSignStep*>&
192  return mySteps;
193 }
194 
195 
196 void
198  // declare a vector to keep sorted steps
199  std::vector<GNEVariableSpeedSignStep*> sortedSteps;
200  // sort intervals usin time as criterium
201  while (mySteps.size() > 0) {
202  int time_small = 0;
203  // find the interval with the small begin
204  for (int i = 0; i < (int)mySteps.size(); i++) {
205  if (mySteps.at(i)->getTime() < mySteps.at(time_small)->getTime()) {
206  time_small = i;
207  }
208  }
209  // add it to sorted steps and remove it from mySteps
210  sortedSteps.push_back(mySteps.at(time_small));
211  mySteps.erase(mySteps.begin() + time_small);
212  }
213  // restore mySteps using sorted steps
214  mySteps = sortedSteps;
215 }
216 
217 
218 const std::string&
220  return myViewNet->getNet()->getMicrosimID();
221 }
222 
223 
224 void
226  // Start drawing adding an gl identificator
227  glPushName(getGlID());
228 
229  // Add a draw matrix for drawing logo
230  glPushMatrix();
231  glTranslated(myShape[0].x(), myShape[0].y(), getType());
232  glColor3d(1, 1, 1);
233  glRotated(180, 0, 0, 1);
234 
235  // Draw icon depending of variable speed sign is or isn't selected
236  if (isAdditionalSelected()) {
238  } else {
240  }
241 
242  // Pop draw icon matrix
243  glPopMatrix();
244 
245  // Show Lock icon depending of the Edit mode
246  drawLockIcon(0.4);
247 
248  // obtain exxageration
249  const double exaggeration = s.addSize.getExaggeration(s);
250 
251  // iterate over symbols and rotation
252  for (auto i : mySymbolsPositionAndRotation) {
253  glPushMatrix();
254  glScaled(exaggeration, exaggeration, 1);
255  glTranslated(i.first.x(), i.first.y(), getType());
256  glRotated(-1 * i.second, 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((double) 1.3, noPoints);
268  if (s.scale >= 5) {
269  glTranslated(0, 0, .1);
270  glColor3d(0, 0, 0);
271  GLHelper::drawFilledCircle((double) 1.1, noPoints);
272  // draw the speed string
273  //draw
274  glColor3d(1, 1, 0);
275  glTranslated(0, 0, .1);
276  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
277 
278  // draw last value string
279  GLHelper::drawText("S", Position(0, 0), .1, 1.2, RGBColor(255, 255, 0), 180);
280  }
281  glPopMatrix();
282  }
283 
284  // Draw connections
286 
287  // Pop symbol matrix
288  glPopMatrix();
289 
290  // Draw name
291  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
292 
293  // Pop name
294  glPopName();
295 }
296 
297 
298 std::string
300  switch (key) {
301  case SUMO_ATTR_ID:
302  return getAdditionalID();
303  case SUMO_ATTR_LANES:
304  return parseGNELanes(myLaneChilds);
305  case SUMO_ATTR_POSITION:
306  return toString(myPosition);
307  case SUMO_ATTR_FILE:
308  return myFilename;
310  return toString(myBlocked);
311  default:
312  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
313  }
314 }
315 
316 
317 void
318 GNEVariableSpeedSign::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
319  if (value == getAttribute(key)) {
320  return; //avoid needless changes, later logic relies on the fact that attributes have changed
321  }
322  switch (key) {
323  case SUMO_ATTR_ID:
324  case SUMO_ATTR_LANES:
325  case SUMO_ATTR_POSITION:
326  case SUMO_ATTR_FILE:
328  undoList->p_add(new GNEChange_Attribute(this, key, value));
329  break;
330  default:
331  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
332  }
333 }
334 
335 
336 bool
337 GNEVariableSpeedSign::isValid(SumoXMLAttr key, const std::string& value) {
338  switch (key) {
339  case SUMO_ATTR_ID:
340  return isValidAdditionalID(value);
341  case SUMO_ATTR_POSITION: {
342  bool ok;
343  return (GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false).size() == 1);
344  }
345  case SUMO_ATTR_LANES:
346  if (value.empty()) {
347  return false;
348  } else {
349  return checkGNELanesValid(myViewNet->getNet(), value, false);
350  }
351  case SUMO_ATTR_FILE:
352  return isValidFilename(value);
354  return canParse<bool>(value);
355  default:
356  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
357  }
358 }
359 
360 
361 void
362 GNEVariableSpeedSign::setAttribute(SumoXMLAttr key, const std::string& value) {
363  switch (key) {
364  case SUMO_ATTR_ID:
365  changeAdditionalID(value);
366  break;
367  case SUMO_ATTR_LANES:
369  break;
370  case SUMO_ATTR_POSITION:
371  bool ok;
372  myPosition = GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false)[0];
373  break;
374  case SUMO_ATTR_FILE:
375  myFilename = value;
376  break;
378  myBlocked = parse<bool>(value);
379  break;
380  default:
381  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
382  }
383  // After setting attribute always update Geometry
384  updateGeometry();
385 }
386 
387 /****************************************************************************/
static void drawTexturedBox(int which, double size)
Draws a named texture as a box with the given size.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:260
void close()
Closes the device and removes it from the dictionary.
double scale
information about a lane&#39;s width (temporary, used for a single view)
void sortVariableSpeedSignSteps()
sort steps
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
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:132
void refreshElement(GUIGlObject *o)
refreshes boundary information for o and update
Definition: GNENet.cpp:1082
const std::string & getAdditionalID() const
returns Additional ID
const std::vector< GNEVariableSpeedSignStep * > & getVariableSpeedSignSteps() const
get values of variable speed signal
static GUIGlID getTexture(GUITexture which)
returns a texture previously defined in the enum GUITexture
void removeVariableSpeedSignStep(GNEVariableSpeedSignStep *step)
remove an existent step of variable speed signal
Stores the information about how to visualize structures.
double y() const
Returns the y-position.
Definition: Position.h:67
void openAdditionalDialog()
open GNEVariableSpeedSignDialog
double x() const
Returns the x-position.
Definition: Position.h:62
static void drawText(const std::string &text, const Position &pos, const double layer, const double size, const RGBColor &col=RGBColor::BLACK, const double angle=0, int align=0, double width=-1)
Definition: GLHelper.cpp:487
Position getPositionInView() const
Returns position of additional in view.
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:84
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static bool isValidFilename(const std::string &value)
true if value is a valid file value
static void drawFilledCircle(double width, int steps=8)
Draws a filled circle around (0,0)
Definition: GLHelper.cpp:350
void changeAdditionalID(const std::string &newID)
change ID of additional
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.
GNEViewNet * myViewNet
The GNEViewNet this additional element belongs.
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
GUIVisualizationSizeSettings addSize
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
void drawChildConnections() const
draw connections between Parent and childrens
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
void drawLockIcon(double size=0.5) const
draw lock icon
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
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:91
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
friend class GNEChange_Attribute
declare friend class
static bool checkGNELanesValid(GNENet *net, const std::string &value, bool report)
check if a list of Lane IDs is valid
bool mySaveInFilename
enable or disable save in external filename
block movement of a graphic element
void drawName(const Position &pos, const double scale, const GUIVisualizationTextSettings &settings, const double angle=0) const
draw name of item
void updateGeometry()
update pre-computed geometry information
void updateChildConnections()
update Connection&#39;s geometry
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
std::vector< GNEVariableSpeedSignStep * > mySteps
values of variable speed signal
const std::string getID() const
function to support debugging
std::string getAttribute(SumoXMLAttr key) const
This functions has to be implemented in all GNEAttributeCarriers.
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.
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:59
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
void setBlockIconRotation(GNELane *additionalLane=NULL)
std::vector< std::pair< Position, double > > mySymbolsPositionAndRotation
position and rotation of every simbol over lane
~GNEVariableSpeedSign()
Destructor.
GNENet * getNet() const
get the net object
GUIGlID getGlID() const
Returns the numerical id of the object.
double getExaggeration(const GUIVisualizationSettings &s, double factor=20) const
return the drawing size including exaggeration and constantSize values
void moveGeometry(const Position &oldPos, const Position &offset)
change the position of the element geometry without saving in undoList
A variable speed sign.
GNEVariableSpeedSign(const std::string &id, GNEViewNet *viewNet, Position pos, std::vector< GNELane *> lanes, const std::string &filename)
Constructor.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
Position myPosition
position of VSS in view
bool closeTag()
Closes the most recently opened tag.
bool isValidAdditionalID(const std::string &newID) const
check if a new additional ID is valid
bool isAdditionalSelected() const
std::string myFilename
filename of variable speed sign
static std::vector< GNELane * > parseGNELanes(GNENet *net, const std::string &value)
parse string into vector of GNELanes
const std::string & getParentName() const
Returns the name of the parent object.
void writeAdditional(OutputDevice &device) const
writte additional element into a xml file
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 addVariableSpeedSignStep(GNEVariableSpeedSignStep *step)
insert a new step in variable speed signal
Position myBlockIconPosition
position of the block icon
std::vector< GNELane * > myLaneChilds
vector with the lane childs of this additional
SumoXMLTag getTag() const
get XML Tag assigned to this object