Eclipse SUMO - Simulation of Urban MObility
NIImporter_OpenDrive.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Importer for networks stored in openDrive format
18 /****************************************************************************/
19 #ifndef NIImporter_OpenDrive_h
20 #define NIImporter_OpenDrive_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <string>
29 #include <map>
32 
33 
34 // ===========================================================================
35 // class declarations
36 // ===========================================================================
37 class NBNetBuilder;
38 class NBEdge;
39 class OptionsCont;
40 class NBNode;
41 class NBNodeCont;
42 
43 
44 #define UNSET_CONNECTION 100000
45 
46 // ===========================================================================
47 // class definitions
48 // ===========================================================================
55 public:
71  static void loadNetwork(const OptionsCont& oc, NBNetBuilder& nb);
72 
73 
74 protected:
75 
111  };
112 
113 
168  OPENDRIVE_ATTR_UNIT // xodr v1.4
169  };
170 
171 
174  enum LinkType {
177  };
178 
179 
182  enum ElementType {
186  };
187 
188 
195  };
196 
206  };
207 
208 
209 
214  struct OpenDriveLink {
219  OpenDriveLink(LinkType linkTypeArg, const std::string& elementIDArg)
220  : linkType(linkTypeArg), elementID(elementIDArg),
222 
224  std::string elementID;
227  };
228 
229 
242  OpenDriveGeometry(double lengthArg, double sArg, double xArg, double yArg, double hdgArg)
243  : length(lengthArg), s(sArg), x(xArg), y(yArg), hdg(hdgArg),
244  type(OPENDRIVE_GT_UNKNOWN) { }
245 
246  double length;
247  double s;
248  double x;
249  double y;
250  double hdg;
252  std::vector<double> params;
253  };
254 
259  struct Poly3 {
267  Poly3(double _s, double _a, double _b, double _c, double _d) :
268  s(_s), a(_a), b(_b), c(_c), d(_d) {}
269 
270  double computeAt(double pos) const {
271  const double ds = pos - s;
272  return a + b * ds + c * ds * ds + d * ds * ds * ds;
273  }
274 
275  double s;
276  double a;
277  double b;
278  double c;
279  double d;
280  };
281 
286 
287 
292  struct OpenDriveLane {
298  OpenDriveLane(int idArg, const std::string& levelArg, const std::string& typeArg) :
299  id(idArg), level(levelArg), type(typeArg), successor(UNSET_CONNECTION), predecessor(UNSET_CONNECTION),
300  speed(0), width(NBEdge::UNSPECIFIED_WIDTH) { }
301 
302  int id;
303  std::string level;
304  std::string type;
305  int successor;
307  std::vector<std::pair<double, double> > speeds;
308  double speed;
309  double width; //The lane's maximum width
310  std::vector<OpenDriveWidth> widthData;
311  };
312 
313 
322  OpenDriveLaneSection(double sArg);
323 
324 
331  void buildLaneMapping(const NBTypeCont& tc);
332 
333 
339  std::map<int, int> getInnerConnections(OpenDriveXMLTag dir, const OpenDriveLaneSection& prev);
340 
341 
342  bool buildSpeedChanges(const NBTypeCont& tc, std::vector<OpenDriveLaneSection>& newSections);
343  OpenDriveLaneSection buildLaneSection(double startPos);
344 
346  double s;
348  double sOrig;
350  double length;
352  std::map<int, int> laneMap;
354  std::map<OpenDriveXMLTag, std::vector<OpenDriveLane> > lanesByDir;
356  std::string sumoID;
358  int rightLaneNumber, leftLaneNumber;
360  std::string rightType;
361  std::string leftType;
362  };
363 
364 
365 
379  OpenDriveSignal(const std::string& idArg, const std::string typeArg, const std::string nameArg, int orientationArg, bool dynamicArg, double sArg)
380  : id(idArg), type(typeArg), name(nameArg), orientation(orientationArg), dynamic(dynamicArg), s(sArg) { }
381 
382  std::string id;
383  std::string type;
384  std::string name;
386  bool dynamic;
387  double s;
388  };
389 
390 
395  struct Connection {
396  std::string fromEdge;
397  std::string toEdge;
398  int fromLane;
399  int toLane;
402  bool all;
403  std::string origID;
404  int origLane;
406 
407  std::string getDescription() const {
408  return "Connection from=" + fromEdge + "_" + toString(fromLane)
409  + " to=" + toEdge + "_" + toString(toLane)
410  + " fromCP=" + (fromCP == OPENDRIVE_CP_START ? "start" : fromCP == OPENDRIVE_CP_END ? "end" : "unknown")
411  + " toCP=" + (toCP == OPENDRIVE_CP_START ? "start" : toCP == OPENDRIVE_CP_END ? "end" : "unknown")
412  + " all=" + toString(all);
413  //+ " origID=" + origID + " origLane=" + toString(origLane);
414  }
415  };
416 
422  std::string type;
423  std::string name;
424  std::string id;
425  double s;
426  double t;
427  double zOffset;
428  double length;
429  double width;
430  double height;
431  double radius;
432  double hdg;
433  double pitch;
434  double roll;
435  };
436 
441  struct OpenDriveEdge {
442  OpenDriveEdge(const std::string& idArg, const std::string& streetNameArg, const std::string& junctionArg, double lengthArg) :
443  id(idArg),
444  streetName(streetNameArg),
445  junction(junctionArg),
446  length(lengthArg),
447  from(0),
448  to(0) {
449  isInner = junction != "" && junction != "-1";
450  }
451 
452 
459  int getPriority(OpenDriveXMLTag dir) const;
460 
461 
463  std::string id;
465  std::string streetName;
467  std::string junction;
469  double length;
470  std::vector<OpenDriveLink> links;
471  std::vector<OpenDriveGeometry> geometries;
472  std::vector<OpenDriveElevation> elevations;
473  std::vector<OpenDriveLaneOffset> offsets;
477  std::vector<double> laneOffsets;
478  std::vector<OpenDriveLaneSection> laneSections;
479  std::vector<OpenDriveSignal> signals;
480  std::set<Connection> connections;
481  std::vector<OpenDriveObject> objects;
482  bool isInner;
483  };
484 
485 
488  public:
490  explicit sections_by_s_sorter() { }
491 
494  return ls1.s < ls2.s;
495  }
496  };
497 
498  /* @brief A class for search in position/speed tuple vectors for the given position */
500  public:
502  explicit same_position_finder(double pos) : myPosition(pos) { }
503 
505  bool operator()(const std::pair<double, double>& ps) {
506  return ps.first == myPosition;
507  }
508 
509  private:
510  same_position_finder& operator=(const same_position_finder&); // just to avoid a compiler warning
511  private:
513  double myPosition;
514 
515  };
516 
517 protected:
522  NIImporter_OpenDrive(const NBTypeCont& tc, std::map<std::string, OpenDriveEdge*>& edges);
523 
524 
527 
528 
529 
531 
532 
543  void myStartElement(int element, const SUMOSAXAttributes& attrs);
544 
553  void myCharacters(int element, const std::string& chars);
554 
555 
562  void myEndElement(int element);
564 
565 
566 
567 private:
568  void addLink(LinkType lt, const std::string& elementType, const std::string& elementID,
569  const std::string& contactPoint);
570  void addGeometryShape(GeometryType type, const std::vector<double>& vals);
571  static void setEdgeLinks2(OpenDriveEdge& e, const std::map<std::string, OpenDriveEdge*>& edges);
572  static void buildConnectionsToOuter(const Connection& c, const std::map<std::string, OpenDriveEdge*>& innerEdges, std::vector<Connection>& into, std::set<Connection>& seen);
573  static bool laneSectionsConnected(OpenDriveEdge* edge, int in, int out);
574  friend bool operator<(const Connection& c1, const Connection& c2);
575  static std::string revertID(const std::string& id);
578 
579  std::map<std::string, OpenDriveEdge*>& myEdges;
580  std::vector<int> myElementStack;
582  std::string myCurrentJunctionID;
587 
588  static bool myImportAllTypes;
589  static bool myImportWidths;
590  static double myMinWidth;
592 
593 
594 protected:
608  static NBNode* getOrBuildNode(const std::string& id, const Position& pos, NBNodeCont& nc);
609 
610 
611  static PositionVector geomFromLine(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
612  static PositionVector geomFromSpiral(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
613  static PositionVector geomFromArc(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
614  static PositionVector geomFromPoly(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
615  static PositionVector geomFromParamPoly(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
616  static Position calculateStraightEndPoint(double hdg, double length, const Position& start);
617  static void calculateCurveCenter(double* ad_x, double* ad_y, double ad_radius, double ad_hdg);
618  static void calcPointOnCurve(double* ad_x, double* ad_y, double ad_centerX, double ad_centerY,
619  double ad_r, double ad_length);
620 
621 
625  static void computeShapes(std::map<std::string, OpenDriveEdge*>& edges);
626 
627  static bool hasNonLinearElevation(OpenDriveEdge& e);
628 
634  static void revisitLaneSections(const NBTypeCont& tc, std::map<std::string, OpenDriveEdge*>& edges);
635 
636  static void setNodeSecure(NBNodeCont& nc, OpenDriveEdge& e,
637  const std::string& nodeID, NIImporter_OpenDrive::LinkType lt);
638 
639 
640  static void splitMinWidths(OpenDriveEdge* e, const NBTypeCont& tc, double minDist);
641 
642  static void findWidthSplit(const NBTypeCont& tc, std::vector<OpenDriveLane>& lanes,
643  int section, double sectionStart, double sectionEnd,
644  std::vector<double>& splitPositions);
645 
646  static void sanitizeWidths(OpenDriveEdge* e);
647  static void sanitizeWidths(std::vector<OpenDriveLane>& lanes, double length);
648 
649  static void setStraightConnections(std::vector<OpenDriveLane>& lanes);
650  static void recomputeWidths(OpenDriveLaneSection& sec, double start, double end, double sectionStart, double sectionEnd);
651  static void recomputeWidths(std::vector<OpenDriveLane>& lanes, double start, double end, double sectionStart, double sectionEnd);
652  static void setLaneAttributes(const OpenDriveEdge* e, NBEdge::Lane& sumoLane, const OpenDriveLane& odLane, bool saveOrigIDs, const NBTypeCont& tc);
653 
656 
659 
660 
661 
662 };
663 
664 
665 #endif
666 
667 /****************************************************************************/
668 
std::map< std::string, OpenDriveEdge * > & myEdges
std::vector< int > myElementStack
std::string rightType
the composite type built from all used lane types
double sOrig
The original starting offset of this lane section (differs from s if the section had to be split) ...
double myPosition
The position to search for.
static StringBijection< int >::Entry openDriveAttrs[]
The names of openDrive-XML attributes (for passing to GenericSAXHandler)
std::vector< OpenDriveWidth > widthData
void addLink(LinkType lt, const std::string &elementType, const std::string &elementID, const std::string &contactPoint)
OpenDriveXMLAttr
Numbers representing openDrive-XML - attributes.
static PositionVector geomFromArc(const OpenDriveEdge &e, const OpenDriveGeometry &g, double resolution)
ContactPoint
OpenDrive contact type enumeration.
std::string junction
The id of the junction the edge belongs to.
std::vector< OpenDriveElevation > elevations
static void calculateCurveCenter(double *ad_x, double *ad_y, double ad_radius, double ad_hdg)
GeometryType
OpenDrive geometry type enumeration.
int rightLaneNumber
The number of lanes on the right and on the left side, respectively.
static void computeShapes(std::map< std::string, OpenDriveEdge *> &edges)
Computes a polygon representation of each edge&#39;s geometry.
std::vector< OpenDriveObject > objects
Representation of a lane section.
The representation of a single edge during network building.
Definition: NBEdge.h:86
NIImporter_OpenDrive(const NBTypeCont &tc, std::map< std::string, OpenDriveEdge *> &edges)
Constructor.
Representation of an openDrive "link".
ContactPoint myCurrentContactPoint
std::map< OpenDriveXMLTag, std::vector< OpenDriveLane > > lanesByDir
The lanes, sorted by their direction.
static PositionVector geomFromPoly(const OpenDriveEdge &e, const OpenDriveGeometry &g, double resolution)
void myCharacters(int element, const std::string &chars)
Callback method for characters to implement by derived classes.
static void calcPointOnCurve(double *ad_x, double *ad_y, double ad_centerX, double ad_centerY, double ad_r, double ad_length)
double length
The length of the edge.
Poly3 OpenDriveElevation
LaneOffset has the same fields as Elevation.
Poly3(double _s, double _a, double _b, double _c, double _d)
Constructor.
friend bool operator<(const Connection &c1, const Connection &c2)
static bool hasNonLinearElevation(OpenDriveEdge &e)
static NBNode * getOrBuildNode(const std::string &id, const Position &pos, NBNodeCont &nc)
Builds a node or returns the already built.
static std::string revertID(const std::string &id)
int predecessor
The lane&#39;s predecessor lane.
static void findWidthSplit(const NBTypeCont &tc, std::vector< OpenDriveLane > &lanes, int section, double sectionStart, double sectionEnd, std::vector< double > &splitPositions)
int successor
The lane&#39;s successor lane.
OpenDriveXMLTag myCurrentLaneDirection
An (internal) definition of a single lane of an edge.
Definition: NBEdge.h:137
OpenDriveLane(int idArg, const std::string &levelArg, const std::string &typeArg)
Constructor.
std::string level
The lane&#39;s level (not used)
std::vector< OpenDriveLink > links
A handler which converts occuring elements and attributes into enums.
static PositionVector geomFromSpiral(const OpenDriveEdge &e, const OpenDriveGeometry &g, double resolution)
const GenericSAXHandler & operator=(const GenericSAXHandler &s)
invalidated assignment operator
static void setStraightConnections(std::vector< OpenDriveLane > &lanes)
OpenDriveSignal(const std::string &idArg, const std::string typeArg, const std::string nameArg, int orientationArg, bool dynamicArg, double sArg)
Constructor.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
std::string type
The lane&#39;s type.
Encapsulated SAX-Attributes.
static Position calculateStraightEndPoint(double hdg, double length, const Position &start)
static void recomputeWidths(OpenDriveLaneSection &sec, double start, double end, double sectionStart, double sectionEnd)
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
std::string id
The id of the edge.
A list of positions.
static void loadNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Loads content of the optionally given SUMO file.
static void splitMinWidths(OpenDriveEdge *e, const NBTypeCont &tc, double minDist)
const NBTypeCont & myTypeContainer
double length
The length of this lane section.
OpenDriveGeometry(double lengthArg, double sArg, double xArg, double yArg, double hdgArg)
Constructor.
std::vector< OpenDriveLaneOffset > offsets
double speed
The lane&#39;s speed (set in post-processing)
std::vector< OpenDriveLaneSection > laneSections
std::map< int, int > laneMap
A mapping from OpenDrive to SUMO-index (the first is signed, the second unsigned) ...
std::string sumoID
The id (generic, without the optionally leading &#39;-&#39;) of the edge generated for this section...
ElementType
OpenDrive element type enumeration.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
static void setLaneAttributes(const OpenDriveEdge *e, NBEdge::Lane &sumoLane, const OpenDriveLane &odLane, bool saveOrigIDs, const NBTypeCont &tc)
OpenDriveXMLTag
Numbers representing openDrive-XML - element names.
static void sanitizeWidths(OpenDriveEdge *e)
static PositionVector geomFromParamPoly(const OpenDriveEdge &e, const OpenDriveGeometry &g, double resolution)
static StringBijection< int >::Entry openDriveTags[]
The names of openDrive-XML elements (for passing to GenericSAXHandler)
std::vector< OpenDriveSignal > signals
static PositionVector geomFromLine(const OpenDriveEdge &e, const OpenDriveGeometry &g, double resolution)
LinkType
OpenDrive link type enumeration.
static void revisitLaneSections(const NBTypeCont &tc, std::map< std::string, OpenDriveEdge *> &edges)
Rechecks lane sections of the given edges.
Instance responsible for building networks.
Definition: NBNetBuilder.h:110
static bool laneSectionsConnected(OpenDriveEdge *edge, int in, int out)
Representation of an OpenDrive geometry part.
A storage for options typed value containers)
Definition: OptionsCont.h:90
static void setEdgeLinks2(OpenDriveEdge &e, const std::map< std::string, OpenDriveEdge *> &edges)
std::vector< OpenDriveGeometry > geometries
Represents a single node (junction) during network building.
Definition: NBNode.h:68
void addGeometryShape(GeometryType type, const std::vector< double > &vals)
A class for sorting lane sections by their s-value.
double computeAt(double pos) const
static void setNodeSecure(NBNodeCont &nc, OpenDriveEdge &e, const std::string &nodeID, NIImporter_OpenDrive::LinkType lt)
bool operator()(const std::pair< double, double > &ps)
the comparing function
A connection between two roads.
OpenDriveEdge(const std::string &idArg, const std::string &streetNameArg, const std::string &junctionArg, double lengthArg)
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:60
std::vector< std::pair< double, double > > speeds
List of positions/speeds of speed changes.
Importer for networks stored in openDrive format.
double s
The starting offset of this lane section.
int operator()(const OpenDriveLaneSection &ls1, const OpenDriveLaneSection &ls2)
Sorting function; compares OpenDriveLaneSection::s.
#define UNSET_CONNECTION
A storage for available types of edges.
Definition: NBTypeCont.h:55
std::string streetName
The road name of the edge.
static void buildConnectionsToOuter(const Connection &c, const std::map< std::string, OpenDriveEdge *> &innerEdges, std::vector< Connection > &into, std::set< Connection > &seen)
void myEndElement(int element)
Called when a closing tag occurs.