Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GNEDetector.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-2025 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>
23#include <netedit/GNESegment.h>
26
27#include "GNEDetector.h"
28
29// ===========================================================================
30// member method definitions
31// ===========================================================================
32
34 GNEAdditional("", net, "", tag, "") {
35}
36
37
38GNEDetector::GNEDetector(const std::string& id, GNENet* net, const std::string& filename, SumoXMLTag tag,
39 const SUMOTime period, const std::string& outputFilename, const std::vector<std::string>& vehicleTypes,
40 const std::vector<std::string>& nextEdges, const std::string& detectPersons, const std::string& name,
41 const Parameterised::Map& parameters) :
42 GNEAdditional(id, net, filename, tag, name),
43 Parameterised(parameters),
44 myPeriod(period),
45 myOutputFilename(outputFilename),
46 myVehicleTypes(vehicleTypes),
47 myNextEdges(nextEdges),
48 myDetectPersons(detectPersons) {
49 // update output filename
50 if (outputFilename.empty()) {
51 myOutputFilename = id + ".xml";
52 }
53}
54
55
56GNEDetector::GNEDetector(GNEAdditional* additionalParent, SumoXMLTag tag, const SUMOTime period,
57 const std::string& outputFilename, const std::string& name,
58 const Parameterised::Map& parameters) :
59 GNEAdditional(additionalParent, tag, name),
60 Parameterised(parameters),
61 myPeriod(period),
62 myOutputFilename(outputFilename) {
63 // set parents
64 setParent<GNEAdditional*>(additionalParent);
65}
66
67
69
70
73 return this;
74}
75
76
77const Parameterised*
79 return this;
80}
81
82
83bool
85 // get edit modes
86 const auto& editModes = myNet->getViewNet()->getEditModes();
87 // check if we're in move mode
88 if (!myNet->getViewNet()->isCurrentlyMovingElements() && editModes.isCurrentSupermodeNetwork() &&
90 (editModes.networkEditMode == NetworkEditMode::NETWORK_MOVE) && myNet->getViewNet()->checkOverLockedElement(this, mySelected)) {
91 // only move the first element
93 // special case for multiple lane area detectors
95 auto segment = gViewObjectsHandler.getSelectedSegment(this);
96 if (segment && segment->getJunction()) {
97 return false;
98 } else if (segment && segment->getLane()) {
99 // ensure that is the first or the last lane
100 if (segment->getLaneIndex() == 0) {
101 return true;
102 } else if (segment->getLaneIndex() == ((int)getParentLanes().size() - 1)) {
103 return true;
104 }
105 } else {
106 // this is the start or end point
107 return true;
108 }
109 } else {
110 return true;
111 }
112 }
113 }
114 return false;
115}
116
117
122
123
124void
125GNEDetector::updateCenteringBoundary(const bool /*updateGrid*/) {
126 // nothing to update
127}
128
129
130void
131GNEDetector::splitEdgeGeometry(const double splitPosition, const GNENetworkElement* originalElement,
132 const GNENetworkElement* newElement, GNEUndoList* undoList) {
133 // only split geometry of E2 multilane detectors
135 // obtain new list of E2 lanes
136 std::string newE2Lanes = getNewListOfParents(originalElement, newElement);
137 // update E2 Lanes
138 if (newE2Lanes.size() > 0) {
139 setAttribute(SUMO_ATTR_LANES, newE2Lanes, undoList);
140 }
141 } else if (splitPosition < getAttributeDouble(SUMO_ATTR_POSITION)) {
142 // change lane
143 setAttribute(SUMO_ATTR_LANE, newElement->getID(), undoList);
144 // now adjust start position
146 }
147}
148
149
150std::string
152 return getParentLanes().front()->getID();
153}
154
155
159
160
161std::string
163 return getTagStr() + ": " + getID();
164}
165
166
167std::string
169 return getTagStr();
170}
171
172
173std::string
175 switch (key) {
176 case SUMO_ATTR_ID:
177 return getMicrosimID();
178 case SUMO_ATTR_PERIOD:
180 return "";
181 } else {
182 return time2string(myPeriod);
183 }
184 case SUMO_ATTR_NAME:
185 return myAdditionalName;
186 case SUMO_ATTR_FILE:
187 return myOutputFilename;
188 case SUMO_ATTR_VTYPES:
189 return toString(myVehicleTypes);
191 return toString(myNextEdges);
195 return "";
196 default:
197 return getMoveElement()->getMovingAttribute(key);
198 }
199}
200
201
202double
206
207
212
213
214void
215GNEDetector::setDetectorAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
216 switch (key) {
217 case SUMO_ATTR_ID:
218 case SUMO_ATTR_PERIOD:
219 case SUMO_ATTR_NAME:
220 case SUMO_ATTR_FILE:
221 case SUMO_ATTR_VTYPES:
225 GNEChange_Attribute::changeAttribute(this, key, value, undoList);
226 break;
227 default:
228 getMoveElement()->setMovingAttribute(key, value, undoList);
229 break;
230 }
231}
232
233
234
235bool
236GNEDetector::isDetectorValid(SumoXMLAttr key, const std::string& value) {
237 switch (key) {
238 case SUMO_ATTR_ID:
239 return isValidDetectorID(value);
240 case SUMO_ATTR_PERIOD:
241 if (value.empty()) {
242 return true;
243 } else {
244 return (canParse<double>(value) && (parse<double>(value) >= 0));
245 }
246 case SUMO_ATTR_NAME:
248 case SUMO_ATTR_FILE:
250 case SUMO_ATTR_VTYPES:
251 if (value.empty()) {
252 return true;
253 } else {
255 }
257 if (value.empty()) {
258 return true;
259 } else {
261 }
263 if (value.empty()) {
264 return true;
265 } else {
267 }
268 default:
269 return getMoveElement()->isMovingAttributeValid(key, value);
270 }
271}
272
273
274void
292
293
294void
295GNEDetector::setDetectorAttribute(SumoXMLAttr key, const std::string& value) {
296 switch (key) {
297 case SUMO_ATTR_ID:
298 // update microsimID
299 setAdditionalID(value);
300 break;
301 case SUMO_ATTR_PERIOD:
302 if (value.empty()) {
304 } else {
305 myPeriod = string2time(value);
306 }
307 break;
308 case SUMO_ATTR_FILE:
309 myOutputFilename = value;
310 break;
311 case SUMO_ATTR_NAME:
312 myAdditionalName = value;
313 break;
314 case SUMO_ATTR_VTYPES:
315 myVehicleTypes = parse<std::vector<std::string> >(value);
316 break;
318 myNextEdges = parse<std::vector<std::string> >(value);
319 break;
321 myDetectPersons = value;
322 break;
325 break;
326 default:
327 getMoveElement()->setMovingAttribute(key, value);
328 break;
329 }
330}
331
332
333void
335 const RGBColor& mainColor, const RGBColor& secondColor) const {
336 // push matrix
338 // set line width
339 glLineWidth(1.0);
340 // translate to center geometry
341 glTranslated(myAdditionalGeometry.getShape().front().x(), myAdditionalGeometry.getShape().front().y(), 0);
342 // rotate over lane
344 // scale
345 glScaled(exaggeration, exaggeration, 1);
346 // set main color
347 GLHelper::setColor(mainColor);
348 // begin draw square
349 glBegin(GL_QUADS);
350 // draw square
351 glVertex2d(-1.0, 2);
352 glVertex2d(-1.0, -2);
353 glVertex2d(1.0, -2);
354 glVertex2d(1.0, 2);
355 // end draw square
356 glEnd();
357 // move top
358 glTranslated(0, 0, .01);
359 // begin draw line
360 glBegin(GL_LINES);
361 // draw lines
362 glVertex2d(0, 2 - .1);
363 glVertex2d(0, -2 + .1);
364 // end draw line
365 glEnd();
366 // draw center only in draw in level 2
368 // set main color
369 GLHelper::setColor(secondColor);
370 // set polygon mode
371 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
372 // begin draw square
373 glBegin(GL_QUADS);
374 // draw square
375 glVertex2f(-1.0, 2);
376 glVertex2f(-1.0, -2);
377 glVertex2f(1.0, -2);
378 glVertex2f(1.0, 2);
379 // end draw square
380 glEnd();
381 // rotate 90 degrees
382 glRotated(90, 0, 0, -1);
383 //set polygon mode
384 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
385 // begin draw line
386 glBegin(GL_LINES);
387 // draw line
388 glVertex2d(0, 1.7);
389 glVertex2d(0, -1.7);
390 // end draw line
391 glEnd();
392 //arrow
393 glTranslated(2, 0, 0);
394 GLHelper::setColor(mainColor);
395 GLHelper::drawTriangleAtEnd(Position(0, 0), Position(0.5, 0), (double) 0.5, (double) 1);
396 }
397 // pop matrix
399}
400
401
402void
404 const double exaggeration, const std::string& logo, const RGBColor& textColor) const {
405 // only draw in level 2
407 // calculate position
408 const Position pos = myAdditionalGeometry.getShape().front();
409 // calculate rotation
410 const double rot = s.getTextAngle(myAdditionalGeometry.getShapeRotations().front() + 90);
411 // Start pushing matrix
413 // Traslate to position
414 glTranslated(pos.x(), pos.y(), 0.1);
415 // scale text
416 glScaled(exaggeration, exaggeration, 1);
417 // draw E1 logo
418 GLHelper::drawText(logo + " ", Position(), .1, 1.5, textColor, rot);
419 // pop matrix
421 }
422}
423
424
425void
427 const double exaggeration, const std::string& logo, const RGBColor& textColor) const {
428 // only draw in level 2
430 // calculate middle point
431 const double middlePoint = (myAdditionalGeometry.getShape().length2D() * 0.5);
432 // calculate position
433 const Position pos = myAdditionalGeometry.getShape().positionAtOffset2D(middlePoint);
434 // calculate rotation
435 const double rot = s.getTextAngle(myAdditionalGeometry.getShape().rotationDegreeAtOffset(middlePoint) + 90);
436 // Start pushing matrix
438 // Traslate to position
439 glTranslated(pos.x(), pos.y(), 0.1);
440 // scale text
441 glScaled(exaggeration, exaggeration, 1);
442 // draw E1 logo
443 GLHelper::drawText(logo, Position(), .1, 1.5, textColor, rot);
444 // pop matrix
446 }
447}
448
449/****************************************************************************/
@ NETWORK_MOVE
mode for moving network elements
long long int SUMOTime
Definition GUI.h:36
GUIViewObjectsHandler gViewObjectsHandler
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:91
#define SUMOTime_MAX_PERIOD
Definition SUMOTime.h:36
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ GNE_TAG_MULTI_LANE_AREA_DETECTOR
an e2 detector over multiple lanes (placed here due create Additional Frame)
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_LANE
@ SUMO_ATTR_NEXT_EDGES
@ SUMO_ATTR_FILE
@ SUMO_ATTR_LANES
@ SUMO_ATTR_VTYPES
@ SUMO_ATTR_NAME
@ SUMO_ATTR_PERIOD
@ SUMO_ATTR_ID
@ SUMO_ATTR_POSITION
@ GNE_ATTR_SHIFTLANEINDEX
shift lane index (only used by elements over lanes)
@ SUMO_ATTR_DETECT_PERSONS
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition GLHelper.cpp:649
static void drawTriangleAtEnd(const Position &p1, const Position &p2, double tLength, double tWidth, const double extraOffset=0)
Draws a triangle at the end of the given line.
Definition GLHelper.cpp:624
static void popMatrix()
pop matrix
Definition GLHelper.cpp:131
static void pushMatrix()
push matrix
Definition GLHelper.cpp:118
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, const int align=0, double width=-1)
Definition GLHelper.cpp:742
void setAdditionalID(const std::string &newID)
set additional ID
GUIGeometry myAdditionalGeometry
geometry to be precomputed in updateGeometry(...)
bool isValidDetectorID(const std::string &value) const
check if a new detector ID is valid
void shiftLaneIndex()
shift lane index
std::string myAdditionalName
name of additional
virtual double getAttributeDouble(SumoXMLAttr key) const =0
const std::string getID() const
get ID (all Attribute Carriers have one)
bool mySelected
boolean to check if this AC is selected (more quickly as checking GUIGlObjectStorage)
virtual GNEMoveElement * getMoveElement() const =0
get GNEMoveElement associated with this AttributeCarrier
PositionVector getCommonAttributePositionVector(SumoXMLAttr key) const
const std::string & getTagStr() const
get tag assigned to this object in string format
GNENet * myNet
pointer to net
virtual void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)=0
const GNETagProperties * myTagProperty
reference to tagProperty associated with this attribute carrier
static void changeAttribute(GNEAttributeCarrier *AC, SumoXMLAttr key, const std::string &value, GNEUndoList *undoList, const bool force=false)
change attribute
SUMOTime myPeriod
The aggregation period the values the detector collects shall be summed up.
std::string getPopUpID() const override
get PopPup ID (Used in AC Hierarchy)
void drawE2DetectorLogo(const GUIVisualizationSettings &s, const GUIVisualizationSettings::Detail d, const double exaggeration, const std::string &logo, const RGBColor &textColor) const
draw E2 detector Logo
Position getPositionInView() const
Returns position of additional in view.
Position getDetectorAttributePosition(SumoXMLAttr key) const
std::string myOutputFilename
The path to the output file.
bool checkDrawMoveContour() const override
check if draw move contour (red)
~GNEDetector()
Destructor.
bool isDetectorValid(SumoXMLAttr key, const std::string &value)
Parameterised * getParameters() override
methods to retrieve the elements linked to this detector
void setDetectorAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
std::vector< std::string > myNextEdges
next edges
PositionVector getAttributePositionVector(SumoXMLAttr key) const override
void drawE1Shape(const GUIVisualizationSettings::Detail d, const double exaggeration, const RGBColor &mainColor, const RGBColor &secondColor) const
draw E1 shape
double getDetectorAttributeDouble(SumoXMLAttr key) const
GNEDetector(GNENet *net, SumoXMLTag tag)
Default constructor.
void writeDetectorValues(OutputDevice &device) const
write additional element into a xml file
void splitEdgeGeometry(const double splitPosition, const GNENetworkElement *originalElement, const GNENetworkElement *newElement, GNEUndoList *undoList)
split geometry
void updateCenteringBoundary(const bool updateGrid)
update centering boundary (implies change in RTREE)
std::string getHierarchyName() const override
get Hierarchy Name (Used in AC Hierarchy)
std::string getDetectorAttribute(SumoXMLAttr key) const
std::vector< std::string > myVehicleTypes
attribute vehicle types
void drawE1DetectorLogo(const GUIVisualizationSettings &s, const GUIVisualizationSettings::Detail d, const double exaggeration, const std::string &logo, const RGBColor &textColor) const
draw E1 detector Logo
std::string myDetectPersons
detect persons
std::string getParentName() const
Returns the name of the parent object.
const GNEHierarchicalContainerParents< GNELane * > & getParentLanes() const
get parent lanes
std::string getNewListOfParents(const GNENetworkElement *currentElement, const GNENetworkElement *newNextElement) const
if use edge/parent lanes as a list of consecutive elements, obtain a list of IDs of elements after in...
virtual double getMovingAttributeDouble(SumoXMLAttr key) const =0
get moving attribute double
virtual std::string getMovingAttribute(SumoXMLAttr key) const =0
get moving attribute
virtual void setMovingAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)=0
set moving attribute (using undo-list)
virtual bool isMovingAttributeValid(SumoXMLAttr key, const std::string &value) const =0
check if the given moving attribute is valid
virtual Position getMovingAttributePosition(SumoXMLAttr key) const =0
get moving attribute position
A NBNetBuilder extended by visualisation and editing capabilities.
Definition GNENet.h:42
GNEViewNet * getViewNet() const
get view net
Definition GNENet.cpp:2193
SumoXMLTag getTag() const
get Tag vinculated with this attribute Property
const GUIGlObject * getGUIGlObjectFront() const
get front GUIGLObject or a pointer to nullptr
bool isCurrentlyMovingElements() const
check if an element is being moved
const GNEViewNetHelper::EditModes & getEditModes() const
get edit modes
const GNEViewNetHelper::EditNetworkElementShapes & getEditNetworkElementShapes() const
get Edit Shape module
bool checkOverLockedElement(const GUIGlObject *GLObject, const bool isSelected) const
check if given element is locked (used for drawing select and delete contour)
const GNEViewNetHelper::ViewObjectsSelector & getViewObjectsSelector() const
get objects under cursor
static void rotateOverLane(const double rot)
rotate over lane (used by Lock icons, detector logos, etc.)
const std::vector< double > & getShapeRotations() const
The rotations of the single shape parts.
const PositionVector & getShape() const
The shape of the additional element.
const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
const GNESegment * getSelectedSegment(const GUIGlObject *GLObject) const
get segment associated with the given GLObject (if exist)
Stores the information about how to visualize structures.
double getTextAngle(double objectAngle) const
return an angle that is suitable for reading text aligned with the given angle (degrees)
Static storage of an output device and its base (abstract) implementation.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
An upper class for objects with additional parameters.
std::map< std::string, std::string > Map
parameters map
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
double x() const
Returns the x-position.
Definition Position.h:52
double y() const
Returns the y-position.
Definition Position.h:57
A list of positions.
double length2D() const
Returns the length.
Position getPolygonCenter() const
Returns the arithmetic of all corner points.
double rotationDegreeAtOffset(double pos) const
Returns the rotation at the given length.
Position positionAtOffset2D(double pos, double lateralOffset=0, bool extrapolateBeyond=false) const
Returns the position at the given length.
static StringBijection< PersonMode > PersonModeValues
person modes
static bool isValidListOfNetIDs(const std::string &value)
whether the given string is a valid list of id for a network (empty aren't allowed)
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)
const std::string & getString(const T key) const
get string
bool hasString(const std::string &str) const
check if the given string exist
GNENetworkElement * getEditedNetworkElement() const
pointer to edited network element