SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  // Refresh element (neccesary to avoid grabbing problems)
138 }
139 
140 
141 Position
144 }
145 
146 
147 void
148 GNEDetectorE2::writeAdditional(OutputDevice& device, const std::string&) {
149  // Write parameters
150  device.openTag(getTag());
151  device.writeAttr(SUMO_ATTR_ID, getID());
152  device.writeAttr(SUMO_ATTR_LANE, myLane->getID());
156  if (!myFilename.empty()) {
158  }
163  if (myBlocked) {
165  }
166  // Close tag
167  device.closeTag();
168 }
169 
170 
171 void
173  // Start drawing adding an gl identificator
174  glPushName(getGlID());
175 
176  // Add a draw matrix
177  glPushMatrix();
178 
179  // Start with the drawing of the area traslating matrix to origing
180  glTranslated(0, 0, getType());
181 
182  // Set color of the base
183  if (isAdditionalSelected()) {
185  } else {
187  }
188 
189  // Obtain exaggeration of the draw
190  const SUMOReal exaggeration = s.addSize.getExaggeration(s);
191 
192  // Draw the area using shape, shapeRotations, shapeLenghts and value of exaggeration
194 
195  // Pop last matrix
196  glPopMatrix();
197 
198  // Check if the distance is enought to draw details
199  if (s.scale * exaggeration >= 10) {
200  // Draw icon
202 
203  // Show Lock icon depending of the Edit mode
204  drawLockIcon();
205  }
206 
207  // Draw name
208  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
209 
210  // Pop name
211  glPopName();
212 }
213 
214 
215 std::string
217  switch (key) {
218  case SUMO_ATTR_ID:
219  return getAdditionalID();
220  case SUMO_ATTR_LANE:
222  case SUMO_ATTR_POSITION:
223  return toString(myPosition.x());
224  case SUMO_ATTR_FREQUENCY:
225  return toString(myFreq);
226  case SUMO_ATTR_LENGTH:
227  return toString(myLength);
228  case SUMO_ATTR_FILE:
229  return myFilename;
230  case SUMO_ATTR_CONT:
231  return toString(myCont);
235  return toString(mySpeedThreshold);
237  return toString(myJamThreshold);
239  return toString(myBlocked);
240  default:
241  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
242  }
243 }
244 
245 
246 void
247 GNEDetectorE2::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
248  if (value == getAttribute(key)) {
249  return; //avoid needless changes, later logic relies on the fact that attributes have changed
250  }
251  switch (key) {
252  case SUMO_ATTR_ID:
253  case SUMO_ATTR_LANE:
254  case SUMO_ATTR_POSITION:
255  case SUMO_ATTR_FREQUENCY:
256  case SUMO_ATTR_LENGTH:
257  case SUMO_ATTR_FILE:
258  case SUMO_ATTR_CONT:
263  undoList->p_add(new GNEChange_Attribute(this, key, value));
264  updateGeometry();
265  break;
266  default:
267  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
268  }
269 }
270 
271 
272 bool
273 GNEDetectorE2::isValid(SumoXMLAttr key, const std::string& value) {
274  switch (key) {
275  case SUMO_ATTR_ID:
276  if (myViewNet->getNet()->getAdditional(getTag(), value) == NULL) {
277  return true;
278  } else {
279  return false;
280  }
281  case SUMO_ATTR_LANE:
282  if (myViewNet->getNet()->retrieveLane(value, false) != NULL) {
283  return true;
284  } else {
285  return false;
286  }
287  case SUMO_ATTR_POSITION:
288  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0 && parse<SUMOReal>(value) <= (myLane->getLaneParametricLenght()));
289  case SUMO_ATTR_FREQUENCY:
290  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0);
291  case SUMO_ATTR_LENGTH:
292  return (canParse<SUMOReal>(value) && parse<SUMOReal>(value) >= 0);
293  case SUMO_ATTR_FILE:
294  return isValidFileValue(value);
295  case SUMO_ATTR_CONT:
296  return canParse<bool>(value);
298  return canParse<int>(value);
300  return canParse<SUMOReal>(value);
302  return canParse<SUMOReal>(value);
304  return canParse<bool>(value);
305  default:
306  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
307  }
308 }
309 
310 // ===========================================================================
311 // private
312 // ===========================================================================
313 
314 void
315 GNEDetectorE2::setAttribute(SumoXMLAttr key, const std::string& value) {
316  switch (key) {
317  case SUMO_ATTR_ID:
318  setAdditionalID(value);
319  break;
320  case SUMO_ATTR_LANE:
321  changeLane(value);
322  break;
323  case SUMO_ATTR_POSITION:
324  myPosition = Position(parse<SUMOReal>(value), 0);
325  updateGeometry();
326  getViewNet()->update();
327  break;
328  case SUMO_ATTR_FREQUENCY:
329  myFreq = parse<SUMOReal>(value);
330  break;
331  case SUMO_ATTR_LENGTH:
332  myLength = parse<SUMOReal>(value);
333  updateGeometry();
334  getViewNet()->update();
335  break;
336  case SUMO_ATTR_FILE:
337  myFilename = value;
338  break;
339  case SUMO_ATTR_CONT:
340  myCont = parse<bool>(value);
341  break;
343  myTimeThreshold = string2time(value);
344  break;
346  mySpeedThreshold = parse<SUMOReal>(value);
347  break;
349  myJamThreshold = parse<SUMOReal>(value);
350  break;
352  myBlocked = parse<bool>(value);
353  getViewNet()->update();
354  break;
355  default:
356  throw InvalidArgument(toString(getType()) + " attribute '" + toString(key) + "' not allowed");
357  }
358 }
359 
360 /****************************************************************************/
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
GUIVisualizationTextSettings addName
GNELane * myLane
The lane this additional belongs NULL if additional doesnt' belongs to a lane.
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:55
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.
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'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:681
void refreshAdditional(GNEAdditional *additional)
refreshes boundary information of an additional after a geometry update
Definition: GNENet.cpp:784
SUMOReal getPositionRelativeToParametricLenght(SUMOReal position) const
Definition: GNELane.cpp:693
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:744
void setBlockIconRotation(GNELane *lane=NULL)
Position myDetectorLogoOffset
The position of detector.
Definition: GNEDetector.h:159
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
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:151
std::string getAttribute(SumoXMLAttr key) const
Definition: GNELane.cpp:788
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1250
const PositionVector & getShape() const
returns the shape of the lane
Definition: GNELane.cpp:583
bool myBlocked
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
RGBColor myBaseColor
base color (Default green)
std::vector< SUMOReal > myShapeRotations
GNENet * getNet() const
get the net object
Definition: GNEViewNet.cpp:912
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:214
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:148
Position myBlockIconPosition
position of the block icon
void writeAdditional(OutputDevice &device, const std::string &)
writte additional element into a xml file