SUMO - Simulation of Urban MObility
GNEConnection.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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 // A class for visualizing connections between lanes
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <string>
31 #include <iostream>
32 #include <utility>
33 #include <time.h>
39 #include <utils/common/ToString.h>
43 #include <utils/gui/div/GLHelper.h>
46 
47 #include "GNEConnection.h"
48 #include "GNEJunction.h"
49 #include "GNEEdge.h"
50 #include "GNELane.h"
51 #include "GNENet.h"
52 #include "GNEChange_Attribute.h"
53 #include "GNEChange_TLS.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" + from->getMicrosimID() + "to" + to->getMicrosimID(),
72  myFromLane(from),
73  myToLane(to),
74  myLinkState(LINKSTATE_TL_OFF_NOSIGNAL) {
75  // geometry will be updated later
76 }
77 
78 
80 
81 
82 void
84  // Clear containers
85  myShapeRotations.clear();
86  myShapeLengths.clear();
87  // Get shape of from and to lanes
89  PositionVector laneShapeFrom;
90  if ((int)getEdgeFrom()->getNBEdge()->getLanes().size() > nbCon.fromLane) {
91  laneShapeFrom = getEdgeFrom()->getNBEdge()->getLanes().at(nbCon.fromLane).shape;
92  } else {
93  return;
94  }
95  PositionVector laneShapeTo;
96  if ((int)nbCon.toEdge->getLanes().size() > nbCon.toLane) {
97  laneShapeTo = nbCon.toEdge->getLanes().at(nbCon.toLane).shape;
98  } else {
99  return;
100  }
101  // Calculate shape of connection depending of the size of Junction shape
102  // value obtanied from GNEJunction::drawgl
103  if (nbCon.customShape.size() != 0) {
104  myShape = nbCon.customShape;
105  } else if (getEdgeFrom()->getNBEdge()->getToNode()->getShape().area() > 4) {
106  if (nbCon.shape.size() != 0) {
107  myShape = nbCon.shape;
108  myShape.append(nbCon.viaShape);
109  } else {
110  // Calculate shape so something can be drawn immidiately
112  laneShapeFrom,
113  laneShapeTo,
114  NUM_POINTS, getEdgeFrom()->getNBEdge()->getTurnDestination() == nbCon.toEdge,
115  (double) 5. * (double) getEdgeFrom()->getNBEdge()->getNumLanes(),
116  (double) 5. * (double) nbCon.toEdge->getNumLanes());
117  }
118  } else {
119  myShape.clear();
120  myShape.push_back(laneShapeFrom.positionAtOffset(laneShapeFrom.length() - 1));
121  myShape.push_back(laneShapeTo.positionAtOffset(1));
122  }
123  if (nbCon.haveVia && nbCon.shape.size() != 0) {
124  // create marker for interal junction waiting position (contPos)
125  const double orthoLength = 0.5;
126  Position pos = nbCon.shape.back();
127  myInternalJunctionMarker = nbCon.shape.getOrthogonal(pos, 10, true, 0.1);
128  if (myInternalJunctionMarker.length() < orthoLength) {
130  }
131  }
132  // Obtain lengths and shape rotations
133  int segments = (int) myShape.size() - 1;
134  if (segments >= 0) {
135  myShapeRotations.reserve(segments);
136  myShapeLengths.reserve(segments);
137  for (int i = 0; i < segments; ++i) {
138  const Position& f = myShape[i];
139  const Position& s = myShape[i + 1];
140  myShapeLengths.push_back(f.distanceTo2D(s));
141  myShapeRotations.push_back((double) atan2((s.x() - f.x()), (f.y() - s.y())) * (double) 180.0 / (double)M_PI);
142  }
143  }
144 }
145 
146 
147 Boundary
149  return myShape.getBoxBoundary();
150 }
151 
152 
153 GNEEdge*
155  return &(myFromLane->getParentEdge());
156 }
157 
158 
159 GNEEdge*
161  return &(myToLane->getParentEdge());
162 }
163 
164 
165 GNELane*
167  return myFromLane;
168 }
169 
170 
171 GNELane*
173  return myToLane;
174 }
175 
176 
177 int
179  return myFromLane->getIndex();
180 }
181 
182 
183 int
185  return myToLane->getIndex();
186 }
187 
188 
192 }
193 
194 
197  return NBConnection(getEdgeFrom()->getNBEdge(), getFromLaneIndex(),
198  getEdgeTo()->getNBEdge(), getToLaneIndex(),
199  (int)getNBEdgeConnection().tlLinkNo);
200 }
201 
202 
203 void
206 }
207 
208 
209 LinkState
211  return myLinkState;
212 }
213 
214 
217  if (myShape.size() > 0) {
218  return myShape;
219  } else {
221  }
222 }
223 
224 
225 void
229  nbCon.toEdge,
230  nbCon.fromLane,
231  nbCon.toLane,
232  nbCon.mayDefinitelyPass,
233  nbCon.tlID);
234 }
235 
236 
239  GUIGLObjectPopupMenu* ret = new GUIGLObjectPopupMenu(app, parent, *this);
240  buildPopupHeader(ret, app);
243  // build selection and show parameters menu
246  // build position copy entry
247  buildPositionCopyEntry(ret, false);
248  // create menu commands
249  FXMenuCommand* mcCustomShape = new FXMenuCommand(ret, "Set custom connection shape", 0, &parent, MID_GNE_CONNECTION_EDIT_SHAPE);
250  // check if menu commands has to be disabled
251  EditMode editMode = myNet->getViewNet()->getCurrentEditMode();
252  const bool wrongMode = (editMode == GNE_MODE_CONNECT || editMode == GNE_MODE_TLS || editMode == GNE_MODE_CREATE_EDGE);
253  if (wrongMode) {
254  mcCustomShape->disable();
255  }
256  return ret;
257 }
258 
259 
260 Boundary
263  b.grow(20);
264  return b;
265 }
266 
267 
268 void
270  // Check if connection must be drawed
271  if (myNet->getViewNet()->showConnections()) {
272  // Push draw matrix 1
273  glPushMatrix();
274  // Push name
275  glPushName(getGlID());
276  // Traslate matrix
277  glTranslated(0, 0, GLO_JUNCTION + 0.1); // must draw on top of junction
278  // Set color
280  // override with special colors (unless the color scheme is based on selection)
282  } else {
283  // Set color depending of the link state
285  }
286  // draw connection checking whether it is not too small
287  if (s.scale < 1.) {
288  // If it's small, dra a simple line
290  } else {
291  // draw a list of lines
293  glTranslated(0, 0, 0.1);
294  GLHelper::setColor(GLHelper::getColor().changedBrightness(51));;
296  }
297  // Pop name
298  glPopName();
299  // Pop draw matrix 1
300  glPopMatrix();
301  }
302 }
303 
304 
305 std::string
307  if (key == SUMO_ATTR_ID) {
308  // used by GNEReferenceCounter
309  // @note: may be called for connections without a valid nbCon reference
310  return getMicrosimID();
311  }
313  switch (key) {
314  case SUMO_ATTR_FROM:
315  return getEdgeFrom()->getID();
316  case SUMO_ATTR_TO:
317  return nbCon.toEdge->getID();
318  case SUMO_ATTR_FROM_LANE:
319  return toString(nbCon.toLane);
320  case SUMO_ATTR_TO_LANE:
321  return toString(nbCon.toLane);
322  case SUMO_ATTR_PASS:
323  return toString(nbCon.mayDefinitelyPass);
325  return toString(nbCon.keepClear);
326  case SUMO_ATTR_CONTPOS:
327  return toString(nbCon.contPos);
329  return toString(nbCon.uncontrolled);
331  return toString(nbCon.visibility);
333  return toString(nbCon.tlLinkNo);
334  case SUMO_ATTR_SPEED:
335  return toString(nbCon.speed);
337  return toString(nbCon.customShape);
338  default:
339  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
340  }
341 }
342 
343 
344 void
345 GNEConnection::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
346  switch (key) {
347  case SUMO_ATTR_FROM:
348  case SUMO_ATTR_TO:
349  case SUMO_ATTR_FROM_LANE:
350  case SUMO_ATTR_TO_LANE:
351  case SUMO_ATTR_PASS:
353  case SUMO_ATTR_CONTPOS:
356  case SUMO_ATTR_SPEED:
358  // no special handling
359  undoList->p_add(new GNEChange_Attribute(this, key, value));
360  break;
362  if (value != getAttribute(key)) {
363  // trigger GNEChange_TLS
364  undoList->p_begin("change tls linkIndex for connection");
365  // make a copy
366  std::set<NBTrafficLightDefinition*> defs = getEdgeFrom()->getNBEdge()->getToNode()->getControllingTLS();
367  for (NBTrafficLightDefinition* tlDef : defs) {
368  NBTrafficLightLogic* tllogic = tlDef->compute(OptionsCont::getOptions());
369  NBLoadedSUMOTLDef* newDef = new NBLoadedSUMOTLDef(tlDef, tllogic);
370  newDef->addConnection(getEdgeFrom()->getNBEdge(), getEdgeTo()->getNBEdge(),
371  getLaneFrom()->getIndex(), getLaneTo()->getIndex(), parse<int>(value), false);
372  std::vector<NBNode*> nodes = tlDef->getNodes();
373  for (NBNode* node : nodes) {
374  GNEJunction* junction = getNet()->retrieveJunction(node->getID());
375  undoList->add(new GNEChange_TLS(junction, tlDef, false), true);
376  undoList->add(new GNEChange_TLS(junction, newDef, true), true);
377  }
378  undoList->p_end();
379  }
380  }
381  break;
382  default:
383  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
384  }
385 }
386 
387 
388 bool
389 GNEConnection::isValid(SumoXMLAttr key, const std::string& value) {
390  // Currently ignored before implementation to avoid warnings
391  switch (key) {
392  case SUMO_ATTR_FROM:
393  case SUMO_ATTR_TO:
394  case SUMO_ATTR_FROM_LANE:
395  case SUMO_ATTR_TO_LANE:
396  return false;
397  case SUMO_ATTR_PASS:
398  return canParse<bool>(value);
400  return canParse<bool>(value);
401  case SUMO_ATTR_CONTPOS:
402  return canParse<double>(value);
404  return canParse<bool>(value);
406  return canParse<double>(value) && isPositive<double>(value);
408  if (getNBEdgeConnection().tlID != "" && canParse<int>(value) && parse<int>(value) >= 0
409  && getEdgeFrom()->getNBEdge()->getToNode()->getControllingTLS().size() > 0) {
413  return logic != 0 && logic->getNumLinks() > parse<int>(value);
414  } else {
415  return false;
416  }
417  case SUMO_ATTR_SPEED:
418  return canParse<double>(value) && isPositive<double>(value);
419  case SUMO_ATTR_CUSTOMSHAPE: {
420  bool ok = true;
421  PositionVector shape = GeomConvHelper::parseShapeReporting(value, "user-supplied shape", 0, ok, true);
422  return ok;
423  }
424  default:
425  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
426  }
427 }
428 
429 
430 void
431 GNEConnection::setAttribute(SumoXMLAttr key, const std::string& value) {
433  switch (key) {
434  case SUMO_ATTR_PASS:
435  nbCon.mayDefinitelyPass = parse<bool>(value);
436  break;
438  nbCon.keepClear = parse<bool>(value);
439  break;
441  nbCon.uncontrolled = parse<bool>(value);
442  break;
443  case SUMO_ATTR_CONTPOS:
444  nbCon.contPos = parse<double>(value);
445  break;
447  nbCon.visibility = parse<double>(value);
448  break;
449  case SUMO_ATTR_SPEED:
450  nbCon.speed = parse<double>(value);
451  break;
452  case SUMO_ATTR_CUSTOMSHAPE: {
453  bool ok;
454  nbCon.customShape = GeomConvHelper::parseShapeReporting(value, "user-supplied shape", 0, ok, true);
455  updateGeometry();
456  myNet->getViewNet()->update();
457  break;
458  }
459  default:
460  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
461  }
462 }
463 
464 
465 /****************************************************************************/
int NUM_POINTS
int tlLinkNo
The index of this connection within the controlling traffic light.
Definition: NBEdge.h:195
void updateGeometry()
update pre-computed geometry information
PositionVector getShape() const
get Position vector calculated in updateGeometry()
virtual void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
std::vector< double > myShapeLengths
The lengths of the shape parts.
A structure which describes a connection between edges or lanes.
Definition: NBEdge.h:161
LinkState getLinkState(const NBEdge *incoming, NBEdge *outgoing, int fromLane, int toLane, bool mayDefinitelyPass, const std::string &tlID) const
get link state
Definition: NBNode.cpp:1606
int toLane
The lane the connections yields in.
Definition: NBEdge.h:189
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.
whether a given shape is user-defined
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:183
void append(const PositionVector &v, double sameThreshold=2.0)
PositionVector getOrthogonal(const Position &p, double extend, bool before, double length=1.0) const
return orthogonal through p (extending this vector if necessary)
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:186
GNENet * getNet() const
get Net in which this element is placed
int getNumLinks()
Returns the number of participating links.
A loaded (complete) traffic light logic.
LinkState getLinkState() const
get LinkState
double distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition: Position.h:249
A SUMO-compliant built logic for a traffic light.
Stores the information about how to visualize structures.
connectio between two lanes
a connection
double y() const
Returns the y-position.
Definition: Position.h:67
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:62
NBTrafficLightLogic * compute(OptionsCont &oc)
Computes the traffic light logic.
mode for editing tls
Definition: GNEViewNet.h:62
The base class for traffic light logic definitions.
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:53
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:84
const std::vector< NBEdge::Lane > & getLanes() const
Returns the lane definitions.
Definition: NBEdge.h:569
bool mayDefinitelyPass
Information about being definitely free to drive (on-ramps)
Definition: NBEdge.h:198
GNELane * myFromLane
incoming lane of this connection
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
edit junction shape
Definition: GUIAppEnum.h:729
double visibility
custom foe visiblity for connection
Definition: NBEdge.h:207
const std::string & getID() const
Returns the id.
Definition: Named.h:74
void buildShowParamsPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to open the parameter window.
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.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:47
PositionVector customShape
custom shape for connection
Definition: NBEdge.h:213
GNEEdge * getEdgeTo() const
get the name of the edge the vehicles may reach when leaving "from"
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
GUIGLObjectPopupMenu * getPopUpMenu(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own popup-menu.
GNELane * getLaneFrom() const
get lane of the incoming lane
double area() const
Returns the area (0 for non-closed)
PositionVector shape
shape of Connection
Definition: NBEdge.h:219
bool keepClear
whether the junction must be kept clear when using this connection
Definition: NBEdge.h:201
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:683
double speed
custom speed for connection
Definition: NBEdge.h:210
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
void updateID()
update internal ID of Connection
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:449
GNEEdge & getParentEdge()
Returns underlying parent edge.
Definition: GNELane.cpp:1121
std::vector< double > myShapeRotations
LinkState myLinkState
Linkstate.
std::string tlID
The id of the traffic light that controls this connection.
Definition: NBEdge.h:192
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:412
int fromLane
The lane the connections starts at.
Definition: NBEdge.h:183
GNEConnection(GNELane *from, GNELane *to)
void p_end()
End undo command sub-group. If the sub-group is still empty, it will be deleted; otherwise, the sub-group will be added as a new command into parent group. A matching begin() must have been called previously.
Definition: GNEUndoList.cpp:91
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
GNEJunction * retrieveJunction(const std::string &id, bool failHard=true)
get junction by id
Definition: GNENet.cpp:889
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
void addConnection(NBEdge *from, NBEdge *to, int fromLane, int toLane, int linkIndex, bool reconstruct=true)
Adds a connection and immediately informs the edges.
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.
Boundary & grow(double by)
extends the boundary by the given amount
Definition: Boundary.cpp:301
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:276
EditMode
Definition: GNEViewNet.h:46
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:460
EditMode getCurrentEditMode() const
get the current edit mode
const PositionVector & getShape() const
retrieve the junction shape
Definition: NBNode.cpp:1726
A road/street connecting two junctions (netedit-version)
Definition: GNEEdge.h:56
void extrapolate(const double val, const bool onlyFirst=false, const bool onlyLast=false)
extrapolate position vector
double length() const
Returns the length.
PositionVector viaShape
shape of via
Definition: NBEdge.h:231
NBConnection getNBConnection() const
get NBConnection
virtual void setMicrosimID(const std::string &newID)
Changes the microsimID of the object.
#define M_PI
Definition: odrSpiral.cpp:40
const std::set< NBTrafficLightDefinition * > & getControllingTLS() const
Returns the traffic lights that were assigned to this node (The set of tls that control this node) ...
Definition: NBNode.h:302
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
bool showConnections()
show connections over junctions
Definition: GNEViewNet.cpp:334
static const RGBColor selectedConnectionColor
color of selected connection
Definition: GNENet.h:113
PositionVector myInternalJunctionMarker
waiting position for internal junction
double contPos
custom position for internal junction on this connection
Definition: NBEdge.h:204
bool uncontrolled
check if Connection is uncontrolled
Definition: NBEdge.h:243
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
This functions has to be implemented in all GNEAttributeCarriers.
Represents a single node (junction) during network building.
Definition: NBNode.h:74
GUIGlID getGlID() const
Returns the numerical id of the object.
void updateLinkState()
recompute cached myLinkState
link: the index of the link within the traffic light
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.
mode for connecting lanes
Definition: GNEViewNet.h:60
mode for creating new edges
Definition: GNEViewNet.h:50
NBEdge * getNBEdge()
returns the internal NBEdge
Definition: GNEEdge.cpp:412
bool haveVia
check if Connection have a Via
Definition: NBEdge.h:225
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
GUISelectedStorage gSelected
A global holder of selected objects.
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:1063
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:433
static PositionVector parseShapeReporting(const std::string &shpdef, const std::string &objecttype, const char *objectid, bool &ok, bool allowEmpty, bool report=true)
Builds a PositionVector from a string representation, reporting occured errors.
void buildPopupHeader(GUIGLObjectPopupMenu *ret, GUIMainWindow &app, bool addSeparator=true)
Builds the header.
GNEViewNet * getViewNet() const
get view net
Definition: GNENet.cpp:1607
a junction
static RGBColor getColor()
gets the gl-color
Definition: GLHelper.cpp:455
SumoXMLTag getTag() const
get XML Tag assigned to this object