RegulatoryElement.cpp
Go to the documentation of this file.
2 
8 
9 namespace lanelet {
10 namespace {
11 class HasIdVisitor : public RuleParameterVisitor {
12  public:
13  explicit HasIdVisitor(Id id) : id_{id} {}
14  void operator()(const ConstPoint3d& p) override { found_ |= p.id() == id_; }
15  void operator()(const ConstLineString3d& l) override { found_ |= l.id() == id_ || utils::has(l, id_); }
16  void operator()(const ConstPolygon3d& p) override { found_ |= p.id() == id_ || utils::has(p, id_); }
17  void operator()(const ConstWeakLanelet& ll) override {
18  if (ll.expired()) {
19  return;
20  }
21  ConstLanelet llet(ll.lock());
22  found_ |= llet.id() == id_ || utils::has(llet, id_);
23  }
24  void operator()(const ConstWeakArea& ar) override {
25  if (ar.expired()) {
26  return;
27  }
28  ConstArea area(ar.lock());
29  found_ |= area.id() == id_ || utils::has(area, id_);
30  }
31  bool operator!() const { return !found_; }
32 
33  private:
35  bool found_{false};
36 };
37 
38 class GetIdVisitor : public RuleParameterVisitor {
39  public:
40  static Id id(const ConstRuleParameter& param) {
41  GetIdVisitor visitor;
42  boost::apply_visitor(visitor, param);
43  return visitor.id_;
44  }
45  template <typename PrimT>
46  void appendID(const PrimT& p) {
47  id_ = p.id();
48  }
49 
50  void operator()(const ConstPoint3d& p) override { appendID(p); }
51  void operator()(const ConstLineString3d& l) override { appendID(l); }
52  void operator()(const ConstPolygon3d& p) override { appendID(p); }
53  void operator()(const ConstWeakLanelet& ll) override {
54  if (!ll.expired()) {
55  appendID(ll.lock());
56  }
57  }
58  void operator()(const ConstWeakArea& ar) override {
59  if (!ar.expired()) {
60  appendID(ar.lock());
61  }
62  }
63 
64  private:
65  Id id_{};
66 };
67 
68 class ToConstVisitor : public RuleParameterVisitor {
69  public:
70  void operator()(const ConstPoint3d& p) override { param_ = p; }
71  void operator()(const ConstLineString3d& l) override { param_ = l; }
72  void operator()(const ConstPolygon3d& p) override { param_ = p; }
73  void operator()(const ConstWeakLanelet& ll) override { param_ = ll; }
74  void operator()(const ConstWeakArea& ar) override { param_ = ar; }
75 
76  ConstRuleParameter apply(const RuleParameter& rule) {
77  boost::apply_visitor(*this, rule);
78  return param_;
79  }
80 
81  private:
83 };
84 
85 } // namespace
86 
88 #if __cplusplus < 201703L
89 constexpr char GenericRegulatoryElement::RuleName[];
90 
91 constexpr const char RoleNameString::Refers[];
92 constexpr const char RoleNameString::RefLine[];
93 constexpr const char RoleNameString::Yield[];
94 constexpr const char RoleNameString::RightOfWay[];
95 constexpr const char RoleNameString::Cancels[];
96 constexpr const char RoleNameString::CancelLine[];
97 
98 // roles not included in fast lookup
99 constexpr const char RoleNameString::Left[];
100 constexpr const char RoleNameString::Right[];
101 constexpr const char RoleNameString::Centerline[];
102 constexpr const char RoleNameString::Inner[];
103 constexpr const char RoleNameString::Outer[];
104 constexpr const char RoleNameString::Lanelet[];
105 constexpr const char RoleNameString::RegulatoryElement[];
106 
107 constexpr char RegulatoryElement::RuleName[];
108 
110 #endif
111 
113 
115  if (ruleName.empty()) {
117  }
118  data->attributes[AttributeNamesString::Subtype] = ruleName;
119  auto& inst = RegulatoryElementFactory::instance();
120  auto it = inst.registry_.find(ruleName);
121  if (it != inst.registry_.end()) {
122  return it->second(data);
123  }
124  throw InvalidInputError("No regulatory element found that implements rule " + ruleName);
125 }
126 
127 std::vector<std::string> RegulatoryElementFactory::availableRules() {
129  return utils::transform(registry, [](const auto& elem) { return elem.first; });
130 }
131 
133  static RegulatoryElementFactory factory;
134  return factory;
135 }
136 
138  ConstRuleParameterMap params;
139  for (const auto& param : parameters()) {
140  params.insert(std::make_pair(
141  param.first, utils::transform(param.second, [](const auto& elem) { return traits::toConst(elem); })));
142  }
143  return params;
144 }
145 
147  for (const auto& elems : parameters()) {
148  visitor.role = elems.first;
149  for (const auto& elem : elems.second) {
150  boost::apply_visitor(visitor, elem);
151  }
152  }
153 }
154 
156  for (const auto& elems : parameters()) {
157  visitor.role = elems.first;
158  for (const auto& elem : elems.second) {
159  boost::apply_visitor(visitor, elem);
160  }
161  }
162 }
163 
164 bool utils::has(const RegulatoryElement& regElem, Id id) {
165  HasIdVisitor hasId(id);
166  regElem.applyVisitor(hasId);
167  return !!hasId;
168 }
169 
170 template <>
172  return ToConstVisitor().apply(primitive);
173 }
174 
175 std::ostream& operator<<(std::ostream& stream, const RegulatoryElement& obj) {
176  stream << "[id: " << obj.id();
177  if (!obj.empty()) {
178  stream << ", parameters: ";
179  for (auto& param : obj.getParameters()) {
180  stream << '{' << param.first << ':' << ' ';
181  for (auto& rule : param.second) {
182  stream << GetIdVisitor::id(rule) << ' ';
183  }
184  stream << '}';
185  }
186  }
187  return stream << ']';
188 }
189 
190 template <>
192  return GetIdVisitor::id(prim);
193 }
194 
195 template <>
197  return GetIdVisitor::id(prim);
198 }
199 
200 } // namespace lanelet
Id id() const noexcept
get the unique id of this primitive
Definition: Primitive.h:96
template class for registering new RegulatoryElements.
static constexpr const char Outer[]
BasicPoint p
std::string role
applyVisitor will set the current role here
std::shared_ptr< RegulatoryElement > RegulatoryElementPtr
Definition: Forward.h:192
A hybrid map is just like a normal map with keys as string, but elements can also be accessed using a...
Definition: HybridMap.h:67
Id getId< RuleParameter >(const RuleParameter &prim)
Extracts the id of a rule parameter.
static constexpr const char CancelLine[]
static constexpr const char RefLine[]
int64_t Id
Definition: Forward.h:198
Id id_
std::pair< iterator, bool > insert(const value_type &v)
Definition: HybridMap.h:130
auto transform(Iterator begin, Iterator end, const Func f)
Definition: Utilities.h:176
std::ostream & operator<<(std::ostream &stream, const Attribute &obj)
Definition: Attribute.h:369
static RegulatoryElementFactory & instance()
static constexpr const char Left[]
static constexpr const char Yield[]
std::shared_ptr< RegulatoryElementData > RegulatoryElementDataPtr
Definition: Forward.h:184
static constexpr const char Right[]
std::map< std::string, FactoryFcn > registry_
bool has(const ConstArea &ll, Id id)
returns true if element of a regulatory element has a matching Id
Creates regulatory elements based on their type.
static RegulatoryElementPtr create(std::string ruleName, const RegulatoryElementDataPtr &data)
create a regulatory element based on the name of the rule
std::string role
applyVisitor will set the current role here
boost::variant< ConstPoint3d, ConstLineString3d, ConstPolygon3d, ConstWeakLanelet, ConstWeakArea > ConstRuleParameter
Const-version of the parameters.
bool empty() const
returns true if this object contains no parameters
static constexpr const char RegulatoryElement[]
static constexpr const char Inner[]
static constexpr const char Lanelet[]
static constexpr const char RightOfWay[]
bool found_
Thrown when a function was called with invalid input arguments.
Definition: Exceptions.h:56
ConstRuleParameter param_
Id id
Definition: LaneletMap.cpp:63
ConstRuleParameter toConst< RuleParameter >(const RuleParameter &primitive)
You can inherit from this visitor to perform an operation on each parameter of a regulatory element...
Id getId< ConstRuleParameter >(const ConstRuleParameter &prim)
A general rule or limitation for a lanelet (abstract base class)
static constexpr const char Subtype[]
Definition: Attribute.h:205
static RegisterRegulatoryElement< GenericRegulatoryElement > genRegelem
void applyVisitor(RuleParameterVisitor &visitor) const
applies a visitor to every parameter in the regulatory element
static constexpr const char Refers[]
ConstRuleParameterMap getParameters() const
Returns all parameters as const object (coversion overhead for const)
static std::vector< std::string > availableRules()
returns regulatory element names that this factory can handle
static constexpr RoleNamesItem Map[]
std::pair< const char *, const RoleName > RoleNamesItem
static constexpr const char Centerline[]
boost::variant< Point3d, LineString3d, Polygon3d, WeakLanelet, WeakArea > RuleParameter
static constexpr const char Cancels[]


lanelet2_core
Author(s): Fabian Poggenhans
autogenerated on Tue Jun 6 2023 02:23:32