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-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Importer for networks stored in openDrive format
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
25 #include <string>
26 #include <map>
29 
30 
31 // ===========================================================================
32 // class declarations
33 // ===========================================================================
34 class NBNetBuilder;
35 class NBEdge;
36 class OptionsCont;
37 class NBNode;
38 class NBNodeCont;
39 
40 
41 #define UNSET_CONNECTION 100000
42 
43 // ===========================================================================
44 // class definitions
45 // ===========================================================================
52 public:
68  static void loadNetwork(const OptionsCont& oc, NBNetBuilder& nb);
69 
70 
71 protected:
72 
111  };
112 
113 
170  OPENDRIVE_ATTR_UNIT // xodr v1.4
171  };
172 
173 
176  enum LinkType {
179  };
180 
181 
184  enum ElementType {
188  };
189 
190 
197  };
198 
207  OPENDRIVE_GT_PARAMPOLY3 // rev 1.4
208  };
209 
210 
211 
216  struct OpenDriveLink {
221  OpenDriveLink(LinkType linkTypeArg, const std::string& elementIDArg)
222  : linkType(linkTypeArg), elementID(elementIDArg),
224 
226  std::string elementID;
229  };
230 
231 
244  OpenDriveGeometry(double lengthArg, double sArg, double xArg, double yArg, double hdgArg)
245  : length(lengthArg), s(sArg), x(xArg), y(yArg), hdg(hdgArg),
247 
248  double length;
249  double s;
250  double x;
251  double y;
252  double hdg;
254  std::vector<double> params;
255  };
256 
261  struct Poly3 {
269  Poly3(double _s, double _a, double _b, double _c, double _d) :
270  s(_s), a(_a), b(_b), c(_c), d(_d) {}
271 
272  double computeAt(double pos) const {
273  const double ds = pos - s;
274  return a + b * ds + c * ds * ds + d * ds * ds * ds;
275  }
276 
277  double s;
278  double a;
279  double b;
280  double c;
281  double d;
282  };
283 
288 
289 
294  struct OpenDriveLane {
300  OpenDriveLane(int idArg, const std::string& levelArg, const std::string& typeArg) :
301  id(idArg), level(levelArg), type(typeArg), successor(UNSET_CONNECTION), predecessor(UNSET_CONNECTION),
302  speed(0), width(NBEdge::UNSPECIFIED_WIDTH) { }
303 
304  int id;
305  std::string level;
306  std::string type;
307  int successor;
309  std::vector<std::pair<double, double> > speeds;
310  double speed;
311  double width; //The lane's maximum width
312  std::vector<OpenDriveWidth> widthData;
313  };
314 
315 
324  OpenDriveLaneSection(double sArg);
325 
326 
333  void buildLaneMapping(const NBTypeCont& tc);
334 
335 
341  std::map<int, int> getInnerConnections(OpenDriveXMLTag dir, const OpenDriveLaneSection& prev);
342 
343 
344  bool buildSpeedChanges(const NBTypeCont& tc, std::vector<OpenDriveLaneSection>& newSections);
345  OpenDriveLaneSection buildLaneSection(double startPos);
346 
348  double s;
350  double sOrig;
352  double length;
354  std::map<int, int> laneMap;
356  std::map<OpenDriveXMLTag, std::vector<OpenDriveLane> > lanesByDir;
358  std::string sumoID;
362  std::string rightType;
363  std::string leftType;
367  };
368 
369 
370 
384  OpenDriveSignal(const std::string& idArg, const std::string typeArg, const std::string nameArg,
385  int orientationArg, bool dynamicArg, double sArg) :
386  id(idArg), type(typeArg), name(nameArg),
387  orientation(orientationArg), dynamic(dynamicArg), s(sArg),
388  minLane(0), maxLane(0)
389  { }
390 
393 
394  std::string id;
395  std::string type;
396  std::string name;
398  bool dynamic;
399  double s;
401  int minLane;
402  int maxLane;
403  };
404 
405 
410  struct Connection {
411  std::string fromEdge;
412  std::string toEdge;
413  int fromLane;
414  int toLane;
417  bool all;
418  std::string origID;
419  int origLane;
421 
422  std::string getDescription() const {
423  return "Connection from=" + fromEdge + "_" + toString(fromLane)
424  + " to=" + toEdge + "_" + toString(toLane)
425  + " fromCP=" + (fromCP == OPENDRIVE_CP_START ? "start" : fromCP == OPENDRIVE_CP_END ? "end" : "unknown")
426  + " toCP=" + (toCP == OPENDRIVE_CP_START ? "start" : toCP == OPENDRIVE_CP_END ? "end" : "unknown")
427  + " all=" + toString(all);
428  //+ " origID=" + origID + " origLane=" + toString(origLane);
429  }
430  };
431 
437  std::string type;
438  std::string name;
439  std::string id;
440  double s;
441  double t;
442  double zOffset;
443  double length;
444  double width;
445  double height;
446  double radius;
447  double hdg;
448  double pitch;
449  double roll;
450  };
451 
456  struct OpenDriveEdge {
457  OpenDriveEdge(const std::string& idArg, const std::string& streetNameArg, const std::string& junctionArg, double lengthArg) :
458  id(idArg),
459  streetName(streetNameArg),
460  junction(junctionArg),
461  length(lengthArg),
462  from(0),
463  to(0) {
464  isInner = junction != "" && junction != "-1";
465  }
466 
467 
474  int getPriority(OpenDriveXMLTag dir) const;
475 
476 
478  std::string id;
480  std::string streetName;
482  std::string junction;
484  double length;
485  std::vector<OpenDriveLink> links;
486  std::vector<OpenDriveGeometry> geometries;
487  std::vector<OpenDriveElevation> elevations;
488  std::vector<OpenDriveLaneOffset> offsets;
492  std::vector<double> laneOffsets;
493  std::vector<OpenDriveLaneSection> laneSections;
494  std::vector<OpenDriveSignal> signals;
495  std::set<Connection> connections;
496  std::vector<OpenDriveObject> objects;
497  bool isInner;
498  };
499 
500 
503  public:
505  explicit sections_by_s_sorter() { }
506 
509  return ls1.s < ls2.s;
510  }
511  };
512 
513  /* @brief A class for search in position/speed tuple vectors for the given position */
515  public:
517  explicit same_position_finder(double pos) : myPosition(pos) { }
518 
520  bool operator()(const std::pair<double, double>& ps) {
521  return ps.first == myPosition;
522  }
523 
524  private:
525  same_position_finder& operator=(const same_position_finder&) = delete; // just to avoid a compiler warning
526  private:
528  double myPosition;
529 
530  };
531 
532 protected:
537  NIImporter_OpenDrive(const NBTypeCont& tc, std::map<std::string, OpenDriveEdge*>& edges);
538 
539 
542 
543 
544 
546 
547 
558  void myStartElement(int element, const SUMOSAXAttributes& attrs);
559 
568  void myCharacters(int element, const std::string& chars);
569 
570 
577  void myEndElement(int element);
579 
580  std::map<std::string, OpenDriveSignal>& getSignals() {
581  return mySignals;
582  }
583 
584 private:
585  void addLink(LinkType lt, const std::string& elementType, const std::string& elementID,
586  const std::string& contactPoint);
587  void addGeometryShape(GeometryType type, const std::vector<double>& vals);
588  static void setEdgeLinks2(OpenDriveEdge& e, const std::map<std::string, OpenDriveEdge*>& edges);
589  static void buildConnectionsToOuter(const Connection& c,
590  const std::map<std::string, OpenDriveEdge*>& innerEdges,
591  const std::map<std::string, OpenDriveEdge*>& edges,
592  const NBTypeCont& tc,
593  std::vector<Connection>& into, std::set<Connection>& seen);
594  static bool laneSectionsConnected(OpenDriveEdge* edge, int in, int out);
595  friend bool operator<(const Connection& c1, const Connection& c2);
596  static std::string revertID(const std::string& id);
599 
600  std::map<std::string, OpenDriveEdge*>& myEdges;
601  std::vector<int> myElementStack;
603  std::string myCurrentJunctionID;
608  std::map<std::string, OpenDriveSignal> mySignals;
610 
611  static bool myImportAllTypes;
612  static bool myImportWidths;
613  static double myMinWidth;
615 
616 
617 protected:
631  static NBNode* getOrBuildNode(const std::string& id, const Position& pos, NBNodeCont& nc);
632 
633 
634  static PositionVector geomFromLine(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
635  static PositionVector geomFromSpiral(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
636  static PositionVector geomFromArc(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
637  static PositionVector geomFromPoly(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
638  static PositionVector geomFromParamPoly(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
639  static Position calculateStraightEndPoint(double hdg, double length, const Position& start);
640  static void calculateCurveCenter(double* ad_x, double* ad_y, double ad_radius, double ad_hdg);
641  static void calcPointOnCurve(double* ad_x, double* ad_y, double ad_centerX, double ad_centerY,
642  double ad_r, double ad_length);
643 
644 
648  static void computeShapes(std::map<std::string, OpenDriveEdge*>& edges);
649 
650  static bool hasNonLinearElevation(OpenDriveEdge& e);
651 
653  static std::vector<double> discretizeOffsets(PositionVector& geom, const std::vector<OpenDriveLaneOffset>& offsets, const std::string& id);
654 
655  static void addOffsets(bool left, PositionVector& geom, const std::vector<OpenDriveLaneOffset>& offsets, const std::string& id, std::vector<double>& result);
656 
662  static void revisitLaneSections(const NBTypeCont& tc, std::map<std::string, OpenDriveEdge*>& edges);
663 
664  static void setNodeSecure(NBNodeCont& nc, OpenDriveEdge& e,
665  const std::string& nodeID, NIImporter_OpenDrive::LinkType lt, std::vector<NodeSet>& joinedNodeIDs);
666 
668 
669 
670  static std::pair<NBEdge*, NBEdge*> retrieveSignalEdges(NBNetBuilder& nb, const std::string& fromID, const std::string& toID, const std::string& junction);
671 
672  static void splitMinWidths(OpenDriveEdge* e, const NBTypeCont& tc, double minDist);
673 
674  static void findWidthSplit(const NBTypeCont& tc, std::vector<OpenDriveLane>& lanes,
675  int section, double sectionStart, double sectionEnd,
676  std::vector<double>& splitPositions);
677 
678  static void sanitizeWidths(OpenDriveEdge* e);
679  static void sanitizeWidths(std::vector<OpenDriveLane>& lanes, double length);
680 
681  static void setStraightConnections(std::vector<OpenDriveLane>& lanes);
682  static void recomputeWidths(OpenDriveLaneSection& sec, double start, double end, double sectionStart, double sectionEnd);
683  static void recomputeWidths(std::vector<OpenDriveLane>& lanes, double start, double end, double sectionStart, double sectionEnd);
684  static void setLaneAttributes(const OpenDriveEdge* e, NBEdge::Lane& sumoLane, const OpenDriveLane& odLane, bool saveOrigIDs, const NBTypeCont& tc);
685 
688 
691 
692 
693  class LaneSorter {
694  public:
696  explicit LaneSorter() {}
697 
699  int operator()(const OpenDriveLane& a, const OpenDriveLane& b) const {
700  // sort from the reference line outwards (desceding order of sumo lane ids)
701  return abs(a.id) < abs(b.id);
702  }
703 
704  };
705 
706 };
#define UNSET_CONNECTION
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
A handler which converts occuring elements and attributes into enums.
The representation of a single edge during network building.
Definition: NBEdge.h:91
Instance responsible for building networks.
Definition: NBNetBuilder.h:107
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:58
Represents a single node (junction) during network building.
Definition: NBNode.h:66
The base class for traffic light logic definitions.
A storage for available edgeTypes of edges.
Definition: NBTypeCont.h:52
int operator()(const OpenDriveLane &a, const OpenDriveLane &b) const
comparing operation
double myPosition
The position to search for.
bool operator()(const std::pair< double, double > &ps)
the comparing function
same_position_finder & operator=(const same_position_finder &)=delete
A class for sorting lane sections by their s-value.
int operator()(const OpenDriveLaneSection &ls1, const OpenDriveLaneSection &ls2)
Sorting function; compares OpenDriveLaneSection::s.
Importer for networks stored in openDrive format.
static void loadNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Loads content of the optionally given SUMO file.
static void recomputeWidths(OpenDriveLaneSection &sec, double start, double end, double sectionStart, double sectionEnd)
static std::vector< double > discretizeOffsets(PositionVector &geom, const std::vector< OpenDriveLaneOffset > &offsets, const std::string &id)
transform Poly3 into a list of offsets, adding intermediate points to geom if needed
static void addOffsets(bool left, PositionVector &geom, const std::vector< OpenDriveLaneOffset > &offsets, const std::string &id, std::vector< double > &result)
std::map< std::string, OpenDriveSignal > & getSignals()
static std::pair< NBEdge *, NBEdge * > retrieveSignalEdges(NBNetBuilder &nb, const std::string &fromID, const std::string &toID, const std::string &junction)
static PositionVector geomFromParamPoly(const OpenDriveEdge &e, const OpenDriveGeometry &g, double resolution)
void myEndElement(int element)
Called when a closing tag occurs.
static void calcPointOnCurve(double *ad_x, double *ad_y, double ad_centerX, double ad_centerY, double ad_r, double ad_length)
OpenDriveXMLTag
Numbers representing openDrive-XML - element names.
void addGeometryShape(GeometryType type, const std::vector< double > &vals)
static void setStraightConnections(std::vector< OpenDriveLane > &lanes)
static void setLaneAttributes(const OpenDriveEdge *e, NBEdge::Lane &sumoLane, const OpenDriveLane &odLane, bool saveOrigIDs, const NBTypeCont &tc)
std::vector< int > myElementStack
ElementType
OpenDrive element type enumeration.
static void buildConnectionsToOuter(const Connection &c, const std::map< std::string, OpenDriveEdge * > &innerEdges, const std::map< std::string, OpenDriveEdge * > &edges, const NBTypeCont &tc, std::vector< Connection > &into, std::set< Connection > &seen)
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
static StringBijection< int >::Entry openDriveAttrs[]
The names of openDrive-XML attributes (for passing to GenericSAXHandler)
std::map< std::string, OpenDriveSignal > mySignals
static bool laneSectionsConnected(OpenDriveEdge *edge, int in, int out)
void addLink(LinkType lt, const std::string &elementType, const std::string &elementID, const std::string &contactPoint)
OpenDriveXMLAttr
Numbers representing openDrive-XML - attributes.
static PositionVector geomFromSpiral(const OpenDriveEdge &e, const OpenDriveGeometry &g, double resolution)
static PositionVector geomFromLine(const OpenDriveEdge &e, const OpenDriveGeometry &g, double resolution)
static NBNode * getOrBuildNode(const std::string &id, const Position &pos, NBNodeCont &nc)
Builds a node or returns the already built.
const NBTypeCont & myTypeContainer
NIImporter_OpenDrive(const NBTypeCont &tc, std::map< std::string, OpenDriveEdge * > &edges)
Constructor.
static Position calculateStraightEndPoint(double hdg, double length, const Position &start)
OpenDriveXMLTag myCurrentLaneDirection
static PositionVector geomFromPoly(const OpenDriveEdge &e, const OpenDriveGeometry &g, double resolution)
static void revisitLaneSections(const NBTypeCont &tc, std::map< std::string, OpenDriveEdge * > &edges)
Rechecks lane sections of the given edges.
static void sanitizeWidths(OpenDriveEdge *e)
GeometryType
OpenDrive geometry type enumeration.
static void computeShapes(std::map< std::string, OpenDriveEdge * > &edges)
Computes a polygon representation of each edge's geometry.
static void calculateCurveCenter(double *ad_x, double *ad_y, double ad_radius, double ad_hdg)
static std::string revertID(const std::string &id)
static void setEdgeLinks2(OpenDriveEdge &e, const std::map< std::string, OpenDriveEdge * > &edges)
static void splitMinWidths(OpenDriveEdge *e, const NBTypeCont &tc, double minDist)
friend bool operator<(const Connection &c1, const Connection &c2)
void myCharacters(int element, const std::string &chars)
Callback method for characters to implement by derived classes.
static NBTrafficLightDefinition * getTLSSecure(NBEdge *inEdge, NBNetBuilder &nb)
static StringBijection< int >::Entry openDriveTags[]
The names of openDrive-XML elements (for passing to GenericSAXHandler)
Poly3 OpenDriveElevation
LaneOffset has the same fields as Elevation.
ContactPoint myCurrentContactPoint
ContactPoint
OpenDrive contact type enumeration.
static void findWidthSplit(const NBTypeCont &tc, std::vector< OpenDriveLane > &lanes, int section, double sectionStart, double sectionEnd, std::vector< double > &splitPositions)
static PositionVector geomFromArc(const OpenDriveEdge &e, const OpenDriveGeometry &g, double resolution)
static void setNodeSecure(NBNodeCont &nc, OpenDriveEdge &e, const std::string &nodeID, NIImporter_OpenDrive::LinkType lt, std::vector< NodeSet > &joinedNodeIDs)
LinkType
OpenDrive link type enumeration.
static bool hasNonLinearElevation(OpenDriveEdge &e)
std::map< std::string, OpenDriveEdge * > & myEdges
A storage for options typed value containers)
Definition: OptionsCont.h:89
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
A list of positions.
Encapsulated SAX-Attributes.
An (internal) definition of a single lane of an edge.
Definition: NBEdge.h:142
A connection between two roads.
Representation of an openDrive "link".
double length
The length of the edge.
std::string id
The id of the edge.
std::string junction
The id of the junction the edge belongs to.
std::string streetName
The road name of the edge.
int getPriority(OpenDriveXMLTag dir) const
Returns the edge's priority, regarding the direction.
OpenDriveEdge(const std::string &idArg, const std::string &streetNameArg, const std::string &junctionArg, double lengthArg)
std::vector< OpenDriveLink > links
std::vector< OpenDriveSignal > signals
std::vector< OpenDriveLaneSection > laneSections
std::vector< OpenDriveLaneOffset > offsets
std::vector< OpenDriveObject > objects
std::vector< OpenDriveGeometry > geometries
std::vector< OpenDriveElevation > elevations
Representation of an OpenDrive geometry part.
OpenDriveGeometry(double lengthArg, double sArg, double xArg, double yArg, double hdgArg)
Constructor.
int predecessor
The lane's predecessor lane.
std::vector< std::pair< double, double > > speeds
List of positions/speeds of speed changes.
OpenDriveLane(int idArg, const std::string &levelArg, const std::string &typeArg)
Constructor.
int successor
The lane's successor lane.
std::vector< OpenDriveWidth > widthData
std::string level
The lane's level (not used)
double speed
The lane's speed (set in post-processing)
double length
The length of this lane section.
std::map< OpenDriveXMLTag, std::vector< OpenDriveLane > > lanesByDir
The lanes, sorted by their direction.
double discardedInnerWidthLeft
average width of removed inside lanes
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 '-') of the edge generated for this section.
OpenDriveLaneSection buildLaneSection(double startPos)
std::string rightType
the composite type built from all used lane types
int rightLaneNumber
The number of lanes on the right and on the left side, respectively.
double sOrig
The original starting offset of this lane section (differs from s if the section had to be split)
void buildLaneMapping(const NBTypeCont &tc)
Build the mapping from OpenDrive to SUMO lanes.
std::map< int, int > getInnerConnections(OpenDriveXMLTag dir, const OpenDriveLaneSection &prev)
Returns the links from the previous to this lane section.
bool buildSpeedChanges(const NBTypeCont &tc, std::vector< OpenDriveLaneSection > &newSections)
double s
The starting offset of this lane section.
OpenDriveSignal(const std::string &idArg, const std::string typeArg, const std::string nameArg, int orientationArg, bool dynamicArg, double sArg)
Constructor.
OpenDriveSignal()
dummy constructor for use in maps
double computeAt(double pos) const
Poly3(double _s, double _a, double _b, double _c, double _d)
Constructor.