SUMO - Simulation of Urban MObility
GNEDetectorE2.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 //
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyrigth (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 #include "GNEEdge.h"
57 
58 
59 // ===========================================================================
60 // member method definitions
61 // ===========================================================================
62 
63 GNEDetectorE2::GNEDetectorE2(const std::string& id, GNELane* lane, GNEViewNet* viewNet, double pos, double length, double freq, const std::string& filename,
64  bool cont, const double timeThreshold, double speedThreshold, double jamThreshold, bool friendlyPos) :
65  GNEDetector(id, viewNet, SUMO_TAG_E2DETECTOR, ICON_E2, lane, pos, freq, filename, friendlyPos),
66  myRelativeLength(length / lane->getLaneParametricLength()),
67  myCont(cont),
68  myTimeThreshold(timeThreshold),
69  mySpeedThreshold(speedThreshold),
70  myJamThreshold(jamThreshold) {
71  // Update geometry;
73 }
74 
75 
77 }
78 
79 
80 void
82  // Clear all containers
83  myShapeRotations.clear();
84  myShapeLengths.clear();
85 
86  // Get shape of lane parent
87  myShape = myLane->getShape();
88 
89  // Cut shape using as delimitators fixed start position and fixed end position
90  double startPosFixed = (myPositionOverLane < 0) ? 0 : myPositionOverLane;
91  double endPosFixed = ((myPositionOverLane + myRelativeLength) > 1) ? 1 : (myPositionOverLane + myRelativeLength);
92  myShape = myShape.getSubpart(startPosFixed * myShape.length(), endPosFixed * myShape.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)M_PI);
118  }
119  }
120 
121  // Set block icon position
123 
124  // Set offset of the block icon
125  myBlockIconOffset = Position(-0.75, 0);
126 
127  // Set block icon rotation, and using their rotation for draw logo
129 
130  // Refresh element (neccesary to avoid grabbing problems)
132 }
133 
134 
135 void
136 GNEDetectorE2::writeAdditional(OutputDevice& device, bool volatileOptionsEnabled) const {
137  // Write parameters
138  device.openTag(getTag());
139  device.writeAttr(SUMO_ATTR_ID, getID());
140  // Check if another lane ID must be changed if sidewalks.guess option is enabled
141  if (volatileOptionsEnabled && OptionsCont::getOptions().getBool("sidewalks.guess") && (myLane->getParentEdge().getLanes().front()->isRestricted(SVC_PEDESTRIAN) == false)) {
142  // add a new extra lane to edge
144  // write ID (now is different because there are a new lane)
145  device.writeAttr(SUMO_ATTR_LANE, myLane->getID());
146  // undo set extra lane
147  myViewNet->getUndoList()->undo();
148  } else {
149  device.writeAttr(SUMO_ATTR_LANE, myLane->getID());
150  }
154  if (!myFilename.empty()) {
156  }
162  if (myBlocked) {
164  }
165  // Close tag
166  device.closeTag();
167 }
168 
169 
171  // with friendly position enabled position are "always fixed"
172  if (myFriendlyPosition) {
173  return true;
174  } else {
175  // floors are needed to avoid precision problems
176  return ((floor(myPositionOverLane * 1000) / 1000) >= 0) && ((floor((myPositionOverLane + myRelativeLength) * 1000) / 1000) <= 1);
177  }
178 }
179 
180 
183 }
184 
185 
186 void
188  // Start drawing adding an gl identificator
189  glPushName(getGlID());
190 
191  // Add a draw matrix
192  glPushMatrix();
193 
194  // Start with the drawing of the area traslating matrix to origing
195  glTranslated(0, 0, getType());
196 
197  // Set color of the base
198  if (isAdditionalSelected()) {
200  } else {
201  GLHelper::setColor(RGBColor(0, 204, 204));
202  }
203 
204  // Obtain exaggeration of the draw
205  const double exaggeration = s.addSize.getExaggeration(s);
206 
207  // Draw the area using shape, shapeRotations, shapeLengths and value of exaggeration
209 
210  // Pop last matrix
211  glPopMatrix();
212 
213  // Check if the distance is enougth to draw details
214  if (s.scale * exaggeration >= 10) {
215  // Push matrix
216  glPushMatrix();
217  // Traslate to center of detector
218  glTranslated(myShape.getLineCenter().x(), myShape.getLineCenter().y(), getType() + 0.1);
219  // Rotate depending of myBlockIconRotation
220  glRotated(myBlockIconRotation, 0, 0, -1);
221  //move to logo position
222  glTranslated(-0.75, 0, 0);
223  // draw E2 logo
224  if (isAdditionalSelected()) {
226  } else {
227  GLHelper::drawText("E2", Position(), .1, 1.5, RGBColor::BLACK);
228  }
229  // pop matrix
230  glPopMatrix();
231 
232  // Show Lock icon depending of the Edit mode
233  drawLockIcon();
234  }
235 
236  // Draw name
237  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
238 
239  // Pop name
240  glPopName();
241 }
242 
243 
244 std::string
246  switch (key) {
247  case SUMO_ATTR_ID:
248  return getAdditionalID();
249  case SUMO_ATTR_LANE:
251  case SUMO_ATTR_POSITION:
253  case SUMO_ATTR_FREQUENCY:
254  return toString(myFreq);
255  case SUMO_ATTR_LENGTH:
257  case SUMO_ATTR_FILE:
258  return myFilename;
259  case SUMO_ATTR_CONT:
260  return toString(myCont);
262  return toString(myTimeThreshold);
264  return toString(mySpeedThreshold);
266  return toString(myJamThreshold);
270  return toString(myBlocked);
271  default:
272  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
273  }
274 }
275 
276 
277 void
278 GNEDetectorE2::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
279  if (value == getAttribute(key)) {
280  return; //avoid needless changes, later logic relies on the fact that attributes have changed
281  }
282  switch (key) {
283  case SUMO_ATTR_ID:
284  case SUMO_ATTR_LANE:
285  case SUMO_ATTR_POSITION:
286  case SUMO_ATTR_FREQUENCY:
287  case SUMO_ATTR_LENGTH:
288  case SUMO_ATTR_FILE:
289  case SUMO_ATTR_CONT:
295  undoList->p_add(new GNEChange_Attribute(this, key, value));
296  updateGeometry();
297  break;
298  default:
299  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
300  }
301 }
302 
303 
304 bool
305 GNEDetectorE2::isValid(SumoXMLAttr key, const std::string& value) {
306  switch (key) {
307  case SUMO_ATTR_ID:
308  if (isValidID(value) && (myViewNet->getNet()->getAdditional(getTag(), value) == NULL)) {
309  return true;
310  } else {
311  return false;
312  }
313  case SUMO_ATTR_LANE:
314  if (myViewNet->getNet()->retrieveLane(value, false) != NULL) {
315  return true;
316  } else {
317  return false;
318  }
319  case SUMO_ATTR_POSITION:
320  return canParse<double>(value);
321  case SUMO_ATTR_FREQUENCY:
322  return (canParse<double>(value) && (parse<double>(value) >= 0));
323  case SUMO_ATTR_LENGTH:
324  if (canParse<double>(value)) {
325  // obtain relative new start position
326  double newLength = parse<double>(value);
327  // lengths withs size 0 aren't valid
328  if (newLength <= 0) {
329  return false;
330  } else {
331  newLength = newLength / myLane->getLaneParametricLength();
332  if ((myPositionOverLane + newLength) > 1) {
333  return false;
334  } else {
335  return true;
336  }
337  }
338  }
339  case SUMO_ATTR_FILE:
340  return isValidFilename(value);
341  case SUMO_ATTR_CONT:
342  return canParse<bool>(value);
344  return (canParse<double>(value) && (parse<double>(value) >= 0));
346  return (canParse<double>(value) && (parse<double>(value) >= 0));
348  return (canParse<double>(value) && (parse<double>(value) >= 0));
350  return canParse<bool>(value);
352  return canParse<bool>(value);
353  default:
354  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
355  }
356 }
357 
358 // ===========================================================================
359 // private
360 // ===========================================================================
361 
362 void
363 GNEDetectorE2::setAttribute(SumoXMLAttr key, const std::string& value) {
364  switch (key) {
365  case SUMO_ATTR_ID:
366  setAdditionalID(value);
367  break;
368  case SUMO_ATTR_LANE:
369  changeLane(value);
370  break;
371  case SUMO_ATTR_POSITION:
372  myPositionOverLane = parse<double>(value) / myLane->getLaneParametricLength();
373  updateGeometry();
374  getViewNet()->update();
375  break;
376  case SUMO_ATTR_FREQUENCY:
377  myFreq = parse<double>(value);
378  break;
379  case SUMO_ATTR_LENGTH:
380  myRelativeLength = parse<double>(value) / myLane->getLaneParametricLength();
381  updateGeometry();
382  getViewNet()->update();
383  break;
384  case SUMO_ATTR_FILE:
385  myFilename = value;
386  break;
387  case SUMO_ATTR_CONT:
388  myCont = parse<bool>(value);
389  break;
391  myTimeThreshold = parse<double>(value);
392  break;
394  mySpeedThreshold = parse<double>(value);
395  break;
397  myJamThreshold = parse<double>(value);
398  break;
400  myFriendlyPosition = parse<bool>(value);
401  getViewNet()->update();
402  break;
404  myBlocked = parse<bool>(value);
405  getViewNet()->update();
406  break;
407  default:
408  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
409  }
410 }
411 
412 /****************************************************************************/
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:261
void drawName(const Position &pos, const double scale, const GUIVisualizationTextSettings &settings, const double angle=0) const
draw name of item
void setBlockIconRotation()
double scale
information about a lane&#39;s width (temporary, used for a single view)
is a pedestrian
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
double x() const
Returns the x-position.
Definition: Position.h:63
GNELane * myLane
The lane this additional belongs.
Stores the information about how to visualize structures.
std::string getAttribute(SumoXMLAttr key) const
This functions has to be implemented in allGNEAttributeCarrier.
bool isAdditionalSelected() const
double myRelativeLength
relative length [0,1]
double myFreq
The aggregation period the values the detector collects shall be summed up.
Definition: GNEDetector.h:158
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 const RGBColor BLACK
Definition: RGBColor.h:187
static bool isValidFilename(const std::string &value)
true if value is a valid file value
std::vector< double > myShapeRotations
GUIGlID getGlID() const
Returns the numerical id of the object.
void undo()
undo the last command group
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
GNEUndoList * getUndoList() const
get the undoList object
~GNEDetectorE2()
Destructor.
GNEViewNet * myViewNet
The GNEViewNet this additional element belongs.
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
static bool isValidID(const std::string &value)
true if value is a valid sumo ID
GUIVisualizationSizeSettings addSize
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
static const RGBColor selectedAdditionalColor
color of selected additionals
Definition: GNENet.h:112
double getExaggeration(const GUIVisualizationSettings &s, double factor=20) const
return the drawing size including exaggeration and constantSize values
GNEViewNet * getViewNet() const
Returns a pointer to GNEViewNet in which additional element is located.
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:438
GNEEdge & getParentEdge()
Returns underlying parent edge.
Definition: GNELane.cpp:1085
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:998
PositionVector myShape
The shape of the additional element.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
bool isDetectorPositionFixed() const
check if Position of detector is fixed
GNELane * retrieveLane(const std::string &id, bool failHard=true)
get lane by id
Definition: GNENet.cpp:958
friend class GNEChange_Attribute
declare friend class
double y() const
Returns the y-position.
Definition: Position.h:68
block movement of a graphic element
double length() const
Returns the length.
std::vector< double > myShapeLengths
The lengths of the shape parts.
double getAbsolutePositionOverLane() const
get absolute position over Lane
void setAdditionalID(const std::string &id)
set the ID of additional
const std::string getID() const
function to support debugging
void drawLockIcon(double size=0.5) const
draw lock icon
bool myFriendlyPosition
Flag for friendly position.
Definition: GNEDetector.h:164
void changeLane(const std::string &laneID)
change lane of additional
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:240
Position myBlockIconOffset
The offSet of the block icon.
std::string myFilename
The path to the output file.
Definition: GNEDetector.h:161
std::string getAttribute(SumoXMLAttr key) const
This functions has to be implemented in allGNEAttributeCarrier.
Definition: GNELane.cpp:770
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1680
const PositionVector & getShape() const
returns the shape of the lane
Definition: GNELane.cpp:625
bool myBlocked
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
const std::vector< GNELane * > & getLanes()
returns a reference to the lane vector
Definition: GNEEdge.cpp:599
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
#define M_PI
Definition: odrSpiral.cpp:40
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
GNENet * getNet() const
get the net object
Position getLineCenter() const
get line center
double myTimeThreshold
The time-based threshold that describes how much time has to pass until a vehicle is recognized as ha...
SumoXMLTag getTag() const
get XML Tag assigned to this object
void writeAdditional(OutputDevice &device, bool volatileOptionsEnabled) const
writte additional element into a xml file
PositionVector getSubpart(double beginOffset, double endOffset) const
get subpart of a position vector
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
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, bool friendlyPos)
Constructor.
double myJamThreshold
The minimum distance to the next standing vehicle in order to make this vehicle count as a participan...
static const RGBColor selectionColor
Definition: GNENet.h:103
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)
draw Text with given parameters
Definition: GLHelper.cpp:455
double getLaneParametricLength() const
returns the parameteric length of the lane
Definition: GNELane.cpp:719
double getAbsoluteLenght() const
get absolute lenght
double myBlockIconRotation
The rotation of the block icon.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void duplicateLane(GNELane *lane, GNEUndoList *undoList)
duplicates lane
Definition: GNENet.cpp:544
double myPositionOverLane
position of detector over Lane
Definition: GNEDetector.h:155
Position myBlockIconPosition
position of the block icon