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#include <config.h>
21
22#include <netedit/GNENet.h>
24#include <netedit/GNESegment.h>
27
28#include "GNEDetector.h"
29
30// ===========================================================================
31// member method definitions
32// ===========================================================================
33
35 GNEAdditional("", net, "", tag, "") {
36}
37
38
39GNEDetector::GNEDetector(const std::string& id, GNENet* net, const std::string& filename, SumoXMLTag tag,
40 const SUMOTime period, const std::string& outputFilename, const std::vector<std::string>& vehicleTypes,
41 const std::vector<std::string>& nextEdges, const std::string& detectPersons, const std::string& name,
42 const Parameterised::Map& parameters) :
43 GNEAdditional(id, net, filename, tag, name),
44 Parameterised(parameters),
45 myPeriod(period),
46 myOutputFilename(outputFilename),
47 myVehicleTypes(vehicleTypes),
48 myNextEdges(nextEdges),
49 myDetectPersons(detectPersons) {
50 // update output filename
51 if (outputFilename.empty()) {
52 myOutputFilename = id + ".xml";
53 }
54}
55
56
57GNEDetector::GNEDetector(GNEAdditional* additionalParent, SumoXMLTag tag, const SUMOTime period,
58 const std::string& outputFilename, const std::string& name,
59 const Parameterised::Map& parameters) :
60 GNEAdditional(additionalParent, tag, name),
61 Parameterised(parameters),
62 myPeriod(period),
63 myOutputFilename(outputFilename) {
64 // set parents
65 setParent<GNEAdditional*>(additionalParent);
66}
67
68
70
71
74 return this;
75}
76
77
78const Parameterised*
80 return this;
81}
82
83
84bool
86 // get edit modes
87 const auto& editModes = myNet->getViewNet()->getEditModes();
88 // check if we're in move mode
89 if (!myNet->getViewNet()->isCurrentlyMovingElements() && editModes.isCurrentSupermodeNetwork() &&
91 (editModes.networkEditMode == NetworkEditMode::NETWORK_MOVE) && myNet->getViewNet()->checkOverLockedElement(this, mySelected)) {
92 // only move the first element
94 // special case for multiple lane area detectors
96 auto segment = gViewObjectsHandler.getSelectedSegment(this);
97 if (segment && segment->getJunction()) {
98 return false;
99 } else if (segment && segment->getLane()) {
100 // ensure that is the first or the last lane
101 if (segment->getLaneIndex() == 0) {
102 return true;
103 } else if (segment->getLaneIndex() == ((int)getParentLanes().size() - 1)) {
104 return true;
105 }
106 } else {
107 // this is the start or end point
108 return true;
109 }
110 } else {
111 return true;
112 }
113 }
114 }
115 return false;
116}
117
118
123
124
125void
126GNEDetector::updateCenteringBoundary(const bool /*updateGrid*/) {
127 // nothing to update
128}
129
130
131void
132GNEDetector::splitEdgeGeometry(const double splitPosition, const GNENetworkElement* originalElement,
133 const GNENetworkElement* newElement, GNEUndoList* undoList) {
134 // only split geometry of E2 multilane detectors
136 // obtain new list of E2 lanes
137 std::string newE2Lanes = getNewListOfParents(originalElement, newElement);
138 // update E2 Lanes
139 if (newE2Lanes.size() > 0) {
140 setAttribute(SUMO_ATTR_LANES, newE2Lanes, undoList);
141 }
142 } else if (splitPosition < getAttributeDouble(SUMO_ATTR_POSITION)) {
143 // change lane
144 setAttribute(SUMO_ATTR_LANE, newElement->getID(), undoList);
145 // now adjust start position
147 }
148}
149
150
151std::string
153 return getParentLanes().front()->getID();
154}
155
156
160
161
162std::string
164 return getTagStr() + ": " + getID();
165}
166
167
168std::string
170 return getTagStr();
171}
172
173
174std::string
176 switch (key) {
177 case SUMO_ATTR_ID:
178 return getMicrosimID();
179 case SUMO_ATTR_PERIOD:
181 return "";
182 } else {
183 return time2string(myPeriod);
184 }
185 case SUMO_ATTR_NAME:
186 return myAdditionalName;
187 case SUMO_ATTR_FILE:
188 return myOutputFilename;
189 case SUMO_ATTR_VTYPES:
190 return toString(myVehicleTypes);
192 return toString(myNextEdges);
196 return "";
197 default:
198 return getMoveElement()->getMovingAttribute(key);
199 }
200}
201
202
203double
207
208
213
214
215void
216GNEDetector::setDetectorAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
217 switch (key) {
218 case SUMO_ATTR_ID:
219 case SUMO_ATTR_PERIOD:
220 case SUMO_ATTR_NAME:
221 case SUMO_ATTR_FILE:
222 case SUMO_ATTR_VTYPES:
226 GNEChange_Attribute::changeAttribute(this, key, value, undoList);
227 break;
228 default:
229 getMoveElement()->setMovingAttribute(key, value, undoList);
230 break;
231 }
232}
233
234
235
236bool
237GNEDetector::isDetectorValid(SumoXMLAttr key, const std::string& value) {
238 switch (key) {
239 case SUMO_ATTR_ID:
240 return isValidDetectorID(value);
241 case SUMO_ATTR_PERIOD:
242 if (value.empty()) {
243 return true;
244 } else {
245 return (canParse<double>(value) && (parse<double>(value) >= 0));
246 }
247 case SUMO_ATTR_NAME:
249 case SUMO_ATTR_FILE:
251 case SUMO_ATTR_VTYPES:
252 if (value.empty()) {
253 return true;
254 } else {
256 }
258 if (value.empty()) {
259 return true;
260 } else {
262 }
264 if (value.empty()) {
265 return true;
266 } else {
268 }
269 default:
270 return getMoveElement()->isMovingAttributeValid(key, value);
271 }
272}
273
274
275void
293
294
295void
296GNEDetector::setDetectorAttribute(SumoXMLAttr key, const std::string& value) {
297 switch (key) {
298 case SUMO_ATTR_ID:
299 // update microsimID
300 setAdditionalID(value);
301 break;
302 case SUMO_ATTR_PERIOD:
303 if (value.empty()) {
305 } else {
306 myPeriod = string2time(value);
307 }
308 break;
309 case SUMO_ATTR_FILE:
310 myOutputFilename = value;
311 break;
312 case SUMO_ATTR_NAME:
313 myAdditionalName = value;
314 break;
315 case SUMO_ATTR_VTYPES:
316 myVehicleTypes = parse<std::vector<std::string> >(value);
317 break;
319 myNextEdges = parse<std::vector<std::string> >(value);
320 break;
322 myDetectPersons = value;
323 break;
326 break;
327 default:
328 getMoveElement()->setMovingAttribute(key, value);
329 break;
330 }
331}
332
333
334void
336 const RGBColor& mainColor, const RGBColor& secondColor) const {
337 // push matrix
339 // set line width
340 glLineWidth(1.0);
341 // translate to center geometry
342 glTranslated(myAdditionalGeometry.getShape().front().x(), myAdditionalGeometry.getShape().front().y(), 0);
343 // rotate over lane
345 // scale
346 glScaled(exaggeration, exaggeration, 1);
347 // set main color
348 GLHelper::setColor(mainColor);
349 // begin draw square
350 glBegin(GL_QUADS);
351 // draw square
352 glVertex2d(-1.0, 2);
353 glVertex2d(-1.0, -2);
354 glVertex2d(1.0, -2);
355 glVertex2d(1.0, 2);
356 // end draw square
357 glEnd();
358 // move top
359 glTranslated(0, 0, .01);
360 // begin draw line
361 glBegin(GL_LINES);
362 // draw lines
363 glVertex2d(0, 2 - .1);
364 glVertex2d(0, -2 + .1);
365 // end draw line
366 glEnd();
367 // draw center only in draw in level 2
369 // set main color
370 GLHelper::setColor(secondColor);
371 // set polygon mode
372 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
373 // begin draw square
374 glBegin(GL_QUADS);
375 // draw square
376 glVertex2f(-1.0, 2);
377 glVertex2f(-1.0, -2);
378 glVertex2f(1.0, -2);
379 glVertex2f(1.0, 2);
380 // end draw square
381 glEnd();
382 // rotate 90 degrees
383 glRotated(90, 0, 0, -1);
384 //set polygon mode
385 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
386 // begin draw line
387 glBegin(GL_LINES);
388 // draw line
389 glVertex2d(0, 1.7);
390 glVertex2d(0, -1.7);
391 // end draw line
392 glEnd();
393 //arrow
394 glTranslated(2, 0, 0);
395 GLHelper::setColor(mainColor);
396 GLHelper::drawTriangleAtEnd(Position(0, 0), Position(0.5, 0), (double) 0.5, (double) 1);
397 }
398 // pop matrix
400}
401
402
403void
405 const double exaggeration, const std::string& logo, const RGBColor& textColor) const {
406 // only draw in level 2
408 // calculate position
409 const Position pos = myAdditionalGeometry.getShape().front();
410 // calculate rotation
411 const double rot = s.getTextAngle(myAdditionalGeometry.getShapeRotations().front() + 90);
412 // Start pushing matrix
414 // Traslate to position
415 glTranslated(pos.x(), pos.y(), 0.1);
416 // scale text
417 glScaled(exaggeration, exaggeration, 1);
418 // draw E1 logo
419 GLHelper::drawText(logo + " ", Position(), .1, 1.5, textColor, rot);
420 // pop matrix
422 }
423}
424
425
426void
428 const double exaggeration, const std::string& logo, const RGBColor& textColor) const {
429 // only draw in level 2
431 // calculate middle point
432 const double middlePoint = (myAdditionalGeometry.getShape().length2D() * 0.5);
433 // calculate position
434 const Position pos = myAdditionalGeometry.getShape().positionAtOffset2D(middlePoint);
435 // calculate rotation
436 const double rot = s.getTextAngle(myAdditionalGeometry.getShape().rotationDegreeAtOffset(middlePoint) + 90);
437 // Start pushing matrix
439 // Traslate to position
440 glTranslated(pos.x(), pos.y(), 0.1);
441 // scale text
442 glScaled(exaggeration, exaggeration, 1);
443 // draw E1 logo
444 GLHelper::drawText(logo, Position(), .1, 1.5, textColor, rot);
445 // pop matrix
447 }
448}
449
450/****************************************************************************/
@ 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
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
const std::string getID() const override
get ID (all Attribute Carriers have one)
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
void splitEdgeGeometry(const double splitPosition, const GNENetworkElement *originalElement, const GNENetworkElement *newElement, GNEUndoList *undoList) override
split geometry
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)
Position getPositionInView() const override
Returns position of additional in view.
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 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
std::string getParentName() const override
Returns the name of the parent object.
GNEDetector(GNENet *net, SumoXMLTag tag)
Default constructor.
void writeDetectorValues(OutputDevice &device) const
write additional element into a xml file
std::string getHierarchyName() const override
get Hierarchy Name (Used in AC Hierarchy)
void updateCenteringBoundary(const bool updateGrid) override
update centering boundary (implies change in RTREE)
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
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