02_regulatory_elements/main.cpp
Go to the documentation of this file.
3 #include <lanelet2_core/primitives/Lanelet.h>
4 
6 
7 // we want assert statements to work in release mode
8 #undef NDEBUG
9 
13 
14 int main() {
15  // this tutorial show you how to use regulatory elements in a lanelet map. This tutorial is divided in 4 parts:
19  return 0;
20 }
21 
23  using namespace lanelet;
24  // lanelet2 defines some basic regulatory elements such as traffic lights and speed limits. This tutorial shows you
25  // how they work and interact with lanelets. We could do the same thing with areas, because the interface is
26  // identical.
27 
28  // traffic lights are created from a linestring that shows a traffic light and optionally a stop line.
29  LineString3d trafficLight = examples::getLineStringAtY(1);
31 
32  // this creates our traffic light. Regelems are passed around as shared pointers.
33  RegulatoryElementPtr trafficLightRegelem = lanelet::TrafficLight::make(utils::getId(), {}, {trafficLight});
34 
35  // actually, traffic light regelem is not a RegulatoryElement (this is the base class), but a TrafficLight.
36  // we could cast it back to get the actual type. But first lets see, how to get this into a lanelet:
38  lanelet.addRegulatoryElement(trafficLightRegelem); // thats it.
39  assert(lanelet.regulatoryElements().size() == 1);
40 
41  // to get the regulatory element back, we can either get it like this
42  RegulatoryElementPtr regelem = lanelet.regulatoryElements()[0];
43 
44  // but we can also ask the lanelet to give it to use with its actual type using regulatoryElementAs:
45  assert(lanelet.regulatoryElementsAs<SpeedLimit>().empty()); // no speed limits
46  std::vector<TrafficLight::Ptr> trafficLightRegelems = lanelet.regulatoryElementsAs<TrafficLight>();
47  assert(trafficLightRegelems.size() == 1);
48  TrafficLight::Ptr tlRegelem = trafficLightRegelems.front(); // here it is with its correct type.
49  assert(tlRegelem->constData() == trafficLightRegelem->constData()); // they are actually the same.
50 
51  // from traffic lights we can directly get the relevant lights and the stop line (we didnt set one, but we could).
52  // since traffic lights can either be a polygon or a linestring, we get an object that represents both.
53  LineStringOrPolygon3d theLight = tlRegelem->trafficLights().front();
54  assert(theLight == trafficLight);
55 
56  // we can also modify it, and since regulatory element data is shared, this also affects the lanelet that holds it
57  tlRegelem->setStopLine(examples::getLineStringAtY(2));
58  assert(!!tlRegelem->stopLine());
59 
60  // there are much more regulatory elements, but they all work basically in the same way as shown here.
61 }
62 
64  using namespace lanelet;
65  // actually, the parts of the interface we just showed to you are mostly only for convenience. The internal strucure
66  // is a bit more complicated. To show that, we use a GenericRegulatoryElement that can be used to model any rule.
67  // However, it should not be used in practice, because it generic structure makes it too hard to interpret it.
69 
70  // to the generic regulatory elements we can add any primitive (point, linestring, lanelet, area) with any role:
73  Point3d point(utils::getId(), 0, 0, 0);
74  regelem.addParameter(RoleName::Refers, point);
75 
76  // now two different primitives have been added with the same role. Internally they are stored as boost::variants.
77  // to read them from the regelem, we have to pass the type we are looking for:
79  assert(!pts.empty() && pts.front() == point);
80 
81  // this interface could be used add more things with more nonsense role names. But that would be hard to interpret.
82  // For that reason, the implementations of regulatory elements provide an interface that gives less opportunity for
83  // abuse.
84 }
85 
86 // as an example, we create a new regulatory element and register it with lanelet2. It is a "LightsOn" regulatory
87 // element, that tells the vehicle to turn its lights on after passing a specific line
88 namespace example {
89 class LightsOn : public lanelet::RegulatoryElement { // we have to inherit from the abstract regulatoryElement
90  public:
91  // lanelet2 looks for this string when matching the subtype of a regulatory element to the respective type
92  static constexpr char RuleName[] = "lights_on";
93 
94  // returns the line where we are supposed to stop
96  return getParameters<lanelet::ConstLineString3d>(lanelet::RoleName::RefLine).front();
97  }
98 
99  private:
101  : RegulatoryElement{std::make_shared<lanelet::RegulatoryElementData>(id)} {
103  }
104 
105  // the following lines are required so that lanelet2 can create this object when loading a map with this regulatory
106  // element
109 };
110 
111 #if __cplusplus < 201703L
112 constexpr char LightsOn::RuleName[]; // instanciate string in cpp file
113 #endif
114 } // namespace example
115 
116 namespace {
117 // this object actually does the registration work for us
119 } // namespace
120 
122  using namespace lanelet;
123  // after creating our new class and registering it, we can test if it works. For that we use the
124  // RegulatoryElementFactory that is used by Lanelet2_io when loading a map. If we did it right, it should now return
125  // a regulatory element of the LightsOn class.
126 
127  // for that we create a valid regulatory element data object
129  RuleParameterMap rules{{RoleNameString::RefLine, {fromWhere}}};
130 
131  RegulatoryElementPtr regelem = RegulatoryElementFactory::create("lights_on", utils::getId(), rules);
132 
133  // now we can add it to a lanelet and query for it
135  lanelet.addRegulatoryElement(regelem);
136  assert(!lanelet.regulatoryElementsAs<example::LightsOn>().empty());
137 }
lanelet::RoleName::RefLine
@ RefLine
lanelet::GenericRegulatoryElement
LaneletMap.h
lanelet
lanelet::RegulatoryElement::empty
bool empty() const
RegulatoryElementPtr
std::shared_ptr< RegulatoryElement > RegulatoryElementPtr
lanelet::RegulatoryElement::parameters
RuleParameterMap & parameters()
HybridMap< RuleParameters, decltype(RoleNameString::Map)&, RoleNameString::Map >::insert
std::pair< iterator, bool > insert(const value_type &v)
lanelet::utils::getId
Id getId()
lanelet::RegulatoryElementDataPtr
std::shared_ptr< RegulatoryElementData > RegulatoryElementDataPtr
lanelet::Id
int64_t Id
lanelet::examples::getALanelet
Lanelet getALanelet()
Definition: ExampleHelpers.h:32
lanelet::RegulatoryElement
Primitive< ConstLineString3d >::attributes
AttributeMap & attributes() noexcept
example::LightsOn
Definition: 02_regulatory_elements/main.cpp:89
lanelet::SpeedLimit
BasicRegulatoryElements.h
lanelet::AttributeValueString::TrafficLight
static constexpr const char TrafficLight[]
example
Definition: 02_regulatory_elements/main.cpp:88
example::LightsOn::RuleName
static constexpr char RuleName[]
Definition: 02_regulatory_elements/main.cpp:92
lanelet::RegulatoryElement::getParameters
ConstRuleParameterMap getParameters() const
lanelet::Lanelet
lanelet::TrafficLight::Ptr
std::shared_ptr< TrafficLight > Ptr
part3AddingNewRegulatoryElements
void part3AddingNewRegulatoryElements()
Definition: 02_regulatory_elements/main.cpp:121
lanelet::TrafficLight
lanelet::AttributeName::Type
@ Type
part2HandlingRegulatoryElements
void part2HandlingRegulatoryElements()
Definition: 02_regulatory_elements/main.cpp:63
Points3d
std::vector< Point3d > Points3d
lanelet::GenericRegulatoryElement::addParameter
void addParameter(const std::string &role, const PrimitiveT &primitive)
lanelet::examples::getLineStringAtY
LineString3d getLineStringAtY(double y)
Definition: ExampleHelpers.h:12
ExampleHelpers.h
example::LightsOn::fromWhere
lanelet::ConstLineString3d fromWhere() const
Definition: 02_regulatory_elements/main.cpp:95
lanelet::Point3d
HybridMap< RuleParameters, decltype(RoleNameString::Map)&, RoleNameString::Map >
lanelet::RegisterRegulatoryElement
ConstLineStringImpl< Point3d >::front
const ConstPointType & front() const noexcept
lanelet::LineStringOrPolygon3d
lanelet::ConstLineString3d
lanelet::RoleNameString::RefLine
static constexpr const char RefLine[]
example::LightsOn::LightsOn
LightsOn(lanelet::Id id, lanelet::LineString3d fromWhere)
Definition: 02_regulatory_elements/main.cpp:100
lanelet::RegulatoryElement::data
RegulatoryElementDataPtr data()
lanelet::RegulatoryElementFactory::create
static RegulatoryElementPtr create(const std::string &ruleName, Id id, const RuleParameterMap &map, const AttributeMap &attributes=AttributeMap())
lanelet::examples::getLineStringAtX
LineString3d getLineStringAtX(double x)
Definition: ExampleHelpers.h:7
part1BasicRegulatoryElements
void part1BasicRegulatoryElements()
Definition: 02_regulatory_elements/main.cpp:22
lanelet::RoleName::Refers
@ Refers
lanelet::LineString3d
main
int main()
Definition: 02_regulatory_elements/main.cpp:14
example::LightsOn::LightsOn
LightsOn(const lanelet::RegulatoryElementDataPtr &data)
Definition: 02_regulatory_elements/main.cpp:108
lanelet::TrafficLight::make
static Ptr make(Id id, const AttributeMap &attributes, const LineStringsOrPolygons3d &trafficLights, const Optional< LineString3d > &stopLine={})


lanelet2_examples
Author(s): Fabian Poggenhans
autogenerated on Thu Mar 6 2025 03:26:15