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 2001-2010 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/SUMOSAXHandler.h>
00035 #include <utils/geom/Position2DVector.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 SUMOSAXHandler {
00059 public:
00075     static void loadNetwork(const OptionsCont &oc, NBNetBuilder &nb);
00076 
00077 
00078 protected:
00079     enum LinkType {
00080         OPENDRIVE_LT_SUCCESSOR,
00081         OPENDRIVE_LT_PREDECESSOR
00082     };
00083 
00084     enum ElementType {
00085         OPENDRIVE_ET_UNKNOWN,
00086         OPENDRIVE_ET_ROAD,
00087         OPENDRIVE_ET_JUNCTION
00088     };
00089 
00090     enum ContactPoint {
00091         OPENDRIVE_CP_UNKNOWN,
00092         OPENDRIVE_CP_START,
00093         OPENDRIVE_CP_END
00094     };
00095 
00096     enum GeometryType {
00097         OPENDRIVE_GT_UNKNOWN,
00098         OPENDRIVE_GT_LINE,
00099         OPENDRIVE_GT_SPIRAL,
00100         OPENDRIVE_GT_ARC,
00101         OPENDRIVE_GT_POLY3
00102     };
00103 
00108     struct OpenDriveLink {
00109         OpenDriveLink(LinkType linkTypeArg, const std::string &elementIDArg)
00110                 : linkType(linkTypeArg), elementID(elementIDArg),
00111                 elementType(OPENDRIVE_ET_UNKNOWN), contactPoint(OPENDRIVE_CP_UNKNOWN) { }
00112 
00113         LinkType linkType;
00114         std::string elementID;
00115         ElementType elementType;
00116         ContactPoint contactPoint;
00117     };
00118 
00119 
00124     struct OpenDriveGeometry {
00125         OpenDriveGeometry(SUMOReal lengthArg, SUMOReal sArg, SUMOReal xArg, SUMOReal yArg, SUMOReal hdgArg)
00126                 : length(lengthArg), s(sArg), x(xArg), y(yArg), hdg(hdgArg),
00127                 type(OPENDRIVE_GT_UNKNOWN) { }
00128 
00129         SUMOReal length;
00130         SUMOReal s;
00131         SUMOReal x;
00132         SUMOReal y;
00133         SUMOReal hdg;
00134         GeometryType type;
00135         std::vector<SUMOReal> params;
00136     };
00137 
00138 
00143     struct OpenDriveLane {
00144         OpenDriveLane(int idArg, int levelArg, const std::string &typeArg)
00145                 : id(idArg), level(levelArg), type(typeArg), successor(UNSET_CONNECTION), predecessor(UNSET_CONNECTION) { }
00146 
00147         int id;
00148         int level;
00149         std::string type;
00150         int successor;
00151         int predecessor;
00152     };
00153 
00154 
00159     struct OpenDriveLaneSection {
00160         OpenDriveLaneSection(SUMOReal sArg)
00161                 : s(sArg) {
00162             lanesByDir[SUMO_TAG_OPENDRIVE_LEFT] = std::vector<OpenDriveLane>();
00163             lanesByDir[SUMO_TAG_OPENDRIVE_RIGHT] = std::vector<OpenDriveLane>();
00164             lanesByDir[SUMO_TAG_OPENDRIVE_CENTER] = std::vector<OpenDriveLane>();
00165         }
00166 
00167         unsigned int getLaneNumber(SumoXMLTag dir) const throw() {
00168             unsigned int laneNum = 0;
00169             const std::vector<OpenDriveLane> &dirLanes = lanesByDir.find(dir)->second;
00170             for (std::vector<OpenDriveLane>::const_iterator i=dirLanes.begin(); i!=dirLanes.end(); ++i) {
00171                 if ((*i).type=="driving") {
00172                     ++laneNum;
00173                 }
00174             }
00175             return laneNum;
00176         }
00177 
00178         std::map<int, int> buildLaneMapping(SumoXMLTag dir) {
00179             std::map<int, int> ret;
00180             unsigned int sumoLane = 0;
00181             const std::vector<OpenDriveLane> &dirLanes = lanesByDir.find(dir)->second;
00182             if (dir==SUMO_TAG_OPENDRIVE_RIGHT) {
00183                 for (std::vector<OpenDriveLane>::const_reverse_iterator i=dirLanes.rbegin(); i!=dirLanes.rend(); ++i) {
00184                     if ((*i).type=="driving") {
00185                         ret[(*i).id] = sumoLane++;
00186                     }
00187                 }
00188             } else {
00189                 for (std::vector<OpenDriveLane>::const_iterator i=dirLanes.begin(); i!=dirLanes.end(); ++i) {
00190                     if ((*i).type=="driving") {
00191                         ret[(*i).id] = sumoLane++;
00192                     }
00193                 }
00194             }
00195             return ret;
00196         }
00197 
00198         SUMOReal s;
00199         std::map<SumoXMLTag, std::vector<OpenDriveLane> > lanesByDir;
00200     };
00201 
00202 
00207     struct OpenDriveEdge {
00208         OpenDriveEdge(const std::string &idArg, const std::string &junctionArg, SUMOReal lengthArg)
00209                 : id(idArg), junction(junctionArg), length(lengthArg),
00210                 from(0), to(0) { }
00211 
00212         unsigned int getMaxLaneNumber(SumoXMLTag dir) const throw() {
00213             unsigned int maxLaneNum = 0;
00214             for (std::vector<OpenDriveLaneSection>::const_iterator i=laneSections.begin(); i!=laneSections.end(); ++i) {
00215                 maxLaneNum = MAX2(maxLaneNum, (*i).getLaneNumber(dir));
00216             }
00217             return maxLaneNum;
00218         }
00219 
00221         std::string id;
00223         std::string junction;
00225         SUMOReal length;
00226         std::vector<OpenDriveLink> links;
00227         std::vector<OpenDriveGeometry> geometries;
00228         NBNode *from;
00229         NBNode *to;
00230         std::map<int, int> beginLaneMap;
00231         std::map<int, int> endLaneMap;
00232         Position2DVector geom;
00233         std::vector<OpenDriveLaneSection> laneSections;
00234     };
00235 
00236 
00237     struct Connection {
00238         Connection(NBEdge *fromArg, const std::string &viaArg, NBEdge *toArg)
00239                 : from(fromArg), via(viaArg), to(toArg) { }
00240         NBEdge *from;
00241         NBEdge *to;
00242         std::string via;
00243         std::vector<std::pair<int, int> > lanes;
00244     };
00245 
00246 protected:
00250     NIImporter_OpenDrive(NBNodeCont &nc, std::vector<OpenDriveEdge> &innerEdges, std::vector<OpenDriveEdge> &outerEdges);
00251 
00252 
00254     ~NIImporter_OpenDrive() throw();
00255 
00256 
00257 
00259 
00260 
00271     void myStartElement(SumoXMLTag element,
00272                         const SUMOSAXAttributes &attrs) throw(ProcessError);
00273 
00274 
00282     void myCharacters(SumoXMLTag element,
00283                       const std::string &chars) throw(ProcessError);
00284 
00285 
00292     void myEndElement(SumoXMLTag element) throw(ProcessError);
00294 
00295 
00296 
00297 private:
00298     void addLink(LinkType lt, const std::string &elementType, const std::string &elementID,
00299                  const std::string &contactPoint) throw(ProcessError);
00300 
00301     void addGeometryShape(GeometryType type, const std::vector<SUMOReal> &vals) throw(ProcessError);
00302 
00303     OpenDriveEdge myCurrentEdge;
00304 
00305     std::vector<OpenDriveEdge> &myInnerEdges;
00306     std::vector<OpenDriveEdge> &myOuterEdges;
00307     std::vector<SumoXMLTag> myElementStack;
00308     SumoXMLTag myCurrentLaneDirection;
00309 
00310 
00311 protected:
00325     static NBNode *getOrBuildNode(const std::string &id, Position2D &pos, NBNodeCont &nc) throw(ProcessError);
00326 
00327 
00328     static std::vector<Position2D> geomFromLine(const OpenDriveEdge &e, const OpenDriveGeometry &g) throw();
00329     static std::vector<Position2D> geomFromSpiral(const OpenDriveEdge &e, const OpenDriveGeometry &g) throw();
00330     static std::vector<Position2D> geomFromArc(const OpenDriveEdge &e, const OpenDriveGeometry &g) throw();
00331     static std::vector<Position2D> geomFromPoly(const OpenDriveEdge &e, const OpenDriveGeometry &g) throw();
00332     static Position2D calculateStraightEndPoint(double hdg, double length, const Position2D &start) throw();
00333     static void calculateCurveCenter(SUMOReal *ad_x, SUMOReal *ad_y, SUMOReal ad_radius, SUMOReal ad_hdg) throw();
00334     static void calcPointOnCurve(SUMOReal *ad_x, SUMOReal *ad_y, SUMOReal ad_centerX, SUMOReal ad_centerY,
00335                                  SUMOReal ad_r, SUMOReal ad_length) throw();
00336     static NBEdge *getOutgoingDirectionalEdge(const NBEdgeCont &ec, const NBNodeCont &nc,
00337             const std::string &edgeID, const std::string &nodeID) throw();
00338     static NBEdge *getIncomingDirectionalEdge(const NBEdgeCont &ec,const NBNodeCont &nc,
00339             const std::string &edgeID, const std::string &nodeID) throw();
00340 
00341     static void computeShapes(std::vector<OpenDriveEdge> &edges) throw();
00342     static void setNodeSecure(NBNodeCont &nc, OpenDriveEdge &e,
00343                               const std::string &nodeID, NIImporter_OpenDrive::LinkType lt) throw(ProcessError);
00344 
00345     static void addE2EConnectionsSecure(const NBEdgeCont &ec, const NBNode * const node,
00346                                         const OpenDriveEdge &from, const OpenDriveEdge &to,
00347                                         std::vector<NIImporter_OpenDrive::Connection> &connections);
00348     static void addViaConnectionSecure(const NBEdgeCont &ec, const NBNode * const node, const OpenDriveEdge &e,
00349                                        LinkType lt, const std::string &via,
00350                                        std::vector<NIImporter_OpenDrive::Connection> &connections);
00351 
00352     static void setLaneConnections(NIImporter_OpenDrive::Connection &c,
00353                                    const OpenDriveEdge &from, bool fromAtBegin, SumoXMLTag fromLaneDir,
00354                                    const OpenDriveEdge &to, bool toAtEnd, SumoXMLTag toLaneDir);
00355 
00356     static void setLaneConnections(NIImporter_OpenDrive::Connection &c,
00357                                    const OpenDriveEdge &from, bool fromAtBegin, SumoXMLTag fromLaneDir,
00358                                    const OpenDriveEdge &via, bool viaIsReversed, SumoXMLTag viaLaneDir,
00359                                    const OpenDriveEdge &to, bool fromAtEnd, SumoXMLTag toLaneDir);
00360 
00361 
00362     class edge_by_id_finder {
00363     public:
00364         explicit edge_by_id_finder(const std::string &id)
00365                 : myEdgeID(id) { }
00366 
00367         bool operator()(const OpenDriveEdge &e) {
00368             return e.id==myEdgeID;
00369         }
00370 
00371     private:
00372         const std::string &myEdgeID;
00373 
00374     };
00375 
00376 
00377 };
00378 
00379 
00380 #endif
00381 
00382 /****************************************************************************/
00383 

Generated on Wed May 5 00:06:34 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6