SUMO - Simulation of Urban MObility
GNEDetectorE1Instant.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-2018 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 "GNEDetectorE1Instant.h"
32 #include "GNEAdditionalHandler.h"
33 
34 
35 // ===========================================================================
36 // member method definitions
37 // ===========================================================================
38 
39 GNEDetectorE1Instant::GNEDetectorE1Instant(const std::string& id, GNELane* lane, GNEViewNet* viewNet, double pos, const std::string& filename, const std::string& vehicleTypes, const std::string& name, bool friendlyPos, bool blockMovement) :
40  GNEDetector(id, viewNet, GLO_E1DETECTOR_INSTANT, SUMO_TAG_INSTANT_INDUCTION_LOOP, pos, 0, filename, vehicleTypes, name, friendlyPos, blockMovement),
41  myLane(lane) {
42 }
43 
44 
46 }
47 
48 
49 bool
51  // with friendly position enabled position are "always fixed"
52  if (myFriendlyPosition) {
53  return true;
54  } else {
56  }
57 }
58 
59 
60 std::string
62  // declare variable for error position
63  std::string errorPosition;
64  // check positions over lane
65  if (myPositionOverLane < 0) {
66  errorPosition = (toString(SUMO_ATTR_POSITION) + " < 0");
67  }
69  errorPosition = (toString(SUMO_ATTR_POSITION) + " > lanes's length");
70  }
71  return errorPosition;
72 }
73 
74 
75 void
77  // declare new position
78  double newPositionOverLane = myPositionOverLane;
79  // fix pos and lenght checkAndFixDetectorPosition
81  // set new position
83 }
84 
85 
86 void
88  // Calculate new position using old position
89  Position newPosition = myMove.originalViewPosition;
90  newPosition.add(offset);
91  // filtern position using snap to active grid
92  newPosition = myViewNet->snapToActiveGrid(newPosition);
94  // Update geometry
95  updateGeometry(false);
96 }
97 
98 
99 void
101  // commit new position allowing undo/redo
102  undoList->p_begin("position of " + getTagStr());
104  undoList->p_end();
105 }
106 
107 
108 void
110  // first check if object has to be removed from grid (SUMOTree)
111  if (updateGrid) {
113  }
114 
115  // Clear all containers
117 
118  // obtain position over lane
120  myGeometry.shape.push_back(myLane->getShape().positionAtOffset(fixedPositionOverLane * myLane->getLengthGeometryFactor()));
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(myLane->getShape().rotationDegreeAtOffset(fixedPositionOverLane) * -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  // last step is to check if object has to be added into grid (SUMOTree) again
141  if (updateGrid) {
143  }
144 }
145 
146 
147 GNELane*
149  return myLane;
150 }
151 
152 
153 void
155  // get values
156  glPushName(getGlID());
157  double width = (double) 2.0 * s.scale;
158  glLineWidth(1.0);
159  const double exaggeration = s.addSize.getExaggeration(s, this);
160 
161  // set color
164  } else {
166  }
167  // draw shape
168  glPushMatrix();
169  glTranslated(0, 0, getType());
170  glTranslated(myGeometry.shape[0].x(), myGeometry.shape[0].y(), 0);
171  glRotated(myGeometry.shapeRotations[0], 0, 0, 1);
172  glScaled(exaggeration, exaggeration, 1);
173  glBegin(GL_QUADS);
174  glVertex2d(-1.0, 2);
175  glVertex2d(-1.0, -2);
176  glVertex2d(1.0, -2);
177  glVertex2d(1.0, 2);
178  glEnd();
179  glTranslated(0, 0, .01);
180  glBegin(GL_LINES);
181  glVertex2d(0, 2 - .1);
182  glVertex2d(0, -2 + .1);
183  glEnd();
184 
185  // outline if isn't being drawn for selecting
186  if ((width * exaggeration > 1) && !s.drawForSelecting) {
187  // set color
190  } else {
192  }
193  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
194  glBegin(GL_QUADS);
195  glVertex2f(-1.0, 2);
196  glVertex2f(-1.0, -2);
197  glVertex2f(1.0, -2);
198  glVertex2f(1.0, 2);
199  glEnd();
200  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
201  }
202 
203  // position indicator if isn't being drawn for selecting
204  if ((width * exaggeration > 1) && !s.drawForSelecting) {
205  // set color
208  } else {
210  }
211  glRotated(90, 0, 0, -1);
212  glBegin(GL_LINES);
213  glVertex2d(0, 1.7);
214  glVertex2d(0, -1.7);
215  glEnd();
216  }
217 
218  // Pop shape matrix
219  glPopMatrix();
220 
221  // Check if the distance is enought to draw details and isn't being drawn for selecting
222  if ((s.scale * exaggeration >= 10) && !s.drawForSelecting) {
223  // Push matrix
224  glPushMatrix();
225  // Traslate to center of detector
226  glTranslated(myGeometry.shape.getLineCenter().x(), myGeometry.shape.getLineCenter().y(), getType() + 0.1);
227  // Rotate depending of myBlockIcon.rotation
228  glRotated(myBlockIcon.rotation, 0, 0, -1);
229  //move to logo position
230  glTranslated(-1, 0, 0);
231  // draw E1 logo
233  GLHelper::drawText("E1", Position(), .1, 1.5, s.selectionColor);
234  } else {
235  GLHelper::drawText("E1", Position(), .1, 1.5, RGBColor::BLACK);
236  }
237  // pop matrix
238  glPopMatrix();
239  // Show Lock icon depending of the Edit mode
240  myBlockIcon.draw();
241  }
242 
243  // Finish draw if isn't being drawn for selecting
244  if (!s.drawForSelecting) {
245  drawName(getCenteringBoundary().getCenter(), s.scale, s.addName);
246  }
247  glPopName();
248 }
249 
250 
251 std::string
253  switch (key) {
254  case SUMO_ATTR_ID:
255  return getAdditionalID();
256  case SUMO_ATTR_LANE:
257  return myLane->getID();
258  case SUMO_ATTR_POSITION:
260  case SUMO_ATTR_NAME:
261  return myAdditionalName;
262  case SUMO_ATTR_FILE:
263  return myFilename;
264  case SUMO_ATTR_VTYPES:
265  return myVehicleTypes;
269  return toString(myBlockMovement);
270  case GNE_ATTR_SELECTED:
272  case GNE_ATTR_GENERIC:
273  return getGenericParametersStr();
274  default:
275  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
276  }
277 }
278 
279 
280 void
281 GNEDetectorE1Instant::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
282  if (value == getAttribute(key)) {
283  return; //avoid needless changes, later logic relies on the fact that attributes have changed
284  }
285  switch (key) {
286  case SUMO_ATTR_ID:
287  case SUMO_ATTR_LANE:
288  case SUMO_ATTR_POSITION:
289  case SUMO_ATTR_NAME:
290  case SUMO_ATTR_FILE:
291  case SUMO_ATTR_VTYPES:
294  case GNE_ATTR_SELECTED:
295  case GNE_ATTR_GENERIC:
296  undoList->p_add(new GNEChange_Attribute(this, key, value));
297  break;
298  default:
299  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
300  }
301 
302 }
303 
304 
305 bool
306 GNEDetectorE1Instant::isValid(SumoXMLAttr key, const std::string& value) {
307  switch (key) {
308  case SUMO_ATTR_ID:
309  return isValidDetectorID(value);
310  case SUMO_ATTR_LANE:
311  if (myViewNet->getNet()->retrieveLane(value, false) != nullptr) {
312  return true;
313  } else {
314  return false;
315  }
316  case SUMO_ATTR_POSITION:
317  return canParse<double>(value);
318  case SUMO_ATTR_NAME:
320  case SUMO_ATTR_FILE:
322  case SUMO_ATTR_VTYPES:
323  if (value.empty()) {
324  return true;
325  } else {
327  }
329  return canParse<bool>(value);
331  return canParse<bool>(value);
332  case GNE_ATTR_SELECTED:
333  return canParse<bool>(value);
334  case GNE_ATTR_GENERIC:
335  return isGenericParametersValid(value);
336  default:
337  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
338  }
339 }
340 
341 // ===========================================================================
342 // private
343 // ===========================================================================
344 
345 void
346 GNEDetectorE1Instant::setAttribute(SumoXMLAttr key, const std::string& value) {
347  switch (key) {
348  case SUMO_ATTR_ID:
349  changeAdditionalID(value);
350  break;
351  case SUMO_ATTR_LANE:
352  myLane = changeLane(myLane, value);
353  break;
354  case SUMO_ATTR_POSITION:
355  myPositionOverLane = parse<double>(value);
356  break;
357  case SUMO_ATTR_NAME:
358  myAdditionalName = value;
359  break;
360  case SUMO_ATTR_FILE:
361  myFilename = value;
362  break;
363  case SUMO_ATTR_VTYPES:
364  myVehicleTypes = value;
365  break;
367  myFriendlyPosition = parse<bool>(value);
368  break;
370  myBlockMovement = parse<bool>(value);
371  break;
372  case GNE_ATTR_SELECTED:
373  if (parse<bool>(value)) {
375  } else {
377  }
378  break;
379  case GNE_ATTR_GENERIC:
381  break;
382  default:
383  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
384  }
385  // Update Geometry after setting a new attribute (but avoided for certain attributes)
386  if((key != SUMO_ATTR_ID) && (key != GNE_ATTR_GENERIC) && (key != GNE_ATTR_SELECTED)) {
387  updateGeometry(true);
388  }
389 }
390 
391 /****************************************************************************/
GNELane * getLane() const
get lane
GNELane * myLane
The lane in which this detector is placed.
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)
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) ...
GUIVisualizationTextSettings addName
std::vector< double > shapeRotations
The rotations of the single shape parts.
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:127
void moveGeometry(const Position &offset)
change the position of the element geometry without saving in undoList
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
void setGenericParametersStr(const std::string &value)
set generic parameters in string format
const std::string & getAdditionalID() const
returns Additional ID
static const RGBColor WHITE
Definition: RGBColor.h:191
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
Position snapToActiveGrid(const Position &pos) const
Returns a position that is mapped to the closest grid point if the grid is active.
double x() const
Returns the x-position.
Definition: Position.h:57
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:611
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
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:47
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.
static const RGBColor BLACK
Definition: RGBColor.h:192
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
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.
std::string getAttribute(SumoXMLAttr key) const
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
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:49
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:573
GNEEdge & getParentEdge()
Returns underlying parent edge.
Definition: GNELane.cpp:1260
An instantenous induction loop.
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.
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes ...
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
void removeGLObjectFromGrid(GUIGlObject *o)
add GL Object into net
Definition: GNENet.cpp:1160
void commitGeometryMoving(GNEUndoList *undoList)
commit geometry changes in the attributes of an element after use of moveGeometry(...)
friend class GNEChange_Attribute
declare friend class
bool isAdditionalValid() const
check if current additional is valid to be writed into XML (by default true, can be reimplemented in ...
void selectAttributeCarrier(bool changeFlag=true)
block movement of a graphic element
RGBColor selectionColor
NETEDIT special colors.
std::string myVehicleTypes
attribute vehicle types
Definition: GNEDetector.h:160
double getLengthGeometryFactor() const
get lenght geometry factor
Definition: GNELane.cpp:1317
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
GNEDetectorE1Instant(const std::string &id, GNELane *lane, GNEViewNet *viewNet, double pos, const std::string &filename, const std::string &vehicleTypes, const std::string &name, bool friendlyPos, bool blockMovement)
Constructor.
bool myFriendlyPosition
Flag for friendly position.
Definition: GNEDetector.h:163
static const RGBColor SUMO_color_E1Instant
color for E1 Instant detectors
double getFinalLength() const
get length that will be assigned to the lanes in the final network
Definition: NBEdge.cpp:3575
std::string myFilename
The path to the output file.
Definition: GNEDetector.h:157
bool myBlockMovement
boolean to check if additional element is blocked (i.e. cannot be moved with mouse) ...
~GNEDetectorE1Instant()
Destructor.
void updateGeometry(bool updateGrid)
update pre-computed geometry information
void fixAdditionalProblem()
fix additional problem
const PositionVector & getShape() const
returns the shape of the lane
Definition: GNELane.cpp:669
AdditionalGeometry myGeometry
geometry to be precomputed in updateGeometry(...)
const std::string & getTagStr() const
get tag assigned to this object in string format
std::string getAdditionalProblem() const
return a string with the current additional problem
element is selected
std::string getGenericParametersStr() const
return generic parameters in string format
GNENet * getNet() const
get the net object
GUIGlID getGlID() const
Returns the numerical id of the object.
std::string firstOriginalLanePosition
value for saving first original position over lane before moving
NBEdge * getNBEdge()
returns the internal NBEdge
Definition: GNEEdge.cpp:613
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
double nearest_offset_to_point2D(const Position &p, bool perpendicular=true) const
return the nearest offest to point 2D
void addGLObjectIntoGrid(GUIGlObject *o)
add GL Object into net
Definition: GNENet.cpp:1153
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
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:1059
GNELane * changeLane(GNELane *oldLane, const std::string &newLaneID)
change lane of additional
void setRotation(GNELane *additionalLane=nullptr)
set Rotation of block Icon (must be called in updateGeometry(bool updateGrid) function) ...
double myPositionOverLane
position of detector over Lane
Definition: GNEDetector.h:151
void draw(double size=0.5) const
draw lock icon