Eclipse SUMO - Simulation of Urban MObility
GNEDetector.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 <config.h>
21 
22 #include <netedit/GNENet.h>
23 #include <netedit/GNEUndoList.h>
24 #include <netedit/GNEViewNet.h>
25 #include <netedit/GNEViewParent.h>
30 #include <utils/gui/div/GLHelper.h>
32 
33 #include "GNEDetector.h"
34 
35 
36 // ===========================================================================
37 // member method definitions
38 // ===========================================================================
39 
40 GNEDetector::GNEDetector(const std::string& id, GNENet* net, GUIGlObjectType type, SumoXMLTag tag, double pos, const SUMOTime freq,
41  const std::vector<GNELane*>& parentLanes, const std::string& filename, const std::vector<std::string>& vehicleTypes, const std::string& name,
42  const bool friendlyPos, const std::map<std::string, std::string>& parameters) :
43  GNEAdditional(id, net, type, tag, name, {}, {}, parentLanes, {}, {}, {}, {}, {}, parameters),
44  myPositionOverLane(pos),
45  myFreq(freq),
46  myFilename(filename),
47  myVehicleTypes(vehicleTypes),
48 myFriendlyPosition(friendlyPos) {
49 }
50 
51 
52 GNEDetector::GNEDetector(GNEAdditional* additionalParent, GNENet* net, GUIGlObjectType type, SumoXMLTag tag, const double pos, const SUMOTime freq,
53  const std::vector<GNELane*>& parentLanes, const std::string& filename, const std::string& name, const bool friendlyPos,
54  const std::map<std::string, std::string>& parameters) :
55  GNEAdditional(net, type, tag, name, {}, {}, parentLanes, {additionalParent}, {}, {}, {}, {}, parameters),
56 myPositionOverLane(pos),
57 myFreq(freq),
58 myFilename(filename),
59 myFriendlyPosition(friendlyPos) {
60 }
61 
62 
64 
65 
68  // check detector type
73  } else {
74  // return move operation for detectors with single position placed over shape
75  return new GNEMoveOperation(this, getParentLanes().front(), myPositionOverLane,
77  }
78 }
79 
80 
81 double
83  return myPositionOverLane;
84 }
85 
86 
87 GNELane*
89  return getParentLanes().front();
90 }
91 
92 
96 }
97 
98 
99 void
100 GNEDetector::updateCenteringBoundary(const bool /*updateGrid*/) {
102  // add center
104  // grow
106 }
107 
108 void
109 GNEDetector::splitEdgeGeometry(const double splitPosition, const GNENetworkElement* originalElement,
110  const GNENetworkElement* newElement, GNEUndoList* undoList) {
111  // only split geometry of E2 multilane detectors
113  // obtain new list of E2 lanes
114  std::string newE2Lanes = getNewListOfParents(originalElement, newElement);
115  // update E2 Lanes
116  if (newE2Lanes.size() > 0) {
117  setAttribute(SUMO_ATTR_LANES, newE2Lanes, undoList);
118  }
119  } else if (splitPosition < myPositionOverLane) {
120  // change lane
121  setAttribute(SUMO_ATTR_LANE, newElement->getID(), undoList);
122  // now adjust start position
123  setAttribute(SUMO_ATTR_POSITION, toString(myPositionOverLane - splitPosition), undoList);
124  }
125 }
126 
127 
128 double
130  double fixedPos = myPositionOverLane;
131  const double len = getLane()->getParentEdge()->getNBEdge()->getFinalLength();
133  return fixedPos * getLane()->getLengthGeometryFactor();
134 }
135 
136 
137 
138 std::string
140  return getLane()->getID();
141 }
142 
143 
144 std::string
146  return getTagStr() + ": " + getID();
147 }
148 
149 
150 std::string
152  return getTagStr();
153 }
154 
155 
156 void
157 GNEDetector::drawE1Shape(const GUIVisualizationSettings& s, const double exaggeration, const double scaledWidth,
158  const RGBColor& mainColor, const RGBColor& secondColor) const {
159  // push matrix
161  // set line width
162  glLineWidth(1.0);
163  // translate to center geometry
164  glTranslated(myAdditionalGeometry.getShape().front().x(), myAdditionalGeometry.getShape().front().y(), 0);
165  // rotate over lane
167  // scale
168  glScaled(exaggeration, exaggeration, 1);
169  // set main color
170  GLHelper::setColor(mainColor);
171  // begin draw square
172  glBegin(GL_QUADS);
173  // draw square
174  glVertex2d(-1.0, 2);
175  glVertex2d(-1.0, -2);
176  glVertex2d(1.0, -2);
177  glVertex2d(1.0, 2);
178  // end draw square
179  glEnd();
180  // move top
181  glTranslated(0, 0, .01);
182  // begin draw line
183  glBegin(GL_LINES);
184  // draw lines
185  glVertex2d(0, 2 - .1);
186  glVertex2d(0, -2 + .1);
187  // end draw line
188  glEnd();
189  // outline if isn't being drawn for selecting
190  if ((scaledWidth * exaggeration > 1) && !s.drawForRectangleSelection) {
191  // set main color
192  GLHelper::setColor(secondColor);
193  // set polygon mode
194  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
195  // begin draw square
196  glBegin(GL_QUADS);
197  // draw square
198  glVertex2f(-1.0, 2);
199  glVertex2f(-1.0, -2);
200  glVertex2f(1.0, -2);
201  glVertex2f(1.0, 2);
202  // end draw square
203  glEnd();
204  // rotate 90 degrees
205  glRotated(90, 0, 0, -1);
206  //set polygon mode
207  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
208  // begin draw line
209  glBegin(GL_LINES);
210  // draw line
211  glVertex2d(0, 1.7);
212  glVertex2d(0, -1.7);
213  // end draw line
214  glEnd();
215  }
216  // pop matrix
218 }
219 
220 
221 void
222 GNEDetector::drawDetectorLogo(const GUIVisualizationSettings& s, const double exaggeration,
223  const std::string& logo, const RGBColor& textColor) const {
225  // calculate middle point
226  const double middlePoint = (myAdditionalGeometry.getShape().length2D() * 0.5);
227  // calculate position
229  // calculate rotation
230  double rot = 0;
231  if (myAdditionalGeometry.getShapeRotations().size() > 0) {
232  rot = myAdditionalGeometry.getShapeRotations().front();
233  } else if (myAdditionalGeometry.getShape().size() > 1) {
235  }
236  // Start pushing matrix
238  // Traslate to position
239  glTranslated(pos.x(), pos.y(), 0.1);
240  // rotate over lane
242  // move
243  glTranslated(-1, 0, 0);
244  // scale text
245  glScaled(exaggeration, exaggeration, 1);
246  // draw E1 logo
247  GLHelper::drawText(logo, Position(), .1, 1.5, textColor);
248  // pop matrix
250  }
251 }
252 
253 
256  // get allow change lane
257  const bool allowChangeLane = myNet->getViewNet()->getViewParent()->getMoveFrame()->getCommonModeOptions()->getAllowChangeLane();
258  // fist check if we're moving only extremes
262  // get snap radius
264  // get mouse position
265  const Position mousePosition = myNet->getViewNet()->getPositionInformation();
266  // check if we clicked over start or end position
267  if (myAdditionalGeometry.getShape().front().distanceSquaredTo2D(mousePosition) <= (snap_radius * snap_radius)) {
268  // move only start position
271  } else if (myAdditionalGeometry.getShape().back().distanceSquaredTo2D(mousePosition) <= (snap_radius * snap_radius)) {
272  // move only end position
275  } else {
276  return nullptr;
277  }
278  } else {
279  // move both start and end positions
282  }
283 }
284 
285 
288  // fist check if we're moving only extremes
292  // get snap radius
294  // get mouse position
295  const Position mousePosition = myNet->getViewNet()->getPositionInformation();
296  // calculate both geometries
297  GUIGeometry fromGeometry, toGeometry;
298  fromGeometry.updateGeometry(getParentLanes().front()->getLaneGeometry().getShape(), myPositionOverLane, 0);
299  toGeometry.updateGeometry(getParentLanes().back()->getLaneGeometry().getShape(), getAttributeDouble(SUMO_ATTR_ENDPOS), 0);
300  // check if we clicked over start or end position
301  if (fromGeometry.getShape().front().distanceSquaredTo2D(mousePosition) <= (snap_radius * snap_radius)) {
302  // move only start position
303  return new GNEMoveOperation(this, getParentLanes().front(), myPositionOverLane,
306  } else if (toGeometry.getShape().back().distanceSquaredTo2D(mousePosition) <= (snap_radius * snap_radius)) {
307  // move only end position
308  return new GNEMoveOperation(this, getParentLanes().front(), myPositionOverLane,
311  } else {
312  return nullptr;
313  }
314  } else {
315  // move both start and end positions
318  }
319 }
320 
321 /****************************************************************************/
@ NETWORK_MOVE
mode for moving network elements
GUIGlObjectType
long long int SUMOTime
Definition: SUMOTime.h:32
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_E2DETECTOR
an e2 detector
@ GNE_TAG_E2DETECTOR_MULTILANE
an e2 detector over multiple lanes (placed here due create Additional Frame)
@ SUMO_ATTR_LANE
@ SUMO_ATTR_ENDPOS
@ SUMO_ATTR_LANES
@ SUMO_ATTR_POSITION
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
void add(double x, double y, double z=0)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:77
void reset()
Resets the boundary.
Definition: Boundary.cpp:65
Boundary & grow(double by)
extends the boundary by the given amount
Definition: Boundary.cpp:299
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:507
static void popMatrix()
pop matrix
Definition: GLHelper.cpp:123
static void pushMatrix()
push matrix
Definition: GLHelper.cpp:114
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:609
static void fixSinglePositionOverLane(double &pos, const double laneLength)
fix given position over lane
An Element which don't belongs to GNENet but has influency in the simulation.
Definition: GNEAdditional.h:48
const std::string & getID() const
get ID
GUIGeometry myAdditionalGeometry
geometry to be precomputed in updateGeometry(...)
Boundary myAdditionalBoundary
Additional Boundary.
const std::string & getTagStr() const
get tag assigned to this object in string format
GNENet * myNet
pointer to net
const GNETagProperties & myTagProperty
reference to tagProperty associated with this attribute carrier
GNEDetector(const std::string &id, GNENet *net, GUIGlObjectType type, SumoXMLTag tag, const double pos, const SUMOTime freq, const std::vector< GNELane * > &parentLanes, const std::string &filename, const std::vector< std::string > &vehicleTypes, const std::string &name, const bool friendlyPos, const std::map< std::string, std::string > &parameters)
Constructor.
Definition: GNEDetector.cpp:40
double myPositionOverLane
position of detector over Lane
Definition: GNEDetector.h:174
Position getPositionInView() const
Returns position of additional in view.
Definition: GNEDetector.cpp:94
std::string getHierarchyName() const
get Hierarchy Name (Used in AC Hierarchy)
~GNEDetector()
Destructor.
Definition: GNEDetector.cpp:63
double getPositionOverLane() const
get position over lane
Definition: GNEDetector.cpp:82
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
GNELane * getLane() const
get lane
Definition: GNEDetector.cpp:88
virtual double getAttributeDouble(SumoXMLAttr key) const =0
GNEMoveOperation * getMoveOperationE2MultiLane()
get moveOperation for E2 multi lane
GNEMoveOperation * getMoveOperation()
get move operation
Definition: GNEDetector.cpp:67
void splitEdgeGeometry(const double splitPosition, const GNENetworkElement *originalElement, const GNENetworkElement *newElement, GNEUndoList *undoList)
split geometry
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::string getPopUpID() const
get PopPup ID (Used in AC Hierarchy)
GNEMoveOperation * getMoveOperationE2SingleLane()
get moveOperation for E2 single lane
virtual void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)=0
method for setting the attribute and letting the object perform additional changes
std::string getParentName() const
Returns the name of the parent object.
NBEdge * getNBEdge() const
returns the internal NBEdge
Definition: GNEEdge.cpp:435
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...
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 getLengthGeometryFactor() const
get length geometry factor
Definition: GNELane.cpp:1713
GNEEdge * getParentEdge() const
get arent edge
Definition: GNELane.cpp:113
bool getAllowChangeLane() const
allow change lane
CommonModeOptions * getCommonModeOptions() const
get common mode options
move operation
A NBNetBuilder extended by visualisation and editing capabilities.
Definition: GNENet.h:42
GNEViewNet * getViewNet() const
get view net
Definition: GNENet.cpp:1964
const std::string & getID() const
get ID
SumoXMLTag getTag() const
get Tag vinculated with this attribute Property
const GNEViewNetHelper::EditModes & getEditModes() const
get edit modes
Definition: GNEViewNet.cpp:513
const GNEViewNetHelper::MouseButtonKeyPressed & getMouseButtonKeyPressed() const
get Key Pressed modul
Definition: GNEViewNet.cpp:543
GNEViewParent * getViewParent() const
get the net object
GNEMoveFrame * getMoveFrame() const
get frame for move elements
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.
void updateGeometry(const PositionVector &shape)
update entire geometry
Definition: GUIGeometry.cpp:58
GUIVisualizationSettings & getVisualisationSettings() const
get visualization settings
Position getPositionInformation() const
Returns the cursor's x/y position within the network.
Stores the information about how to visualize structures.
bool drawForRectangleSelection
whether drawing is performed for the purpose of selecting objects using a rectangle
bool drawForPositionSelection
whether drawing is performed for the purpose of selecting objects with a single click
GUIVisualizationNeteditSizeSettings neteditSizeSettings
netedit size settings
double getFinalLength() const
get length that will be assigned to the lanes in the final network
Definition: NBEdge.cpp:4354
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:55
double y() const
Returns the y-position.
Definition: Position.h:60
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) const
Returns the position at the given length.
NetworkEditMode networkEditMode
the current Network edit mode
bool isCurrentSupermodeNetwork() const
@check if current supermode is Network
bool shiftKeyPressed() const
check if SHIFT is pressed during current event
static const double additionalGeometryPointRadius
moving additional geometry point radius