SUMO - Simulation of Urban MObility
GNEDetectorE2.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-2017 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>
37 #include <utils/common/ToString.h>
38 #include <utils/geom/GeomHelper.h>
45 #include <utils/gui/div/GLHelper.h>
49 
50 #include "GNEDetectorE2.h"
51 #include "GNELane.h"
52 #include "GNEViewNet.h"
53 #include "GNEUndoList.h"
54 #include "GNENet.h"
55 #include "GNEChange_Attribute.h"
56 
57 
58 // ===========================================================================
59 // member method definitions
60 // ===========================================================================
61 
62 GNEDetectorE2::GNEDetectorE2(const std::string& id, GNELane* lane, GNEViewNet* viewNet, double pos, double length, double freq, const std::string& filename,
63  bool cont, const double timeThreshold, double speedThreshold, double jamThreshold) :
64  GNEDetector(id, viewNet, SUMO_TAG_E2DETECTOR, ICON_E2, lane, pos, freq, filename),
65  myLength(length),
66  myCont(cont),
67  myTimeThreshold(timeThreshold),
68  mySpeedThreshold(speedThreshold),
69  myJamThreshold(jamThreshold) {
70  // Update geometry;
72  // Set Colors
73  myBaseColor = RGBColor(0, 204, 204, 255);
74  myBaseColorSelected = RGBColor(125, 204, 204, 255);
75 }
76 
77 
79 }
80 
81 
82 void
84  // Clear all containers
85  myShapeRotations.clear();
86  myShapeLengths.clear();
87 
88  // Get shape of lane parent
89  myShape = myLane->getShape();
90 
91  // Cut shape using as delimitators myPos and their length (myPos + length)
93 
94  // Get number of parts of the shape
95  int numberOfSegments = (int) myShape.size() - 1;
96 
97  // If number of segments is more than 0
98  if (numberOfSegments >= 0) {
99 
100  // Reserve memory (To improve efficiency)
101  myShapeRotations.reserve(numberOfSegments);
102  myShapeLengths.reserve(numberOfSegments);
103 
104  // For every part of the shape
105  for (int i = 0; i < numberOfSegments; ++i) {
106 
107  // Obtain first position
108  const Position& f = myShape[i];
109 
110  // Obtain next position
111  const Position& s = myShape[i + 1];
112 
113  // Save distance between position into myShapeLengths
114  myShapeLengths.push_back(f.distanceTo(s));
115 
116  // Save rotation (angle) of the vector constructed by points f and s
117  myShapeRotations.push_back((double) atan2((s.x() - f.x()), (f.y() - s.y())) * (double) 180.0 / (double) PI);
118  }
119  }
120 
121  // Set offset of logo
122  myDetectorLogoOffset = Position(0.5, 0);
123 
124  // Set block icon position
126 
127  // Set offset of the block icon
128  myBlockIconOffset = Position(-0.5, 0);
129 
130  // Set block icon rotation, and using their rotation for draw logo
132 
133  // Refresh element (neccesary to avoid grabbing problems)
135 }
136 
137 
138 Position
141 }
142 
143 
144 void
146  // Write parameters
147  device.openTag(getTag());
148  device.writeAttr(SUMO_ATTR_ID, getID());
149  device.writeAttr(SUMO_ATTR_LANE, myLane->getID());
153  if (!myFilename.empty()) {
155  }
160  if (myBlocked) {
162  }
163  // Close tag
164  device.closeTag();
165 }
166 
167 
168 void
170  // Start drawing adding an gl identificator
171  glPushName(getGlID());
172 
173  // Add a draw matrix
174  glPushMatrix();
175 
176  // Start with the drawing of the area traslating matrix to origing
177  glTranslated(0, 0, getType());
178 
179  // Set color of the base
180  if (isAdditionalSelected()) {
182  } else {
184  }
185 
186  // Obtain exaggeration of the draw
187  const double exaggeration = s.addSize.getExaggeration(s);
188 
189  // Draw the area using shape, shapeRotations, shapeLengths and value of exaggeration
191 
192  // Pop last matrix
193  glPopMatrix();
194 
195  // Check if the distance is enought to draw details
196  if (s.scale * exaggeration >= 10) {
197  // Draw icon
199 
200  // Show Lock icon depending of the Edit mode
201  drawLockIcon();
202  }
203 
204  // Draw name
205  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
206 
207  // Pop name
208  glPopName();
209 }
210 
211 
212 std::string
214  switch (key) {
215  case SUMO_ATTR_ID:
216  return getAdditionalID();
217  case SUMO_ATTR_LANE:
219  case SUMO_ATTR_POSITION:
220  return toString(myPosition.x());
221  case SUMO_ATTR_FREQUENCY:
222  return toString(myFreq);
223  case SUMO_ATTR_LENGTH:
224  return toString(myLength);
225  case SUMO_ATTR_FILE:
226  return myFilename;
227  case SUMO_ATTR_CONT:
228  return toString(myCont);
230  return toString(myTimeThreshold);
232  return toString(mySpeedThreshold);
234  return toString(myJamThreshold);
236  return toString(myBlocked);
237  default:
238  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
239  }
240 }
241 
242 
243 void
244 GNEDetectorE2::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
245  if (value == getAttribute(key)) {
246  return; //avoid needless changes, later logic relies on the fact that attributes have changed
247  }
248  switch (key) {
249  case SUMO_ATTR_ID:
250  case SUMO_ATTR_LANE:
251  case SUMO_ATTR_POSITION:
252  case SUMO_ATTR_FREQUENCY:
253  case SUMO_ATTR_LENGTH:
254  case SUMO_ATTR_FILE:
255  case SUMO_ATTR_CONT:
260  undoList->p_add(new GNEChange_Attribute(this, key, value));
261  updateGeometry();
262  break;
263  default:
264  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
265  }
266 }
267 
268 
269 bool
270 GNEDetectorE2::isValid(SumoXMLAttr key, const std::string& value) {
271  switch (key) {
272  case SUMO_ATTR_ID:
273  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
274  return true;
275  } else {
276  return false;
277  }
278  case SUMO_ATTR_LANE:
279  if (myViewNet->getNet()->retrieveLane(value, false) != NULL) {
280  return true;
281  } else {
282  return false;
283  }
284  case SUMO_ATTR_POSITION:
285  return (canParse<double>(value) && parse<double>(value) >= 0 && parse<double>(value) <= (myLane->getLaneParametricLength()));
286  case SUMO_ATTR_FREQUENCY:
287  return (canParse<double>(value) && parse<double>(value) >= 0);
288  case SUMO_ATTR_LENGTH:
289  return (canParse<double>(value) && parse<double>(value) >= 0);
290  case SUMO_ATTR_FILE:
291  return isValidFilename(value);
292  case SUMO_ATTR_CONT:
293  return canParse<bool>(value);
295  return (canParse<double>(value) && parse<double>(value) >= 0);
297  return (canParse<double>(value) && parse<double>(value) >= 0);
299  return (canParse<double>(value) && parse<double>(value) >= 0);
301  return canParse<bool>(value);
302  default:
303  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
304  }
305 }
306 
307 // ===========================================================================
308 // private
309 // ===========================================================================
310 
311 void
312 GNEDetectorE2::setAttribute(SumoXMLAttr key, const std::string& value) {
313  switch (key) {
314  case SUMO_ATTR_ID:
315  setAdditionalID(value);
316  break;
317  case SUMO_ATTR_LANE:
318  changeLane(value);
319  break;
320  case SUMO_ATTR_POSITION:
321  myPosition = Position(parse<double>(value), 0);
322  updateGeometry();
323  getViewNet()->update();
324  break;
325  case SUMO_ATTR_FREQUENCY:
326  myFreq = parse<double>(value);
327  break;
328  case SUMO_ATTR_LENGTH:
329  myLength = parse<double>(value);
330  updateGeometry();
331  getViewNet()->update();
332  break;
333  case SUMO_ATTR_FILE:
334  myFilename = value;
335  break;
336  case SUMO_ATTR_CONT:
337  myCont = parse<bool>(value);
338  break;
340  myTimeThreshold = parse<double>(value);
341  break;
343  mySpeedThreshold = parse<double>(value);
344  break;
346  myJamThreshold = parse<double>(value);
347  break;
349  myBlocked = parse<bool>(value);
350  getViewNet()->update();
351  break;
352  default:
353  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
354  }
355 }
356 
357 /****************************************************************************/
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
double scale
information about a lane&#39;s width (temporary, used for a single view)
static void drawBoxLines(const PositionVector &geom, const std::vector< double > &rots, const std::vector< double > &lengths, double width, int cornerDetail=0, double offset=0)
Draws thick lines.
Definition: GLHelper.cpp:172
GUIVisualizationTextSettings addName
const std::string & getAdditionalID() const
returns the ID of additional
static GUIGlID getTexture(GUITexture which)
returns a texture previously defined in the enum GUITexture
void writeAdditional(OutputDevice &device) const
writte additional element into a xml file
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1331
GNELane * myLane
The lane this additional belongs.
Stores the information about how to visualize structures.
double y() const
Returns the y-position.
Definition: Position.h:68
double getPositionRelativeToParametricLength(double position) const
Definition: GNELane.cpp:727
double x() const
Returns the x-position.
Definition: Position.h:63
double myFreq
The aggregation period the values the detector collects shall be summed up.
Definition: GNEDetector.h:147
bool myCont
attribute to enable or disable splitByType
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:54
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes ...
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
void updateGeometry()
update pre-computed geometry information
static bool isValidFilename(const std::string &value)
true if value is a valid file value
std::vector< double > myShapeRotations
Position getLineCenter() const
get line center
~GNEDetectorE2()
Destructor.
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)
double mySpeedThreshold
The speed-based threshold that describes how slow a vehicle has to be to be recognized as halting...
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
GUIVisualizationSizeSettings addSize
Position getPositionInView() const
Returns position of detector E2 in view.
double getLaneParametricLength() const
returns the parameteric length of the lane
Definition: GNELane.cpp:715
#define PI
Definition: polyfonts.c:61
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:56
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:439
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.
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
Position myDetectorLogoOffset
The position of detector.
Definition: GNEDetector.h:158
friend class GNEChange_Attribute
declare friend class
block movement of a graphic element
std::vector< double > myShapeLengths
The lengths of the shape parts.
void drawName(const Position &pos, const double scale, const GUIVisualizationTextSettings &settings, const double angle=0) const
draw name of item
void setAdditionalID(const std::string &id)
set the ID of additional
const std::string getID() const
function to support debugging
double myLength
attribute length
PositionVector getSubpart(double beginOffset, double endOffset) const
get subpart of a position vector
void changeLane(const std::string &laneID)
change lane of additional
Position myBlockIconOffset
The offSet of the block icon.
std::string myFilename
The path to the output file.
Definition: GNEDetector.h:150
std::string getAttribute(SumoXMLAttr key) const
Definition: GNELane.cpp:780
std::string getAttribute(SumoXMLAttr key) const
void drawDetectorIcon(const int GNELogoID, double sizex=0.5, double sizey=0.5) const
GNEViewNet * getViewNet() const
Returns a pointer to GNEViewNet in which additional element is located.
const PositionVector & getShape() const
returns the shape of the lane
Definition: GNELane.cpp:621
bool myBlocked
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
RGBColor myBaseColor
base color (Default green)
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
double myTimeThreshold
The time-based threshold that describes how much time has to pass until a vehicle is recognized as ha...
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:240
Position myPosition
The position in which this additional element is located.
bool closeTag()
Closes the most recently opened tag.
GNEDetectorE2(const std::string &id, GNELane *lane, GNEViewNet *viewNet, double pos, double length, double freq, const std::string &filename, bool cont, const double timeThreshold, double speedThreshold, double jamThreshold)
Constructor.
double myJamThreshold
The minimum distance to the next standing vehicle in order to make this vehicle count as a participan...
bool isAdditionalSelected() const
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Position myBlockIconPosition
position of the block icon
SumoXMLTag getTag() const
get XML Tag assigned to this object