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-2020 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 
25 #include <netedit/GNEUndoList.h>
26 #include <utils/gui/div/GLHelper.h>
28 
29 #include "GNEDetector.h"
30 
31 
32 // ===========================================================================
33 // member method definitions
34 // ===========================================================================
35 
36 GNEDetector::GNEDetector(const std::string& id, GNENet* net, GUIGlObjectType type, SumoXMLTag tag,
37  double pos, const std::string& freq, const std::string& filename, const std::string& vehicleTypes,
38  const std::string& name, bool friendlyPos, bool blockMovement, const std::vector<GNELane*>& parentLanes) :
39  GNEAdditional(id, net, type, tag, name, blockMovement, {}, {}, parentLanes, {}, {}, {}, {}, {}),
40  myPositionOverLane(pos),
41  myFreq(freq),
42  myFilename(filename),
43  myVehicleTypes(vehicleTypes),
44 myFriendlyPosition(friendlyPos) {
45 }
46 
47 
49  double pos, const std::string& freq, const std::string& filename, const std::string& name, bool friendlyPos,
50  bool blockMovement, const std::vector<GNELane*>& parentLanes) :
51  GNEAdditional(net, type, tag, name, blockMovement, {}, {}, parentLanes, {additionalParent}, {}, {}, {}, {}),
52 myPositionOverLane(pos),
53 myFreq(freq),
54 myFilename(filename),
55 myFriendlyPosition(friendlyPos) {
56 }
57 
58 
60 
61 
63 GNEDetector::getMoveOperation(const double /*shapeOffset*/) {
64  // check conditions
65  if (myBlockMovement) {
66  // element blocked, then nothing to move
67  return nullptr;
68  } else {
69  // return move operation for additional placed over shape
70  return new GNEMoveOperation(this, getParentLanes().front(), {myPositionOverLane});
71  }
72 }
73 
74 
75 double
77  return myPositionOverLane;
78 }
79 
80 
81 GNELane*
83  return getParentLanes().front();
84 }
85 
86 
87 void
88 GNEDetector::updateCenteringBoundary(const bool /*updateGrid*/) {
89  // now update geometry
91  // add shape boundary
93  // grow
94  myBoundary.grow(10);
95 }
96 
97 void
98 GNEDetector::splitEdgeGeometry(const double splitPosition, const GNENetworkElement* originalElement,
99  const GNENetworkElement* newElement, GNEUndoList* undoList) {
100  // only split geometry of E2 multilane detectors
102  // obtain new list of E2 lanes
103  std::string newE2Lanes = getNewListOfParents(originalElement, newElement);
104  // update E2 Lanes
105  if (newE2Lanes.size() > 0) {
106  setAttribute(SUMO_ATTR_LANES, newE2Lanes, undoList);
107  }
108  } else if (splitPosition < myPositionOverLane) {
109  // change lane
110  setAttribute(SUMO_ATTR_LANE, newElement->getID(), undoList);
111  // now adjust start position
112  setAttribute(SUMO_ATTR_POSITION, toString(myPositionOverLane - splitPosition), undoList);
113  }
114 }
115 
116 
117 double
119  double fixedPos = myPositionOverLane;
120  const double len = getLane()->getParentEdge()->getNBEdge()->getFinalLength();
121  if (fixedPos < 0) {
122  fixedPos += len;
123  }
125  return fixedPos * getLane()->getLengthGeometryFactor();
126 }
127 
128 
129 double
131  throw InvalidArgument(getTagStr() + " doesn't have a double attribute of type '" + toString(key) + "'");
132 }
133 
134 
135 std::string
137  return getLane()->getID();
138 }
139 
140 
141 std::string
143  return getTagStr() + ": " + getID();
144 }
145 
146 
147 std::string
149  return getTagStr();
150 }
151 
152 
153 void
154 GNEDetector::drawE1Shape(const GUIVisualizationSettings& s, const double exaggeration, const double scaledWidth,
155  const RGBColor& mainColor, const RGBColor& secondColor) const {
156  // push matrix
157  glPushMatrix();
158  // set line width
159  glLineWidth(1.0);
160  // translate to center geometry
161  glTranslated(myAdditionalGeometry.getShape().front().x(), myAdditionalGeometry.getShape().front().y(), 0);
162  // rotate over lane
164  // scale
165  glScaled(exaggeration, exaggeration, 1);
166  // set main color
167  GLHelper::setColor(mainColor);
168  // begin draw square
169  glBegin(GL_QUADS);
170  // draw square
171  glVertex2d(-1.0, 2);
172  glVertex2d(-1.0, -2);
173  glVertex2d(1.0, -2);
174  glVertex2d(1.0, 2);
175  // end draw square
176  glEnd();
177  // move top
178  glTranslated(0, 0, .01);
179  // begin draw line
180  glBegin(GL_LINES);
181  // draw lines
182  glVertex2d(0, 2 - .1);
183  glVertex2d(0, -2 + .1);
184  // end draw line
185  glEnd();
186  // outline if isn't being drawn for selecting
187  if ((scaledWidth * exaggeration > 1) && !s.drawForRectangleSelection) {
188  // set main color
189  GLHelper::setColor(secondColor);
190  // set polygon mode
191  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
192  // begin draw square
193  glBegin(GL_QUADS);
194  // draw square
195  glVertex2f(-1.0, 2);
196  glVertex2f(-1.0, -2);
197  glVertex2f(1.0, -2);
198  glVertex2f(1.0, 2);
199  // end draw square
200  glEnd();
201  // rotate 90 degrees
202  glRotated(90, 0, 0, -1);
203  //set polygon mode
204  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
205  // begin draw line
206  glBegin(GL_LINES);
207  // draw line
208  glVertex2d(0, 1.7);
209  glVertex2d(0, -1.7);
210  // end draw line
211  glEnd();
212  }
213  // pop matrix
214  glPopMatrix();
215 }
216 
217 
218 void
219 GNEDetector::drawDetectorLogo(const GUIVisualizationSettings& s, const double exaggeration,
220  const std::string& logo, const RGBColor& textColor) const {
222  // calculate middle point
223  const double middlePoint = (myAdditionalGeometry.getShape().length2D() * 0.5);
224  // calculate position
226  // calculate rotation
227  double rot = 0;
228  if (myAdditionalGeometry.getShapeRotations().size() > 0) {
229  rot = myAdditionalGeometry.getShapeRotations().front();
230  } else if (myAdditionalGeometry.getShape().size() > 1) {
232  }
233  // Start pushing matrix
234  glPushMatrix();
235  // Traslate to position
236  glTranslated(pos.x(), pos.y(), 0.1);
237  // rotate over lane
239  // move
240  glTranslated(-1, 0, 0);
241  // scale text
242  glScaled(exaggeration, exaggeration, 1);
243  // draw E1 logo
244  GLHelper::drawText(logo, Position(), .1, 1.5, textColor);
245  // pop matrix
246  glPopMatrix();
247  }
248 }
249 
250 
251 void
253  // change both position
254  myPositionOverLane = moveResult.shapeToUpdate.front().x();
255  // update geometry
256  updateGeometry();
257 }
258 
259 
260 void
262  undoList->p_begin("position of " + getTagStr());
263  // now adjust start position
264  setAttribute(SUMO_ATTR_POSITION, toString(moveResult.shapeToUpdate.front().x()), undoList);
265  undoList->p_end();
266 }
267 
268 /****************************************************************************/
GUIGlObjectType
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_E2DETECTOR_MULTILANE
an e2 detector over multiple lanes (used by Netedit)
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_LANE
@ SUMO_ATTR_LANES
@ SUMO_ATTR_POSITION
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
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:446
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:498
static bool checkAndFixDetectorPosition(double &pos, const double laneLength, const bool friendlyPos)
check if the position of a detector over a lane is valid
An Element which don't belongs to GNENet but has influency in the simulation.
Definition: GNEAdditional.h:47
const std::string & getID() const
get ID
GNEGeometry::Geometry myAdditionalGeometry
geometry to be precomputed in updateGeometry(...)
bool myBlockMovement
boolean to check if additional element is blocked (i.e. cannot be moved with mouse)
Boundary myBoundary
Additional Boundary.
const std::string & getTagStr() const
get tag assigned to this object in string format
const GNETagProperties & myTagProperty
the xml tag to which this attribute carrier corresponds
GNEMoveOperation * getMoveOperation(const double shapeOffset)
get move operation for the given shapeOffset
Definition: GNEDetector.cpp:63
GNEDetector(const std::string &id, GNENet *net, GUIGlObjectType type, SumoXMLTag tag, double pos, const std::string &freq, const std::string &filename, const std::string &vehicleTypes, const std::string &name, bool friendlyPos, bool blockMovement, const std::vector< GNELane * > &parentLanes)
Constructor.
Definition: GNEDetector.cpp:36
double myPositionOverLane
position of detector over Lane
Definition: GNEDetector.h:163
std::string getHierarchyName() const
get Hierarchy Name (Used in AC Hierarchy)
double getAttributeDouble(SumoXMLAttr key) const
~GNEDetector()
Destructor.
Definition: GNEDetector.cpp:59
double getPositionOverLane() const
get position over lane
Definition: GNEDetector.cpp:76
void setMoveShape(const GNEMoveResult &moveResult)
set move shape
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:82
virtual void updateGeometry()=0
update pre-computed geometry information
void commitMoveShape(const GNEMoveResult &moveResult, GNEUndoList *undoList)
commit move shape
void splitEdgeGeometry(const double splitPosition, const GNENetworkElement *originalElement, const GNENetworkElement *newElement, GNEUndoList *undoList)
split geometry
Definition: GNEDetector.cpp:98
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)
Definition: GNEDetector.cpp:88
std::string getPopUpID() const
get PopPup ID (Used in AC Hierarchy)
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:399
const std::vector< double > & getShapeRotations() const
The rotations of the single shape parts.
const PositionVector & getShape() const
The shape of the additional element.
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:45
double getLengthGeometryFactor() const
get length geometry factor
Definition: GNELane.cpp:1615
GNEEdge * getParentEdge() const
get arent edge
Definition: GNELane.cpp:111
move operation
move result
PositionVector shapeToUpdate
shape to update (edited in moveElement)
A NBNetBuilder extended by visualisation and editing capabilities.
Definition: GNENet.h:40
const std::string & getID() const
get ID
SumoXMLTag getTag() const
get Tag vinculated with this attribute Property
void p_begin(const std::string &description)
Begin undo command sub-group. This begins a new group of commands that are treated as a single comman...
Definition: GNEUndoList.cpp:71
void p_end()
End undo command sub-group. If the sub-group is still empty, it will be deleted; otherwise,...
Definition: GNEUndoList.cpp:78
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
double getFinalLength() const
get length that will be assigned to the lanes in the final network
Definition: NBEdge.cpp:3954
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
double x() const
Returns the x-position.
Definition: Position.h:54
double y() const
Returns the y-position.
Definition: Position.h:59
double length2D() const
Returns the length.
double rotationDegreeAtOffset(double pos) const
Returns the rotation at the given length.
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
Position positionAtOffset2D(double pos, double lateralOffset=0) const
Returns the position at the given length.
static void rotateOverLane(const double rot)
rotate over lane (used by Lock icons, detector logos, etc.)