Eclipse 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-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 "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  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 
117  // Clear all containers
119 
120  // obtain position over lane
121  myGeometry.shape.push_back(getPositionInView());
122 
123  // Obtain first position
124  Position f = myGeometry.shape[0] - Position(1, 0);
125 
126  // Obtain next position
127  Position s = myGeometry.shape[0] + Position(1, 0);
128 
129  // Save rotation (angle) of the vector constructed by points f and s
130  myGeometry.shapeRotations.push_back(getLaneParents().front()->getGeometry().shape.rotationDegreeAtOffset(getGeometryPositionOverLane()) * -1);
131 
132  // Set block icon position
134 
135  // Set offset of the block icon
136  myBlockIcon.offset = Position(-1, 0);
137 
138  // Set block icon rotation, and using their rotation for logo
140 }
141 
142 
143 void
145  // get values
146  const double width = (double) 2.0 * s.scale;
147  const double exaggeration = s.addSize.getExaggeration(s, this);
148  // start drawing
149  glPushName(getGlID());
150  glLineWidth(1.0);
151  // set color
152  if (drawUsingSelectColor()) {
154  } else {
156  }
157  // draw shape
158  glPushMatrix();
159  glTranslated(0, 0, getType());
160  glTranslated(myGeometry.shape[0].x(), myGeometry.shape[0].y(), 0);
161  glRotated(myGeometry.shapeRotations[0], 0, 0, 1);
162  glScaled(exaggeration, exaggeration, 1);
163  glBegin(GL_QUADS);
164  glVertex2d(-1.0, 2);
165  glVertex2d(-1.0, -2);
166  glVertex2d(1.0, -2);
167  glVertex2d(1.0, 2);
168  glEnd();
169  glTranslated(0, 0, .01);
170  glBegin(GL_LINES);
171  glVertex2d(0, 2 - .1);
172  glVertex2d(0, -2 + .1);
173  glEnd();
174  // outline if isn't being drawn for selecting
175  if ((width * exaggeration > 1) && !s.drawForSelecting) {
176  // set color
177  if (drawUsingSelectColor()) {
179  } else {
181  }
182  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
183  glBegin(GL_QUADS);
184  glVertex2f(-1.0, 2);
185  glVertex2f(-1.0, -2);
186  glVertex2f(1.0, -2);
187  glVertex2f(1.0, 2);
188  glEnd();
189  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
190  }
191  // position indicator if isn't being drawn for selecting
192  if ((width * exaggeration > 1) && !s.drawForSelecting) {
193  // set color
194  if (drawUsingSelectColor()) {
196  } else {
198  }
199  glRotated(90, 0, 0, -1);
200  glBegin(GL_LINES);
201  glVertex2d(0, 1.7);
202  glVertex2d(0, -1.7);
203  glEnd();
204  }
205  // Pop shape matrix
206  glPopMatrix();
207  // Check if the distance is enought to draw details and isn't being drawn for selecting
208  if ((s.drawDetail(s.detailSettings.detectorDetails, exaggeration)) && !s.drawForSelecting) {
209  // Push matrix
210  glPushMatrix();
211  // Traslate to center of detector
212  glTranslated(myGeometry.shape.getLineCenter().x(), myGeometry.shape.getLineCenter().y(), getType() + 0.1);
213  // Rotate depending of myBlockIcon.rotation
214  glRotated(myBlockIcon.rotation, 0, 0, -1);
215  //move to logo position
216  glTranslated(-1, 0, 0);
217  // draw E1 logo
218  if (drawUsingSelectColor()) {
220  } else {
221  GLHelper::drawText("E1", Position(), .1, 1.5, RGBColor::BLACK);
222  }
223  // pop matrix
224  glPopMatrix();
225  // Show Lock icon depending of the Edit mode
226  myBlockIcon.drawIcon(s, exaggeration);
227  }
228  // Finish draw if isn't being drawn for selecting
229  if (!s.drawForSelecting) {
231  }
232  // check if dotted contour has to be drawn
233  if (myViewNet->getDottedAC() == this) {
235  }
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_NAME:
250  return myAdditionalName;
251  case SUMO_ATTR_FILE:
252  return myFilename;
253  case SUMO_ATTR_VTYPES:
254  return myVehicleTypes;
258  return toString(myBlockMovement);
259  case GNE_ATTR_SELECTED:
261  case GNE_ATTR_GENERIC:
262  return getGenericParametersStr();
263  default:
264  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
265  }
266 }
267 
268 
269 void
270 GNEDetectorE1Instant::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
271  if (value == getAttribute(key)) {
272  return; //avoid needless changes, later logic relies on the fact that attributes have changed
273  }
274  switch (key) {
275  case SUMO_ATTR_ID:
276  case SUMO_ATTR_LANE:
277  case SUMO_ATTR_POSITION:
278  case SUMO_ATTR_NAME:
279  case SUMO_ATTR_FILE:
280  case SUMO_ATTR_VTYPES:
283  case GNE_ATTR_SELECTED:
284  case GNE_ATTR_GENERIC:
285  undoList->p_add(new GNEChange_Attribute(this, myViewNet->getNet(), key, value));
286  break;
287  default:
288  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
289  }
290 
291 }
292 
293 
294 bool
295 GNEDetectorE1Instant::isValid(SumoXMLAttr key, const std::string& value) {
296  switch (key) {
297  case SUMO_ATTR_ID:
298  return isValidDetectorID(value);
299  case SUMO_ATTR_LANE:
300  if (myViewNet->getNet()->retrieveLane(value, false) != nullptr) {
301  return true;
302  } else {
303  return false;
304  }
305  case SUMO_ATTR_POSITION:
306  return canParse<double>(value) && fabs(parse<double>(value)) < getLaneParents().front()->getParentEdge().getNBEdge()->getFinalLength();
307  case SUMO_ATTR_NAME:
309  case SUMO_ATTR_FILE:
311  case SUMO_ATTR_VTYPES:
312  if (value.empty()) {
313  return true;
314  } else {
316  }
318  return canParse<bool>(value);
320  return canParse<bool>(value);
321  case GNE_ATTR_SELECTED:
322  return canParse<bool>(value);
323  case GNE_ATTR_GENERIC:
324  return isGenericParametersValid(value);
325  default:
326  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
327  }
328 }
329 
330 // ===========================================================================
331 // private
332 // ===========================================================================
333 
334 void
335 GNEDetectorE1Instant::setAttribute(SumoXMLAttr key, const std::string& value) {
336  switch (key) {
337  case SUMO_ATTR_ID:
338  changeAdditionalID(value);
339  break;
340  case SUMO_ATTR_LANE:
341  changeLaneParents(this, value);
342  break;
343  case SUMO_ATTR_POSITION:
344  myPositionOverLane = parse<double>(value);
345  break;
346  case SUMO_ATTR_NAME:
347  myAdditionalName = value;
348  break;
349  case SUMO_ATTR_FILE:
350  myFilename = value;
351  break;
352  case SUMO_ATTR_VTYPES:
353  myVehicleTypes = value;
354  break;
356  myFriendlyPosition = parse<bool>(value);
357  break;
359  myBlockMovement = parse<bool>(value);
360  break;
361  case GNE_ATTR_SELECTED:
362  if (parse<bool>(value)) {
364  } else {
366  }
367  break;
368  case GNE_ATTR_GENERIC:
370  break;
371  default:
372  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
373  }
374 }
375 
376 /****************************************************************************/
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.
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.
Definition: GNEAdditional.h:74
void updateGeometry()
update pre-computed geometry information
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
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
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:668
static const RGBColor E1Instant
color for E1 Instant detectors
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
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: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
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
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
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
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.
Definition: GNEAdditional.h:68
RGBColor selectionColor
basic selection color
Position getPositionInView() const
Returns position of additional in view.
Definition: GNEDetector.cpp:74
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 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)
const std::vector< GNELane * > & getLaneParents() const
get lanes of VSS
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
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: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) ...
~GNEDetectorE1Instant()
Destructor.
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
GUIVisualizationDetailSettings detailSettings
detail settings
void fixAdditionalProblem()
fix additional problem
void changeLaneParents(GNEShape *elementChild, const std::string &newLaneIDs)
change edge parents of a shape
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
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
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
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