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-2023 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
44// ===========================================================================
45// class definitions
46// ===========================================================================
53public:
69 static void loadNetwork(const OptionsCont& oc, NBNetBuilder& nb);
70
71
72protected:
73
116
117
179
180
187
188
196
197
205
216
217
218
228 OpenDriveLink(LinkType linkTypeArg, const std::string& elementIDArg)
229 : linkType(linkTypeArg), elementID(elementIDArg),
231
233 std::string elementID;
236 };
237
238
251 OpenDriveGeometry(double lengthArg, double sArg, double xArg, double yArg, double hdgArg)
252 : length(lengthArg), s(sArg), x(xArg), y(yArg), hdg(hdgArg),
254
255 double length;
256 double s;
257 double x;
258 double y;
259 double hdg;
261 std::vector<double> params;
262 };
263
268 struct Poly3 {
276 Poly3(double _s, double _a, double _b, double _c, double _d) :
277 s(_s), a(_a), b(_b), c(_c), d(_d) {}
278
279 double computeAt(double pos) const {
280 const double ds = pos - s;
281 return a + b * ds + c * ds * ds + d * ds * ds * ds;
282 }
283
284 double s;
285 double a;
286 double b;
287 double c;
288 double d;
289 };
290
295
305
306 double speed;
307 std::vector<std::string> allowed;
308 std::vector<std::string> denied;
309 };
310
311
312
323 OpenDriveLane(int idArg, const std::string& levelArg, const std::string& typeArg) :
324 id(idArg), level(levelArg), type(typeArg), successor(UNSET_CONNECTION), predecessor(UNSET_CONNECTION),
325 speed(0), width(NBEdge::UNSPECIFIED_WIDTH), permission(0) { }
326
328 SVCPermissions computePermission(const NBTypeCont& tc, const std::vector<std::string>& allowed, const std::vector<std::string>& denied) const;
329
330 int id;
331 std::string level;
332 std::string type;
335 std::vector < std::pair<double, LaneAttributeChange> > attributeChanges;
336 double speed;
337 double width; //The lane's maximum width
339 std::vector<OpenDriveWidth> widthData;
340 };
341
342
351 OpenDriveLaneSection(double sArg);
352
353
360 void buildLaneMapping(const NBTypeCont& tc);
361
362
368 std::map<int, int> getInnerConnections(OpenDriveXMLTag dir, const OpenDriveLaneSection& prev);
369
370
371 bool buildAttributeChanges(const NBTypeCont& tc, std::vector<OpenDriveLaneSection>& newSections);
372 OpenDriveLaneSection buildLaneSection(const NBTypeCont& tc, double startPos);
374 double s;
376 double sOrig;
378 double length;
380 std::map<int, int> laneMap;
382 std::map<OpenDriveXMLTag, std::vector<OpenDriveLane> > lanesByDir;
384 std::string sumoID;
388 std::string rightType;
389 std::string leftType;
393 };
394
395
396
410 OpenDriveSignal(const std::string& idArg, const std::string typeArg, const std::string nameArg,
411 int orientationArg, bool dynamicArg, double sArg) :
412 id(idArg), type(typeArg), name(nameArg),
413 orientation(orientationArg), dynamic(dynamicArg), s(sArg),
414 minLane(0), maxLane(0)
415 { }
416
419
420 std::string id;
421 std::string type;
422 std::string name;
425 double s;
430 std::string controller;
431 };
432
433
443 OpenDriveController(const std::string& idArg, const std::string nameArg) :
444 id(idArg), name(nameArg) { }
445
448
449 std::string id;
450 std::string name;
451 std::vector<std::string> signalIDs;
452 std::string junction;
453 };
454
459 struct Connection {
460 std::string fromEdge;
461 std::string toEdge;
466 bool all;
467 std::string origID;
470
471 std::string getDescription() const {
472 return "Connection from=" + fromEdge + "_" + toString(fromLane)
473 + " to=" + toEdge + "_" + toString(toLane)
474 + " fromCP=" + (fromCP == OPENDRIVE_CP_START ? "start" : fromCP == OPENDRIVE_CP_END ? "end" : "unknown")
475 + " toCP=" + (toCP == OPENDRIVE_CP_START ? "start" : toCP == OPENDRIVE_CP_END ? "end" : "unknown")
476 + " all=" + toString(all);
477 //+ " origID=" + origID + " origLane=" + toString(origLane);
478 }
479 };
480
486 std::string type;
487 std::string name;
488 std::string id;
489 double s;
490 double t;
491 double zOffset;
492 double length;
493 double width;
494 double height;
495 double radius;
496 double hdg;
497 double pitch;
498 double roll;
499 };
500
506 OpenDriveEdge(const std::string& idArg, const std::string& streetNameArg, const std::string& junctionArg, double lengthArg) :
507 id(idArg),
508 streetName(streetNameArg),
509 junction(junctionArg),
510 length(lengthArg),
511 from(0),
512 to(0) {
513 isInner = junction != "" && junction != "-1";
514 }
515
516
523 int getPriority(OpenDriveXMLTag dir) const;
524
525
527 std::string id;
529 std::string streetName;
531 std::string junction;
533 double length;
534 std::vector<OpenDriveLink> links;
535 std::vector<OpenDriveGeometry> geometries;
536 std::vector<OpenDriveElevation> elevations;
537 std::vector<OpenDriveLaneOffset> offsets;
541 std::vector<double> laneOffsets;
542 std::vector<OpenDriveLaneSection> laneSections;
543 std::vector<OpenDriveSignal> signals;
544 std::set<Connection> connections;
545 std::vector<OpenDriveObject> objects;
547 };
548
549
552 public:
554 explicit sections_by_s_sorter() { }
555
558 return ls1.s < ls2.s;
559 }
560 };
561
562 /* @brief A class for search in position/attribute change tuple vectors for the given position */
564 public:
566 explicit same_position_finder(double pos) : myPosition(pos) { }
567
569 bool operator()(const std::pair<double, LaneAttributeChange>& ps) {
570 return ps.first == myPosition;
571 }
572
573 private:
576
577 };
578
579protected:
584 NIImporter_OpenDrive(const NBTypeCont& tc, std::map<std::string, OpenDriveEdge*>& edges);
585
586
589
590
591
593
594
605 void myStartElement(int element, const SUMOSAXAttributes& attrs);
606
615 void myCharacters(int element, const std::string& chars);
616
617
624 void myEndElement(int element);
626
627 std::map<std::string, OpenDriveSignal>& getSignals() {
628 return mySignals;
629 }
630
631 std::map<std::string, OpenDriveController>& getControllers() {
632 return myControllers;
633 }
634
635 std::map<std::string, std::vector<std::string>>& getJunctions2Controllers() {
637 }
638
639 const OpenDriveController& getController(std::string signalID) {
640 if (mySignals.find(signalID) != mySignals.end() && myControllers.find(mySignals[signalID].controller) != myControllers.end()) {
641 return myControllers[mySignals[signalID].controller];
642 }
643 return myDummyController;
644 }
645
646 int getTLIndexForController(std::string controllerID) {
647 // sort them by their id
648 std::string junctionID = myControllers[controllerID].junction;
649 std::vector<std::string> junctionControllers;
650 for (auto& it : myControllers) {
651 if (it.second.junction == junctionID) {
652 junctionControllers.push_back(it.first);
653 }
654 }
655 std::sort(junctionControllers.begin(), junctionControllers.end());
656 auto it = std::find(junctionControllers.begin(), junctionControllers.end(), controllerID);
657 return (int)(it - junctionControllers.begin());
658 }
659
660
661private:
662 void addLink(LinkType lt, const std::string& elementType, const std::string& elementID,
663 const std::string& contactPoint);
664 void addGeometryShape(GeometryType type, const std::vector<double>& vals);
665 static void setEdgeLinks2(OpenDriveEdge& e, const std::map<std::string, OpenDriveEdge*>& edges);
666 static void buildConnectionsToOuter(const Connection& c,
667 const std::map<std::string, OpenDriveEdge*>& innerEdges,
668 const std::map<std::string, OpenDriveEdge*>& edges,
669 const NBTypeCont& tc,
670 std::vector<Connection>& into, std::set<Connection>& seen);
671 static bool laneSectionsConnected(OpenDriveEdge* edge, int in, int out);
672 friend bool operator<(const Connection& c1, const Connection& c2);
673 static std::string revertID(const std::string& id);
677
678 std::map<std::string, OpenDriveEdge*>& myEdges;
679 std::vector<int> myElementStack;
686 std::map<std::string, OpenDriveSignal> mySignals;
687 std::map<std::string, OpenDriveController> myControllers;
688 std::map<std::string, std::vector<std::string>> myJunctions2Controllers;
690
691 static bool myImportAllTypes;
692 static bool myImportWidths;
693 static double myMinWidth;
696
697
698protected:
712 static NBNode* getOrBuildNode(const std::string& id, const Position& pos, NBNodeCont& nc);
713
714
715 static PositionVector geomFromLine(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
716 static PositionVector geomFromSpiral(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
717 static PositionVector geomFromArc(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
718 static PositionVector geomFromPoly(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
719 static PositionVector geomFromParamPoly(const OpenDriveEdge& e, const OpenDriveGeometry& g, double resolution);
720 static Position calculateStraightEndPoint(double hdg, double length, const Position& start);
721 static void calculateCurveCenter(double* ad_x, double* ad_y, double ad_radius, double ad_hdg);
722 static void calcPointOnCurve(double* ad_x, double* ad_y, double ad_centerX, double ad_centerY,
723 double ad_r, double ad_length);
724
725
729 static void computeShapes(std::map<std::string, OpenDriveEdge*>& edges);
730
731 static bool hasNonLinearElevation(OpenDriveEdge& e);
732
734 static std::vector<double> discretizeOffsets(PositionVector& geom, const std::vector<OpenDriveLaneOffset>& offsets, const std::string& id);
735
736 static void addOffsets(bool left, PositionVector& geom, const std::vector<OpenDriveLaneOffset>& offsets, const std::string& id, std::vector<double>& result);
737
743 static void revisitLaneSections(const NBTypeCont& tc, std::map<std::string, OpenDriveEdge*>& edges);
744
745 static void setNodeSecure(NBNodeCont& nc, OpenDriveEdge& e,
746 const std::string& nodeID, NIImporter_OpenDrive::LinkType lt, std::vector<NodeSet>& joinedNodeIDs);
747
748 static NBTrafficLightDefinition* getTLSSecure(NBEdge* inEdge, /*const NBEdge::Connection& conn,*/ NBNetBuilder& nb);
749
750
751 static std::pair<NBEdge*, NBEdge*> retrieveSignalEdges(NBNetBuilder& nb, const std::string& fromID, const std::string& toID, const std::string& junction);
752
753 static void splitMinWidths(OpenDriveEdge* e, const NBTypeCont& tc, double minDist);
754
755 static void findWidthSplit(const NBTypeCont& tc, std::vector<OpenDriveLane>& lanes,
756 int section, double sectionStart, double sectionEnd,
757 std::vector<double>& splitPositions);
758
759 static void sanitizeWidths(OpenDriveEdge* e);
760 static void sanitizeWidths(std::vector<OpenDriveLane>& lanes, double length);
761
762 static void setStraightConnections(std::vector<OpenDriveLane>& lanes);
763 static void recomputeWidths(OpenDriveLaneSection& sec, double start, double end, double sectionStart, double sectionEnd);
764 static void recomputeWidths(std::vector<OpenDriveLane>& lanes, double start, double end, double sectionStart, double sectionEnd);
765 static void setLaneAttributes(const OpenDriveEdge* e, NBEdge::Lane& sumoLane, const OpenDriveLane& odLane, bool saveOrigIDs, const NBTypeCont& tc);
766 static void writeRoadObjects(const OpenDriveEdge* e);
767
770
773
775 public:
777 explicit LaneSorter() {}
778
780 int operator()(const OpenDriveLane& a, const OpenDriveLane& b) const {
781 // sort from the reference line outwards (desceding order of sumo lane ids)
782 return abs(a.id) < abs(b.id);
783 }
784
785 };
786
787};
#define UNSET_CONNECTION
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 occuring 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 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;.
static StringBijection< int >::Entry openDriveAttrs[]
The names of openDrive-XML attributes (for passing to GenericSAXHandler)
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)
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)
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
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)
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.
static bool hasNonLinearElevation(OpenDriveEdge &e)
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.