SUMO - Simulation of Urban MObility
GNEConnection.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // A class for visualizing connections between lanes
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <string>
32 #include <iostream>
33 #include <utility>
34 #include <time.h>
40 #include <utils/common/ToString.h>
45 #include <utils/gui/div/GLHelper.h>
47 
48 #include "GNEConnection.h"
49 #include "GNEJunction.h"
50 #include "GNEEdge.h"
51 #include "GNELane.h"
52 #include "GNENet.h"
53 #include "GNEChange_Attribute.h"
54 #include "GNEUndoList.h"
55 #include "GNEViewNet.h"
56 #include "GNEInternalLane.h"
57 
58 
59 
60 // ===========================================================================
61 // static member definitions
62 // ===========================================================================
63 int NUM_POINTS = 5;
64 
65 // ===========================================================================
66 // method definitions
67 // ===========================================================================
68 
70  GNENetElement(from->getNet(), from->getMicrosimID() + " -> " + to->getMicrosimID(),
72  myFromLane(from),
73  myToLane(to),
74  myLinkState(LINKSTATE_TL_OFF_NOSIGNAL),
75  myDrawConnection(true) {
76  // geometry will be updated later
77 }
78 
79 
81 
82 
83 void
85  // Clear containers
86  myShapeRotations.clear();
87  myShapeLengths.clear();
88  // Get shape of from and to lanes
90  PositionVector laneShapeFrom;
91  if ((int)getEdgeFrom()->getNBEdge()->getLanes().size() > nbCon.fromLane) {
92  laneShapeFrom = getEdgeFrom()->getNBEdge()->getLanes().at(nbCon.fromLane).shape;
93  } else {
94  return;
95  }
96  PositionVector laneShapeTo;
97  if ((int)nbCon.toEdge->getLanes().size() > nbCon.toLane) {
98  laneShapeTo = nbCon.toEdge->getLanes().at(nbCon.toLane).shape;
99  } else {
100  return;
101  }
102  // Calculate shape of connection depending of the size of Junction shape
103  if (getEdgeFrom()->getNBEdge()->getToNode()->getShape().area() > 4) { // value obtanied from GNEJunction::drawgl
104  // Calculate shape using a smooth shape
106  laneShapeFrom,
107  laneShapeTo,
108  NUM_POINTS, getEdgeFrom()->getNBEdge()->getTurnDestination() == nbCon.toEdge,
109  (double) 5. * (double) getEdgeFrom()->getNBEdge()->getNumLanes(),
110  (double) 5. * (double) nbCon.toEdge->getNumLanes());
111 
112  } else {
113  myShape.clear();
114  myShape.push_back(laneShapeFrom.positionAtOffset(laneShapeFrom.length() - 1));
115  myShape.push_back(laneShapeTo.positionAtOffset(1));
116  }
117 
118  // Obtain lengths and shape rotations
119  int segments = (int) myShape.size() - 1;
120  if (segments >= 0) {
121  myShapeRotations.reserve(segments);
122  myShapeLengths.reserve(segments);
123  for (int i = 0; i < segments; ++i) {
124  const Position& f = myShape[i];
125  const Position& s = myShape[i + 1];
126  myShapeLengths.push_back(f.distanceTo2D(s));
127  myShapeRotations.push_back((double) atan2((s.x() - f.x()), (f.y() - s.y())) * (double) 180.0 / (double) PI);
128  }
129  }
130 }
131 
132 
133 Boundary
135  return myShape.getBoxBoundary();
136 }
137 
138 
139 GNEEdge*
141  return &(myFromLane->getParentEdge());
142 }
143 
144 
145 GNEEdge*
147  return &(myToLane->getParentEdge());
148 }
149 
150 
151 GNELane*
153  return myFromLane;
154 }
155 
156 
157 GNELane*
159  return myToLane;
160 }
161 
162 
163 int
165  return myFromLane->getIndex();
166 }
167 
168 
169 int
171  return myToLane->getIndex();
172 }
173 
174 
178 }
179 
180 
183  return NBConnection(getEdgeFrom()->getNBEdge(), getFromLaneIndex(),
184  getEdgeTo()->getNBEdge(), getToLaneIndex(),
185  (int)getNBEdgeConnection().tlLinkNo);
186 }
187 
188 
189 void
192 }
193 
194 
195 LinkState
197  return myLinkState;
198 }
199 
200 
201 void
205  nbCon.toEdge,
206  nbCon.fromLane,
207  nbCon.toLane,
208  nbCon.mayDefinitelyPass,
209  nbCon.tlID);
210 }
211 
212 
213 bool
215  return myDrawConnection;
216 }
217 
218 
219 void
220 GNEConnection::setDrawConnection(bool drawConnection) {
221  myDrawConnection = drawConnection;
222 }
223 
224 
227  GUIGLObjectPopupMenu* ret = new GUIGLObjectPopupMenu(app, parent, *this);
228  buildPopupHeader(ret, app);
232  buildPositionCopyEntry(ret, false);
233  // let the GNEViewNet store the popup position
234  (dynamic_cast<GNEViewNet&>(parent)).markPopupPosition();
235  return ret;
236 }
237 
238 
241  // Currently ignored before implementation to avoid warnings
242  UNUSED_PARAMETER(app);
243  UNUSED_PARAMETER(parent);
244  return NULL;
245 }
246 
247 
248 Boundary
250  return Boundary();
251 }
252 
253 
254 void
256  // Check if connection must be drawed
258  // Push draw matrix 1
259  glPushMatrix();
260  // Push name
261  glPushName(getGlID());
262  // Traslate matrix
263  glTranslated(0, 0, GLO_JUNCTION + 0.1); // must draw on top of junction
264  // Set color
266  // override with special colors (unless the color scheme is based on selection)
268  } else {
269  // Set color depending of the link state
271  }
272  // draw connection checking whether it is not too small
273  if (s.scale < 1.) {
274  // If it's small, dra a simple line
276  } else {
277  // draw a list of lines
279  }
280  // Pop name
281  glPopName();
282  // Pop draw matrix 1
283  glPopMatrix();
284  }
285 }
286 
287 
288 std::string
290  if (key == SUMO_ATTR_ID) {
291  // used by GNEReferenceCounter
292  // @note: may be called for connections without a valid nbCon reference
293  return getMicrosimID();
294  }
296  switch (key) {
297  case SUMO_ATTR_FROM:
298  return getEdgeFrom()->getID();
299  case SUMO_ATTR_TO:
300  return nbCon.toEdge->getID();
301  case SUMO_ATTR_FROM_LANE:
302  return toString(nbCon.toLane);
303  case SUMO_ATTR_TO_LANE:
304  return toString(nbCon.toLane);
305  case SUMO_ATTR_PASS:
306  return toString(nbCon.mayDefinitelyPass);
308  return toString(nbCon.keepClear);
309  case SUMO_ATTR_CONTPOS:
310  return toString(nbCon.contPos);
312  return toString(nbCon.uncontrolled);
314  return toString(nbCon.visibility);
315  default:
316  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
317  }
318 }
319 
320 
321 void
322 GNEConnection::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
323  switch (key) {
324  case SUMO_ATTR_FROM:
325  case SUMO_ATTR_TO:
326  case SUMO_ATTR_FROM_LANE:
327  case SUMO_ATTR_TO_LANE:
328  case SUMO_ATTR_PASS:
330  case SUMO_ATTR_CONTPOS:
333  // no special handling
334  undoList->p_add(new GNEChange_Attribute(this, key, value));
335  break;
336  default:
337  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
338  }
339 }
340 
341 
342 bool
343 GNEConnection::isValid(SumoXMLAttr key, const std::string& value) {
344  // Currently ignored before implementation to avoid warnings
345  switch (key) {
346  case SUMO_ATTR_FROM:
347  case SUMO_ATTR_TO:
348  case SUMO_ATTR_FROM_LANE:
349  case SUMO_ATTR_TO_LANE:
350  return false;
351  case SUMO_ATTR_PASS:
352  return canParse<bool>(value);
354  return canParse<bool>(value);
355  case SUMO_ATTR_CONTPOS:
356  return canParse<double>(value);
358  return canParse<bool>(value);
360  return isPositive<double>(value);
361  default:
362  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
363  }
364 }
365 
366 
367 void
368 GNEConnection::setAttribute(SumoXMLAttr key, const std::string& value) {
370  switch (key) {
371  case SUMO_ATTR_PASS:
372  nbCon.mayDefinitelyPass = parse<bool>(value);
373  break;
375  nbCon.keepClear = parse<bool>(value);
376  break;
378  nbCon.uncontrolled = parse<bool>(value);
379  break;
380  case SUMO_ATTR_CONTPOS:
381  nbCon.contPos = parse<double>(value);
382  break;
384  nbCon.visibility = parse<double>(value);
385  break;
386  default:
387  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
388  }
389 }
390 
391 
392 /****************************************************************************/
int NUM_POINTS
void updateGeometry()
update pre-computed geometry information
std::vector< double > myShapeLengths
The lengths of the shape parts.
A structure which describes a connection between edges or lanes.
Definition: NBEdge.h:164
LinkState getLinkState(const NBEdge *incoming, NBEdge *outgoing, int fromLane, int toLane, bool mayDefinitelyPass, const std::string &tlID) const
get link state
Definition: NBNode.cpp:1613
int toLane
The lane the connections yields in.
Definition: NBEdge.h:190
double scale
information about a lane&#39;s width (temporary, used for a single view)
bool isValid(SumoXMLAttr key, const std::string &value)
Whether vehicles must keep the junction clear.
GNENet * myNet
the net to inform about updates
static void drawBoxLines(const PositionVector &geom, const std::vector< double > &rots, const std::vector< double > &lengths, double width, int cornerDetail=0, double offset=0)
Draws thick lines.
Definition: GLHelper.cpp:172
void buildNameCopyPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds entries which allow to copy the name / typed name into the clipboard.
NBEdge * toEdge
The edge the connections yields in.
Definition: NBEdge.h:187
bool myDrawConnection
Enable or disable draw connection.
LinkState getLinkState() const
get LinkState
double distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition: Position.h:250
void setDrawConnection(bool drawConnection)
enable or disable draw connection
Stores the information about how to visualize structures.
connectio between two lanes
bool getDrawConnection() const
get Draw connection
a connection
double y() const
Returns the y-position.
Definition: Position.h:68
foe visibility distance of a link
static RGBColor colorForLinksState(FXuint state)
return the color for each linkstate
double x() const
Returns the x-position.
Definition: Position.h:63
void buildCenterPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to center to the object.
bool isSelected(GUIGlObjectType type, GUIGlID id)
Returns the information whether the object with the given type and id is selected.
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:54
const std::vector< NBEdge::Lane > & getLanes() const
Returns the lane definitions.
Definition: NBEdge.h:570
bool mayDefinitelyPass
Information about being definitely free to drive (on-ramps)
Definition: NBEdge.h:199
GNELane * myFromLane
incoming lane of this connection
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
double visibility
custom foe visiblity for connection
Definition: NBEdge.h:208
const std::string & getID() const
Returns the id.
Definition: Named.h:66
The link is controlled by a tls which is off, not blinking, may pass.
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
void buildPositionCopyEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to copy the cursor position if geo projection is used, also builds an entry for copying the geo-position.
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:38
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
GNEEdge * getEdgeTo() const
get the name of the edge the vehicles may reach when leaving "from"
GUIGLObjectPopupMenu * getPopUpMenu(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own popup-menu.
GNELane * getLaneFrom() const
get lane of the incoming lane
bool keepClear
whether the junction must be kept clear when using this connection
Definition: NBEdge.h:202
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
NBEdge::Connection & getNBEdgeConnection() const
get Edge::Connection
int getFromLaneIndex() const
get lane index of the incoming lane
int getToLaneIndex() const
get lane index of the outgoing lane
PositionVector myShape
the shape of the connection
int getIndex() const
returns the index of the lane
Definition: GNELane.cpp:697
#define PI
Definition: polyfonts.c:61
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
GUIParameterTableWindow * getParameterWindow(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own parameter window.
void updateID()
update internal ID of Connection
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:439
GNEEdge & getParentEdge()
Returns underlying parent edge.
Definition: GNELane.cpp:1078
std::vector< double > myShapeRotations
LinkState myLinkState
Linkstate.
std::string tlID
The id of the traffic light that controls this connection.
Definition: NBEdge.h:193
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:413
int fromLane
The lane the connections starts at.
Definition: NBEdge.h:184
GNEConnection(GNELane *from, GNELane *to)
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
GNELane * getLaneTo() const
get lane of the outgoing lane
A list of positions.
GNEEdge * getEdgeFrom() const
get the name of the edge the vehicles leave
friend class GNEChange_Attribute
declare friend class
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
Boundary getBoundary() const
Returns the street&#39;s geometry.
GNELane * myToLane
outgoing lane of this connection
~GNEConnection()
Destructor.
GUIColorer junctionColorer
The junction colorer.
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
const std::string getID() const
function to support debugging
static void drawLine(const Position &beg, double rot, double visLength)
Draws a thin line.
Definition: GLHelper.cpp:265
PositionVector computeSmoothShape(const PositionVector &begShape, const PositionVector &endShape, int numPoints, bool isTurnaround, double extrapolateBeg, double extrapolateEnd, NBNode *recordError=0) const
Compute a smooth curve between the given geometries.
Definition: NBNode.cpp:461
A road/street connecting two junctions (netedit-version)
Definition: GNEEdge.h:57
double length() const
Returns the length.
NBConnection getNBConnection() const
get NBConnection
virtual void setMicrosimID(const std::string &newID)
Changes the microsimID of the object.
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
bool showConnections()
show connections over junctions
Definition: GNEViewNet.cpp:369
static const RGBColor selectedConnectionColor
color of selected connection
Definition: GNENet.h:103
double contPos
custom position for internal junction on this connection
Definition: NBEdge.h:205
bool uncontrolled
check if Connection is uncontrolled
Definition: NBEdge.h:244
The popup menu of a globject.
void buildSelectionPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to (de)select the object.
std::string getAttribute(SumoXMLAttr key) const
GUIGlID getGlID() const
Returns the numerical id of the object.
void updateLinkState()
recompute cached myLinkState
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.
NBEdge * getNBEdge()
returns the internal NBEdge
Definition: GNEEdge.cpp:261
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
GUISelectedStorage gSelected
A global holder of selected objects.
A window containing a gl-object&#39;s parameter.
Connection & getConnectionRef(int fromLane, const NBEdge *to, int toLane)
Returns reference to the specified connection This method goes through "myConnections" and returns th...
Definition: NBEdge.cpp:1036
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:434
void buildPopupHeader(GUIGLObjectPopupMenu *ret, GUIMainWindow &app, bool addSeparator=true)
Builds the header.
GNEViewNet * getViewNet() const
get view net
Definition: GNENet.cpp:1165
a junction
SumoXMLTag getTag() const
get XML Tag assigned to this object