00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef NBEdge_h
00020 #define NBEdge_h
00021
00022
00023
00024
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/UtilExceptions.h>
00038 #include <utils/common/VectorHelper.h>
00039 #include <utils/geom/Bresenham.h>
00040 #include <utils/geom/Position2DVector.h>
00041 #include <utils/geom/Line2D.h>
00042 #include <utils/common/SUMOVehicleClass.h>
00043 #include "NBHelpers.h"
00044
00045
00046
00047
00048
00049 class NBNode;
00050 class NBNodeCont;
00051 class NBEdgeCont;
00052 class OutputDevice;
00053
00054
00055
00056
00057
00062 class NBEdge {
00063 public:
00071 enum LaneSpreadFunction {
00073 LANESPREAD_RIGHT,
00075 LANESPREAD_CENTER
00076 };
00077
00078
00086 enum EdgeBuildingStep {
00088 INIT_REJECT_CONNECTIONS,
00090 INIT,
00092 EDGE2EDGES,
00094 LANES2EDGES,
00096 LANES2LANES_RECHECK,
00098 LANES2LANES_DONE,
00100 LANES2LANES_USER
00101 };
00102
00103
00107 enum Lane2LaneInfoType {
00109 L2L_COMPUTED,
00111 L2L_USER,
00113 L2L_VALIDATED
00114 };
00115
00116
00120 struct Lane {
00122 Position2DVector shape;
00124 SUMOReal speed;
00126 std::vector<SUMOVehicleClass> allowed;
00128 std::vector<SUMOVehicleClass> notAllowed;
00130 std::vector<SUMOVehicleClass> preferred;
00131 };
00132
00133
00137 struct Connection {
00143 Connection(int fromLane_, NBEdge *toEdge_, int toLane_) throw()
00144 : fromLane(fromLane_), toEdge(toEdge_), toLane(toLane_),
00145 mayDefinitelyPass(false) { }
00146
00147
00149 int fromLane;
00151 NBEdge *toEdge;
00153 int toLane;
00155 std::string tlID;
00157 unsigned int tlLinkNo;
00159 bool mayDefinitelyPass;
00160
00161 };
00162
00163
00164 public:
00180 NBEdge(const std::string &id,
00181 NBNode *from, NBNode *to, std::string type,
00182 SUMOReal speed, unsigned int nolanes, int priority,
00183 LaneSpreadFunction spread=LANESPREAD_RIGHT) throw(ProcessError);
00184
00185
00203 NBEdge(const std::string &id,
00204 NBNode *from, NBNode *to, std::string type,
00205 SUMOReal speed, unsigned int nolanes, int priority,
00206 Position2DVector geom,
00207 LaneSpreadFunction spread=LANESPREAD_RIGHT,
00208 bool tryIgnoreNodePositions=false) throw(ProcessError);
00209
00210
00213 ~NBEdge() throw();
00214
00215
00220 void reinit(NBNode *from, NBNode *to, std::string type,
00221 SUMOReal speed, unsigned int nolanes, int priority,
00222 Position2DVector geom,
00223 LaneSpreadFunction spread=LANESPREAD_RIGHT) throw(ProcessError);
00224
00225
00228 void setLeftHanded() throw() {
00229 myAmLeftHand = true;
00230 }
00231
00232
00234
00235
00239 const std::string &getID() const throw() {
00240 return myID;
00241 }
00242
00243
00247 unsigned int getNoLanes() const throw() {
00248 return (unsigned int) myLanes.size();
00249 }
00250
00251
00255 int getPriority() const throw() {
00256 return myPriority;
00257 }
00258
00259
00263 NBNode * const getFromNode() const throw() {
00264 return myFrom;
00265 }
00266
00267
00271 NBNode * const getToNode() const throw() {
00272 return myTo;
00273 }
00274
00275
00283 SUMOReal getAngle() const throw() {
00284 return myAngle;
00285 }
00286
00287
00291 SUMOReal getLength() const throw() {
00292 return myLength;
00293 }
00294
00295
00299 SUMOReal getSpeed() const throw() {
00300 return mySpeed;
00301 }
00302
00303
00309 EdgeBuildingStep getStep() const throw() {
00310 return myStep;
00311 }
00313
00314
00315
00317
00318
00322 const Position2DVector &getGeometry() const throw() {
00323 return myGeom;
00324 }
00325
00326
00336 void setGeometry(const Position2DVector &g) throw();
00337
00338
00348 void addGeometryPoint(int index, const Position2D &p) throw();
00349
00350
00358 void computeEdgeShape() throw();
00359
00360
00364 const Position2DVector &getLaneShape(unsigned int i) const throw();
00365
00366
00371 void setLaneSpreadFunction(LaneSpreadFunction spread) throw();
00372
00373
00378 LaneSpreadFunction getLaneSpreadFunction() const throw() {
00379 return myLaneSpreadFunction;
00380 }
00381
00382
00387 void reshiftPosition(SUMOReal xoff, SUMOReal yoff) throw();
00389
00390
00391
00394
00408 bool addEdge2EdgeConnection(NBEdge *dest) throw();
00409
00410
00431 bool addLane2LaneConnection(unsigned int fromLane, NBEdge *dest,
00432 unsigned int toLane, Lane2LaneInfoType type,
00433 bool mayUseSameDestination=false,
00434 bool mayDefinitelyPass=false) throw();
00435
00436
00454 bool addLane2LaneConnections(unsigned int fromLane,
00455 NBEdge *dest, unsigned int toLane, unsigned int no,
00456 Lane2LaneInfoType type, bool invalidatePrevious=false,
00457 bool mayDefinitelyPass=false) throw();
00458
00459
00470 void setConnection(unsigned int lane, NBEdge *destEdge,
00471 unsigned int destLane,
00472 Lane2LaneInfoType type,
00473 bool mayUseSameDestination=false,
00474 bool mayDefinitelyPass=false) throw();
00475
00476
00483 std::vector<Connection> getConnectionsFromLane(unsigned int lane) const throw();
00484
00485
00492 bool hasConnectionTo(NBEdge *destEdge, unsigned int destLane) const throw();
00494
00495
00498
00501 void setAsMacroscopicConnector() throw() {
00502 myAmMacroscopicConnector = true;
00503 }
00504
00505
00509 bool isMacroscopicConnector() throw() {
00510 return myAmMacroscopicConnector;
00511 }
00512
00513
00516 void setIsInnerEdge() throw() {
00517 myAmInnerEdge = true;
00518 }
00519
00520
00524 bool isInnerEdge() const throw() {
00525 return myAmInnerEdge;
00526 }
00528
00529
00531 void computeTurningDirections();
00532
00533
00539 void setJunctionPriority(const NBNode * const node, int prio);
00540
00541
00551 int getJunctionPriority(const NBNode * const node) const;
00552
00553
00556 void writeXMLStep1(OutputDevice &into);
00557
00559 void writeXMLStep2(OutputDevice &into, bool includeInternal);
00560
00561 bool hasRestrictions() const;
00562 void writeLanesPlain(OutputDevice &into);
00563 void setLoadedLength(SUMOReal val);
00564 void dismissVehicleClassInformation();
00565
00566
00567 const std::string &getTypeID() const throw() {
00568 return myType;
00569 }
00570
00571
00573 bool computeEdge2Edges();
00574
00576 bool computeLanes2Edges();
00577
00579 void sortOutgoingLanesConnections();
00580
00583 bool recheckLanes();
00584
00593 void appendTurnaround(bool noTLSControlled) throw();
00594
00596 std::vector<int> getConnectionLanes(NBEdge *currentOutgoing) const;
00597
00598
00604 bool isTurningDirectionAt(const NBNode *n, const NBEdge * const edge) const throw();
00605
00606
00607
00611 NBNode *tryGetNodeAtPosition(SUMOReal pos, SUMOReal tolerance=5.0) const;
00612
00613 void replaceInConnections(NBEdge *which, NBEdge *by, unsigned int laneOff);
00614
00615 SUMOReal getMaxLaneOffset();
00616
00617 Position2D getMinLaneOffsetPositionAt(NBNode *node, SUMOReal width) const;
00618 Position2D getMaxLaneOffsetPositionAt(NBNode *node, SUMOReal width) const;
00619 const std::vector<Connection> &getConnections() const {
00620 return myConnections;
00621 }
00622 std::vector<Connection> &getConnections() {
00623 return myConnections;
00624 }
00625
00627 bool isConnectedTo(NBEdge *e);
00628
00631 const std::vector<NBEdge*> *getConnectedSorted();
00632
00634 std::vector<NBEdge*> getConnectedEdges() const throw();
00635
00638 void remapConnections(const EdgeVector &incoming);
00639
00640 void removeFromConnections(NBEdge *which, int lane=-1);
00641
00642 void invalidateConnections(bool reallowSetting=false);
00643
00644 bool lanesWereAssigned() const;
00645
00646 bool mayBeTLSControlled(int fromLane, NBEdge *toEdge, int toLane) const throw();
00648 bool setControllingTLInformation(int fromLane, NBEdge *toEdge, int toLane,
00649 const std::string &tlID, unsigned int tlPos);
00650
00651 void addCrossingPointsAsIncomingWithGivenOutgoing(NBEdge *o,
00652 Position2DVector &into);
00653
00654 SUMOReal width() const;
00655
00656 Position2DVector getCWBoundaryLine(const NBNode &n, SUMOReal offset) const;
00657 Position2DVector getCCWBoundaryLine(const NBNode &n, SUMOReal offset) const;
00658
00659 bool expandableBy(NBEdge *possContinuation) const;
00660 void append(NBEdge *continuation);
00661 SUMOReal getNormedAngle() const;
00662
00663 bool hasSignalisedConnectionTo(const NBEdge * const e) const throw();
00664
00665
00668 friend class NBEdgeSuccessorBuilder;
00669
00670 friend class NBEdgeCont;
00671
00672 void moveOutgoingConnectionsFrom(NBEdge *e, unsigned int laneOff);
00673
00674 NBEdge *getTurnDestination() const;
00675
00676 std::string getLaneID(unsigned int lane);
00677
00678 void setLaneSpeed(unsigned int lane, SUMOReal speed);
00679
00680 SUMOReal getLaneSpeed(unsigned int lane) const;
00681
00682 bool isNearEnough2BeJoined2(NBEdge *e);
00683
00684 SUMOReal getAngle(const NBNode &atNode) const;
00685
00686 SUMOReal getNormedAngle(const NBNode &atNode) const;
00687
00688
00689 void incLaneNo(unsigned int by);
00690
00691 void decLaneNo(unsigned int by, int dir=0);
00692
00693 void copyConnectionsFrom(NBEdge *src);
00694
00695 void markAsInLane2LaneState();
00696
00697 void allowVehicleClass(int lane, SUMOVehicleClass vclass);
00698 void disallowVehicleClass(int lane, SUMOVehicleClass vclass);
00699 void preferVehicleClass(int lane, SUMOVehicleClass vclass);
00700 std::vector<SUMOVehicleClass> getAllowedVehicleClasses() const;
00701 void setVehicleClasses(const std::vector<SUMOVehicleClass> &allowed, const std::vector<SUMOVehicleClass> &disallowed, int lane=-1);
00702
00703 void disableConnection4TLS(int fromLane, NBEdge *toEdge, int toLane);
00704
00705
00706 int getMinConnectedLane(NBEdge *of) const;
00707 int getMaxConnectedLane(NBEdge *of) const;
00708
00709 void setTurningDestination(NBEdge *e);
00710
00711 private:
00717 class ToEdgeConnectionsAdder : public Bresenham::BresenhamCallBack {
00718 private:
00720 std::map<NBEdge*, std::vector<unsigned int> > myConnections;
00721
00723 const std::vector<NBEdge*> &myTransitions;
00724
00725 public:
00727 ToEdgeConnectionsAdder(const std::vector<NBEdge*> &transitions) throw()
00728 : myTransitions(transitions) { }
00729
00731 ~ToEdgeConnectionsAdder() throw() { }
00732
00734 void execute(SUMOReal lane, SUMOReal virtEdge) throw();
00735
00736 const std::map<NBEdge*, std::vector<unsigned int> > &getBuiltConnections() const throw() {
00737 return myConnections;
00738 }
00739
00740 private:
00742 ToEdgeConnectionsAdder(const ToEdgeConnectionsAdder&);
00743
00745 ToEdgeConnectionsAdder& operator=(const ToEdgeConnectionsAdder&);
00746
00747 };
00748
00749
00757 class MainDirections {
00758 public:
00760 enum Direction { DIR_RIGHTMOST, DIR_LEFTMOST, DIR_FORWARD };
00761
00764 std::vector<Direction> myDirs;
00765
00766 public:
00768 MainDirections(const std::vector<NBEdge*> &outgoing,
00769 NBEdge *parent, NBNode *to);
00770
00772 ~MainDirections();
00773
00776 bool empty() const;
00777
00780 bool includes(Direction d) const;
00781
00782 private:
00784 MainDirections(const MainDirections&);
00785
00787 MainDirections& operator=(const MainDirections&);
00788
00789 };
00790
00792 Position2DVector computeLaneShape(unsigned int lane) throw(InvalidArgument);
00793
00795 std::pair<SUMOReal, SUMOReal> laneOffset(const Position2D &from,
00796 const Position2D &to, SUMOReal lanewidth, unsigned int lane) throw(InvalidArgument);
00797
00798 void computeLaneShapes() throw();
00799
00800 bool splitGeometry(NBEdgeCont &ec, NBNodeCont &nc);
00801 protected:
00802 bool acceptBeingTurning(NBEdge *e);
00803
00804
00805 private:
00821 void init(unsigned int noLanes, bool tryIgnoreNodePositions) throw(ProcessError);
00822
00823
00825 void divideOnEdges(const std::vector<NBEdge*> *outgoing);
00826
00829 std::vector<unsigned int> *preparePriorities(
00830 const std::vector<NBEdge*> *outgoing);
00831
00833 unsigned int computePrioritySum(std::vector<unsigned int> *priorities);
00834
00837 void moveConnectionToLeft(unsigned int lane);
00838
00841 void moveConnectionToRight(unsigned int lane);
00842
00843
00845 void writeLane(OutputDevice &into, NBEdge::Lane &lane, unsigned int index) const;
00846
00847
00848 void writeSucceeding(OutputDevice &into, unsigned int lane,
00849 bool includeInternal);
00850
00851
00852
00853
00854
00855
00856 void writeSingleSucceeding(OutputDevice &into,
00857 const NBEdge::Connection &c, bool includeInternal);
00858
00859
00860
00861
00862 private:
00866 EdgeBuildingStep myStep;
00867
00869 std::string myID;
00870
00872 std::string myType;
00873
00875 NBNode *myFrom, *myTo;
00876
00878 SUMOReal myLength;
00879
00881 SUMOReal myAngle;
00882
00884 int myPriority;
00885
00887 SUMOReal mySpeed;
00888
00892 std::vector<Connection> myConnections;
00893
00895 NBEdge *myTurnDestination;
00896
00898 int myFromJunctionPriority;
00899
00901 int myToJunctionPriority;
00902
00904 Position2DVector myGeom;
00905
00907 LaneSpreadFunction myLaneSpreadFunction;
00908
00912 std::vector<Lane> myLanes;
00913
00915 SUMOReal myLoadedLength;
00916
00918 bool myAmLeftHand;
00919
00920
00923
00925 SUMOReal myAmTurningWithAngle;
00927 NBEdge *myAmTurningOf;
00929
00931 bool myAmInnerEdge;
00932
00934 bool myAmMacroscopicConnector;
00935
00936 struct TLSDisabledConnection {
00937 int fromLane;
00938 NBEdge *to;
00939 int toLane;
00940 };
00941
00942 std::vector<TLSDisabledConnection> myTLSDisabledConnections;
00943
00944 public:
00948 class tls_disable_finder {
00949 public:
00951 tls_disable_finder(const TLSDisabledConnection &tpl) : myDefinition(tpl) { }
00952
00953 bool operator()(const TLSDisabledConnection &e) const {
00954 if (e.to!=myDefinition.to) {
00955 return false;
00956 }
00957 if (e.fromLane!=myDefinition.fromLane) {
00958 return false;
00959 }
00960 if (e.toLane!=myDefinition.toLane) {
00961 return false;
00962 }
00963 return true;
00964 }
00965
00966 private:
00967 TLSDisabledConnection myDefinition;
00968
00969 private:
00971 tls_disable_finder &operator=(const tls_disable_finder &s);
00972
00973 };
00974
00975
00976
00980 class connections_toedge_finder {
00981 public:
00983 connections_toedge_finder(NBEdge * const edge2find) : myEdge2Find(edge2find) { }
00984
00985 bool operator()(const Connection &c) const {
00986 return c.toEdge == myEdge2Find;
00987 }
00988
00989 private:
00990 NBEdge * const myEdge2Find;
00991
00992 private:
00994 connections_toedge_finder &operator=(const connections_toedge_finder &s);
00995
00996 };
00997
01001 class connections_toedgelane_finder {
01002 public:
01004 connections_toedgelane_finder(NBEdge * const edge2find, int lane2find) : myEdge2Find(edge2find), myLane2Find(lane2find) { }
01005
01006 bool operator()(const Connection &c) const {
01007 return c.toEdge == myEdge2Find && c.toLane == myLane2Find;
01008 }
01009
01010 private:
01011 NBEdge * const myEdge2Find;
01012 int myLane2Find;
01013
01014 private:
01016 connections_toedgelane_finder &operator=(const connections_toedgelane_finder &s);
01017
01018 };
01019
01020
01024 class connections_finder {
01025 public:
01027 connections_finder(int fromLane, NBEdge * const edge2find, int lane2find) : myFromLane(fromLane), myEdge2Find(edge2find), myLane2Find(lane2find) { }
01028
01029 bool operator()(const Connection &c) const {
01030 return c.fromLane==myFromLane && c.toEdge == myEdge2Find && c.toLane == myLane2Find;
01031 }
01032
01033 private:
01034 int myFromLane;
01035 NBEdge * const myEdge2Find;
01036 int myLane2Find;
01037
01038 private:
01040 connections_finder &operator=(const connections_finder &s);
01041
01042 };
01043
01047 class connections_fromlane_finder {
01048 public:
01050 connections_fromlane_finder(int lane2find) : myLane2Find(lane2find) { }
01051
01052 bool operator()(const Connection &c) const {
01053 return c.fromLane == myLane2Find;
01054 }
01055
01056 private:
01057 int myLane2Find;
01058
01059 private:
01061 connections_fromlane_finder &operator=(const connections_fromlane_finder &s);
01062
01063 };
01064
01068 class connections_sorter {
01069 public:
01071 explicit connections_sorter() { }
01072
01073 int operator()(const Connection &c1, const Connection &c2) const {
01074 if (c1.fromLane!=c2.fromLane) {
01075 return c1.fromLane<c2.fromLane;
01076 }
01077 return c1.toLane<c2.toLane;
01078 }
01079
01080 };
01081
01086 class connections_relative_edgelane_sorter {
01087 public:
01089 explicit connections_relative_edgelane_sorter(NBEdge *e, NBNode *n)
01090 : myEdge(e), myNode(n) {}
01091
01092 public:
01094 int operator()(const Connection &c1, const Connection &c2) const {
01095 if (c1.toEdge!=c2.toEdge) {
01096 SUMOReal relAngle1 = NBHelpers::normRelAngle(
01097 myEdge->getAngle(), c1.toEdge->getAngle());
01098 SUMOReal relAngle2 = NBHelpers::normRelAngle(
01099 myEdge->getAngle(), c2.toEdge->getAngle());
01100 return relAngle1 > relAngle2;
01101 }
01102 return c1.toLane<c2.toLane;
01103 }
01104
01105 private:
01107 NBEdge *myEdge;
01108
01110 NBNode *myNode;
01111
01112 };
01113
01114 private:
01116 NBEdge(const NBEdge &s);
01117
01119 NBEdge &operator=(const NBEdge &s);
01120
01121
01122 };
01123
01124
01125 #endif
01126
01127
01128