primitives/Area.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 <memory>
7 #include <utility>
8 
15 
16 namespace lanelet {
37 
38 using InnerBounds = std::vector<LineStrings3d>;
39 using ConstInnerBounds = std::vector<ConstLineStrings3d>;
40 
43 class AreaData : public PrimitiveData {
44  public:
46  AreaData(Id id, LineStrings3d outerBound, std::vector<LineStrings3d> innerBounds = std::vector<LineStrings3d>(),
48  : PrimitiveData(id, std::move(attributes)),
49  outerBound_{std::move(outerBound)},
50  innerBounds_{std::move(innerBounds)},
52  resetCache();
53  }
54 
56  return utils::transform(outerBound_, [](const auto& elem) { return ConstLineString3d(elem); });
57  }
59  return utils::transform(innerBounds_, [](const auto& elem) {
60  return utils::transform(elem, [](const auto& ls) { return ConstLineString3d(ls); });
61  });
62  }
63  const LineStrings3d& outerBound() { return outerBound_; }
64  const InnerBounds& innerBounds() { return innerBounds_; }
65 
67 
69 
71  return utils::transform(regulatoryElements_, [](const auto& elem) { return RegulatoryElementConstPtr(elem); });
72  }
74 
75  template <typename RegElemT>
76  std::vector<std::shared_ptr<const RegElemT>> regulatoryElementsAs() const {
77  return utils::transformSharedPtr<const RegElemT>(regulatoryElements_);
78  }
79  template <typename RegElemT>
80  std::vector<std::shared_ptr<RegElemT>> regulatoryElementsAs() {
81  return utils::transformSharedPtr<RegElemT>(regulatoryElements_);
82  }
83 
85  void setOuterBound(const LineStrings3d& bound) {
86  outerBound_ = bound;
87  resetCache();
88  }
89 
90  void setInnerBounds(const std::vector<LineStrings3d>& bounds) {
91  innerBounds_ = bounds;
92  resetCache();
93  }
94 
95  void resetCache() {
98  [](const auto& elem) { return CompoundPolygon3d(elem); });
99  }
100 
101  private:
103  std::vector<LineStrings3d> innerBounds_;
105 
106  // caches
109 };
110 
114 class ConstArea : public ConstPrimitive<AreaData> {
115  public:
117  using MutableType = Area;
121 
128  explicit ConstArea(Id id = InvalId) : ConstArea(std::make_shared<AreaData>(id, LineStrings3d())) {}
129 
141  ConstArea(Id id, const LineStrings3d& outerBound, const InnerBounds& innerBounds = InnerBounds(),
144  : ConstPrimitive{std::make_shared<AreaData>(id, outerBound, innerBounds, attributes, regulatoryElements)} {}
145 
147  explicit ConstArea(const std::shared_ptr<const AreaData>& data) : ConstPrimitive<AreaData>(data) {}
148 
150  ConstLineStrings3d outerBound() const { return constData()->outerBound(); }
151 
153  ConstInnerBounds innerBounds() const { return constData()->innerBounds(); }
154 
159  CompoundPolygon3d outerBoundPolygon() const { return constData()->outerBoundPolygon(); }
160 
162  CompoundPolygons3d innerBoundPolygons() const { return constData()->innerBoundPolygons(); }
163 
171  return {outerBoundPolygon().basicPolygon(),
172  utils::transform(innerBoundPolygons(), [](const auto& poly) { return poly.basicPolygon(); })};
173  }
174 
182  return {traits::to2D(outerBoundPolygon()).basicPolygon(),
183  utils::transform(innerBoundPolygons(), [](const auto& poly) { return traits::to2D(poly).basicPolygon(); })};
184  }
185 
187  RegulatoryElementConstPtrs regulatoryElements() const { return constData()->regulatoryElements(); }
188 
197  template <typename RegElemT>
198  std::vector<std::shared_ptr<const RegElemT>> regulatoryElementsAs() const {
199  return constData()->regulatoryElementsAs<RegElemT>();
200  }
201 };
202 
206 class Area : public Primitive<ConstArea> {
207  public:
208  using Primitive::Primitive;
209  using TwoDType = Area;
210  using ThreeDType = Area;
211  friend class ConstWeakArea;
212  Area() = default;
213  Area(const AreaDataConstPtr&) = delete;
214  explicit Area(const AreaDataPtr& data) : Primitive{data} {}
215 
216  using Primitive::innerBounds;
217  using Primitive::outerBound;
218  using Primitive::regulatoryElements;
219  using Primitive::regulatoryElementsAs;
220 
222  const LineStrings3d& outerBound() { return data()->outerBound(); }
223 
225  const InnerBounds& innerBounds() { return data()->innerBounds(); }
226 
228  void setOuterBound(const LineStrings3d& bound) { data()->setOuterBound(bound); }
229 
231  void setInnerBounds(const std::vector<LineStrings3d>& bounds) { data()->setInnerBounds(bounds); }
232 
234  const RegulatoryElementPtrs& regulatoryElements() { return data()->regulatoryElements(); }
235 
241  if (regElem == nullptr) {
242  throw NullptrError("regulatory element is a nullptr.");
243  }
244  data()->regulatoryElements().push_back(std::move(regElem));
245  }
246 
249  auto& regElems = data()->regulatoryElements();
250  auto remove = std::find(regElems.begin(), regElems.end(), regElem);
251  if (remove != regElems.end()) {
252  regElems.erase(remove);
253  return true;
254  }
255  return false;
256  }
257 
259  template <typename RegElemT>
260  std::vector<std::shared_ptr<RegElemT>> regulatoryElementsAs() {
261  return data()->regulatoryElementsAs<RegElemT>();
262  }
263 };
264 
272  public:
277  ConstWeakArea() = default;
278  ConstWeakArea(ConstArea area) // NOLINT
279  : areaData_(area.constData()) {}
280 
285  ConstArea lock() const { return ConstArea(areaData_.lock()); }
286 
288  bool expired() const noexcept { return areaData_.expired(); }
289 
290  protected:
291  std::weak_ptr<const AreaData> areaData_; // NOLINT
292 };
293 
300 class WeakArea final : public ConstWeakArea {
301  public:
302  WeakArea() = default;
303  WeakArea(Area llet) : ConstWeakArea(std::move(llet)) {} // NOLINT
304 
309  Area lock() const { return Area(std::const_pointer_cast<AreaData>(areaData_.lock())); }
310 };
311 
312 namespace utils {
324 inline bool has(const ConstArea& ll, Id id) { return has(ll.outerBoundPolygon(), id); }
325 } // namespace utils
326 
327 inline bool operator==(const ConstArea& lhs, const ConstArea& rhs) { return lhs.constData() == rhs.constData(); }
328 inline bool operator!=(const ConstArea& lhs, const ConstArea& rhs) { return !(lhs == rhs); }
329 inline bool operator==(const ConstWeakArea& lhs, const ConstWeakArea& rhs) {
330  return !lhs.expired() && !rhs.expired() && lhs.lock() == rhs.lock();
331 }
332 inline bool operator!=(const ConstWeakArea& lhs, const ConstWeakArea& rhs) { return !(lhs == rhs); }
333 inline std::ostream& operator<<(std::ostream& stream, const ConstArea& obj) {
334  stream << "[id: ";
335  stream << obj.id();
336  auto obId = obj.outerBoundPolygon().ids();
337  if (!obId.empty()) {
338  stream << " outer: [";
339  std::copy(obId.begin(), obId.end(), std::ostream_iterator<Id>(stream, ","));
340  stream << "]";
341  }
342  auto ibs = obj.innerBoundPolygons();
343  if (!ibs.empty()) {
344  stream << " inner: ";
345  for (const auto& ib : ibs) {
346  stream << "[";
347  auto ibId = ib.ids();
348  std::copy(ibId.begin(), ibId.end(), std::ostream_iterator<Id>(stream, ","));
349  stream << "]";
350  }
351  }
352  stream << "]";
353  return stream;
354 }
355 
356 namespace traits {
357 template <typename T>
358 constexpr bool isAreaT() {
359  return isCategory<T, traits::AreaTag>();
360 }
361 } // namespace traits
362 
363 template <typename T, typename RetT>
364 using IfAr = std::enable_if_t<traits::isAreaT<T>(), RetT>;
365 
366 } // namespace lanelet
367 
368 // Hash function for usage in containers
369 namespace std {
370 template <>
371 struct hash<lanelet::Area> : public lanelet::HashBase<lanelet::Area> {};
372 template <>
373 struct hash<lanelet::ConstArea> : public lanelet::HashBase<lanelet::ConstArea> {};
374 } // namespace std
ConstInnerBounds innerBounds() const
Id id() const noexcept
get the unique id of this primitive
Definition: Primitive.h:96
ConstInnerBounds innerBounds() const
get the linestrings that form the inner bounds
RegulatoryElementConstPtrs regulatoryElements() const
const InnerBounds & innerBounds()
Get the linestrings that form the inner bound.
std::shared_ptr< RegulatoryElement > RegulatoryElementPtr
Definition: Forward.h:192
bool expired() const noexcept
tests whether the WeakArea is still valid
const InnerBounds & innerBounds()
std::vector< ConstLineString3d > ConstLineStrings3d
Definition: Forward.h:58
CompoundPolygons3d innerBoundPolygons_
represents the inner bounds of the area
AttributeMap attributes
attributes of this primitive
Definition: Primitive.h:45
auto find(ContainerT &&c, const ValueT &val)
Definition: Utilities.h:186
std::vector< RegulatoryElementPtr > RegulatoryElementPtrs
Definition: Forward.h:193
A (basic) 2d polygon with holes insideThis class is thought for geometry calculations, it has no properties of a normal lanelet primitive.
const LineStrings3d & outerBound()
ConstArea(Id id, const LineStrings3d &outerBound, const InnerBounds &innerBounds=InnerBounds(), const AttributeMap &attributes=AttributeMap(), const RegulatoryElementPtrs &regulatoryElements=RegulatoryElementPtrs())
Constructs a new area.
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
std::vector< std::shared_ptr< const RegElemT > > regulatoryElementsAs() const
get the regulatoryElements that could be converted to a type
ConstWeakArea(ConstArea area)
HybridMap< Attribute, decltype(AttributeNamesString::Map)&, AttributeNamesString::Map > AttributeMap
Definition: Attribute.h:371
void setInnerBounds(const std::vector< LineStrings3d > &bounds)
sets a new inner bound and resets the cache
bool operator==(const Attribute &lhs, const Attribute &rhs)
Definition: Attribute.h:179
RegulatoryElementConstPtrs regulatoryElements() const
get a list of regulatory elements that affect this area
Identifies LaneletPrimitives.
Definition: Traits.h:11
bool removeRegulatoryElement(const RegulatoryElementPtr &regElem)
removes a regulatory element, returns true on success
BasicPolygonWithHoles2d basicPolygonWithHoles2d() const
generates a basic polygon in 2d with holes for this area
RegulatoryElementPtrs & regulatoryElements()
RegulatoryElementPtrs regulatoryElements_
regulatory elements that apply
std::vector< LineStrings3d > InnerBounds
const CompoundPolygons3d & innerBoundPolygons() const
ConstLineStrings3d outerBound() const
Ids ids() const
returns the ids of all linestrings in order
ConstLineStrings3d outerBound() const
Get linestrings that form the outer bound.
A normal 3d linestring with immutable data.
BasicPolygonWithHoles3d basicPolygonWithHoles3d() const
generates a basic polygon in 2d with holes for this area
used internally by RegulatoryElements to avoid cyclic dependencies.
bool has(const ConstArea &ll, Id id)
returns true if element of a regulatory element has a matching Id
const LineStrings3d & outerBound()
Get linestrings that form the outer bound.
std::vector< LineStrings3d > innerBounds_
vector of ls for inner bounds
Area(const AreaDataPtr &data)
void setOuterBound(const LineStrings3d &bound)
sets a new outer bound.
std::vector< std::shared_ptr< RegElemT > > regulatoryElementsAs()
get the regulatoryElements that could be converted to RegElemT
void setInnerBounds(const std::vector< LineStrings3d > &bounds)
Combines multiple linestrings to one polygon in 3d.
BasicPolygon3d basicPolygon() const
std::shared_ptr< const AreaData > AreaDataConstPtr
Definition: Forward.h:132
const std::shared_ptr< const Data > & constData() const
get the internal data of this primitive
Definition: Primitive.h:178
void setOuterBound(const LineStrings3d &bound)
Sets a new outer bound and resets the cache.
CompoundPolygon3d outerBoundPolygon_
represents the outer bounds of the area
bool operator!=(const Attribute &lhs, const Attribute &rhs)
Definition: Attribute.h:180
CompoundPolygon3d outerBoundPolygon() const
get the outer bound as polygon
A (basic) 2d polygon with holes insideThis class is thought for geometry calculations, it has no properties of a normal lanelet primitive.
Area lock() const
Obtains the original ConstArea.
SharedPtrs in lanelet2 must never point to null. If this is violated, this exception is thrown (usual...
Definition: Exceptions.h:80
LineStrings3d outerBound_
linestrings that together form the outer bound
std::shared_ptr< const RegulatoryElement > RegulatoryElementConstPtr
Definition: Forward.h:194
Basic Primitive class for all primitives of lanelet2.
Definition: Primitive.h:70
Base class for all mutable Primitives of lanelet2.
Definition: Primitive.h:243
std::vector< std::shared_ptr< const RegElemT > > regulatoryElementsAs() const
const T & addConst(T &t)
Definition: Utilities.h:149
AreaData(Id id, LineStrings3d outerBound, std::vector< LineStrings3d > innerBounds=std::vector< LineStrings3d >(), AttributeMap attributes=AttributeMap(), RegulatoryElementPtrs regulatoryElements=RegulatoryElementPtrs())
Constructs a new, AreaData object.
Common data class for all lanelet primitivesThis class provides the data that all lanelet primitives ...
Definition: Primitive.h:31
CompoundPolygons3d innerBoundPolygons() const
get the inner bounds as polygon
const CompoundPolygon3d & outerBoundPolygon() const
Famous Area class that represents a basic area as element of the map.
constexpr bool isAreaT()
std::weak_ptr< const AreaData > areaData_
std::vector< RegulatoryElementConstPtr > RegulatoryElementConstPtrs
Definition: Forward.h:195
Common data management class for all Area-Typed objects.
ConstArea lock() const
Obtains the original ConstArea.
std::shared_ptr< AreaData > AreaDataPtr
Definition: Forward.h:130
std::vector< std::shared_ptr< RegElemT > > regulatoryElementsAs()
const RegulatoryElementPtrs & regulatoryElements()
return regulatoryElements that affect this area.
Id id
Id of this primitive (unique across one map)
Definition: Primitive.h:44
A const (i.e. immutable) Area.
BoundingBox2d to2D(const BoundingBox3d &primitive)
std::vector< ConstLineStrings3d > ConstInnerBounds
std::enable_if_t< traits::isAreaT< T >(), RetT > IfAr
std::vector< CompoundPolygon3d > CompoundPolygons3d
Definition: Forward.h:86
ConstArea(const std::shared_ptr< const AreaData > &data)
Constructor to construct from the data of a different Area.
ConstArea(Id id=InvalId)
Constructs an empty or invalid area.
void addRegulatoryElement(RegulatoryElementPtr regElem)
adds a new regulatory element
std::vector< LineString3d > LineStrings3d
Definition: Forward.h:57
constexpr Id InvalId
indicates a primitive that is not part of a map
Definition: Forward.h:199
used internally by RegulatoryElements to avoid cyclic dependencies.


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