SUMO - Simulation of Urban MObility
GNEDetectorE2.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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 //
16 /****************************************************************************/
17 
18 // ===========================================================================
19 // included modules
20 // ===========================================================================
21 #include <config.h>
22 
23 #include <string>
24 #include <iostream>
25 #include <utility>
26 #include <netbuild/NBEdge.h>
30 #include <utils/common/ToString.h>
31 #include <utils/geom/GeomHelper.h>
36 #include <utils/gui/div/GLHelper.h>
42 #include <netedit/GNEViewNet.h>
43 #include <netedit/GNEUndoList.h>
44 #include <netedit/GNENet.h>
47 #include <netedit/GNEViewParent.h>
48 
49 #include "GNEDetectorE2.h"
50 
51 
52 // ===========================================================================
53 // member method definitions
54 // ===========================================================================
55 
56 GNEDetectorE2::GNEDetectorE2(const std::string& id, GNELane* lane, GNEViewNet* viewNet, double pos, double length, double freq, const std::string& filename, const std::string& vehicleTypes,
57  const std::string& name, const double timeThreshold, double speedThreshold, double jamThreshold, bool friendlyPos, bool blockMovement) :
58  GNEDetector(id, viewNet, GLO_E2DETECTOR, SUMO_TAG_E2DETECTOR, lane, pos, freq, filename, vehicleTypes, name, friendlyPos, blockMovement),
59  myLength(length),
60  myTimeThreshold(timeThreshold),
61  mySpeedThreshold(speedThreshold),
62  myJamThreshold(jamThreshold) {
63 }
64 
65 
67 }
68 
69 
70 void
72  // first check if object has to be removed from grid (SUMOTree)
73  if (updateGrid) {
75  }
76 
77  // Clear all containers
78  myShapeRotations.clear();
79  myShapeLengths.clear();
80 
81  // Get shape of lane parent
82  myShape = myLane->getShape();
83 
84  // set start position
85  double startPosFixed;
86  if (myPositionOverLane < 0) {
87  startPosFixed = 0;
89  startPosFixed = myLane->getParentEdge().getNBEdge()->getFinalLength();
90  } else {
91  startPosFixed = myPositionOverLane;
92  }
93 
94  // set end position
95  double endPosFixed;
96  if ((myPositionOverLane + myLength) < 0) {
97  endPosFixed = 0;
99  endPosFixed = myLane->getParentEdge().getNBEdge()->getFinalLength();
100  } else {
101  endPosFixed = (myPositionOverLane + myLength);
102  }
103 
104  // Cut shape using as delimitators fixed start position and fixed end position
106 
107  // Get number of parts of the shape
108  int numberOfSegments = (int)myShape.size() - 1;
109 
110  // If number of segments is more than 0
111  if (numberOfSegments >= 0) {
112 
113  // Reserve memory (To improve efficiency)
114  myShapeRotations.reserve(numberOfSegments);
115  myShapeLengths.reserve(numberOfSegments);
116 
117  // For every part of the shape
118  for (int i = 0; i < numberOfSegments; ++i) {
119 
120  // Obtain first position
121  const Position& f = myShape[i];
122 
123  // Obtain next position
124  const Position& s = myShape[i + 1];
125 
126  // Save distance between position into myShapeLengths
127  myShapeLengths.push_back(f.distanceTo(s));
128 
129  // Save rotation (angle) of the vector constructed by points f and s
130  myShapeRotations.push_back((double)atan2((s.x() - f.x()), (f.y() - s.y())) * (double) 180.0 / (double)M_PI);
131  }
132  }
133 
134  // Set block icon position
136 
137  // Set offset of the block icon
138  myBlockIconOffset = Position(-0.75, 0);
139 
140  // Set block icon rotation, and using their rotation for draw logo
142 
143  // last step is to check if object has to be added into grid (SUMOTree) again
144  if (updateGrid) {
146  }
147 }
148 
149 
150 double
152  return myLength;
153 }
154 
155 
156 bool
158  // with friendly position enabled position are "always fixed"
159  if (myFriendlyPosition) {
160  return true;
161  } else {
163  }
164 }
165 
166 
167 void
169  // Start drawing adding an gl identificator
170  glPushName(getGlID());
171 
172  // Add a draw matrix
173  glPushMatrix();
174 
175  // Start with the drawing of the area traslating matrix to origing
176  glTranslated(0, 0, getType());
177 
178  // Set color of the base
181  } else {
182  GLHelper::setColor(RGBColor(0, 204, 204));
183  }
184 
185  // Obtain exaggeration of the draw
186  const double exaggeration = s.addSize.getExaggeration(s);
187 
188  // Draw the area using shape, shapeRotations, shapeLengths and value of exaggeration
190 
191  // Pop last matrix
192  glPopMatrix();
193 
194  // Check if the distance is enougth to draw details and isn't being drawn for selecting
195  if ((s.scale * exaggeration >= 10) && !s.drawForSelecting) {
196  // Push matrix
197  glPushMatrix();
198  // Traslate to center of detector
199  glTranslated(myShape.getLineCenter().x(), myShape.getLineCenter().y(), getType() + 0.1);
200  // Rotate depending of myBlockIconRotation
201  glRotated(myBlockIconRotation, 0, 0, -1);
202  //move to logo position
203  glTranslated(-0.75, 0, 0);
204  // draw E2 logo
207  } else {
208  GLHelper::drawText("E2", Position(), .1, 1.5, RGBColor::BLACK);
209  }
210  // pop matrix
211  glPopMatrix();
212 
213  // Show Lock icon depending of the Edit mode
214  drawLockIcon();
215  }
216 
217  // Draw name if isn't being drawn for selecting
218  if (!s.drawForSelecting) {
219  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
220  }
221  // check if dotted contour has to be drawn
222  if (!s.drawForSelecting && (myViewNet->getACUnderCursor() == this)) {
224  }
225  // Pop name
226  glPopName();
227 }
228 
229 
230 std::string
232  switch (key) {
233  case SUMO_ATTR_ID:
234  return getAdditionalID();
235  case SUMO_ATTR_LANE:
236  return myLane->getID();
237  case SUMO_ATTR_POSITION:
239  case SUMO_ATTR_FREQUENCY:
240  return toString(myFreq);
241  case SUMO_ATTR_LENGTH:
242  return toString(myLength);
243  case SUMO_ATTR_NAME:
244  return myAdditionalName;
245  case SUMO_ATTR_FILE:
246  return myFilename;
247  case SUMO_ATTR_VTYPES:
248  return myVehicleTypes;
250  return toString(myTimeThreshold);
252  return toString(mySpeedThreshold);
254  return toString(myJamThreshold);
258  return toString(myBlockMovement);
259  case GNE_ATTR_SELECTED:
261  case GNE_ATTR_GENERIC:
262  return getGenericParametersStr();
263  default:
264  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
265  }
266 }
267 
268 
269 void
270 GNEDetectorE2::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
271  if (value == getAttribute(key)) {
272  return; //avoid needless changes, later logic relies on the fact that attributes have changed
273  }
274  switch (key) {
275  case SUMO_ATTR_ID:
276  case SUMO_ATTR_LANE:
277  case SUMO_ATTR_POSITION:
278  case SUMO_ATTR_FREQUENCY:
279  case SUMO_ATTR_LENGTH:
280  case SUMO_ATTR_NAME:
281  case SUMO_ATTR_FILE:
282  case SUMO_ATTR_VTYPES:
288  case GNE_ATTR_SELECTED:
289  case GNE_ATTR_GENERIC:
290  undoList->p_add(new GNEChange_Attribute(this, key, value));
291  break;
292  default:
293  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
294  }
295 }
296 
297 
298 bool
299 GNEDetectorE2::isValid(SumoXMLAttr key, const std::string& value) {
300  switch (key) {
301  case SUMO_ATTR_ID:
302  return isValidAdditionalID(value);
303  case SUMO_ATTR_LANE:
304  if (myViewNet->getNet()->retrieveLane(value, false) != nullptr) {
305  return true;
306  } else {
307  return false;
308  }
309  case SUMO_ATTR_POSITION:
310  return canParse<double>(value);
311  case SUMO_ATTR_FREQUENCY:
312  return (canParse<double>(value) && (parse<double>(value) >= 0));
313  case SUMO_ATTR_LENGTH:
314  return (canParse<double>(value) && (parse<double>(value) >= 0));
315  case SUMO_ATTR_NAME:
317  case SUMO_ATTR_FILE:
319  case SUMO_ATTR_VTYPES:
320  if (value.empty()) {
321  return true;
322  } else {
324  }
326  return (canParse<double>(value) && (parse<double>(value) >= 0));
328  return (canParse<double>(value) && (parse<double>(value) >= 0));
330  return (canParse<double>(value) && (parse<double>(value) >= 0));
332  return canParse<bool>(value);
334  return canParse<bool>(value);
335  case GNE_ATTR_SELECTED:
336  return canParse<bool>(value);
337  case GNE_ATTR_GENERIC:
338  return isGenericParametersValid(value);
339  default:
340  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
341  }
342 }
343 
344 // ===========================================================================
345 // private
346 // ===========================================================================
347 
348 void
349 GNEDetectorE2::setAttribute(SumoXMLAttr key, const std::string& value) {
350  switch (key) {
351  case SUMO_ATTR_ID:
352  changeAdditionalID(value);
353  break;
354  case SUMO_ATTR_LANE:
355  myLane = changeLane(myLane, value);
356  break;
357  case SUMO_ATTR_POSITION:
358  myPositionOverLane = parse<double>(value);
359  break;
360  case SUMO_ATTR_FREQUENCY:
361  myFreq = parse<double>(value);
362  break;
363  case SUMO_ATTR_LENGTH:
364  myLength = parse<double>(value);
365  break;
366  case SUMO_ATTR_NAME:
367  myAdditionalName = value;
368  break;
369  case SUMO_ATTR_FILE:
370  myFilename = value;
371  break;
372  case SUMO_ATTR_VTYPES:
373  myVehicleTypes = value;
374  break;
376  myTimeThreshold = parse<double>(value);
377  break;
379  mySpeedThreshold = parse<double>(value);
380  break;
382  myJamThreshold = parse<double>(value);
383  break;
385  myFriendlyPosition = parse<bool>(value);;
386  break;
388  myBlockMovement = parse<bool>(value);
389  break;
390  case GNE_ATTR_SELECTED:
391  if (parse<bool>(value)) {
393  } else {
395  }
396  break;
397  case GNE_ATTR_GENERIC:
399  break;
400  default:
401  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
402  }
403  // After setting attribute always update Geometry
404  updateGeometry(true);
405 }
406 
407 /****************************************************************************/
const GNEAttributeCarrier * getACUnderCursor() const
get AttributeCarrier under cursor
double scale
information about a lane&#39;s width (temporary, used for a single view)
void setBlockIconRotation(GNELane *additionalLane=nullptr)
static bool isValidAttribute(const std::string &value)
whether the given string is a valid attribute for a certain key (for example, a name) ...
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:178
static bool isValidListOfTypeID(const std::string &value)
whether the given string is a valid list of ids for an edge or vehicle type (empty aren&#39;t allowed) ...
GUIVisualizationTextSettings addName
a E2 detector
void setGenericParametersStr(const std::string &value)
set generic parameters in string format
bool isDetectorPositionFixed() const
check if Position of detector is fixed
const std::string & getAdditionalID() const
returns Additional ID
Stores the information about how to visualize structures.
double y() const
Returns the y-position.
Definition: Position.h:61
double x() const
Returns the x-position.
Definition: Position.h:56
double myFreq
The aggregation period the values the detector collects shall be summed up.
Definition: GNEDetector.h:153
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:611
void unselectAttributeCarrier(bool changeFlag=true)
unselect attribute carrier using GUIGlobalSelection
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:47
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.
static const RGBColor BLACK
Definition: RGBColor.h:180
std::vector< double > myShapeRotations
bool isAttributeCarrierSelected() const
check if attribute carrier is selected
void changeAdditionalID(const std::string &newID)
change ID of additional
generic attribute
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.
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
static const RGBColor selectedAdditionalColor
color of selected additionals
Definition: GNENet.h:105
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:49
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:573
GNEEdge & getParentEdge()
Returns underlying parent edge.
Definition: GNELane.cpp:1298
PositionVector myShape
The shape of the additional element.
void updateGeometry(bool updateGrid)
update pre-computed geometry information
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
std::string myAdditionalName
name of additional
static bool isGenericParametersValid(const std::string &value)
check if given string can be parsed to a map/list of generic parameters
void removeGLObjectFromGrid(GUIGlObject *o)
add GL Object into net
Definition: GNENet.cpp:1175
friend class GNEChange_Attribute
declare friend class
void selectAttributeCarrier(bool changeFlag=true)
block movement of a graphic element
std::string myVehicleTypes
attribute vehicle types
Definition: GNEDetector.h:159
double getLengthGeometryFactor() const
get lenght geometry factor
Definition: GNELane.cpp:1355
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
const std::string getID() const
function to support debugging
double myLength
E2 detector lenght.
PositionVector getSubpart(double beginOffset, double endOffset) const
get subpart of a position vector
GNEDetectorE2(const std::string &id, GNELane *lane, GNEViewNet *viewNet, double pos, double length, double freq, const std::string &filename, const std::string &vehicleTypes, const std::string &name, const double timeThreshold, double speedThreshold, double jamThreshold, bool friendlyPos, bool blockMovement)
Constructor.
bool myFriendlyPosition
Flag for friendly position.
Definition: GNEDetector.h:162
double getFinalLength() const
get length that will be assigned to the lanes in the final network
Definition: NBEdge.cpp:3543
Position myBlockIconOffset
The offSet of the block icon.
std::string myFilename
The path to the output file.
Definition: GNEDetector.h:156
bool myBlockMovement
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
std::string getAttribute(SumoXMLAttr key) const
double getLength() const
get length of E2 Detector
const PositionVector & getShape() const
returns the shape of the lane
Definition: GNELane.cpp:679
#define M_PI
Definition: odrSpiral.cpp:40
static void drawShapeDottedContour(const int type, const PositionVector &shape, const double width)
draw a dotted contour around the given Non closed shape with certain width
Definition: GLHelper.cpp:471
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
element is selected
GNELane * myLane
The lane in which this detector is placed.
Definition: GNEDetector.h:147
std::string getGenericParametersStr() const
return generic parameters in string format
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...
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:233
bool isValidAdditionalID(const std::string &newID) const
check if a new additional ID is valid
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:96
NBEdge * getNBEdge()
returns the internal NBEdge
Definition: GNEEdge.cpp:586
bool drawForSelecting
whether drawing is performed for the purpose of selecting objects
void addGLObjectIntoGrid(GUIGlObject *o)
add GL Object into net
Definition: GNENet.cpp:1168
static bool isValidFilename(const std::string &value)
whether the given string is a valid attribute for a filename (for example, a name) ...
GNELane * retrieveLane(const std::string &id, bool failHard=true, bool checkVolatileChange=false)
get lane by id
Definition: GNENet.cpp:1074
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
double myBlockIconRotation
The rotation of the block icon.
GNELane * changeLane(GNELane *oldLane, const std::string &newLaneID)
change lane of additional
double myPositionOverLane
position of detector over Lane
Definition: GNEDetector.h:150
Position myBlockIconPosition
position of the block icon
SumoXMLTag getTag() const
get XML Tag assigned to this object