SUMO - Simulation of Urban MObility
GNEDetectorE1.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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 //
18 /****************************************************************************/
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #ifdef _MSC_VER
24 #include <windows_config.h>
25 #else
26 #include <config.h>
27 #endif
28 
29 #include <string>
30 #include <iostream>
31 #include <utility>
35 #include <utils/common/ToString.h>
36 #include <utils/geom/GeomHelper.h>
42 #include <utils/gui/div/GLHelper.h>
46 
47 #include "GNEDetectorE1.h"
48 #include "GNELane.h"
49 #include "GNEViewNet.h"
50 #include "GNEUndoList.h"
51 #include "GNENet.h"
52 #include "GNEChange_Attribute.h"
53 #include "GNEEdge.h"
54 
55 
56 // ===========================================================================
57 // member method definitions
58 // ===========================================================================
59 
60 GNEDetectorE1::GNEDetectorE1(const std::string& id, GNELane* lane, GNEViewNet* viewNet, double pos, double freq, const std::string& filename, const std::string& vehicleTypes, bool friendlyPos) :
61  GNEDetector(id, viewNet, SUMO_TAG_E1DETECTOR, ICON_E1, lane, pos, freq, filename, friendlyPos, NULL),
62  myVehicleTypes(vehicleTypes) {
63 }
64 
65 
67 }
68 
69 
70 void
72  // Clear all containers
73  myShapeRotations.clear();
74  myShapeLengths.clear();
75 
76  // clear Shape
77  myShape.clear();
78 
79  // obtain position over lane
80  double fixedPositionOverLane = myPositionOverLane > 1 ? 1 : myPositionOverLane < 0 ? 0 : myPositionOverLane;
81  myShape.push_back(myLane->getShape().positionAtOffset(fixedPositionOverLane * myLane->getShape().length()));
82 
83  // Obtain first position
84  Position f = myShape[0] - Position(1, 0);
85 
86  // Obtain next position
87  Position s = myShape[0] + Position(1, 0);
88 
89  // Save rotation (angle) of the vector constructed by points f and s
90  myShapeRotations.push_back(myLane->getShape().rotationDegreeAtOffset(fixedPositionOverLane * myLane->getShape().length()) * -1);
91 
92  // Set block icon position
94 
95  // Set offset of the block icon
96  myBlockIconOffset = Position(-1, 0);
97 
98  // Set block icon rotation, and using their rotation for logo
100 
101  // Refresh element (neccesary to avoid grabbing problems)
102  myViewNet->getNet()->refreshElement(this);
103 }
104 
105 
106 void
108  // Write parameters
109  device.openTag(getTag());
110  device.writeAttr(SUMO_ATTR_ID, getID());
111  device.writeAttr(SUMO_ATTR_LANE, myLane->getID());
114  if (!myFilename.empty()) {
116  }
117  if (!myVehicleTypes.empty()) {
119  }
121  // Close tag
122  device.closeTag();
123 }
124 
125 
127  // with friendly position enabled position are "always fixed"
128  if (myFriendlyPosition) {
129  return true;
130  } else {
131  // floors are needed to avoid precision problems
132  return ((floor(myPositionOverLane * 1000) / 1000) >= 0) && ((floor(myPositionOverLane * 1000) / 1000) <= 1);
133  }
134 }
135 
136 
137 void
139  // get values
140  glPushName(getGlID());
141  double width = (double) 2.0 * s.scale;
142  glLineWidth(1.0);
143  const double exaggeration = s.addSize.getExaggeration(s);
144 
145  // set color
146  if (isAdditionalSelected()) {
148  } else {
149  GLHelper::setColor(RGBColor(255, 255, 0));
150  }
151  // draw shape
152  glPushMatrix();
153  glTranslated(0, 0, getType());
154  glTranslated(myShape[0].x(), myShape[0].y(), 0);
155  glRotated(myShapeRotations[0], 0, 0, 1);
156  glScaled(exaggeration, exaggeration, 1);
157  glBegin(GL_QUADS);
158  glVertex2d(-1.0, 2);
159  glVertex2d(-1.0, -2);
160  glVertex2d(1.0, -2);
161  glVertex2d(1.0, 2);
162  glEnd();
163  glTranslated(0, 0, .01);
164  glBegin(GL_LINES);
165  glVertex2d(0, 2 - .1);
166  glVertex2d(0, -2 + .1);
167  glEnd();
168 
169  // outline
170  if (width * exaggeration > 1) {
171  // set color
172  if (isAdditionalSelected()) {
174  } else {
176  }
177  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
178  glBegin(GL_QUADS);
179  glVertex2f(-1.0, 2);
180  glVertex2f(-1.0, -2);
181  glVertex2f(1.0, -2);
182  glVertex2f(1.0, 2);
183  glEnd();
184  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
185  }
186 
187  // position indicator
188  if (width * exaggeration > 1) {
189  // set color
190  if (isAdditionalSelected()) {
192  } else {
194  }
195  glRotated(90, 0, 0, -1);
196  glBegin(GL_LINES);
197  glVertex2d(0, 1.7);
198  glVertex2d(0, -1.7);
199  glEnd();
200  }
201 
202  // Pop shape matrix
203  glPopMatrix();
204 
205  // Check if the distance is enought to draw details
206  if (s.scale * exaggeration >= 10) {
207  // Push matrix
208  glPushMatrix();
209  // Traslate to center of detector
210  glTranslated(myShape.getLineCenter().x(), myShape.getLineCenter().y(), getType() + 0.1);
211  // Rotate depending of myBlockIconRotation
212  glRotated(myBlockIconRotation, 0, 0, -1);
213  //move to logo position
214  glTranslated(-1, 0, 0);
215  // draw E1 logo
216  if (isAdditionalSelected()) {
218  } else {
219  GLHelper::drawText("E1", Position(), .1, 1.5, RGBColor::BLACK);
220  }
221  // pop matrix
222  glPopMatrix();
223  // Show Lock icon depending of the Edit mode
224  drawLockIcon();
225  }
226 
227  // Finish draw
228  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
229  glPopName();
230 }
231 
232 
233 std::string
235  switch (key) {
236  case SUMO_ATTR_ID:
237  return getAdditionalID();
238  case SUMO_ATTR_LANE:
239  return myLane->getID();
240  case SUMO_ATTR_POSITION:
242  case SUMO_ATTR_FREQUENCY:
243  return toString(myFreq);
244  case SUMO_ATTR_FILE:
245  return myFilename;
246  case SUMO_ATTR_VTYPES:
247  return myVehicleTypes;
251  return toString(myBlocked);
252  default:
253  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
254  }
255 }
256 
257 
258 void
259 GNEDetectorE1::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
260  if (value == getAttribute(key)) {
261  return; //avoid needless changes, later logic relies on the fact that attributes have changed
262  }
263  switch (key) {
264  case SUMO_ATTR_ID:
265  case SUMO_ATTR_LANE:
266  case SUMO_ATTR_POSITION:
267  case SUMO_ATTR_FREQUENCY:
268  case SUMO_ATTR_FILE:
269  case SUMO_ATTR_VTYPES:
272  undoList->p_add(new GNEChange_Attribute(this, key, value));
273  break;
274  default:
275  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
276  }
277 
278 }
279 
280 
281 bool
282 GNEDetectorE1::isValid(SumoXMLAttr key, const std::string& value) {
283  switch (key) {
284  case SUMO_ATTR_ID:
285  return isValidAdditionalID(value);
286  case SUMO_ATTR_LANE:
287  if (myViewNet->getNet()->retrieveLane(value, false) != NULL) {
288  return true;
289  } else {
290  return false;
291  }
292  case SUMO_ATTR_POSITION:
293  return canParse<double>(value);
294  case SUMO_ATTR_FREQUENCY:
295  return (canParse<double>(value) && (parse<double>(value) >= 0));
296  case SUMO_ATTR_FILE:
297  return isValidFilename(value);
298  case SUMO_ATTR_VTYPES:
299  return true;
301  return canParse<bool>(value);
303  return canParse<bool>(value);
304  default:
305  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
306  }
307 }
308 
309 // ===========================================================================
310 // private
311 // ===========================================================================
312 
313 void
314 GNEDetectorE1::setAttribute(SumoXMLAttr key, const std::string& value) {
315  switch (key) {
316  case SUMO_ATTR_ID:
317  changeAdditionalID(value);
318  break;
319  case SUMO_ATTR_LANE:
320  myLane = changeLane(myLane, value);
321  break;
322  case SUMO_ATTR_POSITION:
323  myPositionOverLane = parse<double>(value) / myLane->getLaneParametricLength();
324  break;
325  case SUMO_ATTR_FREQUENCY:
326  myFreq = parse<double>(value);
327  break;
328  case SUMO_ATTR_FILE:
329  myFilename = value;
330  break;
331  case SUMO_ATTR_VTYPES:
332  myVehicleTypes = value;
333  break;
335  myFriendlyPosition = parse<bool>(value);
336  break;
338  myBlocked = parse<bool>(value);
339  break;
340  default:
341  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
342  }
343 }
344 
345 /****************************************************************************/
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:260
double rotationDegreeAtOffset(double pos) const
Returns the rotation at the given length.
double scale
information about a lane&#39;s width (temporary, used for a single view)
GUIVisualizationTextSettings addName
void refreshElement(GUIGlObject *o)
refreshes boundary information for o and update
Definition: GNENet.cpp:1082
const std::string & getAdditionalID() const
returns Additional ID
static const RGBColor WHITE
Definition: RGBColor.h:185
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.
double y() const
Returns the y-position.
Definition: Position.h:67
double x() const
Returns the x-position.
Definition: Position.h:62
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes ...
void writeAdditional(OutputDevice &device) const
writte additional element into a xml file
double myFreq
The aggregation period the values the detector collects shall be summed up.
Definition: GNEDetector.h:141
~GNEDetectorE1()
Destructor.
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:487
std::string myVehicleTypes
attribute vehicle types
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:53
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static const RGBColor BLACK
Definition: RGBColor.h:186
static bool isValidFilename(const std::string &value)
true if value is a valid file value
std::vector< double > myShapeRotations
void changeAdditionalID(const std::string &newID)
change ID of additional
Position getLineCenter() const
get line center
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.
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:116
double getLaneParametricLength() const
returns the parameteric length of the lane
Definition: GNELane.cpp:701
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:55
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:449
PositionVector myShape
The shape of the additional element.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
friend class GNEChange_Attribute
declare friend class
block movement of a graphic element
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
bool myFriendlyPosition
Flag for friendly position.
Definition: GNEDetector.h:147
Position myBlockIconOffset
The offSet of the block icon.
std::string myFilename
The path to the output file.
Definition: GNEDetector.h:144
void updateGeometry()
update pre-computed geometry information
std::string getAttribute(SumoXMLAttr key) const
This functions has to be implemented in all GNEAttributeCarriers.
const PositionVector & getShape() const
returns the shape of the lane
Definition: GNELane.cpp:599
bool myBlocked
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
double length() const
Returns the length.
bool isDetectorPositionFixed() const
check if Position of detector is fixed
void setBlockIconRotation(GNELane *additionalLane=NULL)
GNELane * myLane
The lane in which this detector is placed.
Definition: GNEDetector.h:135
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
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
bool isValidAdditionalID(const std::string &newID) const
check if a new additional ID is valid
bool isAdditionalSelected() const
static const RGBColor selectionColor
Definition: GNENet.h:107
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
GNELane * retrieveLane(const std::string &id, bool failHard=true, bool checkVolatileChange=false)
get lane by id
Definition: GNENet.cpp:1000
double getAbsolutePositionOverLane() const
get absolute position over Lane
Definition: GNEDetector.cpp:80
double myBlockIconRotation
The rotation of the block icon.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
GNELane * changeLane(GNELane *oldLane, const std::string &newLaneID)
change lane of additional
double myPositionOverLane
position of detector over Lane
Definition: GNEDetector.h:138
Position myBlockIconPosition
position of the block icon
SumoXMLTag getTag() const
get XML Tag assigned to this object