Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
NIImporter_OpenDrive.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2025 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/****************************************************************************/
21// Importer for networks stored in openDrive format
22/****************************************************************************/
23#pragma once
24#include <config.h>
25
26#include <string>
27#include <map>
30
31
32// ===========================================================================
33// class declarations
34// ===========================================================================
35class NBNetBuilder;
36class NBEdge;
37class OptionsCont;
38class NBNode;
39class NBNodeCont;
40
41
42#define UNSET_CONNECTION 100000
43#define UNSET_LANEVALIDITY 100000
44
45// ===========================================================================
46// class definitions
47// ===========================================================================
54public:
70 static void loadNetwork(const OptionsCont& oc, NBNetBuilder& nb);
71
72
73protected:
74
119 };
120
184 };
185
186
193
194
202
203
211
222
223
224
234 OpenDriveLink(LinkType linkTypeArg, const std::string& elementIDArg)
235 : linkType(linkTypeArg), elementID(elementIDArg),
237
239 std::string elementID;
242 };
243
244
257 OpenDriveGeometry(double lengthArg, double sArg, double xArg, double yArg, double hdgArg)
258 : length(lengthArg), s(sArg), x(xArg), y(yArg), hdg(hdgArg),
260
261 double length;
262 double s;
263 double x;
264 double y;
265 double hdg;
267 std::vector<double> params;
268 };
269
274 struct Poly3 {
282 Poly3(double _s, double _a, double _b, double _c, double _d) :
283 s(_s), a(_a), b(_b), c(_c), d(_d) {}
284
285 double computeAt(double pos) const {
286 const double ds = pos - s;
287 return a + b * ds + c * ds * ds + d * ds * ds * ds;
288 }
289
290 double s;
291 double a;
292 double b;
293 double c;
294 double d;
295 };
296
301
311
312 double speed;
313 std::vector<std::string> allowed;
314 std::vector<std::string> denied;
315 };
316
317
318
329 OpenDriveLane(int idArg, const std::string& levelArg, const std::string& typeArg) :
330 id(idArg), level(levelArg), type(typeArg), successor(UNSET_CONNECTION), predecessor(UNSET_CONNECTION),
331 speed(0), width(NBEdge::UNSPECIFIED_WIDTH), permission(0) { }
332
334 SVCPermissions computePermission(const NBTypeCont& tc, const std::vector<std::string>& allowed, const std::vector<std::string>& denied) const;
335
336 int id;
337 std::string level;
338 std::string type;
341 std::vector < std::pair<double, LaneAttributeChange> > attributeChanges;
342 double speed;
343 double width; //The lane's maximum width
345 std::vector<OpenDriveWidth> widthData;
346 };
347
348
357 OpenDriveLaneSection(double sArg);
358
359
366 void buildLaneMapping(const NBTypeCont& tc);
367
368
374 std::map<int, int> getInnerConnections(OpenDriveXMLTag dir, const OpenDriveLaneSection& prev);
375
376
377 bool buildAttributeChanges(const NBTypeCont& tc, std::vector<OpenDriveLaneSection>& newSections);
378 OpenDriveLaneSection buildLaneSection(const NBTypeCont& tc, double startPos);
380 double s;
382 double sOrig;
384 double length;
386 std::map<int, int> laneMap;
388 std::map<OpenDriveXMLTag, std::vector<OpenDriveLane> > lanesByDir;
390 std::string sumoID;
394 std::string rightType;
395 std::string leftType;
399 };
400
401
402
416 OpenDriveSignal(const std::string& idArg, const std::string typeArg, const std::string nameArg,
417 int orientationArg, bool dynamicArg, double sArg) :
418 id(idArg), type(typeArg), name(nameArg),
419 orientation(orientationArg), dynamic(dynamicArg), s(sArg),
421 { }
422
425
426 std::string id;
427 std::string type;
428 std::string name;
429 std::string priority;
432 double s;
437 std::string controller;
438 };
439
440
450 OpenDriveController(const std::string& idArg, const std::string nameArg) :
451 id(idArg), name(nameArg) { }
452
455
456 std::string id;
457 std::string name;
458 std::vector<std::string> signalIDs;
459 std::string junction;
460 };
461
466 struct Connection {
467 std::string fromEdge;
468 std::string toEdge;
473 bool all;
474 std::string origID;
477
478 std::string getDescription() const {
479 return "Connection from=" + fromEdge + "_" + toString(fromLane)
480 + " to=" + toEdge + "_" + toString(toLane)
481 + " fromCP=" + (fromCP == OPENDRIVE_CP_START ? "start" : fromCP == OPENDRIVE_CP_END ? "end" : "unknown")
482 + " toCP=" + (toCP == OPENDRIVE_CP_START ? "start" : toCP == OPENDRIVE_CP_END ? "end" : "unknown")
483 + " all=" + toString(all);
484 //+ " origID=" + origID + " origLane=" + toString(origLane);
485 }
486 };
487
493 std::string type;
494 std::string name;
495 std::string id;
496 double s;
497 double t;
498 double zOffset;
499 double length;
500 double width;
501 double height;
502 double radius;
503 double hdg;
504 double pitch;
505 double roll;
506 };
507
513 OpenDriveEdge(const std::string& idArg, const std::string& streetNameArg, const std::string& junctionArg, double lengthArg) :
514 id(idArg),
515 streetName(streetNameArg),
516 junction(junctionArg),
517 length(lengthArg),
518 from(0),
519 to(0) {
520 isInner = junction != "" && junction != "-1";
521 }
522
523
530 int getPriority(OpenDriveXMLTag dir) const;
531
532
534 std::string id;
536 std::string streetName;
538 std::string junction;
540 double length;
541 std::vector<OpenDriveLink> links;
542 std::vector<OpenDriveGeometry> geometries;
543 std::vector<OpenDriveElevation> elevations;
544 std::vector<OpenDriveLaneOffset> offsets;
548 std::vector<double> laneOffsets;
549 std::vector<OpenDriveLaneSection> laneSections;
550 std::vector<OpenDriveSignal> signals;
551 std::set<Connection> connections;
552 std::vector<OpenDriveObject> objects;
554 };
555
556
559 public:
561 explicit sections_by_s_sorter() { }
562
565 return ls1.s < ls2.s;
566 }
567 };
568
569 /* @brief A class for search in position/attribute change tuple vectors for the given position */
571 public:
573 explicit same_position_finder(double pos) : myPosition(pos) { }
574
576 bool operator()(const std::pair<double, LaneAttributeChange>& ps) {
577 return ps.first == myPosition;
578 }
579
580 private:
583
584 };
585
586protected:
591 NIImporter_OpenDrive(const NBTypeCont& tc, std::map<std::string, OpenDriveEdge*>& edges);
592
593
596
597
598
600
601
612 void myStartElement(int element, const SUMOSAXAttributes& attrs);
613
622 void myCharacters(int element, const std::string& chars);
623
624
631 void myEndElement(int element);
633
634 std::map<std::string, OpenDriveSignal>& getSignals() {
635 return mySignals;
636 }
637
638 std::map<std::string, OpenDriveController>& getControllers() {
639 return myControllers;
640 }
641
642 std::map<std::string, std::vector<std::string>>& getJunctions2Controllers() {
644 }
645
646 const OpenDriveController& getController(std::string signalID) {
647 if (mySignals.find(signalID) != mySignals.end() && myControllers.find(mySignals[signalID].controller) != myControllers.end()) {
648 return myControllers[mySignals[signalID].controller];
649 }
650 return myDummyController;
651 }
652
653 int getTLIndexForController(std::string controllerID) {
654 // sort them by their id
655 std::string junctionID = myControllers[controllerID].junction;
656 std::vector<std::string> junctionControllers;
657 for (auto& it : myControllers) {
658 if (it.second.junction == junctionID) {
659 junctionControllers.push_back(it.first);
660 }
661 }
662 std::sort(junctionControllers.begin(), junctionControllers.end());
663 auto it = std::find(junctionControllers.begin(), junctionControllers.end(), controllerID);
664 return (int)(it - junctionControllers.begin());
665 }
666
667
668private:
669 void addLink(LinkType lt, const std::string& elementType, const std::string& elementID,
670 const std::string& contactPoint);
671 void addGeometryShape(GeometryType type, const std::vector<double>& vals);
672 static void setEdgeLinks2(OpenDriveEdge& e, const std::map<std::string, OpenDriveEdge*>& edges);
673 static void buildConnectionsToOuter(const Connection& c,
674 const std::map<std::string, OpenDriveEdge*>& innerEdges,
675 const std::map<std::string, OpenDriveEdge*>& edges,
676 const NBTypeCont& tc,
677 std::vector<Connection>& into, std::set<Connection>& seen);
678 static bool laneSectionsConnected(OpenDriveEdge* edge, int in, int out);
679 friend bool operator<(const Connection& c1, const Connection& c2);
680 static std::string reversedEdgeID(const std::string& id);
684
685 std::map<std::string, OpenDriveEdge*>& myEdges;
686 std::vector<int> myElementStack;
693 std::map<std::string, OpenDriveSignal> mySignals;
694 std::map<std::string, OpenDriveController> myControllers;
695 std::map<std::string, std::vector<std::string>> myJunctions2Controllers;
698
699 static bool myImportAllTypes;
700 static bool myImportWidths;
701 static double myMinWidth;
705
706
707protected:
721 static NBNode* getOrBuildNode(const std::string& id, const Position& pos, NBNodeCont& nc);
722
723
724 static PositionVector geomFromLine(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
725 static PositionVector geomFromSpiral(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
726 static PositionVector geomFromArc(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
727 static PositionVector geomFromPoly(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
728 static PositionVector geomFromParamPoly(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
729 static Position calculateStraightEndPoint(double hdg, double length, const Position& start);
730 static void calculateCurveCenter(double* ad_x, double* ad_y, double ad_radius, double ad_hdg);
731 static void calcPointOnCurve(double* ad_x, double* ad_y, double ad_centerX, double ad_centerY,
732 double ad_r, double ad_length);
733
734
738 static void computeShapes(std::map<std::string, OpenDriveEdge*>& edges);
739
740 static bool hasNonLinearElevation(const OpenDriveEdge& e);
741
743 static std::vector<double> discretizeOffsets(PositionVector& geom, const std::vector<OpenDriveLaneOffset>& offsets, const std::string& id);
744
745 static void addOffsets(bool left, PositionVector& geom, const std::vector<OpenDriveLaneOffset>& offsets, const std::string& id, std::vector<double>& result);
746
752 static void revisitLaneSections(const NBTypeCont& tc, std::map<std::string, OpenDriveEdge*>& edges);
753
754 static void setNodeSecure(NBNodeCont& nc, OpenDriveEdge& e,
755 const std::string& nodeID, NIImporter_OpenDrive::LinkType lt, std::vector<NodeSet>& joinedNodeIDs);
756
757 static NBTrafficLightDefinition* getTLSSecure(NBEdge* inEdge, /*const NBEdge::Connection& conn,*/ NBNetBuilder& nb);
758
759
760 static std::pair<NBEdge*, NBEdge*> retrieveSignalEdges(NBNetBuilder& nb, const std::string& fromID, const std::string& toID, int signalMinLane);
761
762 static void splitMinWidths(OpenDriveEdge* e, const NBTypeCont& tc, double minDist);
763
764 static void findWidthSplit(const NBTypeCont& tc, std::vector<OpenDriveLane>& lanes,
765 int section, double sectionStart, double sectionEnd,
766 std::vector<double>& splitPositions);
767
768 static void sanitizeWidths(OpenDriveEdge* e);
769 static void sanitizeWidths(std::vector<OpenDriveLane>& lanes, double length);
770
771 static void setStraightConnections(std::vector<OpenDriveLane>& lanes);
772 static void recomputeWidths(OpenDriveLaneSection& sec, double start, double end, double sectionStart, double sectionEnd);
773 static void recomputeWidths(std::vector<OpenDriveLane>& lanes, double start, double end, double sectionStart, double sectionEnd);
774 static void setLaneAttributes(const OpenDriveEdge* e, NBEdge::Lane& sumoLane, const OpenDriveLane& odLane, bool saveOrigIDs, const NBTypeCont& tc);
775 static void writeRoadObjects(const OpenDriveEdge* e);
776
779
782
784 public:
786 explicit LaneSorter() {}
787
789 int operator()(const OpenDriveLane& a, const OpenDriveLane& b) const {
790 // sort from the reference line outwards (desceding order of sumo lane ids)
791 return abs(a.id) < abs(b.id);
792 }
793
794 };
795
796};
#define UNSET_CONNECTION
#define UNSET_LANEVALIDITY
long long int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
A handler which converts occurring elements and attributes into enums.
The representation of a single edge during network building.
Definition NBEdge.h:92
Instance responsible for building networks.
Container for nodes during the netbuilding process.
Definition NBNodeCont.h:57
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, LaneAttributeChange > &ps)
the comparing function
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)
static void writeRoadObjects(const OpenDriveEdge *e)
static PositionVector geomFromParamPoly(const OpenDriveEdge &e, const OpenDriveGeometry &g, double resolution)
void myEndElement(int element)
Called when a closing tag occurs.
static SequentialStringBijection::Entry openDriveAttrs[]
The names of openDrive-XML attributes (for passing to GenericSAXHandler)
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)
OpenDriveController myCurrentController
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;.
std::map< std::string, OpenDriveSignal > & getSignals()
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)
static OpenDriveController myDummyController
std::map< std::string, std::vector< std::string > > myJunctions2Controllers
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
static Position calculateStraightEndPoint(double hdg, double length, const Position &start)
static bool hasNonLinearElevation(const OpenDriveEdge &e)
std::map< std::string, std::vector< std::string > > & getJunctions2Controllers()
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 void setEdgeLinks2(OpenDriveEdge &e, const std::map< std::string, OpenDriveEdge * > &edges)
static std::pair< NBEdge *, NBEdge * > retrieveSignalEdges(NBNetBuilder &nb, const std::string &fromID, const std::string &toID, int signalMinLane)
static void splitMinWidths(OpenDriveEdge *e, const NBTypeCont &tc, double minDist)
friend bool operator<(const Connection &c1, const Connection &c2)
std::map< std::string, OpenDriveController > myControllers
void myCharacters(int element, const std::string &chars)
Callback method for characters to implement by derived classes.
static std::string reversedEdgeID(const std::string &id)
static NBTrafficLightDefinition * getTLSSecure(NBEdge *inEdge, NBNetBuilder &nb)
Poly3 OpenDriveElevation
LaneOffset has the same fields as Elevation.
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 SequentialStringBijection::Entry openDriveTags[]
The names of openDrive-XML elements (for passing to GenericSAXHandler)
static PositionVector geomFromArc(const OpenDriveEdge &e, const OpenDriveGeometry &g, double resolution)
std::map< std::string, OpenDriveController > & getControllers()
static void setNodeSecure(NBNodeCont &nc, OpenDriveEdge &e, const std::string &nodeID, NIImporter_OpenDrive::LinkType lt, std::vector< NodeSet > &joinedNodeIDs)
LinkType
OpenDrive link type enumeration.
std::map< std::string, OpenDriveEdge * > & myEdges
int getTLIndexForController(std::string controllerID)
const OpenDriveController & getController(std::string signalID)
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:143
A connection between two roads.
Attribute set applied at a certain position along a lane.
OpenDriveController()
dummy constructor for use in maps
OpenDriveController(const std::string &idArg, const std::string nameArg)
Constructor.
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< 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.
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)
std::vector< std::pair< double, LaneAttributeChange > > attributeChanges
List of permission and speed changes.
double speed
The lane's speed (set in post-processing)
SVCPermissions computePermission(const NBTypeCont &tc, const std::vector< std::string > &allowed, const std::vector< std::string > &denied) const
compute the actual SUMO lane permissions given the lane type as a start solution
SVCPermissions permission
The access permissions (set in post-processing)
OpenDriveLaneSection buildLaneSection(const NBTypeCont &tc, double startPos)
bool buildAttributeChanges(const NBTypeCont &tc, std::vector< OpenDriveLaneSection > &newSections)
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.
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.
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.
std::string controller
the controller ID
OpenDriveSignal()
dummy constructor for use in maps
double computeAt(double pos) const
Poly3(double _s, double _a, double _b, double _c, double _d)
Constructor.