SUMO - Simulation of Urban MObility

NIImporter_OpenDrive.h

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Importer for networks stored in openDrive format
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 NIImporter_OpenDrive_h
00020 #define NIImporter_OpenDrive_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 <string>
00033 #include <map>
00034 #include <utils/xml/GenericSAXHandler.h>
00035 #include <utils/geom/PositionVector.h>
00036 
00037 
00038 // ===========================================================================
00039 // class declarations
00040 // ===========================================================================
00041 class NBNetBuilder;
00042 class NBEdge;
00043 class OptionsCont;
00044 class NBNode;
00045 class NBNodeCont;
00046 
00047 
00048 #define UNSET_CONNECTION 100000
00049 
00050 // ===========================================================================
00051 // class definitions
00052 // ===========================================================================
00058 class NIImporter_OpenDrive : public GenericSAXHandler {
00059 public:
00075     static void loadNetwork(const OptionsCont& oc, NBNetBuilder& nb);
00076 
00077 
00078 protected:
00079 
00085     enum OpenDriveXMLTag {
00086         OPENDRIVE_TAG_NOTHING,
00087         OPENDRIVE_TAG_HEADER,
00088         OPENDRIVE_TAG_ROAD,
00089         OPENDRIVE_TAG_PREDECESSOR,
00090         OPENDRIVE_TAG_SUCCESSOR,
00091         // !!! OPENDRIVE_TAG_NEIGHBOR,
00092         // !!! OPENDRIVE_TAG_TYPE,
00093         OPENDRIVE_TAG_GEOMETRY,
00094         OPENDRIVE_TAG_LINE,
00095         OPENDRIVE_TAG_SPIRAL,
00096         OPENDRIVE_TAG_ARC,
00097         OPENDRIVE_TAG_POLY3,
00098         OPENDRIVE_TAG_LANESECTION,
00099         OPENDRIVE_TAG_LEFT,
00100         OPENDRIVE_TAG_CENTER,
00101         OPENDRIVE_TAG_RIGHT,
00102         OPENDRIVE_TAG_LANE
00103     };
00104 
00105 
00111     enum OpenDriveXMLAttr {
00112         OPENDRIVE_ATTR_NOTHING,
00113         OPENDRIVE_ATTR_REVMAJOR,
00114         OPENDRIVE_ATTR_REVMINOR,
00115         OPENDRIVE_ATTR_ID,
00116         OPENDRIVE_ATTR_LENGTH,
00117         OPENDRIVE_ATTR_JUNCTION,
00118         OPENDRIVE_ATTR_ELEMENTTYPE,
00119         OPENDRIVE_ATTR_ELEMENTID,
00120         OPENDRIVE_ATTR_CONTACTPOINT,
00121         OPENDRIVE_ATTR_S,
00122         OPENDRIVE_ATTR_X,
00123         OPENDRIVE_ATTR_Y,
00124         OPENDRIVE_ATTR_HDG,
00125         OPENDRIVE_ATTR_CURVSTART,
00126         OPENDRIVE_ATTR_CURVEND,
00127         OPENDRIVE_ATTR_CURVATURE,
00128         OPENDRIVE_ATTR_A,
00129         OPENDRIVE_ATTR_B,
00130         OPENDRIVE_ATTR_C,
00131         OPENDRIVE_ATTR_D,
00132         OPENDRIVE_ATTR_TYPE,
00133         OPENDRIVE_ATTR_LEVEL
00134     };
00135 
00136     enum LinkType {
00137         OPENDRIVE_LT_SUCCESSOR,
00138         OPENDRIVE_LT_PREDECESSOR
00139     };
00140 
00141     enum ElementType {
00142         OPENDRIVE_ET_UNKNOWN,
00143         OPENDRIVE_ET_ROAD,
00144         OPENDRIVE_ET_JUNCTION
00145     };
00146 
00147     enum ContactPoint {
00148         OPENDRIVE_CP_UNKNOWN,
00149         OPENDRIVE_CP_START,
00150         OPENDRIVE_CP_END
00151     };
00152 
00153     enum GeometryType {
00154         OPENDRIVE_GT_UNKNOWN,
00155         OPENDRIVE_GT_LINE,
00156         OPENDRIVE_GT_SPIRAL,
00157         OPENDRIVE_GT_ARC,
00158         OPENDRIVE_GT_POLY3
00159     };
00160 
00165     struct OpenDriveLink {
00166         OpenDriveLink(LinkType linkTypeArg, const std::string& elementIDArg)
00167             : linkType(linkTypeArg), elementID(elementIDArg),
00168               elementType(OPENDRIVE_ET_UNKNOWN), contactPoint(OPENDRIVE_CP_UNKNOWN) { }
00169 
00170         LinkType linkType;
00171         std::string elementID;
00172         ElementType elementType;
00173         ContactPoint contactPoint;
00174     };
00175 
00176 
00181     struct OpenDriveGeometry {
00182         OpenDriveGeometry(SUMOReal lengthArg, SUMOReal sArg, SUMOReal xArg, SUMOReal yArg, SUMOReal hdgArg)
00183             : length(lengthArg), s(sArg), x(xArg), y(yArg), hdg(hdgArg),
00184               type(OPENDRIVE_GT_UNKNOWN) { }
00185 
00186         SUMOReal length;
00187         SUMOReal s;
00188         SUMOReal x;
00189         SUMOReal y;
00190         SUMOReal hdg;
00191         GeometryType type;
00192         std::vector<SUMOReal> params;
00193     };
00194 
00195 
00200     struct OpenDriveLane {
00201         OpenDriveLane(int idArg, const std::string& levelArg, const std::string& typeArg)
00202             : id(idArg), level(levelArg), type(typeArg), successor(UNSET_CONNECTION), predecessor(UNSET_CONNECTION) { }
00203 
00204         int id;
00205         std::string level;
00206         std::string type;
00207         int successor;
00208         int predecessor;
00209     };
00210 
00211 
00216     struct OpenDriveLaneSection {
00217         OpenDriveLaneSection(SUMOReal sArg)
00218             : s(sArg) {
00219             lanesByDir[OPENDRIVE_TAG_LEFT] = std::vector<OpenDriveLane>();
00220             lanesByDir[OPENDRIVE_TAG_RIGHT] = std::vector<OpenDriveLane>();
00221             lanesByDir[OPENDRIVE_TAG_CENTER] = std::vector<OpenDriveLane>();
00222         }
00223 
00224         unsigned int getLaneNumber(OpenDriveXMLTag dir) const throw() {
00225             unsigned int laneNum = 0;
00226             const std::vector<OpenDriveLane> &dirLanes = lanesByDir.find(dir)->second;
00227             for (std::vector<OpenDriveLane>::const_iterator i=dirLanes.begin(); i!=dirLanes.end(); ++i) {
00228                 if ((*i).type=="driving") {
00229                     ++laneNum;
00230                 }
00231             }
00232             return laneNum;
00233         }
00234 
00235         std::map<int, int> buildLaneMapping(OpenDriveXMLTag dir) {
00236             std::map<int, int> ret;
00237             unsigned int sumoLane = 0;
00238             const std::vector<OpenDriveLane> &dirLanes = lanesByDir.find(dir)->second;
00239             if (dir==OPENDRIVE_TAG_RIGHT) {
00240                 for (std::vector<OpenDriveLane>::const_reverse_iterator i=dirLanes.rbegin(); i!=dirLanes.rend(); ++i) {
00241                     if ((*i).type=="driving") {
00242                         ret[(*i).id] = sumoLane++;
00243                     }
00244                 }
00245             } else {
00246                 for (std::vector<OpenDriveLane>::const_iterator i=dirLanes.begin(); i!=dirLanes.end(); ++i) {
00247                     if ((*i).type=="driving") {
00248                         ret[(*i).id] = sumoLane++;
00249                     }
00250                 }
00251             }
00252             return ret;
00253         }
00254 
00255         SUMOReal s;
00256         std::map<OpenDriveXMLTag, std::vector<OpenDriveLane> > lanesByDir;
00257     };
00258 
00259 
00264     struct OpenDriveEdge {
00265         OpenDriveEdge(const std::string& idArg, const std::string& junctionArg, SUMOReal lengthArg)
00266             : id(idArg), junction(junctionArg), length(lengthArg),
00267               from(0), to(0) { }
00268 
00269         unsigned int getMaxLaneNumber(OpenDriveXMLTag dir) const throw() {
00270             unsigned int maxLaneNum = 0;
00271             for (std::vector<OpenDriveLaneSection>::const_iterator i=laneSections.begin(); i!=laneSections.end(); ++i) {
00272                 maxLaneNum = MAX2(maxLaneNum, (*i).getLaneNumber(dir));
00273             }
00274             return maxLaneNum;
00275         }
00276 
00278         std::string id;
00280         std::string junction;
00282         SUMOReal length;
00283         std::vector<OpenDriveLink> links;
00284         std::vector<OpenDriveGeometry> geometries;
00285         NBNode* from;
00286         NBNode* to;
00287         std::map<int, int> beginLaneMap;
00288         std::map<int, int> endLaneMap;
00289         PositionVector geom;
00290         std::vector<OpenDriveLaneSection> laneSections;
00291     };
00292 
00293 
00294     struct Connection {
00295         Connection(NBEdge* fromArg, const std::string& viaArg, NBEdge* toArg)
00296             : from(fromArg), to(toArg), via(viaArg) { }
00297         NBEdge* from;
00298         NBEdge* to;
00299         std::string via;
00300         std::vector<std::pair<int, int> > lanes;
00301     };
00302 
00303 protected:
00307     NIImporter_OpenDrive(std::vector<OpenDriveEdge> &innerEdges, std::vector<OpenDriveEdge> &outerEdges);
00308 
00309 
00311     ~NIImporter_OpenDrive() throw();
00312 
00313 
00314 
00316 
00317 
00328     void myStartElement(int element,
00329                         const SUMOSAXAttributes& attrs) throw(ProcessError);
00330 
00331 
00339     void myCharacters(int element,
00340                       const std::string& chars) throw(ProcessError);
00341 
00342 
00349     void myEndElement(int element) throw(ProcessError);
00351 
00352 
00353 
00354 private:
00355     void addLink(LinkType lt, const std::string& elementType, const std::string& elementID,
00356                  const std::string& contactPoint) throw(ProcessError);
00357 
00358     void addGeometryShape(GeometryType type, const std::vector<SUMOReal> &vals) throw(ProcessError);
00359 
00360     OpenDriveEdge myCurrentEdge;
00361 
00362     std::vector<OpenDriveEdge> &myInnerEdges;
00363     std::vector<OpenDriveEdge> &myOuterEdges;
00364     std::vector<int> myElementStack;
00365     OpenDriveXMLTag myCurrentLaneDirection;
00366 
00367 
00368 protected:
00382     static NBNode* getOrBuildNode(const std::string& id, Position& pos, NBNodeCont& nc) throw(ProcessError);
00383 
00384 
00385     static std::vector<Position> geomFromLine(const OpenDriveEdge& e, const OpenDriveGeometry& g) throw();
00386     static std::vector<Position> geomFromSpiral(const OpenDriveEdge& e, const OpenDriveGeometry& g) throw();
00387     static std::vector<Position> geomFromArc(const OpenDriveEdge& e, const OpenDriveGeometry& g) throw();
00388     static std::vector<Position> geomFromPoly(const OpenDriveEdge& e, const OpenDriveGeometry& g) throw();
00389     static Position calculateStraightEndPoint(double hdg, double length, const Position& start) throw();
00390     static void calculateCurveCenter(SUMOReal* ad_x, SUMOReal* ad_y, SUMOReal ad_radius, SUMOReal ad_hdg) throw();
00391     static void calcPointOnCurve(SUMOReal* ad_x, SUMOReal* ad_y, SUMOReal ad_centerX, SUMOReal ad_centerY,
00392                                  SUMOReal ad_r, SUMOReal ad_length) throw();
00393     static NBEdge* getOutgoingDirectionalEdge(const NBEdgeCont& ec, const NBNodeCont& nc,
00394             const std::string& edgeID, const std::string& nodeID) throw();
00395     static NBEdge* getIncomingDirectionalEdge(const NBEdgeCont& ec,const NBNodeCont& nc,
00396             const std::string& edgeID, const std::string& nodeID) throw();
00397 
00398     static void computeShapes(std::vector<OpenDriveEdge> &edges) throw();
00399     static void setNodeSecure(NBNodeCont& nc, OpenDriveEdge& e,
00400                               const std::string& nodeID, NIImporter_OpenDrive::LinkType lt) throw(ProcessError);
00401 
00402     static void addE2EConnectionsSecure(const NBEdgeCont& ec, const NBNode* const node,
00403                                         const OpenDriveEdge& from, const OpenDriveEdge& to,
00404                                         std::vector<NIImporter_OpenDrive::Connection> &connections);
00405     static void addViaConnectionSecure(const NBEdgeCont& ec, const NBNode* const node, const OpenDriveEdge& e,
00406                                        LinkType lt, const std::string& via,
00407                                        std::vector<NIImporter_OpenDrive::Connection> &connections);
00408 
00409     static void setLaneConnections(NIImporter_OpenDrive::Connection& c,
00410                                    const OpenDriveEdge& from, bool fromAtBegin, OpenDriveXMLTag fromLaneDir,
00411                                    const OpenDriveEdge& to, bool toAtEnd, OpenDriveXMLTag toLaneDir);
00412 
00413     static void setLaneConnections(NIImporter_OpenDrive::Connection& c,
00414                                    const OpenDriveEdge& from, bool fromAtBegin, OpenDriveXMLTag fromLaneDir,
00415                                    const OpenDriveEdge& via, bool viaIsReversed, OpenDriveXMLTag viaLaneDir,
00416                                    const OpenDriveEdge& to, bool fromAtEnd, OpenDriveXMLTag toLaneDir);
00417 
00418 
00419     class edge_by_id_finder {
00420     public:
00421         explicit edge_by_id_finder(const std::string& id)
00422             : myEdgeID(id) { }
00423 
00424         bool operator()(const OpenDriveEdge& e) {
00425             return e.id==myEdgeID;
00426         }
00427 
00428     private:
00429         const std::string& myEdgeID;
00430 
00431     };
00432 
00433 
00434 
00436     static StringBijection<int>::Entry openDriveTags[];
00437 
00439     static StringBijection<int>::Entry openDriveAttrs[];
00440 
00441 
00442 
00443 };
00444 
00445 
00446 #endif
00447 
00448 /****************************************************************************/
00449 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines