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-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 "GNEDetectorE1Instant.h"
28 #include "GNEAdditionalHandler.h"
29 
30 
31 // ===========================================================================
32 // member method definitions
33 // ===========================================================================
34 
36  GNEDetector("", net, GLO_E1DETECTOR_INSTANT, SUMO_TAG_INSTANT_INDUCTION_LOOP, 0, 0, {}, "", {}, "", false, std::map<std::string, std::string>()) {
37  // reset default values
38  resetDefaultValues();
39 }
40 
41 
42 GNEDetectorE1Instant::GNEDetectorE1Instant(const std::string& id, GNELane* lane, GNENet* net, const double pos, const std::string& filename, const std::vector<std::string>& vehicleTypes,
43  const std::string& name, const bool friendlyPos, const std::map<std::string, std::string>& parameters) :
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());
65  if (myFilename.size() > 0) {
67  }
68  if (myVehicleTypes.size() > 0) {
70  }
71  if (myFriendlyPosition) {
72  device.writeAttr(SUMO_ATTR_FRIENDLY_POS, true);
73  }
74  // write parameters (Always after children to avoid problems with additionals.xsd)
75  writeParams(device);
76  device.closeTag();
77 }
78 
79 
80 bool
82  // with friendly position enabled position are "always fixed"
83  if (myFriendlyPosition) {
84  return true;
85  } else {
86  return fabs(myPositionOverLane) <= getParentLanes().front()->getParentEdge()->getNBEdge()->getFinalLength();
87  }
88 }
89 
90 
91 std::string
93  // obtain final lenght
94  const double len = getParentLanes().front()->getParentEdge()->getNBEdge()->getFinalLength();
95  // check if detector has a problem
97  return "";
98  } else {
99  // declare variable for error position
100  std::string errorPosition;
101  // check positions over lane
102  if (myPositionOverLane < 0) {
103  errorPosition = (toString(SUMO_ATTR_POSITION) + " < 0");
104  }
105  if (myPositionOverLane > len) {
106  errorPosition = (toString(SUMO_ATTR_POSITION) + " > lanes's length");
107  }
108  return errorPosition;
109  }
110 }
111 
112 
113 void
115  // declare new position
116  double newPositionOverLane = myPositionOverLane;
117  // fix pos and length checkAndFixDetectorPosition
118  GNEAdditionalHandler::fixSinglePositionOverLane(newPositionOverLane, getParentLanes().front()->getParentEdge()->getNBEdge()->getFinalLength());
119  // set new position
120  setAttribute(SUMO_ATTR_POSITION, toString(newPositionOverLane), myNet->getViewNet()->getUndoList());
121 }
122 
123 
124 void
126  // update geometry
128  // update centering boundary without updating grid
130 }
131 
132 
133 void
135  // Obtain exaggeration of the draw
136  const double E1InstantExaggeration = getExaggeration(s);
137  // first check if additional has to be drawn
139  // check exaggeration
140  if (s.drawAdditionals(E1InstantExaggeration)) {
141  // obtain scaledSize
142  const double scaledWidth = s.detectorSettings.E1InstantWidth * 0.5 * s.scale;
143  // declare colors
144  RGBColor mainColor, secondColor, textColor;
145  // set color
146  if (drawUsingSelectColor()) {
148  secondColor = mainColor.changedBrightness(-32);
149  textColor = mainColor.changedBrightness(32);
150  } else {
151  mainColor = s.detectorSettings.E1InstantColor;
152  secondColor = RGBColor::WHITE;
153  textColor = RGBColor::BLACK;
154  }
155  // draw parent and child lines
157  // start drawing
159  // push layer matrix
161  // translate to front
163  // draw E1Instant shape
164  drawE1Shape(s, E1InstantExaggeration, scaledWidth, mainColor, secondColor);
165  // Check if the distance is enought to draw details
166  if (s.drawDetail(s.detailSettings.detectorDetails, E1InstantExaggeration)) {
167  // draw E1 Logo
168  drawDetectorLogo(s, E1InstantExaggeration, "E1", textColor);
169  }
170  // pop layer matrix
172  // Pop name
174  // draw lock icon
176  // check if dotted contours has to be drawn
179  }
180  if (myNet->getViewNet()->getFrontAttributeCarrier() == this) {
182  }
183  }
184  // Draw additional ID
185  drawAdditionalID(s);
186  // draw additional name
188  }
189 }
190 
191 
192 std::string
194  switch (key) {
195  case SUMO_ATTR_ID:
196  return getID();
197  case SUMO_ATTR_LANE:
198  return getParentLanes().front()->getID();
199  case SUMO_ATTR_POSITION:
201  case SUMO_ATTR_NAME:
202  return myAdditionalName;
203  case SUMO_ATTR_FILE:
204  return myFilename;
205  case SUMO_ATTR_VTYPES:
206  return toString(myVehicleTypes);
209  case GNE_ATTR_SELECTED:
211  case GNE_ATTR_PARAMETERS:
212  return getParametersStr();
214  return "";
215  default:
216  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
217  }
218 }
219 
220 
221 double
223  switch (key) {
224  case SUMO_ATTR_POSITION:
225  return myPositionOverLane;
226  default:
227  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
228  }
229 }
230 
231 
232 void
233 GNEDetectorE1Instant::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
234  switch (key) {
235  case SUMO_ATTR_ID:
236  case SUMO_ATTR_LANE:
237  case SUMO_ATTR_POSITION:
238  case SUMO_ATTR_NAME:
239  case SUMO_ATTR_FILE:
240  case SUMO_ATTR_VTYPES:
242  case GNE_ATTR_SELECTED:
243  case GNE_ATTR_PARAMETERS:
245  undoList->changeAttribute(new GNEChange_Attribute(this, key, value));
246  break;
247  default:
248  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
249  }
250 
251 }
252 
253 
254 bool
255 GNEDetectorE1Instant::isValid(SumoXMLAttr key, const std::string& value) {
256  switch (key) {
257  case SUMO_ATTR_ID:
258  return isValidDetectorID(value);
259  case SUMO_ATTR_LANE:
260  if (myNet->getAttributeCarriers()->retrieveLane(value, false) != nullptr) {
261  return true;
262  } else {
263  return false;
264  }
265  case SUMO_ATTR_POSITION:
266  return canParse<double>(value) && fabs(parse<double>(value)) < getParentLanes().front()->getParentEdge()->getNBEdge()->getFinalLength();
267  case SUMO_ATTR_NAME:
269  case SUMO_ATTR_FILE:
271  case SUMO_ATTR_VTYPES:
272  if (value.empty()) {
273  return true;
274  } else {
276  }
278  return canParse<bool>(value);
279  case GNE_ATTR_SELECTED:
280  return canParse<bool>(value);
281  case GNE_ATTR_PARAMETERS:
282  return Parameterised::areParametersValid(value);
283  default:
284  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
285  }
286 }
287 
288 
289 bool
291  return true;
292 }
293 
294 // ===========================================================================
295 // private
296 // ===========================================================================
297 
298 void
299 GNEDetectorE1Instant::setAttribute(SumoXMLAttr key, const std::string& value) {
300  switch (key) {
301  case SUMO_ATTR_ID:
302  // update microsimID
303  setMicrosimID(value);
304  break;
305  case SUMO_ATTR_LANE:
307  break;
308  case SUMO_ATTR_POSITION:
309  myPositionOverLane = parse<double>(value);
310  break;
311  case SUMO_ATTR_NAME:
312  myAdditionalName = value;
313  break;
314  case SUMO_ATTR_FILE:
315  myFilename = value;
316  break;
317  case SUMO_ATTR_VTYPES:
318  myVehicleTypes = parse<std::vector<std::string> >(value);
319  break;
321  myFriendlyPosition = parse<bool>(value);
322  break;
323  case GNE_ATTR_SELECTED:
324  if (parse<bool>(value)) {
326  } else {
328  }
329  break;
330  case GNE_ATTR_PARAMETERS:
331  setParametersStr(value);
332  break;
334  shiftLaneIndex();
335  break;
336  default:
337  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
338  }
339 }
340 
341 
342 void
344  // change position
345  myPositionOverLane = moveResult.newFirstPos;
346  // set lateral offset
348  // update geometry
349  updateGeometry();
350 }
351 
352 
353 void
355  // reset lateral offset
357  // begin change attribute
358  undoList->begin(myTagProperty.getGUIIcon(), "position of " + getTagStr());
359  // set startPosition
360  setAttribute(SUMO_ATTR_POSITION, toString(moveResult.newFirstPos), undoList);
361  // check if lane has to be changed
362  if (moveResult.newFirstLane) {
363  // set new lane
364  setAttribute(SUMO_ATTR_LANE, moveResult.newFirstLane->getID(), undoList);
365  }
366  // end change attribute
367  undoList->end();
368 }
369 
370 /****************************************************************************/
@ GLO_E1DETECTOR_INSTANT
a E1 detector
@ SUMO_TAG_INSTANT_INDUCTION_LOOP
An instantenous induction loop.
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_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
std::string getAdditionalProblem() const
return a string with the current additional problem
std::string getAttribute(SumoXMLAttr key) const
void writeAdditional(OutputDevice &device) const
writte additional element into a xml file
GNEDetectorE1Instant(GNENet *net)
default Constructor
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
void setMoveShape(const GNEMoveResult &moveResult)
set move shape
void updateGeometry()
update pre-computed geometry information
double getAttributeDouble(SumoXMLAttr key) const
void commitMoveShape(const GNEMoveResult &moveResult, GNEUndoList *undoList)
commit move shape
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform additional changes
bool isAttributeEnabled(SumoXMLAttr key) const
bool isAdditionalValid() const
check if current additional is valid to be writed into XML
void fixAdditionalProblem()
fix additional problem
std::string myFilename
The path to the output file.
Definition: GNEDetector.h:180
double myPositionOverLane
position of detector over Lane
Definition: GNEDetector.h:174
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 double E1InstantWidth
E1 Instant detector widths.
static const RGBColor E1InstantColor
color for E1 Instant detectors