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-2024 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
117 };
118
119
182 };
183
184
191
192
200
201
209
220
221
222
232 OpenDriveLink(LinkType linkTypeArg, const std::string& elementIDArg)
233 : linkType(linkTypeArg), elementID(elementIDArg),
235
237 std::string elementID;
240 };
241
242
255 OpenDriveGeometry(double lengthArg, double sArg, double xArg, double yArg, double hdgArg)
256 : length(lengthArg), s(sArg), x(xArg), y(yArg), hdg(hdgArg),
258
259 double length;
260 double s;
261 double x;
262 double y;
263 double hdg;
265 std::vector<double> params;
266 };
267
272 struct Poly3 {
280 Poly3(double _s, double _a, double _b, double _c, double _d) :
281 s(_s), a(_a), b(_b), c(_c), d(_d) {}
282
283 double computeAt(double pos) const {
284 const double ds = pos - s;
285 return a + b * ds + c * ds * ds + d * ds * ds * ds;
286 }
287
288 double s;
289 double a;
290 double b;
291 double c;
292 double d;
293 };
294
299
309
310 double speed;
311 std::vector<std::string> allowed;
312 std::vector<std::string> denied;
313 };
314
315
316
327 OpenDriveLane(int idArg, const std::string& levelArg, const std::string& typeArg) :
328 id(idArg), level(levelArg), type(typeArg), successor(UNSET_CONNECTION), predecessor(UNSET_CONNECTION),
329 speed(0), width(NBEdge::UNSPECIFIED_WIDTH), permission(0) { }
330
332 SVCPermissions computePermission(const NBTypeCont& tc, const std::vector<std::string>& allowed, const std::vector<std::string>& denied) const;
333
334 int id;
335 std::string level;
336 std::string type;
339 std::vector < std::pair<double, LaneAttributeChange> > attributeChanges;
340 double speed;
341 double width; //The lane's maximum width
343 std::vector<OpenDriveWidth> widthData;
344 };
345
346
355 OpenDriveLaneSection(double sArg);
356
357
364 void buildLaneMapping(const NBTypeCont& tc);
365
366
372 std::map<int, int> getInnerConnections(OpenDriveXMLTag dir, const OpenDriveLaneSection& prev);
373
374
375 bool buildAttributeChanges(const NBTypeCont& tc, std::vector<OpenDriveLaneSection>& newSections);
376 OpenDriveLaneSection buildLaneSection(const NBTypeCont& tc, double startPos);
378 double s;
380 double sOrig;
382 double length;
384 std::map<int, int> laneMap;
386 std::map<OpenDriveXMLTag, std::vector<OpenDriveLane> > lanesByDir;
388 std::string sumoID;
392 std::string rightType;
393 std::string leftType;
397 };
398
399
400
414 OpenDriveSignal(const std::string& idArg, const std::string typeArg, const std::string nameArg,
415 int orientationArg, bool dynamicArg, double sArg) :
416 id(idArg), type(typeArg), name(nameArg),
417 orientation(orientationArg), dynamic(dynamicArg), s(sArg),
419 { }
420
423
424 std::string id;
425 std::string type;
426 std::string name;
429 double s;
434 std::string controller;
435 };
436
437
447 OpenDriveController(const std::string& idArg, const std::string nameArg) :
448 id(idArg), name(nameArg) { }
449
452
453 std::string id;
454 std::string name;
455 std::vector<std::string> signalIDs;
456 std::string junction;
457 };
458
463 struct Connection {
464 std::string fromEdge;
465 std::string toEdge;
470 bool all;
471 std::string origID;
474
475 std::string getDescription() const {
476 return "Connection from=" + fromEdge + "_" + toString(fromLane)
477 + " to=" + toEdge + "_" + toString(toLane)
478 + " fromCP=" + (fromCP == OPENDRIVE_CP_START ? "start" : fromCP == OPENDRIVE_CP_END ? "end" : "unknown")
479 + " toCP=" + (toCP == OPENDRIVE_CP_START ? "start" : toCP == OPENDRIVE_CP_END ? "end" : "unknown")
480 + " all=" + toString(all);
481 //+ " origID=" + origID + " origLane=" + toString(origLane);
482 }
483 };
484
490 std::string type;
491 std::string name;
492 std::string id;
493 double s;
494 double t;
495 double zOffset;
496 double length;
497 double width;
498 double height;
499 double radius;
500 double hdg;
501 double pitch;
502 double roll;
503 };
504
510 OpenDriveEdge(const std::string& idArg, const std::string& streetNameArg, const std::string& junctionArg, double lengthArg) :
511 id(idArg),
512 streetName(streetNameArg),
513 junction(junctionArg),
514 length(lengthArg),
515 from(0),
516 to(0) {
517 isInner = junction != "" && junction != "-1";
518 }
519
520
527 int getPriority(OpenDriveXMLTag dir) const;
528
529
531 std::string id;
533 std::string streetName;
535 std::string junction;
537 double length;
538 std::vector<OpenDriveLink> links;
539 std::vector<OpenDriveGeometry> geometries;
540 std::vector<OpenDriveElevation> elevations;
541 std::vector<OpenDriveLaneOffset> offsets;
545 std::vector<double> laneOffsets;
546 std::vector<OpenDriveLaneSection> laneSections;
547 std::vector<OpenDriveSignal> signals;
548 std::set<Connection> connections;
549 std::vector<OpenDriveObject> objects;
551 };
552
553
556 public:
558 explicit sections_by_s_sorter() { }
559
562 return ls1.s < ls2.s;
563 }
564 };
565
566 /* @brief A class for search in position/attribute change tuple vectors for the given position */
568 public:
570 explicit same_position_finder(double pos) : myPosition(pos) { }
571
573 bool operator()(const std::pair<double, LaneAttributeChange>& ps) {
574 return ps.first == myPosition;
575 }
576
577 private:
580
581 };
582
583protected:
588 NIImporter_OpenDrive(const NBTypeCont& tc, std::map<std::string, OpenDriveEdge*>& edges);
589
590
593
594
595
597
598
609 void myStartElement(int element, const SUMOSAXAttributes& attrs);
610
619 void myCharacters(int element, const std::string& chars);
620
621
628 void myEndElement(int element);
630
631 std::map<std::string, OpenDriveSignal>& getSignals() {
632 return mySignals;
633 }
634
635 std::map<std::string, OpenDriveController>& getControllers() {
636 return myControllers;
637 }
638
639 std::map<std::string, std::vector<std::string>>& getJunctions2Controllers() {
641 }
642
643 const OpenDriveController& getController(std::string signalID) {
644 if (mySignals.find(signalID) != mySignals.end() && myControllers.find(mySignals[signalID].controller) != myControllers.end()) {
645 return myControllers[mySignals[signalID].controller];
646 }
647 return myDummyController;
648 }
649
650 int getTLIndexForController(std::string controllerID) {
651 // sort them by their id
652 std::string junctionID = myControllers[controllerID].junction;
653 std::vector<std::string> junctionControllers;
654 for (auto& it : myControllers) {
655 if (it.second.junction == junctionID) {
656 junctionControllers.push_back(it.first);
657 }
658 }
659 std::sort(junctionControllers.begin(), junctionControllers.end());
660 auto it = std::find(junctionControllers.begin(), junctionControllers.end(), controllerID);
661 return (int)(it - junctionControllers.begin());
662 }
663
664
665private:
666 void addLink(LinkType lt, const std::string& elementType, const std::string& elementID,
667 const std::string& contactPoint);
668 void addGeometryShape(GeometryType type, const std::vector<double>& vals);
669 static void setEdgeLinks2(OpenDriveEdge& e, const std::map<std::string, OpenDriveEdge*>& edges);
670 static void buildConnectionsToOuter(const Connection& c,
671 const std::map<std::string, OpenDriveEdge*>& innerEdges,
672 const std::map<std::string, OpenDriveEdge*>& edges,
673 const NBTypeCont& tc,
674 std::vector<Connection>& into, std::set<Connection>& seen);
675 static bool laneSectionsConnected(OpenDriveEdge* edge, int in, int out);
676 friend bool operator<(const Connection& c1, const Connection& c2);
677 static std::string revertID(const std::string& id);
681
682 std::map<std::string, OpenDriveEdge*>& myEdges;
683 std::vector<int> myElementStack;
690 std::map<std::string, OpenDriveSignal> mySignals;
691 std::map<std::string, OpenDriveController> myControllers;
692 std::map<std::string, std::vector<std::string>> myJunctions2Controllers;
695
696 static bool myImportAllTypes;
697 static bool myImportWidths;
698 static double myMinWidth;
702
703
704protected:
718 static NBNode* getOrBuildNode(const std::string& id, const Position& pos, NBNodeCont& nc);
719
720
721 static PositionVector geomFromLine(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
722 static PositionVector geomFromSpiral(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
723 static PositionVector geomFromArc(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
724 static PositionVector geomFromPoly(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
725 static PositionVector geomFromParamPoly(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
726 static Position calculateStraightEndPoint(double hdg, double length, const Position& start);
727 static void calculateCurveCenter(double* ad_x, double* ad_y, double ad_radius, double ad_hdg);
728 static void calcPointOnCurve(double* ad_x, double* ad_y, double ad_centerX, double ad_centerY,
729 double ad_r, double ad_length);
730
731
735 static void computeShapes(std::map<std::string, OpenDriveEdge*>& edges);
736
737 static bool hasNonLinearElevation(const OpenDriveEdge& e);
738
740 static std::vector<double> discretizeOffsets(PositionVector& geom, const std::vector<OpenDriveLaneOffset>& offsets, const std::string& id);
741
742 static void addOffsets(bool left, PositionVector& geom, const std::vector<OpenDriveLaneOffset>& offsets, const std::string& id, std::vector<double>& result);
743
749 static void revisitLaneSections(const NBTypeCont& tc, std::map<std::string, OpenDriveEdge*>& edges);
750
751 static void setNodeSecure(NBNodeCont& nc, OpenDriveEdge& e,
752 const std::string& nodeID, NIImporter_OpenDrive::LinkType lt, std::vector<NodeSet>& joinedNodeIDs);
753
754 static NBTrafficLightDefinition* getTLSSecure(NBEdge* inEdge, /*const NBEdge::Connection& conn,*/ NBNetBuilder& nb);
755
756
757 static std::pair<NBEdge*, NBEdge*> retrieveSignalEdges(NBNetBuilder& nb, const std::string& fromID, const std::string& toID, const std::string& junction);
758
759 static void splitMinWidths(OpenDriveEdge* e, const NBTypeCont& tc, double minDist);
760
761 static void findWidthSplit(const NBTypeCont& tc, std::vector<OpenDriveLane>& lanes,
762 int section, double sectionStart, double sectionEnd,
763 std::vector<double>& splitPositions);
764
765 static void sanitizeWidths(OpenDriveEdge* e);
766 static void sanitizeWidths(std::vector<OpenDriveLane>& lanes, double length);
767
768 static void setStraightConnections(std::vector<OpenDriveLane>& lanes);
769 static void recomputeWidths(OpenDriveLaneSection& sec, double start, double end, double sectionStart, double sectionEnd);
770 static void recomputeWidths(std::vector<OpenDriveLane>& lanes, double start, double end, double sectionStart, double sectionEnd);
771 static void setLaneAttributes(const OpenDriveEdge* e, NBEdge::Lane& sumoLane, const OpenDriveLane& odLane, bool saveOrigIDs, const NBTypeCont& tc);
772 static void writeRoadObjects(const OpenDriveEdge* e);
773
776
779
781 public:
783 explicit LaneSorter() {}
784
786 int operator()(const OpenDriveLane& a, const OpenDriveLane& b) const {
787 // sort from the reference line outwards (desceding order of sumo lane ids)
788 return abs(a.id) < abs(b.id);
789 }
790
791 };
792
793};
#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 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 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 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)
std::map< std::string, OpenDriveController > myControllers
void myCharacters(int element, const std::string &chars)
Callback method for characters to implement by derived classes.
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.