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-2022 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 #include <netedit/GNENet.h>
21 #include <netedit/GNEUndoList.h>
22 #include <netedit/GNEViewNet.h>
25 #include <utils/gui/div/GLHelper.h>
26 
27 #include "GNEDetectorE1.h"
28 #include "GNEAdditionalHandler.h"
29 
30 
31 // ===========================================================================
32 // member method definitions
33 // ===========================================================================
34 
36  GNEDetector("", net, GLO_E1DETECTOR, SUMO_TAG_E1DETECTOR, 0, 0, {}, "", {}, "", false, std::map<std::string, std::string>()) {
37  // reset default values
38  resetDefaultValues();
39 }
40 
41 
42 GNEDetectorE1::GNEDetectorE1(const std::string& id, GNELane* lane, GNENet* net, const double pos, const SUMOTime freq, const std::string& filename, const std::vector<std::string>& vehicleTypes,
43  const std::string& name, bool friendlyPos, const std::map<std::string, std::string>& parameters) :
44  GNEDetector(id, net, GLO_E1DETECTOR, SUMO_TAG_E1DETECTOR, pos, freq, {
45  lane
46 }, filename, vehicleTypes, name, friendlyPos, parameters) {
47  // update centering boundary without updating grid
48  updateCenteringBoundary(false);
49 }
50 
51 
53 }
54 
55 
56 void
58  device.openTag(getTagProperty().getTag());
59  device.writeAttr(SUMO_ATTR_ID, getID());
60  if (!myAdditionalName.empty()) {
62  }
63  device.writeAttr(SUMO_ATTR_LANE, getParentLanes().front()->getID());
66  if (myFilename.size() > 0) {
68  }
69  if (myVehicleTypes.size() > 0) {
71  }
72  if (myFriendlyPosition) {
73  device.writeAttr(SUMO_ATTR_FRIENDLY_POS, true);
74  }
75  // write parameters (Always after children to avoid problems with additionals.xsd)
76  writeParams(device);
77  device.closeTag();
78 }
79 
80 
81 bool
83  // with friendly position enabled position are "always fixed"
84  if (myFriendlyPosition) {
85  return true;
86  } else {
87  return fabs(myPositionOverLane) <= getParentLanes().front()->getParentEdge()->getNBEdge()->getFinalLength();
88  }
89 }
90 
91 
92 std::string
94  // obtain final lenght
95  const double len = getParentLanes().front()->getParentEdge()->getNBEdge()->getFinalLength();
96  // check if detector has a problem
98  return "";
99  } else {
100  // declare variable for error position
101  std::string errorPosition;
102  // check positions over lane
103  if (myPositionOverLane < 0) {
104  errorPosition = (toString(SUMO_ATTR_POSITION) + " < 0");
105  }
106  if (myPositionOverLane > len) {
107  errorPosition = (toString(SUMO_ATTR_POSITION) + " > lanes's length");
108  }
109  return errorPosition;
110  }
111 }
112 
113 
114 void
116  // declare new position
117  double newPositionOverLane = myPositionOverLane;
118  // fix pos and length checkAndFixDetectorPosition
119  GNEAdditionalHandler::fixSinglePositionOverLane(newPositionOverLane, getParentLanes().front()->getParentEdge()->getNBEdge()->getFinalLength());
120  // set new position
121  setAttribute(SUMO_ATTR_POSITION, toString(newPositionOverLane), myNet->getViewNet()->getUndoList());
122 }
123 
124 
125 void
127  // update geometry
129  // update centering boundary without updating grid
131 }
132 
133 
134 void
136  // Obtain exaggeration of the draw
137  const double E1Exaggeration = getExaggeration(s);
138  // first check if additional has to be drawn
140  // check exaggeration
141  if (s.drawAdditionals(E1Exaggeration)) {
142  // obtain scaledSize
143  const double scaledWidth = s.detectorSettings.E1Width * 0.5 * s.scale;
144  // declare colors
145  RGBColor mainColor, secondColor, textColor;
146  // set color
147  if (drawUsingSelectColor()) {
149  secondColor = mainColor.changedBrightness(-32);
150  textColor = mainColor.changedBrightness(32);
151  } else {
152  mainColor = s.detectorSettings.E1Color;
153  secondColor = RGBColor::WHITE;
154  textColor = RGBColor::BLACK;
155  }
156  // draw parent and child lines
158  // start drawing
160  // push layer matrix
162  // translate to front
164  // draw E1 shape
165  drawE1Shape(s, E1Exaggeration, scaledWidth, mainColor, secondColor);
166  // Check if the distance is enought to draw details
167  if (s.drawDetail(s.detailSettings.detectorDetails, E1Exaggeration)) {
168  // draw E1 Logo
169  drawDetectorLogo(s, E1Exaggeration, "E1", textColor);
170  }
171  // pop layer matrix
173  // Pop name
175  // draw lock icon
177  // check if dotted contours has to be drawn
180  }
181  if (myNet->getViewNet()->getFrontAttributeCarrier() == this) {
183  }
184  }
185  // Draw additional ID
186  drawAdditionalID(s);
187  // draw additional name
189  }
190 }
191 
192 
193 std::string
195  switch (key) {
196  case SUMO_ATTR_ID:
197  return getID();
198  case SUMO_ATTR_LANE:
199  return getParentLanes().front()->getID();
200  case SUMO_ATTR_POSITION:
202  case SUMO_ATTR_FREQUENCY:
203  return time2string(myFreq);
204  case SUMO_ATTR_NAME:
205  return myAdditionalName;
206  case SUMO_ATTR_FILE:
207  return myFilename;
208  case SUMO_ATTR_VTYPES:
209  return toString(myVehicleTypes);
212  case GNE_ATTR_SELECTED:
214  case GNE_ATTR_PARAMETERS:
215  return getParametersStr();
217  return "";
218  default:
219  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
220  }
221 }
222 
223 
224 double
226  switch (key) {
227  case SUMO_ATTR_POSITION:
228  return myPositionOverLane;
229  default:
230  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
231  }
232 }
233 
234 
235 void
236 GNEDetectorE1::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
237  switch (key) {
238  case SUMO_ATTR_ID:
239  case SUMO_ATTR_LANE:
240  case SUMO_ATTR_POSITION:
241  case SUMO_ATTR_FREQUENCY:
242  case SUMO_ATTR_NAME:
243  case SUMO_ATTR_FILE:
244  case SUMO_ATTR_VTYPES:
246  case GNE_ATTR_SELECTED:
247  case GNE_ATTR_PARAMETERS:
249  undoList->changeAttribute(new GNEChange_Attribute(this, key, value));
250  break;
251  default:
252  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
253  }
254 }
255 
256 
257 bool
258 GNEDetectorE1::isValid(SumoXMLAttr key, const std::string& value) {
259  switch (key) {
260  case SUMO_ATTR_ID:
261  return isValidDetectorID(value);
262  case SUMO_ATTR_LANE:
263  if (myNet->getAttributeCarriers()->retrieveLane(value, false) != nullptr) {
264  return true;
265  } else {
266  return false;
267  }
268  case SUMO_ATTR_POSITION:
269  return canParse<double>(value) && fabs(parse<double>(value)) < getParentLanes().front()->getParentEdge()->getNBEdge()->getFinalLength();
270  case SUMO_ATTR_FREQUENCY:
271  return (canParse<double>(value) && (parse<double>(value) >= 0));
272  case SUMO_ATTR_NAME:
274  case SUMO_ATTR_FILE:
276  case SUMO_ATTR_VTYPES:
277  if (value.empty()) {
278  return true;
279  } else {
281  }
283  return canParse<bool>(value);
284  case GNE_ATTR_SELECTED:
285  return canParse<bool>(value);
286  case GNE_ATTR_PARAMETERS:
287  return Parameterised::areParametersValid(value);
288  default:
289  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
290  }
291 }
292 
293 
294 bool
296  return true;
297 }
298 
299 // ===========================================================================
300 // private
301 // ===========================================================================
302 
303 void
304 GNEDetectorE1::setAttribute(SumoXMLAttr key, const std::string& value) {
305  switch (key) {
306  case SUMO_ATTR_ID:
307  // update microsimID
308  setMicrosimID(value);
309  break;
310  case SUMO_ATTR_LANE:
312  break;
313  case SUMO_ATTR_POSITION:
314  myPositionOverLane = parse<double>(value);
315  break;
316  case SUMO_ATTR_FREQUENCY:
317  myFreq = string2time(value);
318  break;
319  case SUMO_ATTR_FILE:
320  myFilename = value;
321  break;
322  case SUMO_ATTR_NAME:
323  myAdditionalName = value;
324  break;
325  case SUMO_ATTR_VTYPES:
326  myVehicleTypes = parse<std::vector<std::string> >(value);
327  break;
329  myFriendlyPosition = parse<bool>(value);
330  break;
331  case GNE_ATTR_SELECTED:
332  if (parse<bool>(value)) {
334  } else {
336  }
337  break;
338  case GNE_ATTR_PARAMETERS:
339  setParametersStr(value);
340  break;
342  shiftLaneIndex();
343  break;
344  default:
345  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
346  }
347 }
348 
349 
350 void
352  // change position
353  myPositionOverLane = moveResult.newFirstPos;
354  // set lateral offset
356  // update geometry
357  updateGeometry();
358 }
359 
360 
361 void
363  // reset lateral offset
365  // begin change attribute
366  undoList->begin(myTagProperty.getGUIIcon(), "position of " + getTagStr());
367  // set startPosition
368  setAttribute(SUMO_ATTR_POSITION, toString(moveResult.newFirstPos), undoList);
369  // check if lane has to be changed
370  if (moveResult.newFirstLane) {
371  // set new lane
372  setAttribute(SUMO_ATTR_LANE, moveResult.newFirstLane->getID(), undoList);
373  }
374  // end change attribute
375  undoList->end();
376 }
377 
378 /****************************************************************************/
@ GLO_E1DETECTOR
a E1 detector
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
long long int SUMOTime
Definition: SUMOTime.h:32
@ SUMO_TAG_E1DETECTOR
an 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_FREQUENCY
@ 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 pushName(unsigned int name)
push Name
Definition: GLHelper.cpp:132
static void popMatrix()
pop matrix
Definition: GLHelper.cpp:123
static void popName()
pop Name
Definition: GLHelper.cpp:141
static void pushMatrix()
push matrix
Definition: GLHelper.cpp:114
static bool checkSinglePositionOverLane(double pos, const double laneLength, const bool friendlyPos)
check if the given position over a lane is valid
static void fixSinglePositionOverLane(double &pos, const double laneLength)
fix given position over lane
const std::string & getID() const
get ID
GUIGeometry myAdditionalGeometry
geometry to be precomputed in updateGeometry(...)
void drawAdditionalID(const GUIVisualizationSettings &s) const
draw additional ID
void replaceAdditionalParentLanes(const std::string &value)
replace additional parent lanes
void shiftLaneIndex()
shift lane index
double getExaggeration(const GUIVisualizationSettings &s) const
return exaggeration asociated with this GLObject
std::string myAdditionalName
name of additional
void drawAdditionalName(const GUIVisualizationSettings &s) const
draw additional name
bool isValidDetectorID(const std::string &newID) const
check if a new detector ID is valid
void drawParentChildLines(const GUIVisualizationSettings &s, const RGBColor &color, const bool onlySymbols=false) const
draw parent and child lines
bool isAttributeCarrierSelected() const
check if attribute carrier is selected
friend class GNEChange_Attribute
declare friend class
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
const GNETagProperties & myTagProperty
reference to tagProperty associated with this attribute carrier
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
bool isAttributeEnabled(SumoXMLAttr key) const
void updateGeometry()
update pre-computed geometry information
std::string getAdditionalProblem() const
return a string with the current additional problem
void fixAdditionalProblem()
fix additional problem
void setMoveShape(const GNEMoveResult &moveResult)
set move shape
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes
void commitMoveShape(const GNEMoveResult &moveResult, GNEUndoList *undoList)
commit move shape
bool isAdditionalValid() const
check if current additional is valid to be writed into XML
~GNEDetectorE1()
Destructor.
std::string getAttribute(SumoXMLAttr key) const
double getAttributeDouble(SumoXMLAttr key) const
GNEDetectorE1(GNENet *net)
default constructor
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
void writeAdditional(OutputDevice &device) const
writte additional element into a xml file
std::string myFilename
The path to the output file.
Definition: GNEDetector.h:180
double myPositionOverLane
position of detector over Lane
Definition: GNEDetector.h:174
SUMOTime myFreq
The aggregation period the values the detector collects shall be summed up.
Definition: GNEDetector.h:177
void drawE1Shape(const GUIVisualizationSettings &s, const double exaggeration, const double scaledWidth, const RGBColor &mainColor, const RGBColor &secondColor) const
draw E1 shape
double getGeometryPositionOverLane() const
get position over lane that is applicable to the shape
bool myFriendlyPosition
Flag for friendly position.
Definition: GNEDetector.h:186
void drawDetectorLogo(const GUIVisualizationSettings &s, const double exaggeration, const std::string &logo, const RGBColor &textColor) const
draw detector Logo
void updateCenteringBoundary(const bool updateGrid)
update centering boundary (implies change in RTREE)
std::vector< std::string > myVehicleTypes
attribute vehicle types
Definition: GNEDetector.h:183
const std::vector< GNELane * > & getParentLanes() const
get parent lanes
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:125
GNEViewNet * getViewNet() const
get view net
Definition: GNENet.cpp:1964
const std::string & getID() const
get ID
GUIIcon getGUIIcon() const
get GUI icon associated to this Tag
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...
void changeAttribute(GNEChange_Attribute *change)
special method for change attributes, avoid empty changes, always execute
const GNEViewNetHelper::DataViewOptions & getDataViewOptions() const
get data view options
Definition: GNEViewNet.cpp:537
const GNEAttributeCarrier * getFrontAttributeCarrier() const
get front attributeCarrier
void drawTranslateFrontAttributeCarrier(const GNEAttributeCarrier *AC, double typeOrLayer, const double extraOffset=0)
draw front attributeCarrier
GNEUndoList * getUndoList() const
get the undoList object
bool isAttributeCarrierInspected(const GNEAttributeCarrier *AC) const
check if attribute carrier is being inspected
static void drawDottedSquaredShape(const DottedContourType type, const GUIVisualizationSettings &s, const Position &pos, const double width, const double height, const double offsetX, const double offsetY, const double rot, const double exaggeration)
draw dotted squared contour (used by additionals and demand elements)
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:58
virtual void setMicrosimID(const std::string &newID)
Changes the microsimID of the object.
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
GUIGlID getGlID() const
Returns the numerical id of the object.
Stores the information about how to visualize structures.
GUIVisualizationDetailSettings detailSettings
detail settings
bool drawAdditionals(const double exaggeration) const
check if additionals must be drawn
GUIVisualizationColorSettings colorSettings
color settings
double scale
information about a lane's width (temporary, used for a single view)
bool drawDetail(const double detail, const double exaggeration) const
check if details can be drawn for the given GUIVisualizationDetailSettings and current scale and exxa...
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:248
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|....
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:197
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 GNEAttributeCarrier *AC, GUIGlObjectType type, const Position viewPosition, 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 double detectorDetails
details for detectors
static const RGBColor E1Color
color for E1 detectors
static const double E1Width
E1 detector widths.