|
SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // The representation of a single edge during network building 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors 00011 /****************************************************************************/ 00012 // 00013 // This program is free software; you can redistribute it and/or modify 00014 // it under the terms of the GNU General Public License as published by 00015 // the Free Software Foundation; either version 2 of the License, or 00016 // (at your option) any later version. 00017 // 00018 /****************************************************************************/ 00019 #ifndef NBEdge_h 00020 #define NBEdge_h 00021 00022 00023 // =========================================================================== 00024 // included modules 00025 // =========================================================================== 00026 #ifdef _MSC_VER 00027 #include <windows_config.h> 00028 #else 00029 #include <config.h> 00030 #endif 00031 00032 #include <map> 00033 #include <vector> 00034 #include <string> 00035 #include <set> 00036 #include "NBCont.h" 00037 #include <utils/common/Named.h> 00038 #include <utils/common/UtilExceptions.h> 00039 #include <utils/common/VectorHelper.h> 00040 #include <utils/geom/Bresenham.h> 00041 #include <utils/geom/PositionVector.h> 00042 #include <utils/geom/Line.h> 00043 #include <utils/common/SUMOVehicleClass.h> 00044 #include <utils/xml/SUMOXMLDefinitions.h> 00045 #include "NBHelpers.h" 00046 00047 00048 // =========================================================================== 00049 // class declarations 00050 // =========================================================================== 00051 class NBNode; 00052 class NBConnection; 00053 class NBNodeCont; 00054 class NBEdgeCont; 00055 class OutputDevice; 00056 class GNELane; 00057 00058 00059 // =========================================================================== 00060 // class definitions 00061 // =========================================================================== 00066 class NBEdge : public Named { 00068 friend class NBEdgeSuccessorBuilder; 00069 friend class NBEdgeCont; 00070 00072 friend class GNELane; 00073 friend class GNEEdge; 00074 00075 public: 00076 00084 enum EdgeBuildingStep { 00086 INIT_REJECT_CONNECTIONS, 00088 INIT, 00090 EDGE2EDGES, 00092 LANES2EDGES, 00094 LANES2LANES_RECHECK, 00096 LANES2LANES_DONE, 00098 LANES2LANES_USER 00099 }; 00100 00101 00105 enum Lane2LaneInfoType { 00107 L2L_COMPUTED, 00109 L2L_USER, 00111 L2L_VALIDATED 00112 }; 00113 00114 00118 struct Lane { 00120 PositionVector shape; 00122 SUMOReal speed; 00124 SUMOVehicleClasses allowed; 00126 SUMOVehicleClasses notAllowed; 00128 SUMOVehicleClasses preferred; 00130 SUMOReal offset; 00132 SUMOReal width; 00133 }; 00134 00135 00139 struct Connection { 00145 Connection(int fromLane_, NBEdge* toEdge_, int toLane_) throw() 00146 : fromLane(fromLane_), toEdge(toEdge_), toLane(toLane_), 00147 mayDefinitelyPass(false), haveVia(false) { } 00148 00149 ~Connection() { } 00150 00151 00153 int fromLane; 00155 NBEdge* toEdge; 00157 int toLane; 00159 std::string tlID; 00161 unsigned int tlLinkNo; 00163 bool mayDefinitelyPass; 00164 00165 00166 std::string id; 00167 PositionVector shape; 00168 SUMOReal vmax; 00169 00170 bool haveVia; 00171 std::string viaID; 00172 SUMOReal viaVmax; 00173 PositionVector viaShape; 00174 00175 std::string crossingNames; 00176 std::string sourceNames; 00177 00178 }; 00179 00181 static std::pair<SUMOReal, SUMOReal> laneOffset(const Position& from, 00182 const Position& to, SUMOReal lanewidth, unsigned int lane, 00183 size_t noLanes, LaneSpreadFunction lsf, bool leftHand); 00184 00186 static const SUMOReal UNSPECIFIED_WIDTH; 00188 static const SUMOReal UNSPECIFIED_OFFSET; 00190 static const SUMOReal UNSPECIFIED_LOADED_LENGTH; 00191 00192 00193 public: 00212 NBEdge(const std::string& id, 00213 NBNode* from, NBNode* to, std::string type, 00214 SUMOReal speed, unsigned int nolanes, int priority, 00215 SUMOReal width, SUMOReal offset, 00216 const std::string& streetName="", 00217 LaneSpreadFunction spread=LANESPREAD_RIGHT) throw(ProcessError); 00218 00219 00240 NBEdge(const std::string& id, 00241 NBNode* from, NBNode* to, std::string type, 00242 SUMOReal speed, unsigned int nolanes, int priority, 00243 SUMOReal width, SUMOReal offset, 00244 PositionVector geom, 00245 const std::string& streetName="", 00246 LaneSpreadFunction spread=LANESPREAD_RIGHT, 00247 bool tryIgnoreNodePositions=false) throw(ProcessError); 00248 00258 NBEdge(const std::string& id, 00259 NBNode* from, NBNode* to, 00260 NBEdge* tpl); 00261 00262 00265 ~NBEdge() throw(); 00266 00267 00282 void reinit(NBNode* from, NBNode* to, const std::string& type, 00283 SUMOReal speed, unsigned int nolanes, int priority, 00284 PositionVector geom, SUMOReal width, SUMOReal offset, 00285 LaneSpreadFunction spread=LANESPREAD_RIGHT, 00286 bool tryIgnoreNodePositions=false) throw(ProcessError); 00287 00288 00289 00292 00297 void reshiftPosition(SUMOReal xoff, SUMOReal yoff) throw(); 00299 00300 00301 00304 void setLeftHanded() throw() { 00305 myAmLeftHand = true; 00306 } 00307 00308 00310 00311 00315 unsigned int getNumLanes() const throw() { 00316 return (unsigned int) myLanes.size(); 00317 } 00318 00319 00323 int getPriority() const throw() { 00324 return myPriority; 00325 } 00326 00327 00331 NBNode* getFromNode() const throw() { 00332 return myFrom; 00333 } 00334 00335 00339 NBNode* getToNode() const throw() { 00340 return myTo; 00341 } 00342 00343 00351 SUMOReal getAngle() const throw() { 00352 return myAngle; 00353 } 00354 00355 00359 SUMOReal getLength() const throw() { 00360 return myLength; 00361 } 00362 00363 00368 SUMOReal getLoadedLength() const throw() { 00369 return myLoadedLength>0 ? myLoadedLength : myLength; 00370 } 00371 00372 00376 bool hasLoadedLength() const throw() { 00377 return myLoadedLength>0; 00378 } 00379 00380 00384 SUMOReal getSpeed() const throw() { 00385 return mySpeed; 00386 } 00387 00388 00394 EdgeBuildingStep getStep() const throw() { 00395 return myStep; 00396 } 00397 00398 00402 SUMOReal getWidth() const throw() { 00403 return myWidth; 00404 } 00405 00406 00409 const std::string& getStreetName() const throw() { 00410 return myStreetName; 00411 } 00412 00415 void setStreetName(const std::string& name) { 00416 myStreetName = name; 00417 } 00418 00422 SUMOReal getOffset() const throw() { 00423 return myOffset; 00424 } 00425 00426 00430 const std::string& getTypeName() const throw() { 00431 return myType; 00432 } 00433 00434 00438 const std::vector<NBEdge::Lane> &getLanes() const { 00439 return myLanes; 00440 } 00442 00443 00444 00446 00447 00451 const PositionVector& getGeometry() const throw() { 00452 return myGeom; 00453 } 00454 00455 00457 const PositionVector getInnerGeometry() const; 00458 00459 00462 bool hasDefaultGeometry() const; 00463 00464 00470 bool hasDefaultGeometryEndpoints() const; 00471 00472 00483 void setGeometry(const PositionVector& g, bool inner=false) throw(); 00484 00485 00495 void addGeometryPoint(int index, const Position& p) throw(); 00496 00497 00505 void computeEdgeShape() throw(); 00506 00507 00511 const PositionVector& getLaneShape(unsigned int i) const throw(); 00512 00513 00518 void setLaneSpreadFunction(LaneSpreadFunction spread) throw(); 00519 00520 00525 LaneSpreadFunction getLaneSpreadFunction() const throw() { 00526 return myLaneSpreadFunction; 00527 } 00528 00529 00535 bool splitGeometry(NBEdgeCont& ec, NBNodeCont& nc); 00537 00538 00539 00542 00557 bool addEdge2EdgeConnection(NBEdge* dest) throw(); 00558 00559 00580 bool addLane2LaneConnection(unsigned int fromLane, NBEdge* dest, 00581 unsigned int toLane, Lane2LaneInfoType type, 00582 bool mayUseSameDestination=false, 00583 bool mayDefinitelyPass=false) throw(); 00584 00585 00603 bool addLane2LaneConnections(unsigned int fromLane, 00604 NBEdge* dest, unsigned int toLane, unsigned int no, 00605 Lane2LaneInfoType type, bool invalidatePrevious=false, 00606 bool mayDefinitelyPass=false) throw(); 00607 00608 00619 void setConnection(unsigned int lane, NBEdge* destEdge, 00620 unsigned int destLane, 00621 Lane2LaneInfoType type, 00622 bool mayUseSameDestination=false, 00623 bool mayDefinitelyPass=false) throw(); 00624 00625 00632 std::vector<Connection> getConnectionsFromLane(unsigned int lane) const throw(); 00633 00634 00641 bool hasConnectionTo(NBEdge* destEdge, unsigned int destLane) const throw(); 00643 00644 00647 00650 void setAsMacroscopicConnector() throw() { 00651 myAmMacroscopicConnector = true; 00652 } 00653 00654 00658 bool isMacroscopicConnector() const throw() { 00659 return myAmMacroscopicConnector; 00660 } 00661 00662 00665 void setIsInnerEdge() throw() { 00666 myAmInnerEdge = true; 00667 } 00668 00669 00673 bool isInnerEdge() const throw() { 00674 return myAmInnerEdge; 00675 } 00677 00678 00680 void computeTurningDirections(); 00681 00682 00688 void setJunctionPriority(const NBNode* const node, int prio); 00689 00690 00700 int getJunctionPriority(const NBNode* const node) const; 00701 00702 void setLoadedLength(SUMOReal val); 00703 void dismissVehicleClassInformation(); 00704 00705 00706 const std::string& getTypeID() const throw() { 00707 return myType; 00708 } 00709 00711 bool needsLaneSpecificOutput() const; 00712 00714 bool hasRestrictions() const; 00715 00717 bool hasLaneSpecificAllow() const; 00718 00720 bool hasLaneSpecificDisallow() const; 00721 00723 bool hasLaneSpecificSpeed() const; 00724 00726 bool hasLaneSpecificOffset() const; 00727 00729 bool hasLaneSpecificWidth() const; 00730 00732 bool computeEdge2Edges(bool noLeftMovers); 00733 00735 bool computeLanes2Edges(); 00736 00738 void sortOutgoingConnectionsByAngle(); 00739 00741 void sortOutgoingConnectionsByIndex(); 00742 00745 bool recheckLanes(); 00746 00755 void appendTurnaround(bool noTLSControlled) throw(); 00756 00758 std::vector<int> getConnectionLanes(NBEdge* currentOutgoing) const; 00759 00760 00766 bool isTurningDirectionAt(const NBNode* n, const NBEdge* const edge) const throw(); 00767 00768 00769 00773 NBNode* tryGetNodeAtPosition(SUMOReal pos, SUMOReal tolerance=5.0) const; 00774 00775 void replaceInConnections(NBEdge* which, NBEdge* by, unsigned int laneOff); 00776 00777 SUMOReal getMaxLaneOffset(); 00778 00779 Position getMinLaneOffsetPositionAt(NBNode* node, SUMOReal width) const; 00780 Position getMaxLaneOffsetPositionAt(NBNode* node, SUMOReal width) const; 00781 const std::vector<Connection> &getConnections() const { 00782 return myConnections; 00783 } 00784 std::vector<Connection> &getConnections() { 00785 return myConnections; 00786 } 00787 00789 bool isConnectedTo(NBEdge* e); 00790 00793 const EdgeVector* getConnectedSorted(); 00794 00796 EdgeVector getConnectedEdges() const throw(); 00797 00800 void remapConnections(const EdgeVector& incoming); 00801 00809 void removeFromConnections(NBEdge* toEdge, int fromLane=-1, int toLane=-1); 00810 00811 void invalidateConnections(bool reallowSetting=false); 00812 00813 bool lanesWereAssigned() const; 00814 00815 bool mayBeTLSControlled(int fromLane, NBEdge* toEdge, int toLane) const throw(); 00816 00818 bool setControllingTLInformation(const NBConnection& c, const std::string& tlID); 00819 00821 void clearControllingTLInformation(); 00822 00823 void addCrossingPointsAsIncomingWithGivenOutgoing(NBEdge* o, 00824 PositionVector& into); 00825 00826 SUMOReal width() const; 00827 00828 PositionVector getCWBoundaryLine(const NBNode& n, SUMOReal offset) const; 00829 PositionVector getCCWBoundaryLine(const NBNode& n, SUMOReal offset) const; 00830 00831 bool expandableBy(NBEdge* possContinuation) const; 00832 void append(NBEdge* continuation); 00833 SUMOReal getNormedAngle() const; 00834 00835 bool hasSignalisedConnectionTo(const NBEdge* const e) const throw(); 00836 00837 00838 void moveOutgoingConnectionsFrom(NBEdge* e, unsigned int laneOff); 00839 00840 NBEdge* getTurnDestination() const; 00841 00842 std::string getLaneID(unsigned int lane) const; 00843 00844 void setLaneSpeed(unsigned int lane, SUMOReal speed); 00845 00846 SUMOReal getLaneSpeed(unsigned int lane) const; 00847 00848 bool isNearEnough2BeJoined2(NBEdge* e, SUMOReal threshold) const; 00849 00850 SUMOReal getAngle(const NBNode& atNode) const; 00851 00852 SUMOReal getNormedAngle(const NBNode& atNode) const; 00853 00854 00855 void incLaneNo(unsigned int by); 00856 00857 void decLaneNo(unsigned int by, int dir=0); 00858 00859 void copyConnectionsFrom(NBEdge* src); 00860 00861 void markAsInLane2LaneState(); 00862 00864 void setVehicleClasses(const SUMOVehicleClasses& allowed, const SUMOVehicleClasses& disallowed, int lane=-1); 00865 00867 void allowVehicleClass(int lane, SUMOVehicleClass vclass); 00868 00870 void disallowVehicleClass(int lane, SUMOVehicleClass vclass); 00871 00872 void preferVehicleClass(int lane, SUMOVehicleClass vclass); 00873 00875 void setWidth(int lane, SUMOReal width); 00876 00878 void setOffset(int lane, SUMOReal offset); 00879 00881 void setSpeed(int lane, SUMOReal offset); 00882 00884 SUMOVehicleClasses getAllowedVehicleClasses() const; 00885 00887 SUMOVehicleClasses getDisallowedVehicleClasses() const; 00888 00890 SUMOVehicleClasses getAllowedVehicleClasses(unsigned int lane) const; 00891 00893 SUMOVehicleClasses getDisallowedVehicleClasses(unsigned int lane) const; 00894 00895 void disableConnection4TLS(int fromLane, NBEdge* toEdge, int toLane); 00896 00897 00898 int getMinConnectedLane(NBEdge* of) const; 00899 int getMaxConnectedLane(NBEdge* of) const; 00900 00901 void setTurningDestination(NBEdge* e); 00902 00903 // returns a reference to the internal structure for the convenience of NETEDIT 00904 Lane& getLaneStruct(unsigned int lane) { 00905 return myLanes[lane]; 00906 } 00907 00908 // returns a reference to the internal structure for the convenience of NETEDIT 00909 const Lane& getLaneStruct(unsigned int lane) const { 00910 return myLanes[lane]; 00911 } 00912 00913 /* declares connections as fully loaded. This is needed to avoid recomputing connections 00914 * if an edge has no connections intentionally. */ 00915 void declareConnectionsAsLoaded() { 00916 myStep = LANES2LANES_USER; 00917 } 00918 00919 void buildInnerEdges(const NBNode& n, unsigned int noInternalNoSplits, unsigned int& lno, unsigned int& splitNo); 00920 00921 00922 private: 00928 class ToEdgeConnectionsAdder : public Bresenham::BresenhamCallBack { 00929 private: 00931 std::map<NBEdge*, std::vector<unsigned int> > myConnections; 00932 00934 const EdgeVector& myTransitions; 00935 00936 public: 00938 ToEdgeConnectionsAdder(const EdgeVector& transitions) throw() 00939 : myTransitions(transitions) { } 00940 00942 ~ToEdgeConnectionsAdder() throw() { } 00943 00945 void execute(const unsigned int lane, const unsigned int virtEdge) throw(); 00946 00947 const std::map<NBEdge*, std::vector<unsigned int> > &getBuiltConnections() const throw() { 00948 return myConnections; 00949 } 00950 00951 private: 00953 ToEdgeConnectionsAdder(const ToEdgeConnectionsAdder&); 00954 00956 ToEdgeConnectionsAdder& operator=(const ToEdgeConnectionsAdder&); 00957 00958 }; 00959 00960 00968 class MainDirections { 00969 public: 00971 enum Direction { DIR_RIGHTMOST, DIR_LEFTMOST, DIR_FORWARD }; 00972 00975 std::vector<Direction> myDirs; 00976 00977 public: 00979 MainDirections(const EdgeVector& outgoing, 00980 NBEdge* parent, NBNode* to); 00981 00983 ~MainDirections(); 00984 00987 bool empty() const; 00988 00991 bool includes(Direction d) const; 00992 00993 private: 00995 MainDirections(const MainDirections&); 00996 00998 MainDirections& operator=(const MainDirections&); 00999 01000 }; 01001 01003 PositionVector computeLaneShape(unsigned int lane) throw(InvalidArgument); 01004 01006 std::pair<SUMOReal, SUMOReal> laneOffset(const Position& from, 01007 const Position& to, SUMOReal lanewidth, unsigned int lane) throw(InvalidArgument); 01008 01009 void computeLaneShapes() throw(); 01010 01011 protected: 01012 bool acceptBeingTurning(NBEdge* e); 01013 01014 01015 private: 01031 void init(unsigned int noLanes, bool tryIgnoreNodePositions) throw(ProcessError); 01032 01033 01035 void divideOnEdges(const EdgeVector* outgoing); 01036 01039 std::vector<unsigned int> *preparePriorities( 01040 const EdgeVector* outgoing); 01041 01043 unsigned int computePrioritySum(std::vector<unsigned int> *priorities); 01044 01047 void moveConnectionToLeft(unsigned int lane); 01048 01051 void moveConnectionToRight(unsigned int lane); 01052 01053 01054 01055 01056 01057 01058 private: 01062 EdgeBuildingStep myStep; 01063 01065 std::string myType; 01066 01068 NBNode* myFrom, *myTo; 01069 01071 SUMOReal myLength; 01072 01074 SUMOReal myAngle; 01075 01077 int myPriority; 01078 01080 SUMOReal mySpeed; 01081 01085 std::vector<Connection> myConnections; 01086 01088 NBEdge* myTurnDestination; 01089 01091 int myFromJunctionPriority; 01092 01094 int myToJunctionPriority; 01095 01097 PositionVector myGeom; 01098 01100 LaneSpreadFunction myLaneSpreadFunction; 01101 01103 std::string myName; 01104 01106 SUMOReal myOffset; 01107 01109 SUMOReal myWidth; 01110 01114 std::vector<Lane> myLanes; 01115 01117 SUMOReal myLoadedLength; 01118 01120 bool myAmLeftHand; 01121 01122 01125 01127 SUMOReal myAmTurningWithAngle; 01129 NBEdge* myAmTurningOf; 01131 01133 bool myAmInnerEdge; 01134 01136 bool myAmMacroscopicConnector; 01137 01138 struct TLSDisabledConnection { 01139 int fromLane; 01140 NBEdge* to; 01141 int toLane; 01142 }; 01143 01144 std::vector<TLSDisabledConnection> myTLSDisabledConnections; 01145 01146 01148 std::string myStreetName; 01149 01150 01151 public: 01155 class tls_disable_finder { 01156 public: 01158 tls_disable_finder(const TLSDisabledConnection& tpl) : myDefinition(tpl) { } 01159 01160 bool operator()(const TLSDisabledConnection& e) const { 01161 if (e.to!=myDefinition.to) { 01162 return false; 01163 } 01164 if (e.fromLane!=myDefinition.fromLane) { 01165 return false; 01166 } 01167 if (e.toLane!=myDefinition.toLane) { 01168 return false; 01169 } 01170 return true; 01171 } 01172 01173 private: 01174 TLSDisabledConnection myDefinition; 01175 01176 private: 01178 tls_disable_finder& operator=(const tls_disable_finder& s); 01179 01180 }; 01181 01182 01183 01187 class connections_toedge_finder { 01188 public: 01190 connections_toedge_finder(NBEdge* const edge2find) : myEdge2Find(edge2find) { } 01191 01192 bool operator()(const Connection& c) const { 01193 return c.toEdge == myEdge2Find; 01194 } 01195 01196 private: 01197 NBEdge* const myEdge2Find; 01198 01199 private: 01201 connections_toedge_finder& operator=(const connections_toedge_finder& s); 01202 01203 }; 01204 01208 class connections_toedgelane_finder { 01209 public: 01211 connections_toedgelane_finder(NBEdge* const edge2find, int lane2find) : myEdge2Find(edge2find), myLane2Find(lane2find) { } 01212 01213 bool operator()(const Connection& c) const { 01214 return c.toEdge == myEdge2Find && c.toLane == myLane2Find; 01215 } 01216 01217 private: 01218 NBEdge* const myEdge2Find; 01219 int myLane2Find; 01220 01221 private: 01223 connections_toedgelane_finder& operator=(const connections_toedgelane_finder& s); 01224 01225 }; 01226 01227 01231 class connections_finder { 01232 public: 01234 connections_finder(int fromLane, NBEdge* const edge2find, int lane2find) : myFromLane(fromLane), myEdge2Find(edge2find), myLane2Find(lane2find) { } 01235 01236 bool operator()(const Connection& c) const { 01237 return c.fromLane==myFromLane && c.toEdge == myEdge2Find && c.toLane == myLane2Find; 01238 } 01239 01240 private: 01241 int myFromLane; 01242 NBEdge* const myEdge2Find; 01243 int myLane2Find; 01244 01245 private: 01247 connections_finder& operator=(const connections_finder& s); 01248 01249 }; 01250 01254 class connections_fromlane_finder { 01255 public: 01257 connections_fromlane_finder(int lane2find) : myLane2Find(lane2find) { } 01258 01259 bool operator()(const Connection& c) const { 01260 return c.fromLane == myLane2Find; 01261 } 01262 01263 private: 01264 int myLane2Find; 01265 01266 private: 01268 connections_fromlane_finder& operator=(const connections_fromlane_finder& s); 01269 01270 }; 01271 01275 static bool connections_sorter(const Connection& c1, const Connection& c2) { 01276 if (c1.fromLane!=c2.fromLane) { 01277 return c1.fromLane<c2.fromLane; 01278 } 01279 if (c1.toEdge != c2.toEdge) { 01280 return c1.toEdge->getID().compare(c1.toEdge->getID()) < 0; 01281 } 01282 return c1.toLane<c2.toLane; 01283 } 01284 01289 class connections_relative_edgelane_sorter { 01290 public: 01292 explicit connections_relative_edgelane_sorter(NBEdge* e, NBNode* n) 01293 : myEdge(e), myNode(n) {} 01294 01295 public: 01297 int operator()(const Connection& c1, const Connection& c2) const { 01298 if (c1.toEdge!=c2.toEdge) { 01299 SUMOReal relAngle1 = NBHelpers::normRelAngle( 01300 myEdge->getAngle(), c1.toEdge->getAngle()); 01301 SUMOReal relAngle2 = NBHelpers::normRelAngle( 01302 myEdge->getAngle(), c2.toEdge->getAngle()); 01303 return relAngle1 > relAngle2; 01304 } 01305 return c1.toLane<c2.toLane; 01306 } 01307 01308 private: 01310 NBEdge* myEdge; 01311 01313 NBNode* myNode; 01314 01315 }; 01316 01317 private: 01319 NBEdge(const NBEdge& s); 01320 01322 NBEdge& operator=(const NBEdge& s); 01323 01324 01325 }; 01326 01327 01328 #endif 01329 01330 /****************************************************************************/ 01331