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-2020 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 
110  };
111 
112 
169  OPENDRIVE_ATTR_UNIT // xodr v1.4
170  };
171 
172 
175  enum LinkType {
178  };
179 
180 
183  enum ElementType {
187  };
188 
189 
196  };
197 
206  OPENDRIVE_GT_PARAMPOLY3 // rev 1.4
207  };
208 
209 
210 
215  struct OpenDriveLink {
220  OpenDriveLink(LinkType linkTypeArg, const std::string& elementIDArg)
221  : linkType(linkTypeArg), elementID(elementIDArg),
223 
225  std::string elementID;
228  };
229 
230 
243  OpenDriveGeometry(double lengthArg, double sArg, double xArg, double yArg, double hdgArg)
244  : length(lengthArg), s(sArg), x(xArg), y(yArg), hdg(hdgArg),
246 
247  double length;
248  double s;
249  double x;
250  double y;
251  double hdg;
253  std::vector<double> params;
254  };
255 
260  struct Poly3 {
268  Poly3(double _s, double _a, double _b, double _c, double _d) :
269  s(_s), a(_a), b(_b), c(_c), d(_d) {}
270 
271  double computeAt(double pos) const {
272  const double ds = pos - s;
273  return a + b * ds + c * ds * ds + d * ds * ds * ds;
274  }
275 
276  double s;
277  double a;
278  double b;
279  double c;
280  double d;
281  };
282 
287 
288 
293  struct OpenDriveLane {
299  OpenDriveLane(int idArg, const std::string& levelArg, const std::string& typeArg) :
300  id(idArg), level(levelArg), type(typeArg), successor(UNSET_CONNECTION), predecessor(UNSET_CONNECTION),
301  speed(0), width(NBEdge::UNSPECIFIED_WIDTH) { }
302 
303  int id;
304  std::string level;
305  std::string type;
306  int successor;
308  std::vector<std::pair<double, double> > speeds;
309  double speed;
310  double width; //The lane's maximum width
311  std::vector<OpenDriveWidth> widthData;
312  };
313 
314 
323  OpenDriveLaneSection(double sArg);
324 
325 
332  void buildLaneMapping(const NBTypeCont& tc);
333 
334 
340  std::map<int, int> getInnerConnections(OpenDriveXMLTag dir, const OpenDriveLaneSection& prev);
341 
342 
343  bool buildSpeedChanges(const NBTypeCont& tc, std::vector<OpenDriveLaneSection>& newSections);
344  OpenDriveLaneSection buildLaneSection(double startPos);
345 
347  double s;
349  double sOrig;
351  double length;
353  std::map<int, int> laneMap;
355  std::map<OpenDriveXMLTag, std::vector<OpenDriveLane> > lanesByDir;
357  std::string sumoID;
361  std::string rightType;
362  std::string leftType;
363  };
364 
365 
366 
380  OpenDriveSignal(const std::string& idArg, const std::string typeArg, const std::string nameArg,
381  int orientationArg, bool dynamicArg, double sArg) :
382  id(idArg), type(typeArg), name(nameArg),
383  orientation(orientationArg), dynamic(dynamicArg), s(sArg),
384  minLane(0), maxLane(0)
385  { }
386 
389 
390  std::string id;
391  std::string type;
392  std::string name;
394  bool dynamic;
395  double s;
397  int minLane;
398  int maxLane;
399  };
400 
401 
406  struct Connection {
407  std::string fromEdge;
408  std::string toEdge;
409  int fromLane;
410  int toLane;
413  bool all;
414  std::string origID;
415  int origLane;
417 
418  std::string getDescription() const {
419  return "Connection from=" + fromEdge + "_" + toString(fromLane)
420  + " to=" + toEdge + "_" + toString(toLane)
421  + " fromCP=" + (fromCP == OPENDRIVE_CP_START ? "start" : fromCP == OPENDRIVE_CP_END ? "end" : "unknown")
422  + " toCP=" + (toCP == OPENDRIVE_CP_START ? "start" : toCP == OPENDRIVE_CP_END ? "end" : "unknown")
423  + " all=" + toString(all);
424  //+ " origID=" + origID + " origLane=" + toString(origLane);
425  }
426  };
427 
433  std::string type;
434  std::string name;
435  std::string id;
436  double s;
437  double t;
438  double zOffset;
439  double length;
440  double width;
441  double height;
442  double radius;
443  double hdg;
444  double pitch;
445  double roll;
446  };
447 
452  struct OpenDriveEdge {
453  OpenDriveEdge(const std::string& idArg, const std::string& streetNameArg, const std::string& junctionArg, double lengthArg) :
454  id(idArg),
455  streetName(streetNameArg),
456  junction(junctionArg),
457  length(lengthArg),
458  from(0),
459  to(0) {
460  isInner = junction != "" && junction != "-1";
461  }
462 
463 
470  int getPriority(OpenDriveXMLTag dir) const;
471 
472 
474  std::string id;
476  std::string streetName;
478  std::string junction;
480  double length;
481  std::vector<OpenDriveLink> links;
482  std::vector<OpenDriveGeometry> geometries;
483  std::vector<OpenDriveElevation> elevations;
484  std::vector<OpenDriveLaneOffset> offsets;
488  std::vector<double> laneOffsets;
489  std::vector<OpenDriveLaneSection> laneSections;
490  std::vector<OpenDriveSignal> signals;
491  std::set<Connection> connections;
492  std::vector<OpenDriveObject> objects;
493  bool isInner;
494  };
495 
496 
499  public:
501  explicit sections_by_s_sorter() { }
502 
505  return ls1.s < ls2.s;
506  }
507  };
508 
509  /* @brief A class for search in position/speed tuple vectors for the given position */
511  public:
513  explicit same_position_finder(double pos) : myPosition(pos) { }
514 
516  bool operator()(const std::pair<double, double>& ps) {
517  return ps.first == myPosition;
518  }
519 
520  private:
521  same_position_finder& operator=(const same_position_finder&); // just to avoid a compiler warning
522  private:
524  double myPosition;
525 
526  };
527 
528 protected:
533  NIImporter_OpenDrive(const NBTypeCont& tc, std::map<std::string, OpenDriveEdge*>& edges);
534 
535 
538 
539 
540 
542 
543 
554  void myStartElement(int element, const SUMOSAXAttributes& attrs);
555 
564  void myCharacters(int element, const std::string& chars);
565 
566 
573  void myEndElement(int element);
575 
576  std::map<std::string, OpenDriveSignal>& getSignals() {
577  return mySignals;
578  }
579 
580 private:
581  void addLink(LinkType lt, const std::string& elementType, const std::string& elementID,
582  const std::string& contactPoint);
583  void addGeometryShape(GeometryType type, const std::vector<double>& vals);
584  static void setEdgeLinks2(OpenDriveEdge& e, const std::map<std::string, OpenDriveEdge*>& edges);
585  static void buildConnectionsToOuter(const Connection& c, const std::map<std::string, OpenDriveEdge*>& innerEdges, std::vector<Connection>& into, std::set<Connection>& seen);
586  static bool laneSectionsConnected(OpenDriveEdge* edge, int in, int out);
587  friend bool operator<(const Connection& c1, const Connection& c2);
588  static std::string revertID(const std::string& id);
591 
592  std::map<std::string, OpenDriveEdge*>& myEdges;
593  std::vector<int> myElementStack;
595  std::string myCurrentJunctionID;
600  std::map<std::string, OpenDriveSignal> mySignals;
601 
602  static bool myImportAllTypes;
603  static bool myImportWidths;
604  static double myMinWidth;
606 
607 
608 protected:
622  static NBNode* getOrBuildNode(const std::string& id, const Position& pos, NBNodeCont& nc);
623 
624 
625  static PositionVector geomFromLine(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
626  static PositionVector geomFromSpiral(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
627  static PositionVector geomFromArc(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
628  static PositionVector geomFromPoly(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
629  static PositionVector geomFromParamPoly(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
630  static Position calculateStraightEndPoint(double hdg, double length, const Position& start);
631  static void calculateCurveCenter(double* ad_x, double* ad_y, double ad_radius, double ad_hdg);
632  static void calcPointOnCurve(double* ad_x, double* ad_y, double ad_centerX, double ad_centerY,
633  double ad_r, double ad_length);
634 
635 
639  static void computeShapes(std::map<std::string, OpenDriveEdge*>& edges);
640 
641  static bool hasNonLinearElevation(OpenDriveEdge& e);
642 
648  static void revisitLaneSections(const NBTypeCont& tc, std::map<std::string, OpenDriveEdge*>& edges);
649 
650  static void setNodeSecure(NBNodeCont& nc, OpenDriveEdge& e,
651  const std::string& nodeID, NIImporter_OpenDrive::LinkType lt, std::vector<NodeSet>& joinedNodeIDs);
652 
654 
655 
656  static std::pair<NBEdge*, NBEdge*> retrieveSignalEdges(NBNetBuilder& nb, const std::string& fromID, const std::string& toID, const std::string& junction);
657 
658  static void splitMinWidths(OpenDriveEdge* e, const NBTypeCont& tc, double minDist);
659 
660  static void findWidthSplit(const NBTypeCont& tc, std::vector<OpenDriveLane>& lanes,
661  int section, double sectionStart, double sectionEnd,
662  std::vector<double>& splitPositions);
663 
664  static void sanitizeWidths(OpenDriveEdge* e);
665  static void sanitizeWidths(std::vector<OpenDriveLane>& lanes, double length);
666 
667  static void setStraightConnections(std::vector<OpenDriveLane>& lanes);
668  static void recomputeWidths(OpenDriveLaneSection& sec, double start, double end, double sectionStart, double sectionEnd);
669  static void recomputeWidths(std::vector<OpenDriveLane>& lanes, double start, double end, double sectionStart, double sectionEnd);
670  static void setLaneAttributes(const OpenDriveEdge* e, NBEdge::Lane& sumoLane, const OpenDriveLane& odLane, bool saveOrigIDs, const NBTypeCont& tc);
671 
674 
677 
678 
679 
680 };
#define UNSET_CONNECTION
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
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
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 &)
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)
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 buildConnectionsToOuter(const Connection &c, const std::map< std::string, OpenDriveEdge * > &innerEdges, std::vector< Connection > &into, std::set< Connection > &seen)
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.
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:36
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.
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.