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-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>
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 #ifdef CHECK_MEMORY_LEAKS
58 #include <foreign/nvwa/debug_new.h>
59 #endif
60 
61 // ===========================================================================
62 // member method definitions
63 // ===========================================================================
64 
65 GNEDetectorE2::GNEDetectorE2(const std::string& id, GNELane* lane, GNEViewNet* viewNet, SUMOReal pos, SUMOReal length, SUMOReal freq, const std::string& filename,
66  bool cont, SUMOTime timeThreshold, SUMOReal speedThreshold, SUMOReal jamThreshold, bool blocked) :
67  GNEDetector(id, viewNet, SUMO_TAG_E2DETECTOR, lane, pos, freq, filename, blocked),
68  myLength(length),
69  myCont(cont),
70  myTimeThreshold(timeThreshold),
71  mySpeedThreshold(speedThreshold),
72  myJamThreshold(jamThreshold) {
73  // Update geometry;
75  // Set Colors
76  myBaseColor = RGBColor(0, 204, 204, 255);
77  myBaseColorSelected = RGBColor(125, 204, 204, 255);
78 }
79 
80 
82 }
83 
84 
85 void
87  // Clear all containers
88  myShapeRotations.clear();
89  myShapeLengths.clear();
90 
91  // Get shape of lane parent
92  myShape = myLane->getShape();
93 
94  // Cut shape using as delimitators myPos and their length (myPos + length)
96 
97  // Get number of parts of the shape
98  int numberOfSegments = (int) myShape.size() - 1;
99 
100  // If number of segments is more than 0
101  if (numberOfSegments >= 0) {
102 
103  // Reserve memory (To improve efficiency)
104  myShapeRotations.reserve(numberOfSegments);
105  myShapeLengths.reserve(numberOfSegments);
106 
107  // For every part of the shape
108  for (int i = 0; i < numberOfSegments; ++i) {
109 
110  // Obtain first position
111  const Position& f = myShape[i];
112 
113  // Obtain next position
114  const Position& s = myShape[i + 1];
115 
116  // Save distance between position into myShapeLengths
117  myShapeLengths.push_back(f.distanceTo(s));
118 
119  // Save rotation (angle) of the vector constructed by points f and s
120  myShapeRotations.push_back((SUMOReal) atan2((s.x() - f.x()), (f.y() - s.y())) * (SUMOReal) 180.0 / (SUMOReal) PI);
121  }
122  }
123 
124  // Set offset of logo
125  myDetectorLogoOffset = Position(0.5, 0);
126 
127  // Set block icon position
129 
130  // Set offset of the block icon
131  myBlockIconOffset = Position(-0.5, 0);
132 
133  // Set block icon rotation, and using their rotation for draw logo
135 }
136 
137 
138 Position
141 }
142 
143 
144 void
145 GNEDetectorE2::writeAdditional(OutputDevice& device, const std::string&) {
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  // Close tag
161  device.closeTag();
162 }
163 
164 
165 void
167  // Start drawing adding an gl identificator
168  glPushName(getGlID());
169 
170  // Add a draw matrix
171  glPushMatrix();
172 
173  // Start with the drawing of the area traslating matrix to origing
174  glTranslated(0, 0, getType());
175 
176  // Set color of the base
177  if (isAdditionalSelected()) {
179  } else {
181  }
182 
183  // Obtain exaggeration of the draw
184  const SUMOReal exaggeration = s.addSize.getExaggeration(s);
185 
186  // Draw the area using shape, shapeRotations, shapeLenghts and value of exaggeration
188 
189  // Pop last matrix
190  glPopMatrix();
191 
192  // Check if the distance is enought to draw details
193  if (s.scale * exaggeration >= 10) {
194  // Draw icon
196 
197  // Show Lock icon depending of the Edit mode
198  drawLockIcon();
199  }
200 
201  // Draw name
202  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
203 
204  // Pop name
205  glPopName();
206 }
207 
208 
209 std::string
211  switch (key) {
212  case SUMO_ATTR_ID:
213  return getAdditionalID();
214  case SUMO_ATTR_LANE:
216  case SUMO_ATTR_POSITION:
217  return toString(myPosition.x());
218  case SUMO_ATTR_FREQUENCY:
219  return toString(myFreq);
220  case SUMO_ATTR_LENGTH:
221  return toString(myLength);
222  case SUMO_ATTR_FILE:
223  return myFilename;
224  case SUMO_ATTR_CONT:
225  return toString(myCont);
229  return toString(mySpeedThreshold);
231  return toString(myJamThreshold);
232  default:
233  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
234  }
235 }
236 
237 
238 void
239 GNEDetectorE2::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
240  if (value == getAttribute(key)) {
241  return; //avoid needless changes, later logic relies on the fact that attributes have changed
242  }
243  switch (key) {
244  case SUMO_ATTR_ID:
245  case SUMO_ATTR_LANE:
246  case SUMO_ATTR_POSITION:
247  case SUMO_ATTR_FREQUENCY:
248  case SUMO_ATTR_LENGTH:
249  case SUMO_ATTR_FILE:
250  case SUMO_ATTR_CONT:
254  undoList->p_add(new GNEChange_Attribute(this, key, value));
255  updateGeometry();
256  break;
257  default:
258  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
259  }
260 }
261 
262 
263 bool
264 GNEDetectorE2::isValid(SumoXMLAttr key, const std::string& value) {
265  switch (key) {
266  case SUMO_ATTR_ID:
267  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
268  return true;
269  } else {
270  return false;
271  }
272  case SUMO_ATTR_LANE:
273  if (myViewNet->getNet()->retrieveLane(value, false) != NULL) {
274  return true;
275  } else {
276  return false;
277  }
278  case SUMO_ATTR_POSITION:
279  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0 && parse<SUMOReal>(value) <= (myLane->getLaneParametricLenght()));
280  case SUMO_ATTR_FREQUENCY:
281  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0);
282  case SUMO_ATTR_LENGTH:
283  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0);
284  case SUMO_ATTR_FILE:
285  return isValidFileValue(value);
286  case SUMO_ATTR_CONT:
287  return canParse<bool>(value);
289  return canParse<int>(value);
291  return canParse<SUMOReal>(value);
293  return canParse<SUMOReal>(value);
294  default:
295  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
296  }
297 }
298 
299 // ===========================================================================
300 // private
301 // ===========================================================================
302 
303 void
304 GNEDetectorE2::setAttribute(SumoXMLAttr key, const std::string& value) {
305  switch (key) {
306  case SUMO_ATTR_ID:
307  setAdditionalID(value);
308  break;
309  case SUMO_ATTR_LANE:
311  break;
312  case SUMO_ATTR_POSITION:
313  myPosition = Position(parse<SUMOReal>(value), 0);
314  updateGeometry();
315  getViewNet()->update();
316  break;
317  case SUMO_ATTR_FREQUENCY:
318  myFreq = parse<SUMOReal>(value);
319  break;
320  case SUMO_ATTR_LENGTH:
321  myLength = parse<SUMOReal>(value);
322  updateGeometry();
323  getViewNet()->update();
324  break;
325  case SUMO_ATTR_FILE:
326  myFilename = value;
327  break;
328  case SUMO_ATTR_CONT:
329  myCont = parse<bool>(value);
330  break;
332  myTimeThreshold = string2time(value);
333  break;
335  mySpeedThreshold = parse<SUMOReal>(value);
336  break;
338  myJamThreshold = parse<SUMOReal>(value);
339  break;
340  default:
341  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
342  }
343 }
344 
345 /****************************************************************************/
SUMOReal getExaggeration(const GUIVisualizationSettings &s, SUMOReal factor=20) const
return the drawing size including exaggeration and constantSize values
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:257
long long int SUMOTime
Definition: SUMOTime.h:43
void changeLane(GNELane *newLane)
change lane of detector
GUIVisualizationTextSettings addName
Stores the information about how to visualize structures.
std::string getAttribute(SumoXMLAttr key) const
GNEDetectorE2(const std::string &id, GNELane *lane, GNEViewNet *viewNet, SUMOReal pos, SUMOReal length, SUMOReal freq, const std::string &filename, bool cont, SUMOTime timeThreshold, SUMOReal speedThreshold, SUMOReal jamThreshold, bool blocked)
Constructor.
bool isAdditionalSelected() const
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
static void drawBoxLines(const PositionVector &geom, const std::vector< SUMOReal > &rots, const std::vector< SUMOReal > &lengths, SUMOReal width, int cornerDetail=0, SUMOReal offset=0)
Draws thick lines.
Definition: GLHelper.cpp:176
bool myCont
attribute to enable or disable splitByType
SUMOReal myLength
attribute lenght
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)
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
SUMOReal distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:221
void updateGeometry()
update pre-computed geometry information
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
GUIGlID getGlID() const
Returns the numerical id of the object.
SUMOReal scale
information about a lane&#39;s width (temporary, used for a single view)
~GNEDetectorE2()
Destructor.
SUMOTime myTimeThreshold
The time-based threshold that describes how much time has to pass until a vehicle is recognized as ha...
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
SUMOReal mySpeedThreshold
The speed-based threshold that describes how slow a vehicle has to be to be recognized as halting...
GUIVisualizationSizeSettings addSize
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
#define PI
Definition: polyfonts.c:61
static bool isValidFileValue(const std::string &value)
true if value is a valid file value
GNEViewNet * getViewNet() const
Returns a pointer to GNEViewNet in which additional element is located.
std::vector< SUMOReal > myShapeLengths
The lengths of the shape parts.
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:443
const std::string & getAdditionalID() const
returns the ID of additional
void drawDetectorIcon(const int GNELogoID, SUMOReal sizex=0.5, SUMOReal sizey=0.5) const
SUMOReal getLaneParametricLenght() const
returns the parameteric length of the lane
Definition: GNELane.cpp:518
SUMOReal getPositionRelativeToParametricLenght(SUMOReal position) const
Definition: GNELane.cpp:530
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:658
void setBlockIconRotation(GNELane *lane=NULL)
Position myDetectorLogoOffset
The position of detector.
Definition: GNEDetector.h:173
Position getPositionInView() const
Returns position of detector E2 in view.
friend class GNEChange_Attribute
declare friend class
Position positionAtOffset(SUMOReal pos, SUMOReal lateralOffset=0) const
Returns the position at the given length.
void drawLockIcon(SUMOReal size=0.5) const
draw lock icon
SUMOReal myJamThreshold
The minimum distance to the next standing vehicle in order to make this vehicle count as a participan...
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
void setAdditionalID(const std::string &id)
set the ID of additional
const std::string getID() const
function to support debugging
void drawName(const Position &pos, const SUMOReal scale, const GUIVisualizationTextSettings &settings, const SUMOReal angle=0) const
draw name of item
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
Position myBlockIconOffset
The offSet of the block icon.
std::string myFilename
The path to the output file.
Definition: GNEDetector.h:165
std::string getAttribute(SumoXMLAttr key) const
Definition: GNELane.cpp:601
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1093
const PositionVector & getShape() const
returns the shape of the lane
Definition: GNELane.cpp:456
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
bool isValid(SumoXMLAttr key, const std::string &value)
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
RGBColor myBaseColor
base color (Default green)
GNELane * myLane
The lane this detector belongs.
Definition: GNEDetector.h:159
std::vector< SUMOReal > myShapeRotations
GNENet * getNet() const
get the net object
Definition: GNEViewNet.cpp:845
Position getLineCenter() const
get line center
SumoXMLTag getTag() const
get Tag assigned to this object
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
static GUIGlID getGif(GUITexture which)
returns a texture Gif previously defined in the enum GUITexture
Information whether the detector shall be continued on the folowing lanes.
PositionVector getSubpart(SUMOReal beginOffset, SUMOReal endOffset) const
get subpart of a position vector
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
int myFreq
The aggregation period the values the detector collects shall be summed up.
Definition: GNEDetector.h:162
Position myBlockIconPosition
position of the block icon
void writeAdditional(OutputDevice &device, const std::string &)
writte additional element into a xml file