primitives/Lanelet.h
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; c-basic-offset: 2;
2 // indent-tabs-mode: nil -*-
3 
4 #pragma once
5 
6 #include <functional>
7 #include <memory>
8 #include <utility>
9 
10 #include "lanelet2_core/Forward.h"
14 
15 namespace lanelet {
17 
22 class LaneletData : public PrimitiveData {
23  public:
28  LaneletData(Id id, LineString3d leftBound, LineString3d rightBound, const AttributeMap& attributes = AttributeMap(),
29  RegulatoryElementPtrs regulatoryElements = RegulatoryElementPtrs())
30  : PrimitiveData(id, attributes),
31  leftBound_{std::move(leftBound)},
32  rightBound_{std::move(rightBound)},
33  regulatoryElements_{std::move(regulatoryElements)} {}
34 
35  const ConstLineString3d& leftBound() const { return leftBound_; }
36  const LineString3d& leftBound() { return leftBound_; }
37 
38  const ConstLineString3d& rightBound() const { return rightBound_; }
39  const LineString3d& rightBound() { return rightBound_; }
40 
42  return utils::transform(regulatoryElements_, [](const auto& elem) { return RegulatoryElementConstPtr(elem); });
43  }
44  RegulatoryElementPtrs& regulatoryElements() { return regulatoryElements_; }
45 
46  template <typename RegElemT>
47  std::vector<std::shared_ptr<const RegElemT>> regulatoryElementsAs() const {
48  return utils::transformSharedPtr<const RegElemT>(regulatoryElements_);
49  }
50  template <typename RegElemT>
51  std::vector<std::shared_ptr<RegElemT>> regulatoryElementsAs() {
52  return utils::transformSharedPtr<RegElemT>(regulatoryElements_);
53  }
54 
56  void setLeftBound(const LineString3d& bound);
57 
59  void setRightBound(const LineString3d& bound);
60 
62  void setCenterline(const LineString3d& centerline);
63 
65  bool hasCustomCenterline() const;
66 
68  void resetCache() const;
69 
71  ConstLineString3d centerline() const;
72 
74  CompoundPolygon3d polygon() const;
75 
76  private:
80 
81  // Cached data
82  mutable std::shared_ptr<ConstLineString3d> centerline_;
83 };
84 
128 class ConstLanelet : public ConstPrimitive<LaneletData> {
132  public:
138 
140  explicit ConstLanelet(Id id = InvalId)
141  : ConstLanelet(std::make_shared<LaneletData>(id, LineString3d(), LineString3d()), false) {}
142 
144  ConstLanelet(Id id, const LineString3d& leftBound, const LineString3d& rightBound,
145  const AttributeMap& attributes = AttributeMap(),
146  const RegulatoryElementPtrs& regulatoryElements = RegulatoryElementPtrs())
147  : ConstPrimitive{std::make_shared<LaneletData>(id, leftBound, rightBound, attributes, regulatoryElements)} {}
148 
150  explicit ConstLanelet(const std::shared_ptr<const LaneletData>& data, bool inverted = false)
151  : ConstPrimitive<lanelet::LaneletData>(data), inverted_{inverted} {}
152 
154  bool inverted() const { return inverted_; }
155 
157  ConstLanelet invert() const { return ConstLanelet{constData(), !inverted()}; }
158 
160  ConstLineString3d leftBound() const { return leftBound3d(); }
162  ConstLineString2d leftBound2d() const { return utils::to2D(leftBound3d()); }
165  return inverted() ? constData()->rightBound().invert() : constData()->leftBound();
166  }
167 
169  ConstLineString3d rightBound() const { return rightBound3d(); }
171  ConstLineString2d rightBound2d() const { return utils::to2D(rightBound3d()); }
174  return inverted() ? constData()->leftBound().invert() : constData()->rightBound();
175  }
176 
178  RegulatoryElementConstPtrs regulatoryElements() const { return constData()->regulatoryElements(); }
179 
181  template <typename RegElemT>
182  std::vector<std::shared_ptr<const RegElemT>> regulatoryElementsAs() const {
183  return constData()->regulatoryElementsAs<RegElemT>();
184  }
185 
187  ConstLineString3d centerline() const { return centerline3d(); }
188 
190  ConstLineString2d centerline2d() const { return utils::to2D(centerline3d()); }
191 
194  return inverted() ? constData()->centerline().invert() : constData()->centerline();
195  }
196 
198  bool hasCustomCenterline() const { return constData()->hasCustomCenterline(); }
199 
201  CompoundPolygon3d polygon3d() const;
202 
204  CompoundPolygon2d polygon2d() const;
205 
212  void resetCache() const { constData()->resetCache(); }
213 
214  private:
215  bool inverted_{false};
216 };
217 
221 class Lanelet : public Primitive<ConstLanelet> {
222  public:
223  using Primitive::Primitive;
224  using TwoDType = Lanelet;
226  friend class ConstWeakLanelet;
227  Lanelet() = default;
228  Lanelet(const std::shared_ptr<const LaneletData>&, bool inverted) = delete;
229  Lanelet(const LaneletDataPtr& data, bool inverted) : Primitive{data, inverted} {}
230 
232  Lanelet invert() const { return Lanelet{data(), !inverted()}; }
233 
234  using Primitive::leftBound;
235  using Primitive::leftBound2d;
236  using Primitive::leftBound3d;
238  LineString3d leftBound() { return leftBound3d(); }
239 
241  LineString2d leftBound2d() { return utils::to2D(leftBound3d()); }
242 
244  LineString3d leftBound3d() { return inverted() ? data()->rightBound().invert() : data()->leftBound(); }
245 
246  using Primitive::rightBound;
247  using Primitive::rightBound2d;
248  using Primitive::rightBound3d;
250  LineString3d rightBound() { return rightBound3d(); }
251 
253  LineString2d rightBound2d() { return utils::to2D(rightBound3d()); }
254 
256  LineString3d rightBound3d() { return inverted() ? data()->leftBound().invert() : data()->rightBound(); }
257 
259  ConstLineString3d rightBound() const { return rightBound3d(); }
260 
262  void setLeftBound(const LineString3d& bound) {
263  inverted() ? data()->setRightBound(bound.invert()) : data()->setLeftBound(bound);
264  }
265 
267  void setRightBound(const LineString3d& bound) {
268  inverted() ? data()->setLeftBound(bound.invert()) : data()->setRightBound(bound);
269  }
270 
278  void setCenterline(const LineString3d& centerline) {
279  inverted() ? data()->setCenterline(centerline.invert()) : data()->setCenterline(centerline);
280  }
281 
283  const RegulatoryElementPtrs& regulatoryElements() { return data()->regulatoryElements(); }
284 
287  if (regElem == nullptr) {
288  throw NullptrError("regulatory element is a nullptr.");
289  }
290  data()->regulatoryElements().push_back(std::move(regElem));
291  }
292 
295  auto& regElems = data()->regulatoryElements();
296  auto remove = std::find(regElems.begin(), regElems.end(), regElem);
297  if (remove != regElems.end()) {
298  regElems.erase(remove);
299  return true;
300  }
301  return false;
302  }
303 
304  using Primitive::regulatoryElements;
305  using Primitive::regulatoryElementsAs;
306 
308  template <typename RegElemT>
309  std::vector<std::shared_ptr<RegElemT>> regulatoryElementsAs() {
310  return data()->regulatoryElementsAs<RegElemT>();
311  }
312 };
313 
317  public:
322  ConstWeakLanelet() = default;
323  ConstWeakLanelet(const ConstLanelet& llet) // NOLINT
324  : laneletData_{llet.constData()}, inverted_{llet.inverted()} {}
325 
330  ConstLanelet lock() const { return ConstLanelet(laneletData_.lock(), inverted_); }
331 
333  bool expired() const noexcept { return laneletData_.expired(); }
334 
335  protected:
336  std::weak_ptr<const LaneletData> laneletData_; // NOLINT
337  bool inverted_{false}; // NOLINT
338 };
339 
342 class WeakLanelet final : public ConstWeakLanelet {
343  public:
344  WeakLanelet() = default;
345  WeakLanelet(const Lanelet& llet) : ConstWeakLanelet(llet) {} // NOLINT
346 
351  Lanelet lock() const { return Lanelet(std::const_pointer_cast<LaneletData>(laneletData_.lock()), inverted_); }
352 };
353 
354 namespace utils {
366 bool has(const ConstLanelet& ll, Id id);
367 } // namespace utils
368 
369 inline bool operator==(const ConstLanelet& lhs, const ConstLanelet& rhs) {
370  return lhs.constData() == rhs.constData() && lhs.inverted() == rhs.inverted();
371 }
372 inline bool operator!=(const ConstLanelet& lhs, const ConstLanelet& rhs) { return !(lhs == rhs); }
373 inline bool operator==(const ConstWeakLanelet& lhs, const ConstWeakLanelet& rhs) {
374  return !lhs.expired() && !rhs.expired() && lhs.lock() == rhs.lock();
375 }
376 inline bool operator!=(const ConstWeakLanelet& lhs, const ConstWeakLanelet& rhs) { return !(lhs == rhs); }
377 std::ostream& operator<<(std::ostream& stream, const ConstLanelet& obj);
378 
379 namespace traits {
380 template <typename T>
381 constexpr bool isLaneletT() {
382  return isCategory<T, traits::LaneletTag>();
383 }
384 } // namespace traits
385 
386 template <typename T, typename RetT>
387 using IfLL = std::enable_if_t<traits::isLaneletT<T>(), RetT>;
388 
389 } // namespace lanelet
390 
391 // Hash function for usage in containers
392 namespace std {
393 template <>
394 struct hash<lanelet::Lanelet> : public lanelet::HashBase<lanelet::Lanelet> {};
395 template <>
396 struct hash<lanelet::ConstLanelet> : public lanelet::HashBase<lanelet::ConstLanelet> {};
397 
398 template <typename T, typename U>
399 struct hash<std::pair<U, T>> {
400  size_t operator()(const std::pair<U, T> x) const noexcept {
401  return std::hash<U>()(x.first) ^ std::hash<T>()(x.second);
402  }
403 };
404 
405 } // namespace std
std::shared_ptr< ConstLineString3d > centerline_
const RegulatoryElementPtrs & regulatoryElements()
Return regulatoryElements that affect this lanelet.
ConstLineString2d centerline2d() const
get the (cached) centerline of this lanelet in 2d. To be used in context of geometric calculations...
std::shared_ptr< RegulatoryElement > RegulatoryElementPtr
Definition: Forward.h:192
ConstLanelet(Id id=InvalId)
Constructs an empty or invalid lanelet.
std::enable_if_t< traits::isLaneletT< T >(), RetT > IfLL
auto find(ContainerT &&c, const ValueT &val)
Definition: Utilities.h:186
size_t operator()(const std::pair< U, T > x) const noexcept
std::vector< RegulatoryElementPtr > RegulatoryElementPtrs
Definition: Forward.h:193
bool has(const ConstLanelet &ll, Id id)
returns true if element of a regulatory element has a matching Id
Definition: Lanelet.cpp:307
LineString3d rightBound_
represents the right bound
ConstLineString3d rightBound3d() const
get the right bound in 3d. To be used over rightBound where geometric calculations are required...
void addRegulatoryElement(RegulatoryElementPtr regElem)
Add a new regulatory element.
bool removeRegulatoryElement(const RegulatoryElementPtr &regElem)
Removes a regulatory element, returns true on success.
RegulatoryElementConstPtrs regulatoryElements() const
get a list of regulatory elements that affect this lanelet
ConstLineString2d rightBound2d() const
get the right bound in 2d. To be used over rightBound where geometric calculations are required...
ConstLineString3d invert() const noexcept
create a new, inverted linestring from this one
int64_t Id
Definition: Forward.h:198
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
Lanelet(const LaneletDataPtr &data, bool inverted)
std::vector< std::shared_ptr< RegElemT > > regulatoryElementsAs()
Get the regulatoryElements that could be converted to RegElemT.
HybridMap< Attribute, decltype(AttributeNamesString::Map)&, AttributeNamesString::Map > AttributeMap
Definition: Attribute.h:371
The famous (mutable) lanelet class.
bool operator==(const Attribute &lhs, const Attribute &rhs)
Definition: Attribute.h:179
Common data management class for all Lanelet-Typed objects.
std::weak_ptr< const LaneletData > laneletData_
void resetCache() const
resets the internal cache of the centerline
void setCenterline(const LineString3d &centerline)
Identifies PolygonPrimitives.
Definition: Traits.h:10
std::vector< std::shared_ptr< const RegElemT > > regulatoryElementsAs() const
get the regulatoryElements that could be converted to RegElemT
LineString2d rightBound2d()
get the right bound in 2d. To be used over rightBound where geometric calculations are required...
A normal 3d linestring with immutable data.
ConstLineString3d centerline3d() const
get the (cached) centerline of this lanelet in 3d. To be used in context of geometric calculations...
LaneletData(Id id, LineString3d leftBound, LineString3d rightBound, const AttributeMap &attributes=AttributeMap(), RegulatoryElementPtrs regulatoryElements=RegulatoryElementPtrs())
Constructs a new, valid LaneletData object.
bool expired() const noexcept
tests whether the WeakLanelet is still valid
ConstLanelet(const std::shared_ptr< const LaneletData > &data, bool inverted=false)
Construct from the data of a different Lanelet.
Combines multiple linestrings to one polygon in 3d.
ConstWeakLanelet(const ConstLanelet &llet)
ConstLineString2d leftBound2d() const
get the left bound in 2d. To be used over leftBound where geometric calculations are required...
bool inverted() const
returns if this is an inverted lanelet
RegulatoryElementPtrs & regulatoryElements()
WeakLanelet(const Lanelet &llet)
void setRightBound(const LineString3d &bound)
Sets a new right bound and resets the cached centerline.
void setLeftBound(const LineString3d &bound)
Sets a new left bound and resets the cached centerline.
A normal 3d linestring with mutable data.
const std::shared_ptr< const Data > & constData() const
get the internal data of this primitive
Definition: Primitive.h:178
const ConstLineString3d & rightBound() const
ConstLineString3d centerline() const
get the (cached) centerline of this lanelet.
bool operator!=(const Attribute &lhs, const Attribute &rhs)
Definition: Attribute.h:180
const LineString3d & leftBound()
ConstLanelet(Id id, const LineString3d &leftBound, const LineString3d &rightBound, const AttributeMap &attributes=AttributeMap(), const RegulatoryElementPtrs &regulatoryElements=RegulatoryElementPtrs())
Constructs a lanelet from id, attributes, regulatoryElements and bounds.
LineString2d leftBound2d()
get the left bound in 2d. To be used over leftBound where geometric calculations are required...
LineString3d leftBound_
represents the left bound
ConstLineString3d leftBound() const
get the left bound.
SharedPtrs in lanelet2 must never point to null. If this is violated, this exception is thrown (usual...
Definition: Exceptions.h:80
std::shared_ptr< const RegulatoryElement > RegulatoryElementConstPtr
Definition: Forward.h:194
An immutable lanelet.
Basic Primitive class for all primitives of lanelet2.
Definition: Primitive.h:70
std::vector< std::shared_ptr< const RegElemT > > regulatoryElementsAs() const
Base class for all mutable Primitives of lanelet2.
Definition: Primitive.h:243
LineString3d leftBound()
Get the left bound.
RegulatoryElementPtrs regulatoryElements_
regulatory elements
LineString3d rightBound()
Get the right bound.
ConstLineString3d rightBound() const
get the right bound.
Common data class for all lanelet primitivesThis class provides the data that all lanelet primitives ...
Definition: Primitive.h:31
bool hasCustomCenterline() const
Returns whether the centerline has been overridden by setCenterline.
const ConstLineString3d & leftBound() const
std::shared_ptr< LaneletData > LaneletDataPtr
Definition: Forward.h:100
std::vector< RegulatoryElementConstPtr > RegulatoryElementConstPtrs
Definition: Forward.h:195
Id id
Definition: LaneletMap.cpp:63
ConstLanelet invert() const
returns the inverted lanelet
LineString3d invert() const noexcept
create a new, inverted linestring from this one
Lanelet invert() const
non-const version to invert a lanelet
Lanelet lock() const
Obtains the original Lanelet.
BoundingBox2d to2D(const BoundingBox3d &primitive)
LineString3d leftBound3d()
get the left bound in 3d. To be used over leftBound where geometric calculations are required...
RegulatoryElementConstPtrs regulatoryElements() const
Combines multiple linestrings to one polygon in 2d.
constexpr bool isLaneletT()
A normal 2d linestring with immutable data.
std::vector< std::shared_ptr< RegElemT > > regulatoryElementsAs()
A normal 2d linestring with mutable data.
ConstLineString3d rightBound() const
get the right bound.
ConstLineString3d leftBound3d() const
get the left bound in 3d. To be used over leftBound where geometric calculations are required...
ConstLanelet lock() const
Obtains the original ConstLanelet.
const LineString3d & rightBound()
constexpr Id InvalId
indicates a primitive that is not part of a map
Definition: Forward.h:199
LineString3d rightBound3d()
get the right bound in 3d. To be used over rightBound where geometric calculations are required...


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