SUMO - Simulation of Urban MObility
GNEDetectorEntry.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 "GNEDetectorEntry.h"
48 #include "GNEDetectorE3.h"
49 #include "GNELane.h"
50 #include "GNEViewNet.h"
51 #include "GNEUndoList.h"
52 #include "GNENet.h"
53 #include "GNEChange_Attribute.h"
54 
55 
56 // ===========================================================================
57 // member method definitions
58 // ===========================================================================
59 
60 GNEDetectorEntry::GNEDetectorEntry(GNEViewNet* viewNet, GNEDetectorE3* parent, GNELane* lane, double pos, bool friendlyPos) :
61  GNEDetector(parent->generateEntryID(), viewNet, SUMO_TAG_DET_ENTRY, ICON_E3ENTRY, lane, pos, 0, "", friendlyPos, parent) {
62 }
63 
64 
66 
67 
68 void
70  // Clear all containers
71  myShapeRotations.clear();
72  myShapeLengths.clear();
73 
74  // clear Shape
75  myShape.clear();
76 
77  // obtain position over lane
78  double fixedPositionOverLane = myPositionOverLane > 1 ? 1 : myPositionOverLane < 0 ? 0 : myPositionOverLane;
79  myShape.push_back(myLane->getShape().positionAtOffset(fixedPositionOverLane * myLane->getShape().length()));
80 
81  // Save rotation (angle) of the vector constructed by points f and s
82  myShapeRotations.push_back(myLane->getShape().rotationDegreeAtOffset(fixedPositionOverLane * myLane->getShape().length()) * -1);
83 
84  // Set block icon position
86 
87  // Set block icon rotation, and using their rotation for logo
89 
90  // Refresh element (neccesary to avoid grabbing problems)
92 
93  // update E3 parent Geometry
95 }
96 
97 
98 void
100  device.openTag(getTag());
101  device.writeAttr(SUMO_ATTR_LANE, myLane->getID());
104  device.closeTag();
105 }
106 
107 
109  // with friendly position enabled position are "always fixed"
110  if (myFriendlyPosition) {
111  return true;
112  } else {
113  // floors are needed to avoid precision problems
114  return ((floor(myPositionOverLane * 1000) / 1000) >= 0) && ((floor(myPositionOverLane * 1000) / 1000) <= 1);
115  }
116 }
117 
118 
119 void
121  // Start drawing adding gl identificator
122  glPushName(getGlID());
123 
124  // Push detector matrix
125  glPushMatrix();
126  glTranslated(0, 0, getType());
127 
128  // Set initial values
129  if (isAdditionalSelected()) {
131  } else {
133  }
134  const double exaggeration = s.addSize.getExaggeration(s);
135  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
136 
137  // Push polygon matrix
138  glPushMatrix();
139  glScaled(exaggeration, exaggeration, 1);
140  glTranslated(myShape[0].x(), myShape[0].y(), 0);
141  glRotated(myShapeRotations[0], 0, 0, 1);
142 
143  // Draw polygon
144  glBegin(GL_LINES);
145  glVertex2d(1.7, 0);
146  glVertex2d(-1.7, 0);
147  glEnd();
148  glBegin(GL_QUADS);
149  glVertex2d(-1.7, .5);
150  glVertex2d(-1.7, -.5);
151  glVertex2d(1.7, -.5);
152  glVertex2d(1.7, .5);
153  glEnd();
154 
155  // first Arrow
156  glTranslated(1.5, 0, 0);
157  GLHelper::drawBoxLine(Position(0, 4), 0, 2, .05);
158  GLHelper::drawTriangleAtEnd(Position(0, 4), Position(0, 1), (double) 1, (double) .25);
159 
160  // second Arrow
161  glTranslated(-3, 0, 0);
162  GLHelper::drawBoxLine(Position(0, 4), 0, 2, .05);
163  GLHelper::drawTriangleAtEnd(Position(0, 4), Position(0, 1), (double) 1, (double) .25);
164 
165  // Pop polygon matrix
166  glPopMatrix();
167 
168  // Pop detector matrix
169  glPopMatrix();
170 
171  // Check if the distance is enought to draw details
172  if (s.scale * exaggeration >= 10) {
173  // Push matrix
174  glPushMatrix();
175  // Traslate to center of detector
176  glTranslated(myShape.getLineCenter().x(), myShape.getLineCenter().y(), getType() + 0.1);
177  // Rotate depending of myBlockIconRotation
178  glRotated(myBlockIconRotation, 0, 0, -1);
179  //move to logo position
180  glTranslated(1.9, 0, 0);
181  // draw Entry logo
182  if (isAdditionalSelected()) {
184  } else {
185  GLHelper::drawText("E3", Position(), .1, 2.8, RGBColor(0, 204, 0));
186  }
187  //move to logo position
188  glTranslated(1.7, 0, 0);
189  // Rotate depending of myBlockIconRotation
190  glRotated(90, 0, 0, 1);
191  // draw Entry text
192  if (isAdditionalSelected()) {
194  } else {
195  GLHelper::drawText("Entry", Position(), .1, 1, RGBColor(0, 204, 0));
196  }
197  // pop matrix
198  glPopMatrix();
199  // Show Lock icon depending of the Edit mode
200  drawLockIcon(0.4);
201  }
202  // Draw name
203  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
204 
205  // pop gl identificator
206  glPopName();
207 }
208 
209 
210 std::string
212  switch (key) {
213  case SUMO_ATTR_ID:
214  return getAdditionalID();
215  case SUMO_ATTR_LANE:
216  return myLane->getID();
217  case SUMO_ATTR_POSITION:
222  return toString(myBlocked);
223  case GNE_ATTR_PARENT:
224  return myAdditionalParent->getID();
225  default:
226  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
227  }
228 }
229 
230 
231 void
232 GNEDetectorEntry::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
233  if (value == getAttribute(key)) {
234  return; //avoid needless changes, later logic relies on the fact that attributes have changed
235  }
236  switch (key) {
237  case SUMO_ATTR_ID:
238  case SUMO_ATTR_LANE:
239  case SUMO_ATTR_POSITION:
242  case GNE_ATTR_PARENT:
243  undoList->p_add(new GNEChange_Attribute(this, key, value));
244  break;
245  default:
246  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
247  }
248 }
249 
250 
251 bool
252 GNEDetectorEntry::isValid(SumoXMLAttr key, const std::string& value) {
253  switch (key) {
254  case SUMO_ATTR_ID:
255  return isValidAdditionalID(value);
256  case SUMO_ATTR_LANE:
257  return (myViewNet->getNet()->retrieveLane(value, false) != NULL);
258  case SUMO_ATTR_POSITION:
259  return canParse<double>(value);
261  return canParse<bool>(value);
263  return canParse<bool>(value);
264  case GNE_ATTR_PARENT:
265  return (myViewNet->getNet()->getAdditional(SUMO_TAG_E3DETECTOR, value) != NULL);
266  default:
267  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
268  }
269 }
270 
271 void
272 GNEDetectorEntry::setAttribute(SumoXMLAttr key, const std::string& value) {
273  switch (key) {
274  case SUMO_ATTR_ID:
275  changeAdditionalID(value);
276  break;
277  case SUMO_ATTR_LANE:
278  myLane = changeLane(myLane, value);
279  break;
280  case SUMO_ATTR_POSITION:
281  myPositionOverLane = parse<double>(value) / myLane->getLaneParametricLength();
282  break;
284  myFriendlyPosition = parse<bool>(value);
285  break;
287  myBlocked = parse<bool>(value);
288  break;
289  case GNE_ATTR_PARENT:
290  changeAdditionalParent(value);
291  break;
292  default:
293  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
294  }
295  // After setting attribute always update Geometry
296  updateGeometry();
297 }
298 
299 /****************************************************************************/
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)
bool isDetectorPositionFixed() const
check if Position of detector is fixed
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
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
GNEAdditional * getAdditional(SumoXMLTag type, const std::string &id) const
Returns the named additional.
Definition: GNENet.cpp:1667
Stores the information about how to visualize structures.
double y() const
Returns the y-position.
Definition: Position.h:67
double x() const
Returns the x-position.
Definition: Position.h:62
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
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:53
GNEDetectorEntry(GNEViewNet *viewNet, GNEDetectorE3 *parent, GNELane *lane, double pos, bool friendlyPos)
Constructor.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
~GNEDetectorEntry()
destructor
void updateGeometry()
update pre-computed geometry information
std::vector< double > myShapeRotations
void changeAdditionalID(const std::string &newID)
change ID of additional
Position getLineCenter() const
get line center
an e3 entry point
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes ...
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
GNEAdditional * myAdditionalParent
pointer to Addititional parent
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
static const RGBColor SUMO_color_E3Entry
bool myFriendlyPosition
Flag for friendly position.
Definition: GNEDetector.h:147
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
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.
void setBlockIconRotation(GNELane *additionalLane=NULL)
std::string getAttribute(SumoXMLAttr key) const
This functions has to be implemented in all GNEAttributeCarriers.
GNELane * myLane
The lane in which this detector is placed.
Definition: GNEDetector.h:135
void writeAdditional(OutputDevice &device) const
writte additional element into a xml file
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 void drawTriangleAtEnd(const Position &p1, const Position &p2, double tLength, double tWidth)
Draws a triangle at the end of the given line.
Definition: GLHelper.cpp:428
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
void changeAdditionalParent(const std::string &newAdditionalParentID)
change additional parent of additional
bool isValidAdditionalID(const std::string &newID) const
check if a new additional ID is valid
bool isAdditionalSelected() const
virtual void updateGeometry()=0
update pre-computed geometry information
parent of an additional element
static void drawBoxLine(const Position &beg, double rot, double visLength, double width, double offset=0)
Draws a thick line.
Definition: GLHelper.cpp:137
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