SUMO - Simulation of Urban MObility
NBEdge.h
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // The representation of a single edge during network building
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 #ifndef NBEdge_h
00023 #define NBEdge_h
00024 
00025 
00026 // ===========================================================================
00027 // included modules
00028 // ===========================================================================
00029 #ifdef _MSC_VER
00030 #include <windows_config.h>
00031 #else
00032 #include <config.h>
00033 #endif
00034 
00035 #include <map>
00036 #include <vector>
00037 #include <string>
00038 #include <set>
00039 #include "NBCont.h"
00040 #include <utils/common/Named.h>
00041 #include <utils/common/UtilExceptions.h>
00042 #include <utils/common/VectorHelper.h>
00043 #include <utils/geom/Bresenham.h>
00044 #include <utils/geom/PositionVector.h>
00045 #include <utils/geom/Line.h>
00046 #include <utils/common/SUMOVehicleClass.h>
00047 #include <utils/xml/SUMOXMLDefinitions.h>
00048 #include "NBHelpers.h"
00049 
00050 
00051 // ===========================================================================
00052 // class declarations
00053 // ===========================================================================
00054 class NBNode;
00055 class NBConnection;
00056 class NBNodeCont;
00057 class NBEdgeCont;
00058 class OutputDevice;
00059 class GNELane;
00060 
00061 
00062 // ===========================================================================
00063 // class definitions
00064 // ===========================================================================
00069 class NBEdge : public Named {
00071     friend class NBEdgeSuccessorBuilder;
00072     friend class NBEdgeCont;
00073 
00075     friend class GNELane;
00076     friend class GNEEdge;
00077 
00078 public:
00079 
00087     enum EdgeBuildingStep {
00089         INIT_REJECT_CONNECTIONS,
00091         INIT,
00093         EDGE2EDGES,
00095         LANES2EDGES,
00097         LANES2LANES_RECHECK,
00099         LANES2LANES_DONE,
00101         LANES2LANES_USER
00102     };
00103 
00104 
00108     enum Lane2LaneInfoType {
00110         L2L_COMPUTED,
00112         L2L_USER,
00114         L2L_VALIDATED
00115     };
00116 
00117 
00121     struct Lane {
00122         Lane(NBEdge* e) : 
00123             speed(e->getSpeed()), permissions(SVCFreeForAll), preferred(0), 
00124             offset(e->getOffset()), width(e->getWidth()) {}
00126         PositionVector shape;
00128         SUMOReal speed;
00130         SVCPermissions permissions;
00132         SVCPermissions preferred;
00134         SUMOReal offset;
00136         SUMOReal width;
00137     };
00138 
00139 
00143     struct Connection {
00149         Connection(int fromLane_, NBEdge* toEdge_, int toLane_)
00150             : fromLane(fromLane_), toEdge(toEdge_), toLane(toLane_),
00151               mayDefinitelyPass(false), haveVia(false) { }
00152 
00153         ~Connection() { }
00154 
00155 
00157         int fromLane;
00159         NBEdge* toEdge;
00161         int toLane;
00163         std::string tlID;
00165         unsigned int tlLinkNo;
00167         bool mayDefinitelyPass;
00168 
00169 
00170         std::string id;
00171         PositionVector shape;
00172         SUMOReal vmax;
00173 
00174         bool haveVia;
00175         std::string viaID;
00176         SUMOReal viaVmax;
00177         PositionVector viaShape;
00178 
00179         std::string foeInternalLanes;
00180         std::string foeIncomingLanes;
00181 
00182     };
00183 
00185     static std::pair<SUMOReal, SUMOReal> laneOffset(const Position& from,
00186             const Position& to, SUMOReal lanewidth, unsigned int lane,
00187             size_t noLanes, LaneSpreadFunction lsf, bool leftHand);
00188 
00190     static const SUMOReal UNSPECIFIED_WIDTH;
00192     static const SUMOReal UNSPECIFIED_OFFSET;
00194     static const SUMOReal UNSPECIFIED_LOADED_LENGTH;
00195 
00196 
00197 public:
00216     NBEdge(const std::string& id,
00217            NBNode* from, NBNode* to, std::string type,
00218            SUMOReal speed, unsigned int nolanes, int priority,
00219            SUMOReal width, SUMOReal offset,
00220            const std::string& streetName = "",
00221            LaneSpreadFunction spread = LANESPREAD_RIGHT) ;
00222 
00223 
00244     NBEdge(const std::string& id,
00245            NBNode* from, NBNode* to, std::string type,
00246            SUMOReal speed, unsigned int nolanes, int priority,
00247            SUMOReal width, SUMOReal offset,
00248            PositionVector geom,
00249            const std::string& streetName = "",
00250            LaneSpreadFunction spread = LANESPREAD_RIGHT,
00251            bool tryIgnoreNodePositions = false) ;
00252 
00262     NBEdge(const std::string& id,
00263            NBNode* from, NBNode* to,
00264            NBEdge* tpl);
00265 
00266 
00269     ~NBEdge() ;
00270 
00271 
00287     void reinit(NBNode* from, NBNode* to, const std::string& type,
00288                 SUMOReal speed, unsigned int nolanes, int priority,
00289                 PositionVector geom, SUMOReal width, SUMOReal offset,
00290                 const std::string& streetName,
00291                 LaneSpreadFunction spread = LANESPREAD_RIGHT,
00292                 bool tryIgnoreNodePositions = false) ;
00293 
00294 
00295 
00298 
00303     void reshiftPosition(SUMOReal xoff, SUMOReal yoff) ;
00305 
00306 
00307 
00310     void setLeftHanded() {
00311         myAmLeftHand = true;
00312     }
00313 
00314 
00316 
00317 
00321     unsigned int getNumLanes() const {
00322         return (unsigned int) myLanes.size();
00323     }
00324 
00325 
00329     int getPriority() const {
00330         return myPriority;
00331     }
00332 
00333 
00337     NBNode* getFromNode() const {
00338         return myFrom;
00339     }
00340 
00341 
00345     NBNode* getToNode() const {
00346         return myTo;
00347     }
00348 
00349 
00357     SUMOReal getAngle() const {
00358         return myAngle;
00359     }
00360 
00361 
00365     SUMOReal getLength() const {
00366         return myLength;
00367     }
00368 
00369 
00374     SUMOReal getLoadedLength() const {
00375         return myLoadedLength > 0 ? myLoadedLength : myLength;
00376     }
00377 
00378 
00382     bool hasLoadedLength() const {
00383         return myLoadedLength > 0;
00384     }
00385 
00386 
00390     SUMOReal getSpeed() const {
00391         return mySpeed;
00392     }
00393 
00394 
00400     EdgeBuildingStep getStep() const {
00401         return myStep;
00402     }
00403 
00404 
00408     SUMOReal getWidth() const {
00409         return myWidth;
00410     }
00411 
00412 
00415     const std::string& getStreetName() const {
00416         return myStreetName;
00417     }
00418 
00421     void setStreetName(const std::string& name) {
00422         myStreetName = name;
00423     }
00424 
00428     SUMOReal getOffset() const {
00429         return myOffset;
00430     }
00431 
00432 
00436     const std::string& getTypeName() const {
00437         return myType;
00438     }
00439 
00440 
00444     const std::vector<NBEdge::Lane> &getLanes() const {
00445         return myLanes;
00446     }
00448 
00449 
00450 
00452 
00453 
00457     const PositionVector& getGeometry() const {
00458         return myGeom;
00459     }
00460 
00461 
00463     const PositionVector getInnerGeometry() const;
00464 
00465 
00468     bool hasDefaultGeometry() const;
00469 
00470 
00476     bool hasDefaultGeometryEndpoints() const;
00477 
00478 
00489     void setGeometry(const PositionVector& g, bool inner = false) ;
00490 
00491 
00501     void addGeometryPoint(int index, const Position& p) ;
00502 
00503 
00513     void computeEdgeShape() ;
00514 
00515 
00519     const PositionVector& getLaneShape(unsigned int i) const ;
00520 
00521 
00526     void setLaneSpreadFunction(LaneSpreadFunction spread) ;
00527 
00528 
00533     LaneSpreadFunction getLaneSpreadFunction() const {
00534         return myLaneSpreadFunction;
00535     }
00536 
00537 
00543     bool splitGeometry(NBEdgeCont& ec, NBNodeCont& nc);
00545 
00546 
00547 
00550 
00565     bool addEdge2EdgeConnection(NBEdge* dest);
00566 
00567 
00588     bool addLane2LaneConnection(unsigned int fromLane, NBEdge* dest,
00589                                 unsigned int toLane, Lane2LaneInfoType type,
00590                                 bool mayUseSameDestination = false,
00591                                 bool mayDefinitelyPass = false);
00592 
00593 
00611     bool addLane2LaneConnections(unsigned int fromLane,
00612                                  NBEdge* dest, unsigned int toLane, unsigned int no,
00613                                  Lane2LaneInfoType type, bool invalidatePrevious = false,
00614                                  bool mayDefinitelyPass = false);
00615 
00616 
00627     void setConnection(unsigned int lane, NBEdge* destEdge,
00628                        unsigned int destLane,
00629                        Lane2LaneInfoType type,
00630                        bool mayUseSameDestination = false,
00631                        bool mayDefinitelyPass = false);
00632 
00633 
00634 
00643     std::vector<Connection> getConnectionsFromLane(unsigned int lane) const;
00644 
00645 
00653     bool hasConnectionTo(NBEdge* destEdge, unsigned int destLane) const;
00654 
00655 
00662     bool isConnectedTo(NBEdge* e);
00663 
00664 
00668     const std::vector<Connection> &getConnections() const {
00669         return myConnections;
00670     }
00671 
00672 
00676     std::vector<Connection> &getConnections() {
00677         return myConnections;
00678     }
00679 
00680 
00684     const EdgeVector* getConnectedSorted();
00685 
00686 
00690     EdgeVector getConnectedEdges() const;
00691 
00692 
00696     std::vector<int> getConnectionLanes(NBEdge* currentOutgoing) const;
00697 
00698 
00701     void sortOutgoingConnectionsByAngle();
00702 
00703 
00706     void sortOutgoingConnectionsByIndex();
00707 
00708 
00714     void remapConnections(const EdgeVector& incoming);
00715 
00716 
00722     void removeFromConnections(NBEdge* toEdge, int fromLane = -1, int toLane = -1);
00723 
00724     void invalidateConnections(bool reallowSetting = false);
00725 
00726     void replaceInConnections(NBEdge* which, NBEdge* by, unsigned int laneOff);
00727     void replaceInConnections(NBEdge* which, const std::vector<NBEdge::Connection> &origConns);
00728     void copyConnectionsFrom(NBEdge* src);
00730 
00731 
00732 
00738     bool isTurningDirectionAt(const NBNode* n, const NBEdge* const edge) const ;
00739     void setTurningDestination(NBEdge* e);
00740 
00741 
00742 
00745 
00748     void setAsMacroscopicConnector() {
00749         myAmMacroscopicConnector = true;
00750     }
00751 
00752 
00756     bool isMacroscopicConnector() const {
00757         return myAmMacroscopicConnector;
00758     }
00759 
00760 
00763     void setIsInnerEdge() {
00764         myAmInnerEdge = true;
00765     }
00766 
00767 
00771     bool isInnerEdge() const {
00772         return myAmInnerEdge;
00773     }
00775 
00776 
00777  
00778 
00784     void setJunctionPriority(const NBNode* const node, int prio);
00785 
00786 
00796     int getJunctionPriority(const NBNode* const node) const;
00797 
00798     void setLoadedLength(SUMOReal val);
00799     void dismissVehicleClassInformation();
00800 
00801 
00802     const std::string& getTypeID() const {
00803         return myType;
00804     }
00805 
00807     bool needsLaneSpecificOutput() const;
00808 
00810     bool hasRestrictions() const;
00811 
00813     bool hasLaneSpecificPermissions() const;
00814 
00816     bool hasLaneSpecificSpeed() const;
00817 
00819     bool hasLaneSpecificOffset() const;
00820 
00822     bool hasLaneSpecificWidth() const;
00823 
00825     bool computeEdge2Edges(bool noLeftMovers);
00826 
00828     bool computeLanes2Edges();
00829 
00832     bool recheckLanes();
00833 
00842     void appendTurnaround(bool noTLSControlled) ;
00843 
00844 
00845 
00849     NBNode* tryGetNodeAtPosition(SUMOReal pos, SUMOReal tolerance = 5.0) const;
00850 
00851 
00852     SUMOReal getMaxLaneOffset();
00853 
00854     Position getMinLaneOffsetPositionAt(NBNode* node, SUMOReal width) const;
00855     Position getMaxLaneOffsetPositionAt(NBNode* node, SUMOReal width) const;
00856 
00857 
00858     bool lanesWereAssigned() const;
00859 
00860     bool mayBeTLSControlled(int fromLane, NBEdge* toEdge, int toLane) const ;
00861 
00863     bool setControllingTLInformation(const NBConnection& c, const std::string& tlID);
00864 
00866     void clearControllingTLInformation();
00867 
00868     void addCrossingPointsAsIncomingWithGivenOutgoing(NBEdge* o,
00869             PositionVector& into);
00870 
00871     SUMOReal width() const;
00872 
00873     PositionVector getCWBoundaryLine(const NBNode& n, SUMOReal offset) const;
00874     PositionVector getCCWBoundaryLine(const NBNode& n, SUMOReal offset) const;
00875 
00876     bool expandableBy(NBEdge* possContinuation) const;
00877     void append(NBEdge* continuation);
00878 
00879     bool hasSignalisedConnectionTo(const NBEdge* const e) const ;
00880 
00881 
00882     void moveOutgoingConnectionsFrom(NBEdge* e, unsigned int laneOff);
00883 
00884     NBEdge* getTurnDestination() const;
00885 
00886     std::string getLaneID(unsigned int lane) const;
00887 
00888     SUMOReal getLaneSpeed(unsigned int lane) const;
00889 
00890     bool isNearEnough2BeJoined2(NBEdge* e, SUMOReal threshold) const;
00891 
00892 
00900     SUMOReal getAngleAtNode(const NBNode * const node) const;
00901 
00902 
00903     void incLaneNo(unsigned int by);
00904 
00905     void decLaneNo(unsigned int by, int dir = 0);
00906 
00907     void markAsInLane2LaneState();
00908 
00910     void setPermissions(SVCPermissions permissions, int lane = -1);
00911 
00912     void setPreferredVehicleClass(SVCPermissions permissions, int lane = -1);
00913 
00915     void allowVehicleClass(int lane, SUMOVehicleClass vclass);
00917     void disallowVehicleClass(int lane, SUMOVehicleClass vclass);
00918 
00919     void preferVehicleClass(int lane, SUMOVehicleClass vclass);
00920 
00921 
00922 
00924     void setWidth(int lane, SUMOReal width);
00925 
00927     void setOffset(int lane, SUMOReal offset);
00928 
00930     void setSpeed(int lane, SUMOReal offset);
00931 
00933     SVCPermissions getPermissions(int lane = -1) const;
00934 
00935     void disableConnection4TLS(int fromLane, NBEdge* toEdge, int toLane);
00936 
00937 
00938     // returns a reference to the internal structure for the convenience of NETEDIT
00939     Lane& getLaneStruct(unsigned int lane) {
00940         return myLanes[lane];
00941     }
00942 
00943     // returns a reference to the internal structure for the convenience of NETEDIT
00944     const Lane& getLaneStruct(unsigned int lane) const {
00945         return myLanes[lane];
00946     }
00947 
00948     /* declares connections as fully loaded. This is needed to avoid recomputing connections
00949     * if an edge has no connections intentionally. */
00950     void declareConnectionsAsLoaded() {
00951         myStep = LANES2LANES_USER;
00952     }
00953 
00954     void buildInnerEdges(const NBNode& n, unsigned int noInternalNoSplits, unsigned int& lno, unsigned int& splitNo);
00955 
00956 
00957 private:
00963     class ToEdgeConnectionsAdder : public Bresenham::BresenhamCallBack {
00964     private:
00966         std::map<NBEdge*, std::vector<unsigned int> > myConnections;
00967 
00969         const EdgeVector& myTransitions;
00970 
00971     public:
00973         ToEdgeConnectionsAdder(const EdgeVector& transitions)
00974             : myTransitions(transitions) { }
00975 
00977         ~ToEdgeConnectionsAdder() { }
00978 
00980         void execute(const unsigned int lane, const unsigned int virtEdge) ;
00981 
00982         const std::map<NBEdge*, std::vector<unsigned int> > &getBuiltConnections() const {
00983             return myConnections;
00984         }
00985 
00986     private:
00988         ToEdgeConnectionsAdder(const ToEdgeConnectionsAdder&);
00989 
00991         ToEdgeConnectionsAdder& operator=(const ToEdgeConnectionsAdder&);
00992 
00993     };
00994 
00995 
01003     class MainDirections {
01004     public:
01006         enum Direction { DIR_RIGHTMOST, DIR_LEFTMOST, DIR_FORWARD };
01007 
01010         std::vector<Direction> myDirs;
01011 
01012     public:
01014         MainDirections(const EdgeVector& outgoing,
01015                        NBEdge* parent, NBNode* to);
01016 
01018         ~MainDirections();
01019 
01022         bool empty() const;
01023 
01026         bool includes(Direction d) const;
01027 
01028     private:
01030         MainDirections(const MainDirections&);
01031 
01033         MainDirections& operator=(const MainDirections&);
01034 
01035     };
01036 
01038     PositionVector computeLaneShape(unsigned int lane) throw(InvalidArgument);
01039 
01041     std::pair<SUMOReal, SUMOReal> laneOffset(const Position& from,
01042             const Position& to, SUMOReal lanewidth, unsigned int lane) throw(InvalidArgument);
01043 
01044     void computeLaneShapes() ;
01045 
01046 
01047 
01048 private:
01064     void init(unsigned int noLanes, bool tryIgnoreNodePositions) ;
01065 
01066 
01068     void divideOnEdges(const EdgeVector* outgoing);
01069 
01072     std::vector<unsigned int> *preparePriorities(
01073         const EdgeVector* outgoing);
01074 
01076     unsigned int computePrioritySum(std::vector<unsigned int> *priorities);
01077 
01078 
01081 
01084     void moveConnectionToLeft(unsigned int lane);
01085 
01088     void moveConnectionToRight(unsigned int lane);
01090 
01091 
01096     PositionVector startShapeAt(const PositionVector& laneShape, const NBNode* startNode, unsigned int laneIndex) const;
01097 
01098 
01099 private:
01103     EdgeBuildingStep myStep;
01104 
01106     std::string myType;
01107 
01109     NBNode* myFrom, *myTo;
01110 
01112     SUMOReal myLength;
01113 
01115     SUMOReal myAngle;
01116 
01118     int myPriority;
01119 
01121     SUMOReal mySpeed;
01122 
01126     std::vector<Connection> myConnections;
01127 
01129     NBEdge* myTurnDestination;
01130 
01132     int myFromJunctionPriority;
01133 
01135     int myToJunctionPriority;
01136 
01138     PositionVector myGeom;
01139 
01141     LaneSpreadFunction myLaneSpreadFunction;
01142 
01144     SUMOReal myOffset;
01145 
01147     SUMOReal myWidth;
01148 
01152     std::vector<Lane> myLanes;
01153 
01155     SUMOReal myLoadedLength;
01156 
01158     bool myAmLeftHand;
01159 
01160 
01162     bool myAmInnerEdge;
01163 
01165     bool myAmMacroscopicConnector;
01166 
01167     struct TLSDisabledConnection {
01168         int fromLane;
01169         NBEdge* to;
01170         int toLane;
01171     };
01172 
01173     std::vector<TLSDisabledConnection> myTLSDisabledConnections;
01174 
01175 
01177     std::string myStreetName;
01178 
01179 
01180 public:
01184     class tls_disable_finder {
01185     public:
01187         tls_disable_finder(const TLSDisabledConnection& tpl) : myDefinition(tpl) { }
01188 
01189         bool operator()(const TLSDisabledConnection& e) const {
01190             if (e.to != myDefinition.to) {
01191                 return false;
01192             }
01193             if (e.fromLane != myDefinition.fromLane) {
01194                 return false;
01195             }
01196             if (e.toLane != myDefinition.toLane) {
01197                 return false;
01198             }
01199             return true;
01200         }
01201 
01202     private:
01203         TLSDisabledConnection myDefinition;
01204 
01205     private:
01207         tls_disable_finder& operator=(const tls_disable_finder& s);
01208 
01209     };
01210 
01211 
01212 
01216     class connections_toedge_finder {
01217     public:
01219         connections_toedge_finder(NBEdge* const edge2find) : myEdge2Find(edge2find) { }
01220 
01221         bool operator()(const Connection& c) const {
01222             return c.toEdge == myEdge2Find;
01223         }
01224 
01225     private:
01226         NBEdge* const myEdge2Find;
01227 
01228     private:
01230         connections_toedge_finder& operator=(const connections_toedge_finder& s);
01231 
01232     };
01233 
01237     class connections_toedgelane_finder {
01238     public:
01240         connections_toedgelane_finder(NBEdge* const edge2find, int lane2find) : myEdge2Find(edge2find), myLane2Find(lane2find) { }
01241 
01242         bool operator()(const Connection& c) const {
01243             return c.toEdge == myEdge2Find && c.toLane == myLane2Find;
01244         }
01245 
01246     private:
01247         NBEdge* const myEdge2Find;
01248         int myLane2Find;
01249 
01250     private:
01252         connections_toedgelane_finder& operator=(const connections_toedgelane_finder& s);
01253 
01254     };
01255 
01256 
01260     class connections_finder {
01261     public:
01263         connections_finder(int fromLane, NBEdge* const edge2find, int lane2find) : myFromLane(fromLane), myEdge2Find(edge2find), myLane2Find(lane2find) { }
01264 
01265         bool operator()(const Connection& c) const {
01266             return c.fromLane == myFromLane && c.toEdge == myEdge2Find && c.toLane == myLane2Find;
01267         }
01268 
01269     private:
01270         int myFromLane;
01271         NBEdge* const myEdge2Find;
01272         int myLane2Find;
01273 
01274     private:
01276         connections_finder& operator=(const connections_finder& s);
01277 
01278     };
01279 
01283     class connections_fromlane_finder {
01284     public:
01286         connections_fromlane_finder(int lane2find) : myLane2Find(lane2find) { }
01287 
01288         bool operator()(const Connection& c) const {
01289             return c.fromLane == myLane2Find;
01290         }
01291 
01292     private:
01293         int myLane2Find;
01294 
01295     private:
01297         connections_fromlane_finder& operator=(const connections_fromlane_finder& s);
01298 
01299     };
01300 
01304     static bool connections_sorter(const Connection& c1, const Connection& c2) {
01305         if (c1.fromLane != c2.fromLane) {
01306             return c1.fromLane < c2.fromLane;
01307         }
01308         if (c1.toEdge != c2.toEdge) {
01309             return c1.toEdge->getID().compare(c1.toEdge->getID()) < 0;
01310         }
01311         return c1.toLane < c2.toLane;
01312     }
01313 
01318     class connections_relative_edgelane_sorter {
01319     public:
01321         explicit connections_relative_edgelane_sorter(NBEdge* e, NBNode* n)
01322             : myEdge(e), myNode(n) {}
01323 
01324     public:
01326         int operator()(const Connection& c1, const Connection& c2) const {
01327             if (c1.toEdge != c2.toEdge) {
01328                 SUMOReal relAngle1 = NBHelpers::normRelAngle(
01329                                          myEdge->getAngle(), c1.toEdge->getAngle());
01330                 SUMOReal relAngle2 = NBHelpers::normRelAngle(
01331                                          myEdge->getAngle(), c2.toEdge->getAngle());
01332                 return relAngle1 > relAngle2;
01333             }
01334             return c1.toLane < c2.toLane;
01335         }
01336 
01337     private:
01339         NBEdge* myEdge;
01340 
01342         NBNode* myNode;
01343 
01344     };
01345 
01346 private:
01348     NBEdge(const NBEdge& s);
01349 
01351     NBEdge& operator=(const NBEdge& s);
01352 
01353 
01354 };
01355 
01356 
01357 #endif
01358 
01359 /****************************************************************************/
01360 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines