SUMO - Simulation of Urban MObility
GNEDetectorE3.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>
51 
52 #include "GNEDetectorE3.h"
53 #include "GNELane.h"
54 #include "GNEViewNet.h"
55 #include "GNEUndoList.h"
56 #include "GNENet.h"
57 #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 GNEDetectorE3::GNEDetectorE3(const std::string& id, GNEViewNet* viewNet, Position pos, int freq, const std::string& filename, SUMOTime timeThreshold, SUMOReal speedThreshold, bool blocked) :
68  GNEAdditionalSet(id, viewNet, pos, SUMO_TAG_E3DETECTOR, blocked),
69  myFreq(freq),
70  myFilename(filename),
71  myTimeThreshold(timeThreshold),
72  mySpeedThreshold(speedThreshold) {
73  // Update geometry;
75  // Set colors
76  myBaseColor = RGBColor(76, 170, 50, 255);
77  myBaseColorSelected = RGBColor(161, 255, 135, 255);
78 }
79 
80 
82 }
83 
84 
85 void
87  // Clear shape
88  myShape.clear();
89 
90  // Set block icon position
92 
93  // Set block icon offset
94  myBlockIconOffset = Position(-0.5, -0.5);
95 
96  // Set block icon rotation, and using their rotation for draw logo
98 
99  // Set position
100  myShape.push_back(myPosition);
101 
102  // Add shape of childs (To avoid graphics errors)
103  for (childAdditionals::iterator i = myChildAdditionals.begin(); i != myChildAdditionals.end(); i++) {
104  myShape.append((*i)->getShape());
105  }
106 
107  // Update connections
109 
110  // Refresh element (neccesary to avoid grabbing problems)
112 }
113 
114 
115 Position
117  return myPosition;
118 }
119 
120 
121 void
123  // change Position
124  myPosition = Position(offsetx, offsety);
125  updateGeometry();
126 }
127 
128 
129 void
131  undoList->p_begin("position of " + toString(getTag()));
132  undoList->p_add(new GNEChange_Attribute(this, SUMO_ATTR_POSITION, toString(myPosition), true, toString(Position(oldPosx, oldPosy))));
133  undoList->p_end();
134  // Refresh element
136 }
137 
138 
139 void
140 GNEDetectorE3::writeAdditional(OutputDevice& device, const std::string& currentDirectory) {
141  // Only save E3 if have Entry/Exits
142  if (getNumberOfAdditionalChilds() > 0) {
143  // Write parameters
144  device.openTag(getTag());
145  device.writeAttr(SUMO_ATTR_ID, getID());
147  if (!myFilename.empty()) {
149  }
152  device.writeAttr(SUMO_ATTR_X, myPosition.x());
153  device.writeAttr(SUMO_ATTR_Y, myPosition.y());
154  if (myBlocked) {
156  }
157  // Write childs of this element
158  writeAdditionalChildrens(device, currentDirectory);
159  // Close tag
160  device.closeTag();
161  } else {
162  WRITE_WARNING(toString(getTag()) + " with ID = '" + getID() + "' cannot be writed in additional file because don't have childs.");
163  }
164 }
165 
166 
167 const std::string&
169  return myViewNet->getNet()->getMicrosimID();
170 }
171 
172 
173 void
175  // Start drawing adding an gl identificator
176  glPushName(getGlID());
177 
178  // Add a draw matrix for drawing logo
179  glPushMatrix();
180  glTranslated(myShape[0].x(), myShape[0].y(), getType());
181  glColor3d(1, 1, 1);
182  glRotated(180, 0, 0, 1);
183 
184  // Draw icon depending of detector is or isn't selected
185  if (isAdditionalSelected()) {
187  } else {
189  }
190 
191  // Pop logo matrix
192  glPopMatrix();
193 
194  // Show Lock icon depending of the Edit mode
195  drawLockIcon(0.4);
196 
197  // Draw connections
198  drawConnections();
199 
200  // Pop name
201  glPopName();
202 
203  // Draw name
204  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
205 }
206 
207 
208 std::string
210  switch (key) {
211  case SUMO_ATTR_ID:
212  return getAdditionalID();
213  case SUMO_ATTR_POSITION:
214  return toString(myPosition);
215  case SUMO_ATTR_FREQUENCY:
216  return toString(myFreq);
217  case SUMO_ATTR_FILE:
218  return myFilename;
220  return toString(myTimeThreshold);
222  return toString(mySpeedThreshold);
224  return toString(myBlocked);
225  default:
226  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
227  }
228 }
229 
230 
231 void
232 GNEDetectorE3::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
233  if (value == getAttribute(key)) {
234  return; //avoid needless changes, later logic relies on the fact that attributes have changed
235  }
236  switch (key) {
237  case SUMO_ATTR_ID:
238  case SUMO_ATTR_FREQUENCY:
239  case SUMO_ATTR_POSITION:
240  case SUMO_ATTR_FILE:
244  undoList->p_add(new GNEChange_Attribute(this, key, value));
245  updateGeometry();
246  break;
247  default:
248  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
249  }
250 }
251 
252 
253 bool
254 GNEDetectorE3::isValid(SumoXMLAttr key, const std::string& value) {
255  switch (key) {
256  case SUMO_ATTR_ID:
257  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
258  return true;
259  } else {
260  return false;
261  }
262  case SUMO_ATTR_POSITION:
263  bool ok;
264  return GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false).size() == 1;
265  case SUMO_ATTR_FREQUENCY:
266  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0);
267  case SUMO_ATTR_FILE:
268  return isValidFileValue(value);
270  // @ToDo SUMOTIME
271  return canParse<int>(value);
273  return canParse<SUMOReal>(value);
275  return canParse<bool>(value);
276  default:
277  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
278  }
279 }
280 
281 
282 void
283 GNEDetectorE3::setAttribute(SumoXMLAttr key, const std::string& value) {
284  switch (key) {
285  case SUMO_ATTR_ID:
286  setAdditionalID(value);
287  break;
288  case SUMO_ATTR_POSITION:
289  bool ok;
290  myPosition = GeomConvHelper::parseShapeReporting(value, "user-supplied position", 0, ok, false)[0];
291  updateGeometry();
292  getViewNet()->update();
293  break;
294  case SUMO_ATTR_FREQUENCY:
295  myFreq = parse<SUMOReal>(value);
296  break;
297  case SUMO_ATTR_FILE:
298  myFilename = value;
299  break;
301  // @todo SUMOTIME
302  myTimeThreshold = parse<int>(value);
303  break;
305  mySpeedThreshold = parse<SUMOReal>(value);
306  break;
308  myBlocked = parse<bool>(value);
309  getViewNet()->update();
310  break;
311  default:
312  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
313  }
314 }
315 
316 /****************************************************************************/
void drawName(const Position &pos, const SUMOReal scale, const GUIVisualizationTextSettings &settings, const SUMOReal angle=0) const
draw name of item
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
const std::string & getParentName() const
Returns the name of the parent object (if any)
long long int SUMOTime
Definition: SUMOTime.h:43
GUIVisualizationTextSettings addName
std::string getAttribute(SumoXMLAttr key) const
const std::string & getAdditionalID() const
returns the ID of additional
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1250
Position getPositionInView() const
Returns position of detector E3 in view.
Stores the information about how to visualize structures.
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
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.
An Element wich group additionalSet elements.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
SUMOReal scale
information about a lane&#39;s width (temporary, used for a single view)
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
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.
RGBColor myBaseColorSelected
base color selected (Default blue)
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
void writeAdditional(OutputDevice &device, const std::string &currentDirectory)
writte additionalSet element into a xml file
void updateConnections()
update connections.
static bool isValidFileValue(const std::string &value)
true if value is a valid file value
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
void updateGeometry()
update pre-computed geometry information
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
int myFreq
frequency of E3 detector
void setBlockIconRotation(GNELane *lane=NULL)
friend class GNEChange_Attribute
declare friend class
void drawConnections() const
draw connections.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
SUMOTime myTimeThreshold
The time-based threshold that describes how much time has to pass until a vehicle is recognized as ha...
void drawLockIcon(SUMOReal size=0.5) const
draw lock icon
void setAdditionalID(const std::string &id)
set the ID of additional
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
const std::string getID() const
function to support debugging
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
void commmitAdditionalGeometryMoved(SUMOReal oldPosx, SUMOReal oldPosy, GNEUndoList *undoList)
updated geometry changes in the attributes of additional
Position myBlockIconOffset
The offSet of the block icon.
void moveAdditionalGeometry(SUMOReal offsetx, SUMOReal offsety)
change the position of the E3 geometry
std::vector< GNEAdditional * > myChildAdditionals
list of additional childs (Position and rotations is derived from additional)
GNEViewNet * getViewNet() const
Returns a pointer to GNEViewNet in which additional element is located.
bool myBlocked
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
SUMOReal mySpeedThreshold
The speed-based threshold that describes how slow a vehicle has to be to be recognized as halting...
RGBColor myBaseColor
base color (Default green)
~GNEDetectorE3()
GNEDetectorE3 6Destructor.
GNENet * getNet() const
get the net object
Definition: GNEViewNet.cpp:912
void writeAdditionalChildrens(OutputDevice &device, const std::string &currentDirectory)
writte children of this additionalSet
GUIGlID getGlID() const
Returns the numerical id of the object.
GNEDetectorE3(const std::string &id, GNEViewNet *viewNet, Position pos, int freq, const std::string &filename, SUMOTime timeThreshold, SUMOReal speedThreshold, bool blocked)
GNEDetectorE3 Constructor.
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
bool isAdditionalSelected() const
int getNumberOfAdditionalChilds() const
get number of additional childs of this additionalSet
static GUIGlID getGif(GUITexture which)
returns a texture Gif previously defined in the enum GUITexture
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
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.
std::string myFilename
fielname of E3 detector
void append(const PositionVector &v, SUMOReal sameThreshold=2.0)
Position myBlockIconPosition
position of the block icon
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes ...
SumoXMLTag getTag() const
get Tag assigned to this object