SUMO - Simulation of Urban MObility
GNEDetectorE1.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 //
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 "GNEDetectorE1.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 GNEDetectorE1::GNEDetectorE1(const std::string& id, GNELane* lane, GNEViewNet* viewNet, double pos, double freq, const std::string& filename, const std::string& vehicleTypes, bool friendlyPos) :
64  GNEDetector(id, viewNet, SUMO_TAG_E1DETECTOR, ICON_E1, lane, pos, freq, filename, friendlyPos),
65  myVehicleTypes(vehicleTypes) {
66  // Update geometry;
68 }
69 
70 
72 }
73 
74 
75 void
77  // Clear all containers
78  myShapeRotations.clear();
79  myShapeLengths.clear();
80 
81  // clear Shape
82  myShape.clear();
83 
84  // obtain position over lane
85  double fixedPositionOverLane = myPositionOverLane > 1 ? 1 : myPositionOverLane < 0 ? 0 : myPositionOverLane;
86  myShape.push_back(myLane->getShape().positionAtOffset(fixedPositionOverLane * myLane->getShape().length()));
87 
88  // Obtain first position
89  Position f = myShape[0] - Position(1, 0);
90 
91  // Obtain next position
92  Position s = myShape[0] + Position(1, 0);
93 
94  // Save rotation (angle) of the vector constructed by points f and s
95  myShapeRotations.push_back(myLane->getShape().rotationDegreeAtOffset(fixedPositionOverLane * myLane->getShape().length()) * -1);
96 
97  // Set block icon position
99 
100  // Set offset of the block icon
101  myBlockIconOffset = Position(-1, 0);
102 
103  // Set block icon rotation, and using their rotation for logo
105 
106  // Refresh element (neccesary to avoid grabbing problems)
108 }
109 
110 
111 void
112 GNEDetectorE1::writeAdditional(OutputDevice& device, bool volatileOptionsEnabled) const {
113  // Write parameters
114  device.openTag(getTag());
115  device.writeAttr(SUMO_ATTR_ID, getID());
116  // Check if another lane ID must be changed if sidewalks.guess option is enabled
117  if (volatileOptionsEnabled && OptionsCont::getOptions().getBool("sidewalks.guess") && (myLane->getParentEdge().getLanes().front()->isRestricted(SVC_PEDESTRIAN) == false)) {
118  // add a new extra lane to edge
120  // write ID (now is different because there are a new lane)
121  device.writeAttr(SUMO_ATTR_LANE, myLane->getID());
122  // undo set extra lane
123  myViewNet->getUndoList()->undo();
124  } else {
125  device.writeAttr(SUMO_ATTR_LANE, myLane->getID());
126  }
129  if (!myFilename.empty()) {
131  }
132  if (!myVehicleTypes.empty()) {
134  }
136  if (myBlocked) {
138  }
139  // Close tag
140  device.closeTag();
141 }
142 
143 
145  // with friendly position enabled position are "always fixed"
146  if (myFriendlyPosition) {
147  return true;
148  } else {
149  // floors are needed to avoid precision problems
150  return ((floor(myPositionOverLane * 1000) / 1000) >= 0) && ((floor(myPositionOverLane * 1000) / 1000) <= 1);
151  }
152 }
153 
154 
155 void
157  // get values
158  glPushName(getGlID());
159  double width = (double) 2.0 * s.scale;
160  glLineWidth(1.0);
161  const double exaggeration = s.addSize.getExaggeration(s);
162 
163  // set color
164  if (isAdditionalSelected()) {
166  } else {
167  GLHelper::setColor(RGBColor(255, 255, 0));
168  }
169  // draw shape
170  glPushMatrix();
171  glTranslated(0, 0, getType());
172  glTranslated(myShape[0].x(), myShape[0].y(), 0);
173  glRotated(myShapeRotations[0], 0, 0, 1);
174  glScaled(exaggeration, exaggeration, 1);
175  glBegin(GL_QUADS);
176  glVertex2d(-1.0, 2);
177  glVertex2d(-1.0, -2);
178  glVertex2d(1.0, -2);
179  glVertex2d(1.0, 2);
180  glEnd();
181  glTranslated(0, 0, .01);
182  glBegin(GL_LINES);
183  glVertex2d(0, 2 - .1);
184  glVertex2d(0, -2 + .1);
185  glEnd();
186 
187  // outline
188  if (width * exaggeration > 1) {
189  // set color
190  if (isAdditionalSelected()) {
192  } else {
194  }
195  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
196  glBegin(GL_QUADS);
197  glVertex2f(-1.0, 2);
198  glVertex2f(-1.0, -2);
199  glVertex2f(1.0, -2);
200  glVertex2f(1.0, 2);
201  glEnd();
202  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
203  }
204 
205  // position indicator
206  if (width * exaggeration > 1) {
207  // set color
208  if (isAdditionalSelected()) {
210  } else {
212  }
213  glRotated(90, 0, 0, -1);
214  glBegin(GL_LINES);
215  glVertex2d(0, 1.7);
216  glVertex2d(0, -1.7);
217  glEnd();
218  }
219 
220  // Pop shape matrix
221  glPopMatrix();
222 
223  // Check if the distance is enought to draw details
224  if (s.scale * exaggeration >= 10) {
225  // Push matrix
226  glPushMatrix();
227  // Traslate to center of detector
228  glTranslated(myShape.getLineCenter().x(), myShape.getLineCenter().y(), getType() + 0.1);
229  // Rotate depending of myBlockIconRotation
230  glRotated(myBlockIconRotation, 0, 0, -1);
231  //move to logo position
232  glTranslated(-1, 0, 0);
233  // draw E1 logo
234  if (isAdditionalSelected()) {
236  } else {
237  GLHelper::drawText("E1", Position(), .1, 1.5, RGBColor::BLACK);
238  }
239  // pop matrix
240  glPopMatrix();
241  // Show Lock icon depending of the Edit mode
242  drawLockIcon();
243  }
244 
245  // Finish draw
246  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
247  glPopName();
248 }
249 
250 
251 std::string
253  switch (key) {
254  case SUMO_ATTR_ID:
255  return getAdditionalID();
256  case SUMO_ATTR_LANE:
258  case SUMO_ATTR_POSITION:
260  case SUMO_ATTR_FREQUENCY:
261  return toString(myFreq);
262  case SUMO_ATTR_FILE:
263  return myFilename;
264  case SUMO_ATTR_VTYPES:
265  return myVehicleTypes;
269  return toString(myBlocked);
270  default:
271  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
272  }
273 }
274 
275 
276 void
277 GNEDetectorE1::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
278  if (value == getAttribute(key)) {
279  return; //avoid needless changes, later logic relies on the fact that attributes have changed
280  }
281  switch (key) {
282  case SUMO_ATTR_ID:
283  case SUMO_ATTR_LANE:
284  case SUMO_ATTR_POSITION:
285  case SUMO_ATTR_FREQUENCY:
286  case SUMO_ATTR_FILE:
287  case SUMO_ATTR_VTYPES:
290  undoList->p_add(new GNEChange_Attribute(this, key, value));
291  updateGeometry();
292  break;
293  default:
294  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
295  }
296 
297 }
298 
299 
300 bool
301 GNEDetectorE1::isValid(SumoXMLAttr key, const std::string& value) {
302  switch (key) {
303  case SUMO_ATTR_ID:
304  if (isValidID(value) && (myViewNet->getNet()->getAdditional(getTag(), value) == NULL)) {
305  return true;
306  } else {
307  return false;
308  }
309  case SUMO_ATTR_LANE:
310  if (myViewNet->getNet()->retrieveLane(value, false) != NULL) {
311  return true;
312  } else {
313  return false;
314  }
315  case SUMO_ATTR_POSITION:
316  return canParse<double>(value);
317  case SUMO_ATTR_FREQUENCY:
318  return (canParse<double>(value) && (parse<double>(value) >= 0));
319  case SUMO_ATTR_FILE:
320  return isValidFilename(value);
321  case SUMO_ATTR_VTYPES:
322  return true;
324  return canParse<bool>(value);
326  return canParse<bool>(value);
327  default:
328  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
329  }
330 }
331 
332 // ===========================================================================
333 // private
334 // ===========================================================================
335 
336 void
337 GNEDetectorE1::setAttribute(SumoXMLAttr key, const std::string& value) {
338  switch (key) {
339  case SUMO_ATTR_ID:
340  setAdditionalID(value);
341  break;
342  case SUMO_ATTR_LANE:
343  changeLane(value);
344  break;
345  case SUMO_ATTR_POSITION:
346  myPositionOverLane = parse<double>(value) / myLane->getLaneParametricLength();
347  updateGeometry();
348  getViewNet()->update();
349  break;
350  case SUMO_ATTR_FREQUENCY:
351  myFreq = parse<double>(value);
352  break;
353  case SUMO_ATTR_FILE:
354  myFilename = value;
355  break;
356  case SUMO_ATTR_VTYPES:
357  myVehicleTypes = value;
358  break;
360  myFriendlyPosition = parse<bool>(value);
361  getViewNet()->update();
362  break;
364  myBlocked = parse<bool>(value);
365  getViewNet()->update();
366  break;
367  default:
368  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
369  }
370 }
371 
372 /****************************************************************************/
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
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
GUIVisualizationTextSettings addName
static const RGBColor WHITE
Definition: RGBColor.h:186
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.
GNEDetectorE1(const std::string &id, GNELane *lane, GNEViewNet *viewNet, double pos, double freq, const std::string &filename, const std::string &vehicleTypes, bool friendlyPos)
Constructor.
bool isAdditionalSelected() const
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes ...
double myFreq
The aggregation period the values the detector collects shall be summed up.
Definition: GNEDetector.h:158
~GNEDetectorE1()
Destructor.
std::string myVehicleTypes
attribute vehicle types
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:54
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
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
GNEViewNet * myViewNet
The GNEViewNet this additional element belongs.
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
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
double rotationDegreeAtOffset(double pos) const
Returns the rotation at the given length.
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
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.
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
void updateGeometry()
update pre-computed geometry information
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
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
bool isDetectorPositionFixed() const
check if Position of detector is fixed
GNENet * getNet() const
get the net object
Position getLineCenter() const
get line center
SumoXMLTag getTag() const
get XML Tag assigned to this object
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
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 myBlockIconRotation
The rotation of the block icon.
void writeAdditional(OutputDevice &device, bool volatileOptionsEnabled) const
writte additional element into a xml file
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