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