Eclipse SUMO - Simulation of Urban MObility
GNEInductionLoopDetector.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 //
19 /****************************************************************************/
20 
21 #include <netedit/GNENet.h>
22 #include <netedit/GNEUndoList.h>
23 #include <netedit/GNEViewNet.h>
24 #include <netedit/GNEViewParent.h>
27 #include <utils/gui/div/GLHelper.h>
29 
31 #include "GNEAdditionalHandler.h"
32 
33 
34 // ===========================================================================
35 // member method definitions
36 // ===========================================================================
37 
40  0, {}, "", {}, "", false, Parameterised::Map()) {
41  // reset default values
42  resetDefaultValues();
43 }
44 
45 
46 GNEInductionLoopDetector::GNEInductionLoopDetector(const std::string& id, GNELane* lane, GNENet* net, const double pos,
47  const SUMOTime freq, const std::string& filename, const std::vector<std::string>& vehicleTypes,
48  const std::string& name, bool friendlyPos, const Parameterised::Map& parameters) :
50  freq, {
51  lane
52 }, filename, vehicleTypes, name, friendlyPos, parameters) {
53  // update centering boundary without updating grid
54  updateCenteringBoundary(false);
55 }
56 
57 
59 }
60 
61 
62 void
64  device.openTag(getTagProperty().getTag());
65  device.writeAttr(SUMO_ATTR_ID, getID());
66  if (!myAdditionalName.empty()) {
68  }
69  device.writeAttr(SUMO_ATTR_LANE, getParentLanes().front()->getID());
71  if (getAttribute(SUMO_ATTR_PERIOD).size() > 0) {
73  }
74  if (myFilename.size() > 0) {
76  }
77  if (myVehicleTypes.size() > 0) {
79  }
80  if (myFriendlyPosition) {
81  device.writeAttr(SUMO_ATTR_FRIENDLY_POS, true);
82  }
83  // write parameters (Always after children to avoid problems with additionals.xsd)
84  writeParams(device);
85  device.closeTag();
86 }
87 
88 
89 bool
91  // with friendly position enabled position are "always fixed"
92  if (myFriendlyPosition) {
93  return true;
94  } else {
95  return fabs(myPositionOverLane) <= getParentLanes().front()->getParentEdge()->getNBEdge()->getFinalLength();
96  }
97 }
98 
99 
100 std::string
102  // obtain final length
103  const double len = getParentLanes().front()->getParentEdge()->getNBEdge()->getFinalLength();
104  // check if detector has a problem
106  return "";
107  } else {
108  // declare variable for error position
109  std::string errorPosition;
110  // check positions over lane
111  if (myPositionOverLane < 0) {
112  errorPosition = (toString(SUMO_ATTR_POSITION) + " < 0");
113  }
114  if (myPositionOverLane > len) {
115  errorPosition = (toString(SUMO_ATTR_POSITION) + TL(" > lanes's length"));
116  }
117  return errorPosition;
118  }
119 }
120 
121 
122 void
124  // declare new position
125  double newPositionOverLane = myPositionOverLane;
126  // fix pos and length checkAndFixDetectorPosition
127  double length = 0;
128  GNEAdditionalHandler::fixLanePosition(newPositionOverLane, length, getParentLanes().front()->getParentEdge()->getNBEdge()->getFinalLength());
129  // set new position
130  setAttribute(SUMO_ATTR_POSITION, toString(newPositionOverLane), myNet->getViewNet()->getUndoList());
131 }
132 
133 
134 void
136  // update geometry
138  // update centering boundary without updating grid
140 }
141 
142 
143 bool
145  // get TLS Attributes
146  const auto& TLSAttributes = myNet->getViewNet()->getViewParent()->getTLSEditorFrame()->getTLSAttributes();
147  // check detectors
149  (TLSAttributes->getE1Detectors().count(getParentLanes().front()->getID()) > 0) &&
150  (TLSAttributes->getE1Detectors().at(getParentLanes().front()->getID()) == getID())) {
151  return true;
152  } else {
153  return false;
154  }
155 }
156 
157 
158 void
160  // Obtain exaggeration of the draw
161  const double E1Exaggeration = getExaggeration(s);
162  // first check if additional has to be drawn
164  // get detail level
165  const auto d = s.getDetailLevel(E1Exaggeration);
166  // draw geometry only if we'rent in drawForObjectUnderCursor mode
167  if (!s.drawForViewObjectsHandler) {
168  // declare colors
169  RGBColor mainColor, secondColor, textColor;
170  // set color
171  if (drawUsingSelectColor()) {
173  secondColor = mainColor.changedBrightness(-32);
174  textColor = mainColor.changedBrightness(32);
175  } else {
176  mainColor = s.detectorSettings.E1Color;
177  secondColor = RGBColor::WHITE;
178  textColor = RGBColor::BLACK;
179  }
180  // draw parent and child lines
182  // push layer matrix
184  // translate to front
186  // draw E1 shape
187  drawE1Shape(d, E1Exaggeration, mainColor, secondColor);
188  // draw E1 Logo
189  drawE1DetectorLogo(s, d, E1Exaggeration, "E1", textColor);
190  // pop layer matrix
192  // draw lock icon
194  // Draw additional ID
195  drawAdditionalID(s);
196  // draw additional name
198  // draw dotted contour
200  }
201  // calculate contour rectangle
203  myAdditionalGeometry.getShapeRotations().front(), E1Exaggeration);
204  }
205 }
206 
207 
208 std::string
210  switch (key) {
211  case SUMO_ATTR_ID:
212  return getMicrosimID();
213  case SUMO_ATTR_LANE:
214  return getParentLanes().front()->getID();
215  case SUMO_ATTR_POSITION:
217  case SUMO_ATTR_PERIOD:
218  if (myPeriod == SUMOTime_MAX_PERIOD) {
219  return "";
220  } else {
221  return time2string(myPeriod);
222  }
223  case SUMO_ATTR_NAME:
224  return myAdditionalName;
225  case SUMO_ATTR_FILE:
226  return myFilename;
227  case SUMO_ATTR_VTYPES:
228  return toString(myVehicleTypes);
231  case GNE_ATTR_SELECTED:
233  case GNE_ATTR_PARAMETERS:
234  return getParametersStr();
236  return "";
237  default:
238  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
239  }
240 }
241 
242 
243 double
245  switch (key) {
246  case SUMO_ATTR_POSITION:
247  return myPositionOverLane;
248  default:
249  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
250  }
251 }
252 
253 
254 void
255 GNEInductionLoopDetector::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
256  switch (key) {
257  case SUMO_ATTR_ID:
258  case SUMO_ATTR_LANE:
259  case SUMO_ATTR_POSITION:
260  case SUMO_ATTR_PERIOD:
261  case SUMO_ATTR_NAME:
262  case SUMO_ATTR_FILE:
263  case SUMO_ATTR_VTYPES:
265  case GNE_ATTR_SELECTED:
266  case GNE_ATTR_PARAMETERS:
268  GNEChange_Attribute::changeAttribute(this, key, value, undoList);
269  break;
270  default:
271  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
272  }
273 }
274 
275 
276 bool
277 GNEInductionLoopDetector::isValid(SumoXMLAttr key, const std::string& value) {
278  switch (key) {
279  case SUMO_ATTR_ID:
280  return isValidDetectorID(value);
281  case SUMO_ATTR_LANE:
282  if (myNet->getAttributeCarriers()->retrieveLane(value, false) != nullptr) {
283  return true;
284  } else {
285  return false;
286  }
287  case SUMO_ATTR_POSITION:
288  return canParse<double>(value) && fabs(parse<double>(value)) < getParentLanes().front()->getParentEdge()->getNBEdge()->getFinalLength();
289  case SUMO_ATTR_PERIOD:
290  if (value.empty()) {
291  return true;
292  } else {
293  return (canParse<double>(value) && (parse<double>(value) >= 0));
294  }
295  case SUMO_ATTR_NAME:
297  case SUMO_ATTR_FILE:
299  case SUMO_ATTR_VTYPES:
300  if (value.empty()) {
301  return true;
302  } else {
304  }
306  return canParse<bool>(value);
307  case GNE_ATTR_SELECTED:
308  return canParse<bool>(value);
309  case GNE_ATTR_PARAMETERS:
310  return areParametersValid(value);
311  default:
312  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
313  }
314 }
315 
316 // ===========================================================================
317 // private
318 // ===========================================================================
319 
320 void
321 GNEInductionLoopDetector::setAttribute(SumoXMLAttr key, const std::string& value) {
322  switch (key) {
323  case SUMO_ATTR_ID:
324  // update microsimID
325  setAdditionalID(value);
326  break;
327  case SUMO_ATTR_LANE:
329  break;
330  case SUMO_ATTR_POSITION:
331  myPositionOverLane = parse<double>(value);
332  break;
333  case SUMO_ATTR_PERIOD:
334  if (value.empty()) {
336  } else {
337  myPeriod = string2time(value);
338  }
339  break;
340  case SUMO_ATTR_FILE:
341  myFilename = value;
342  break;
343  case SUMO_ATTR_NAME:
344  myAdditionalName = value;
345  break;
346  case SUMO_ATTR_VTYPES:
347  myVehicleTypes = parse<std::vector<std::string> >(value);
348  break;
350  myFriendlyPosition = parse<bool>(value);
351  break;
352  case GNE_ATTR_SELECTED:
353  if (parse<bool>(value)) {
355  } else {
357  }
358  break;
359  case GNE_ATTR_PARAMETERS:
360  setParametersStr(value);
361  break;
363  shiftLaneIndex();
364  break;
365  default:
366  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
367  }
368 }
369 
370 
371 void
373  // change position
374  myPositionOverLane = moveResult.newFirstPos;
375  // set lateral offset
377  // update geometry
378  updateGeometry();
379 }
380 
381 
382 void
384  // reset lateral offset
386  // begin change attribute
387  undoList->begin(this, "position of " + getTagStr());
388  // set startPosition
389  setAttribute(SUMO_ATTR_POSITION, toString(moveResult.newFirstPos), undoList);
390  // check if lane has to be changed
391  if (moveResult.newFirstLane) {
392  // set new lane
393  setAttribute(SUMO_ATTR_LANE, moveResult.newFirstLane->getID(), undoList);
394  }
395  // end change attribute
396  undoList->end();
397 }
398 
399 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
@ GLO_E1DETECTOR
a E1 detector
GUIIcon
An enumeration of icons used by the gui applications.
Definition: GUIIcons.h:33
#define TL(string)
Definition: MsgHandler.h:315
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:46
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition: SUMOTime.cpp:69
#define SUMOTime_MAX_PERIOD
Definition: SUMOTime.h:36
@ SUMO_TAG_INDUCTION_LOOP
alternative tag for e1 detector
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_LANE
@ SUMO_ATTR_FILE
@ GNE_ATTR_SELECTED
element is selected
@ GNE_ATTR_PARAMETERS
parameters "key1=value1|key2=value2|...|keyN=valueN"
@ SUMO_ATTR_VTYPES
@ SUMO_ATTR_NAME
@ SUMO_ATTR_PERIOD
@ SUMO_ATTR_FRIENDLY_POS
@ SUMO_ATTR_ID
@ SUMO_ATTR_POSITION
@ GNE_ATTR_SHIFTLANEINDEX
shift lane index (only used by elements over lanes)
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
static void popMatrix()
pop matrix
Definition: GLHelper.cpp:130
static void pushMatrix()
push matrix
Definition: GLHelper.cpp:117
static bool checkLanePosition(double pos, const double length, const double laneLength, const bool friendlyPos)
check if the given position over a lane is valid
static void fixLanePosition(double &pos, double &length, const double laneLength)
fix given position over lane
void setAdditionalID(const std::string &newID)
set additional ID
GNEContour myAdditionalContour
variable used for draw additional contours
GUIGeometry myAdditionalGeometry
geometry to be precomputed in updateGeometry(...)
void drawAdditionalID(const GUIVisualizationSettings &s) const
draw additional ID
bool isValidDetectorID(const std::string &value) const
check if a new detector ID is valid
void replaceAdditionalParentLanes(const std::string &value)
replace additional parent lanes
void shiftLaneIndex()
shift lane index
double getExaggeration(const GUIVisualizationSettings &s) const
return exaggeration associated with this GLObject
std::string myAdditionalName
name of additional
void drawAdditionalName(const GUIVisualizationSettings &s) const
draw additional name
void drawParentChildLines(const GUIVisualizationSettings &s, const RGBColor &color, const bool onlySymbols=false) const
draw parent and child lines
const std::string getID() const
get ID (all Attribute Carriers have one)
bool isAttributeCarrierSelected() const
check if attribute carrier is selected
const std::string & getTagStr() const
get tag assigned to this object in string format
const GNETagProperties & getTagProperty() const
get tagProperty associated with this Attribute Carrier
void unselectAttributeCarrier(const bool changeFlag=true)
unselect attribute carrier using GUIGlobalSelection
bool drawUsingSelectColor() const
check if attribute carrier must be drawn using selecting color.
GNENet * myNet
pointer to net
void selectAttributeCarrier(const bool changeFlag=true)
select attribute carrier using GUIGlobalSelection
static void changeAttribute(GNEAttributeCarrier *AC, SumoXMLAttr key, const std::string &value, GNEUndoList *undoList, const bool force=false)
change attribute
void calculateContourRectangleShape(const GUIVisualizationSettings &s, const GUIVisualizationSettings::Detail d, const GUIGlObject *glObject, const Position &pos, const double width, const double height, const double offsetX, const double offsetY, const double rot, const double scale) const
calculate contour (for rectangled elements)
Definition: GNEContour.cpp:103
void drawDottedContours(const GUIVisualizationSettings &s, const GUIVisualizationSettings::Detail d, const GNEAttributeCarrier *AC, const double lineWidth, const bool addOffset) const
drawing contour functions
Definition: GNEContour.cpp:265
SUMOTime myPeriod
The aggregation period the values the detector collects shall be summed up.
Definition: GNEDetector.h:191
std::string myFilename
The path to the output file.
Definition: GNEDetector.h:194
double myPositionOverLane
position of detector over Lane
Definition: GNEDetector.h:188
double getGeometryPositionOverLane() const
get position over lane that is applicable to the shape
void drawE1Shape(const GUIVisualizationSettings::Detail d, const double exaggeration, const RGBColor &mainColor, const RGBColor &secondColor) const
draw E1 shape
bool myFriendlyPosition
Flag for friendly position.
Definition: GNEDetector.h:200
void updateCenteringBoundary(const bool updateGrid)
update centering boundary (implies change in RTREE)
std::vector< std::string > myVehicleTypes
attribute vehicle types
Definition: GNEDetector.h:197
void drawE1DetectorLogo(const GUIVisualizationSettings &s, const GUIVisualizationSettings::Detail d, const double exaggeration, const std::string &logo, const RGBColor &textColor) const
draw E1 detector Logo
const std::vector< GNELane * > & getParentLanes() const
get parent lanes
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their correspondent attribute are valids
void fixAdditionalProblem()
fix additional problem
void commitMoveShape(const GNEMoveResult &moveResult, GNEUndoList *undoList)
commit move shape
double getAttributeDouble(SumoXMLAttr key) const
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
GNEInductionLoopDetector(GNENet *net)
default constructor
void writeAdditional(OutputDevice &device) const
write additional element into a xml file
bool isAdditionalValid() const
check if current additional is valid to be written into XML
void setMoveShape(const GNEMoveResult &moveResult)
set move shape
void updateGeometry()
update pre-computed geometry information
std::string getAttribute(SumoXMLAttr key) const
std::string getAdditionalProblem() const
return a string with the current additional problem
bool checkDrawRelatedContour() const
check if draw related contour (cyan)
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:46
double myMoveElementLateralOffset
move element lateral offset (used by elements placed over lanes
move result
const GNELane * newFirstLane
new first Lane
double newFirstPos
new first position
double firstLaneOffset
lane offset
GNELane * retrieveLane(const std::string &id, bool hardFail=true, bool checkVolatileChange=false) const
get lane by id
A NBNetBuilder extended by visualisation and editing capabilities.
Definition: GNENet.h:42
GNENetHelper::AttributeCarriers * getAttributeCarriers() const
get all attribute carriers used in this net
Definition: GNENet.cpp:122
GNEViewNet * getViewNet() const
get view net
Definition: GNENet.cpp:2055
GNETLSEditorFrame::TLSAttributes * getTLSAttributes() const
get module for TLS attributes
void end()
End undo command sub-group. If the sub-group is still empty, it will be deleted; otherwise,...
void begin(GUIIcon icon, const std::string &description)
Begin undo command sub-group with current supermode. This begins a new group of commands that are tre...
const GNEViewNetHelper::DataViewOptions & getDataViewOptions() const
get data view options
Definition: GNEViewNet.cpp:727
void drawTranslateFrontAttributeCarrier(const GNEAttributeCarrier *AC, double typeOrLayer, const double extraOffset=0)
draw front attributeCarrier
GNEViewParent * getViewParent() const
get the net object
GNEUndoList * getUndoList() const
get the undoList object
bool selectingDetectorsTLSMode() const
check if we're selecting detectors in TLS mode
Definition: GNEViewNet.cpp:911
GNETLSEditorFrame * getTLSEditorFrame() const
get frame for NETWORK_TLS
const std::vector< double > & getShapeRotations() const
The rotations of the single shape parts.
const PositionVector & getShape() const
The shape of the additional element.
void updateGeometry(const PositionVector &shape)
update entire geometry
Definition: GUIGeometry.cpp:59
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
Definition: GUIGlObject.h:156
const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
Definition: GUIGlObject.h:143
Stores the information about how to visualize structures.
Detail getDetailLevel(const double exaggeration) const
return the detail level
GUIVisualizationColorSettings colorSettings
color settings
GUIVisualizationDottedContourSettings dottedContourSettings
dotted contour settings
bool drawForViewObjectsHandler
whether drawing is performed for the purpose of selecting objects in view using ViewObjectsHandler
GUIVisualizationAdditionalSettings additionalSettings
Additional settings.
GUIVisualizationDetectorSettings detectorSettings
Detector settings.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
static bool areParametersValid(const std::string &value, bool report=false, const std::string kvsep="=", const std::string sep="|")
check if given string can be parsed to a parameters map "key1=value1|key2=value2|....
std::map< std::string, std::string > Map
parameters map
Definition: Parameterised.h:45
void setParametersStr(const std::string &paramsString, const std::string kvsep="=", const std::string sep="|")
set the inner key/value map in string format "key1=value1|key2=value2|...|keyN=valueN"
void writeParams(OutputDevice &device) const
write Params in the given outputdevice
std::string getParametersStr(const std::string kvsep="=", const std::string sep="|") const
Returns the inner key/value map in string format "key1=value1|key2=value2|...|keyN=valueN".
Position getCentroid() const
Returns the centroid (closes the polygon if unclosed)
static const RGBColor WHITE
Definition: RGBColor.h:192
static const RGBColor BLACK
Definition: RGBColor.h:193
RGBColor changedBrightness(int change, int toChange=3) const
Returns a new color with altered brightness.
Definition: RGBColor.cpp:200
static bool isValidFilename(const std::string &value)
whether the given string is a valid attribute for a filename (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't allowed)
static bool isValidAttribute(const std::string &value)
whether the given string is a valid attribute for a certain key (for example, a name)
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.
bool showAdditionals() const
check if additionals has to be drawn
static void drawLockIcon(const GUIVisualizationSettings::Detail d, const GNEAttributeCarrier *AC, GUIGlObjectType type, const Position position, const double exaggeration, const double size=0.5, const double offsetx=0, const double offsety=0)
draw lock icon
static const RGBColor connectionColor
connection color
RGBColor selectedAdditionalColor
additional selection color (busStops, Detectors...)
static const RGBColor E1Color
color for E1 detectors
static const double segmentWidth
width of dotted contour segments