SUMO - Simulation of Urban MObility
GNEDetectorExit.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 "GNEDetectorExit.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 GNEDetectorExit::GNEDetectorExit(GNEViewNet* viewNet, GNEDetectorE3* parent, GNELane* lane, double pos, bool friendlyPos) :
61  GNEDetector(parent->generateExitID(), viewNet, SUMO_TAG_DET_EXIT, ICON_E3EXIT, 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 Exit logo
182  if (isAdditionalSelected()) {
184  } else {
185  GLHelper::drawText("E3", Position(), .1, 2.8, RGBColor(204, 0, 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 Exit text
192  if (isAdditionalSelected()) {
194  } else {
195  GLHelper::drawText("Exit", Position(), .1, 1, RGBColor(204, 0, 0));
196  }
197  // pop matrix
198  glPopMatrix();
199 
200  // Show Lock icon depending of the Edit mode
201  drawLockIcon(0.4);
202  }
203  // Draw name
204  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
205 
206  // pop gl identificator
207  glPopName();
208 }
209 
210 
211 std::string
213  switch (key) {
214  case SUMO_ATTR_ID:
215  return getAdditionalID();
216  case SUMO_ATTR_LANE:
217  return myLane->getID();
218  case SUMO_ATTR_POSITION:
223  return toString(myBlocked);
224  case GNE_ATTR_PARENT:
225  return myAdditionalParent->getID();
226  default:
227  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
228  }
229 }
230 
231 
232 void
233 GNEDetectorExit::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
234  if (value == getAttribute(key)) {
235  return; //avoid needless changes, later logic relies on the fact that attributes have changed
236  }
237  switch (key) {
238  case SUMO_ATTR_ID:
239  case SUMO_ATTR_LANE:
240  case SUMO_ATTR_POSITION:
243  case GNE_ATTR_PARENT:
244  undoList->p_add(new GNEChange_Attribute(this, key, value));
245  break;
246  default:
247  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
248  }
249 }
250 
251 
252 bool
253 GNEDetectorExit::isValid(SumoXMLAttr key, const std::string& value) {
254  switch (key) {
255  case SUMO_ATTR_ID:
256  return isValidAdditionalID(value);
257  case SUMO_ATTR_LANE:
258  return (myViewNet->getNet()->retrieveLane(value, false) != NULL);
259  case SUMO_ATTR_POSITION:
260  return canParse<double>(value);
262  return canParse<bool>(value);
264  return canParse<bool>(value);
265  case GNE_ATTR_PARENT:
266  return (myViewNet->getNet()->getAdditional(SUMO_TAG_E3DETECTOR, value) != NULL);
267  default:
268  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
269  }
270 }
271 
272 void
273 GNEDetectorExit::setAttribute(SumoXMLAttr key, const std::string& value) {
274  switch (key) {
275  case SUMO_ATTR_ID:
276  changeAdditionalID(value);
277  break;
278  case SUMO_ATTR_LANE:
279  myLane = changeLane(myLane, value);
280  break;
281  case SUMO_ATTR_POSITION:
282  myPositionOverLane = parse<double>(value) / myLane->getLaneParametricLength();
283  break;
285  myFriendlyPosition = parse<bool>(value);
286  break;
288  myBlocked = parse<bool>(value);
289  break;
290  case GNE_ATTR_PARENT:
291  changeAdditionalParent(value);
292  break;
293  default:
294  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
295  }
296  // After setting attribute always update Geometry
297  updateGeometry();
298 }
299 
300 /****************************************************************************/
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
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes ...
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
void writeAdditional(OutputDevice &device) const
writte additional element into a xml file
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
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
std::vector< double > myShapeRotations
void changeAdditionalID(const std::string &newID)
change ID of additional
GNEDetectorExit(GNEViewNet *viewNet, GNEDetectorE3 *parent, GNELane *lane, double pos, bool friendlyPos)
Constructor.
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
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
~GNEDetectorExit()
destructor
an e3 exit point
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 isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
bool myFriendlyPosition
Flag for friendly position.
Definition: GNEDetector.h:147
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 drawGL(const GUIVisualizationSettings &s) const
Draws the object.
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 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
static const RGBColor SUMO_color_E3Exit
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
std::string getAttribute(SumoXMLAttr key) const
This functions has to be implemented in all GNEAttributeCarriers.
void updateGeometry()
update pre-computed geometry information
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