Eclipse 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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 //
16 /****************************************************************************/
17 
18 // ===========================================================================
19 // included modules
20 // ===========================================================================
21 
22 #include <netedit/GNENet.h>
23 #include <netedit/GNEUndoList.h>
24 #include <netedit/GNEViewNet.h>
28 #include <utils/gui/div/GLHelper.h>
30 
31 #include "GNEDetectorE1.h"
32 #include "GNEAdditionalHandler.h"
33 
34 
35 // ===========================================================================
36 // member method definitions
37 // ===========================================================================
38 
39 GNEDetectorE1::GNEDetectorE1(const std::string& id, GNELane* lane, GNEViewNet* viewNet, double pos, SUMOTime freq, const std::string& filename, const std::string& vehicleTypes, const std::string& name, bool friendlyPos, bool blockMovement) :
40  GNEDetector(id, viewNet, GLO_E1DETECTOR, SUMO_TAG_E1DETECTOR, pos, freq, filename, vehicleTypes, name, friendlyPos, blockMovement, {
41  lane
42 }) {
43 }
44 
45 
47 }
48 
49 
50 bool
52  // with friendly position enabled position are "always fixed"
53  if (myFriendlyPosition) {
54  return true;
55  } else {
56  return fabs(myPositionOverLane) <= getLaneParents().front()->getParentEdge().getNBEdge()->getFinalLength();
57  }
58 }
59 
60 
61 std::string
63  // declare variable for error position
64  std::string errorPosition;
65  const double len = getLaneParents().front()->getParentEdge().getNBEdge()->getFinalLength();
66  // check positions over lane
67  if (myPositionOverLane < -len) {
68  errorPosition = (toString(SUMO_ATTR_POSITION) + " < 0");
69  }
70  if (myPositionOverLane > len) {
71  errorPosition = (toString(SUMO_ATTR_POSITION) + " > lanes's length");
72  }
73  return errorPosition;
74 }
75 
76 
77 void
79  // declare new position
80  double newPositionOverLane = myPositionOverLane;
81  // fix pos and lenght checkAndFixDetectorPosition
82  GNEAdditionalHandler::checkAndFixDetectorPosition(newPositionOverLane, getLaneParents().front()->getParentEdge().getNBEdge()->getFinalLength(), true);
83  // set new position
85 }
86 
87 
88 void
90  // Calculate new position using old position
91  Position newPosition = myMove.originalViewPosition;
92  newPosition.add(offset);
93  // filtern position using snap to active grid
94  newPosition = myViewNet->snapToActiveGrid(newPosition);
95  const bool storeNegative = myPositionOverLane < 0;
96  myPositionOverLane = getLaneParents().front()->getGeometry().shape.nearest_offset_to_point2D(newPosition, false);
97  if (storeNegative) {
98  myPositionOverLane -= getLaneParents().front()->getParentEdge().getNBEdge()->getFinalLength();
99  }
100  // Update geometry
101  updateGeometry();
102 }
103 
104 
105 void
107  // commit new position allowing undo/redo
108  undoList->p_begin("position of " + getTagStr());
110  undoList->p_end();
111 }
112 
113 
114 void
116  // Clear all containers
118 
119  // obtain position over lane
120  myGeometry.shape.push_back(getPositionInView());
121 
122  // Obtain first position
123  Position f = myGeometry.shape[0] - Position(1, 0);
124 
125  // Obtain next position
126  Position s = myGeometry.shape[0] + Position(1, 0);
127 
128  // Save rotation (angle) of the vector constructed by points f and s
129  myGeometry.shapeRotations.push_back(getLaneParents().front()->getGeometry().shape.rotationDegreeAtOffset(getGeometryPositionOverLane()) * -1);
130 
131  // Set block icon position
133 
134  // Set offset of the block icon
135  myBlockIcon.offset = Position(-1, 0);
136 
137  // Set block icon rotation, and using their rotation for logo
139 }
140 
141 
142 void
144  // get values
145  const double width = (double) 2.0 * s.scale;
146  const double exaggeration = s.addSize.getExaggeration(s, this);
147  // start drawing
148  glPushName(getGlID());
149  glLineWidth(1.0);
150  // set color
151  if (drawUsingSelectColor()) {
153  } else {
155  }
156  // draw shape
157  glPushMatrix();
158  glTranslated(0, 0, getType());
159  glTranslated(myGeometry.shape[0].x(), myGeometry.shape[0].y(), 0);
160  glRotated(myGeometry.shapeRotations[0], 0, 0, 1);
161  glScaled(exaggeration, exaggeration, 1);
162  glBegin(GL_QUADS);
163  glVertex2d(-1.0, 2);
164  glVertex2d(-1.0, -2);
165  glVertex2d(1.0, -2);
166  glVertex2d(1.0, 2);
167  glEnd();
168  glTranslated(0, 0, .01);
169  glBegin(GL_LINES);
170  glVertex2d(0, 2 - .1);
171  glVertex2d(0, -2 + .1);
172  glEnd();
173  // outline if isn't being drawn for selecting
174  if ((width * exaggeration > 1) && !s.drawForSelecting) {
175  // set color
176  if (drawUsingSelectColor()) {
178  } else {
180  }
181  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
182  glBegin(GL_QUADS);
183  glVertex2f(-1.0, 2);
184  glVertex2f(-1.0, -2);
185  glVertex2f(1.0, -2);
186  glVertex2f(1.0, 2);
187  glEnd();
188  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
189  }
190  // position indicator if isn't being drawn for selecting
191  if ((width * exaggeration > 1) && !s.drawForSelecting) {
192  // set color
193  if (drawUsingSelectColor()) {
195  } else {
197  }
198  glRotated(90, 0, 0, -1);
199  glBegin(GL_LINES);
200  glVertex2d(0, 1.7);
201  glVertex2d(0, -1.7);
202  glEnd();
203  }
204  // Pop shape matrix
205  glPopMatrix();
206  // Check if the distance is enought to draw details and isn't being drawn for selecting
207  if ((s.drawDetail(s.detailSettings.detectorDetails, exaggeration)) && !s.drawForSelecting) {
208  // Push matrix
209  glPushMatrix();
210  // Traslate to center of detector
211  glTranslated(myGeometry.shape.getLineCenter().x(), myGeometry.shape.getLineCenter().y(), getType() + 0.1);
212  // Rotate depending of myBlockIcon.rotation
213  glRotated(myBlockIcon.rotation, 0, 0, -1);
214  //move to logo position
215  glTranslated(-1, 0, 0);
216  // draw E1 logo
217  if (drawUsingSelectColor()) {
219  } else {
220  GLHelper::drawText("E1", Position(), .1, 1.5, RGBColor::BLACK);
221  }
222  // pop matrix
223  glPopMatrix();
224  // Show Lock icon depending of the Edit mode
225  myBlockIcon.drawIcon(s, exaggeration);
226  }
227  // Finish draw if isn't being drawn for selecting
228  if (!s.drawForSelecting) {
230  }
231  // check if dotted contour has to be drawn
232  if (myViewNet->getDottedAC() == this) {
234  }
235  // pop name
236  glPopName();
237 }
238 
239 
240 std::string
242  switch (key) {
243  case SUMO_ATTR_ID:
244  return getAdditionalID();
245  case SUMO_ATTR_LANE:
246  return getLaneParents().front()->getID();
247  case SUMO_ATTR_POSITION:
249  case SUMO_ATTR_FREQUENCY:
250  return time2string(myFreq);
251  case SUMO_ATTR_NAME:
252  return myAdditionalName;
253  case SUMO_ATTR_FILE:
254  return myFilename;
255  case SUMO_ATTR_VTYPES:
256  return myVehicleTypes;
260  return toString(myBlockMovement);
261  case GNE_ATTR_SELECTED:
263  case GNE_ATTR_GENERIC:
264  return getGenericParametersStr();
265  default:
266  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
267  }
268 }
269 
270 
271 void
272 GNEDetectorE1::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
273  if (value == getAttribute(key)) {
274  return; //avoid needless changes, later logic relies on the fact that attributes have changed
275  }
276  switch (key) {
277  case SUMO_ATTR_ID:
278  case SUMO_ATTR_LANE:
279  case SUMO_ATTR_POSITION:
280  case SUMO_ATTR_FREQUENCY:
281  case SUMO_ATTR_NAME:
282  case SUMO_ATTR_FILE:
283  case SUMO_ATTR_VTYPES:
286  case GNE_ATTR_SELECTED:
287  case GNE_ATTR_GENERIC:
288  undoList->p_add(new GNEChange_Attribute(this, myViewNet->getNet(), key, value));
289  break;
290  default:
291  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
292  }
293 
294 }
295 
296 
297 bool
298 GNEDetectorE1::isValid(SumoXMLAttr key, const std::string& value) {
299  switch (key) {
300  case SUMO_ATTR_ID:
301  return isValidDetectorID(value);
302  case SUMO_ATTR_LANE:
303  if (myViewNet->getNet()->retrieveLane(value, false) != nullptr) {
304  return true;
305  } else {
306  return false;
307  }
308  case SUMO_ATTR_POSITION:
309  return canParse<double>(value) && fabs(parse<double>(value)) < getLaneParents().front()->getParentEdge().getNBEdge()->getFinalLength();
310  case SUMO_ATTR_FREQUENCY:
311  return (canParse<double>(value) && (parse<double>(value) >= 0));
312  case SUMO_ATTR_NAME:
314  case SUMO_ATTR_FILE:
316  case SUMO_ATTR_VTYPES:
317  if (value.empty()) {
318  return true;
319  } else {
321  }
323  return canParse<bool>(value);
325  return canParse<bool>(value);
326  case GNE_ATTR_SELECTED:
327  return canParse<bool>(value);
328  case GNE_ATTR_GENERIC:
329  return isGenericParametersValid(value);
330  default:
331  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
332  }
333 }
334 
335 // ===========================================================================
336 // private
337 // ===========================================================================
338 
339 void
340 GNEDetectorE1::setAttribute(SumoXMLAttr key, const std::string& value) {
341  switch (key) {
342  case SUMO_ATTR_ID:
343  changeAdditionalID(value);
344  break;
345  case SUMO_ATTR_LANE:
346  changeLaneParents(this, value);
347  break;
348  case SUMO_ATTR_POSITION:
349  myPositionOverLane = parse<double>(value);
350  break;
351  case SUMO_ATTR_FREQUENCY:
352  myFreq = parse<SUMOTime>(value);
353  break;
354  case SUMO_ATTR_FILE:
355  myFilename = value;
356  break;
357  case SUMO_ATTR_NAME:
358  myAdditionalName = value;
359  break;
360  case SUMO_ATTR_VTYPES:
361  myVehicleTypes = value;
362  break;
364  myFriendlyPosition = parse<bool>(value);
365  break;
367  myBlockMovement = parse<bool>(value);
368  break;
369  case GNE_ATTR_SELECTED:
370  if (parse<bool>(value)) {
372  } else {
374  }
375  break;
376  case GNE_ATTR_GENERIC:
378  break;
379  default:
380  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
381  }
382 }
383 
384 /****************************************************************************/
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
Position snapToActiveGrid(const Position &pos, bool snapXY=true) const
Returns a position that is mapped to the closest grid point if the grid is active.
long long int SUMOTime
Definition: SUMOTime.h:35
double scale
information about a lane&#39;s width (temporary, used for a single view)
static bool checkAndFixDetectorPosition(double &pos, const double laneLength, const bool friendlyPos)
check if the position of a detector over a lane is valid
static bool isValidAttribute(const std::string &value)
whether the given string is a valid attribute for a certain key (for example, a name) ...
static bool isValidListOfTypeID(const std::string &value)
whether the given string is a valid list of ids for an edge or vehicle type (empty aren&#39;t allowed) ...
GNEDetectorE1(const std::string &id, GNELane *lane, GNEViewNet *viewNet, double pos, SUMOTime freq, const std::string &filename, const std::string &vehicleTypes, const std::string &name, bool friendlyPos, bool blockMovement)
Constructor.
GUIVisualizationTextSettings addName
std::vector< double > shapeRotations
The rotations of the single shape parts.
Definition: GNEAdditional.h:74
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:127
void setGenericParametersStr(const std::string &value)
set generic parameters in string format
const std::string & getAdditionalID() const
static const RGBColor WHITE
Definition: RGBColor.h:197
Stores the information about how to visualize structures.
Position offset
The offSet of the block icon.
double y() const
Returns the y-position.
Definition: Position.h:62
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
double x() const
Returns the x-position.
Definition: Position.h:57
bool isAdditionalValid() const
check if current additional is valid to be writed into XML (by default true, can be reimplemented in ...
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes ...
~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:668
bool drawDetail(const double detail, const double exaggeration) const
check if details can be drawn for the given GUIVisualizationDetailSettings and current scale and exxa...
void unselectAttributeCarrier(bool changeFlag=true)
unselect attribute carrier using GUIGlobalSelection
Position position
position of the block icon
BlockIcon myBlockIcon
variable BlockIcon
void clearGeometry()
reset geometry
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:46
Position originalViewPosition
value for saving first original position over lane before moving
void p_begin(const std::string &description)
Begin undo command sub-group. This begins a new group of commands that are treated as a single comman...
Definition: GNEUndoList.cpp:73
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
RGBColor selectedAdditionalColor
additional selection color (busStops, Detectors...)
static const RGBColor BLACK
Definition: RGBColor.h:198
void fixAdditionalProblem()
fix additional problem
bool isAttributeCarrierSelected() const
check if attribute carrier is selected
void changeAdditionalID(const std::string &newID)
change ID of additional
generic attribute
Position getLineCenter() const
get line center
double rotation
The rotation of the block icon.
bool isValidDetectorID(const std::string &newID) const
check if a new detector ID is valid
GNEViewNet * myViewNet
The GNEViewNet this additional element belongs.
AdditionalMove myMove
variable AdditionalMove
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
GUIVisualizationSizeSettings addSize
GNEUndoList * getUndoList() const
get the undoList object
Definition: GNEViewNet.cpp:933
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:616
double getGeometryPositionOverLane() const
get position over lane that is applicable to the shape
Definition: GNEDetector.cpp:90
void moveGeometry(const Position &offset)
change the position of the element geometry without saving in undoList
void p_end()
End undo command sub-group. If the sub-group is still empty, it will be deleted; otherwise, the sub-group will be added as a new command into parent group. A matching begin() must have been called previously.
Definition: GNEUndoList.cpp:80
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
PositionVector shape
The shape of the additional element.
Definition: GNEAdditional.h:68
RGBColor selectionColor
basic selection color
Position getPositionInView() const
Returns position of additional in view.
Definition: GNEDetector.cpp:74
std::string myAdditionalName
name of additional
static bool isGenericParametersValid(const std::string &value)
check if given string can be parsed to a map/list of generic parameters
friend class GNEChange_Attribute
declare friend class
void selectAttributeCarrier(bool changeFlag=true)
const std::vector< GNELane * > & getLaneParents() const
get lanes of VSS
void commitGeometryMoving(GNEUndoList *undoList)
commit geometry changes in the attributes of an element after use of moveGeometry(...)
block movement of a graphic element
std::string myVehicleTypes
attribute vehicle types
Definition: GNEDetector.h:170
static const double detectorDetails
details for detectors
void drawName(const Position &pos, const double scale, const GUIVisualizationTextSettings &settings, const double angle=0) const
draw name of item
bool myFriendlyPosition
Flag for friendly position.
Definition: GNEDetector.h:173
std::string myFilename
The path to the output file.
Definition: GNEDetector.h:167
bool myBlockMovement
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
static void drawShapeDottedContourRectangle(const GUIVisualizationSettings &s, const int type, const Position &center, const double width, const double height, const double rotation=0, const double offsetX=0, const double offsetY=0)
draw a dotted contour around the given Position with certain width and height
Definition: GLHelper.cpp:555
static const RGBColor E1
color for E1 detectors
GUIVisualizationDetailSettings detailSettings
detail settings
SUMOTime myFreq
The aggregation period the values the detector collects shall be summed up.
Definition: GNEDetector.h:164
void updateGeometry()
update pre-computed geometry information
void changeLaneParents(GNEShape *elementChild, const std::string &newLaneIDs)
change edge parents of a shape
std::string getAttribute(SumoXMLAttr key) const
bool drawUsingSelectColor() const
check if attribute carrier must be drawn using selecting color.
const GNEAttributeCarrier * getDottedAC() const
get AttributeCarrier under cursor
Definition: GNEViewNet.cpp:939
AdditionalGeometry myGeometry
geometry to be precomputed in updateGeometry(...)
const std::string & getTagStr() const
get tag assigned to this object in string format
a E1 detector
element is selected
std::string getGenericParametersStr() const
return generic parameters in string format
GNENet * getNet() const
get the net object
Definition: GNEViewNet.cpp:927
GUIGlID getGlID() const
Returns the numerical id of the object.
GUIVisualizationColorSettings colorSettings
color settings
std::string firstOriginalLanePosition
value for saving first original position over lane before moving
void drawIcon(const GUIVisualizationSettings &s, const double exaggeration, const double size=0.5) const
draw lock icon
bool drawForSelecting
whether drawing is performed for the purpose of selecting objects
double getExaggeration(const GUIVisualizationSettings &s, const GUIGlObject *o, double factor=20) const
return the drawing size including exaggeration and constantSize values
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
static bool isValidFilename(const std::string &value)
whether the given string is a valid attribute for a filename (for example, a name) ...
GNELane * retrieveLane(const std::string &id, bool failHard=true, bool checkVolatileChange=false)
get lane by id
Definition: GNENet.cpp:1179
void setRotation(GNELane *additionalLane=nullptr)
set Rotation of block Icon (must be called in updateGeometry() function)
double myPositionOverLane
position of detector over Lane
Definition: GNEDetector.h:161
std::string getAdditionalProblem() const
return a string with the current additional problem