geometry/impl/Lanelet.h
Go to the documentation of this file.
1 #pragma once
2 #include <boost/version.hpp>
3 #if BOOST_VERSION > 105800
4 #include <string.h> // NOLINT
5 
6 #include <boost/geometry/algorithms/relate.hpp>
7 #else
8 #include <boost/geometry/algorithms/detail/relate/relate.hpp>
9 #endif
13 
14 namespace lanelet {
15 namespace geometry {
16 namespace internal {
17 template <typename T>
18 struct GetGeometry<T, IfLL<T, void>> {
19  static inline auto twoD(const T& geometry) { return geometry.polygon2d().basicPolygon(); }
20  static inline auto threeD(const T& geometry) { return geometry.polygon3d().basicPolygon(); }
21 };
22 } // namespace internal
23 
24 template <typename LaneletT>
25 IfLL<LaneletT, bool> inside(const LaneletT& lanelet, const BasicPoint2d& point) {
26  return boost::geometry::covered_by(point, lanelet.polygon2d());
27 }
28 
29 template <typename LaneletT>
30 double distanceToCenterline2d(const LaneletT& lanelet, const BasicPoint2d& point) {
31  return distance(lanelet.centerline2d(), point);
32 }
33 
34 template <typename LaneletT>
35 double distanceToCenterline3d(const LaneletT& lanelet, const BasicPoint3d& point) {
36  return distance(lanelet.centerline3d(), point);
37 }
38 
39 template <typename LaneletT>
41  BoundingBox2d bb = boundingBox2d(lanelet.leftBound2d());
42  bb.extend(boundingBox2d(lanelet.rightBound2d()));
43  return bb;
44 }
45 
46 template <typename LaneletT>
48  BoundingBox3d bb = boundingBox3d(lanelet.leftBound3d());
49  bb.extend(boundingBox3d(lanelet.rightBound3d()));
50  return bb;
51 }
52 
53 template <typename Lanelet1T, typename Lanelet2T>
54 IfLL<Lanelet1T, bool> intersects2d(const Lanelet1T& lanelet, const Lanelet2T& otherLanelet) {
55  if (lanelet.constData() == otherLanelet.constData()) {
56  return true;
57  }
58  CompoundHybridPolygon2d p1(lanelet.polygon2d());
59  CompoundHybridPolygon2d p2(otherLanelet.polygon2d());
60  return intersects(p1, p2);
61 }
62 
63 template <typename Lanelet1T, typename Lanelet2T>
64 IfLL<Lanelet1T, bool> overlaps2d(const Lanelet1T& lanelet, const Lanelet2T& otherLanelet) {
65  if (lanelet.constData() == otherLanelet.constData()) {
66  return true;
67  }
68  // check if lanelets are neighbours (share a border)
69  if (lanelet.rightBound() == otherLanelet.leftBound() || lanelet.leftBound() == otherLanelet.rightBound() ||
70  lanelet.leftBound().invert() == otherLanelet.leftBound() ||
71  lanelet.rightBound().invert() == otherLanelet.rightBound() || follows(lanelet, otherLanelet) ||
72  follows(otherLanelet, lanelet) || follows(lanelet.invert(), otherLanelet) ||
73  follows(otherLanelet.invert(), lanelet)) {
74  return false;
75  }
76  if (!intersects(boundingBox2d(lanelet), boundingBox2d(otherLanelet))) {
77  return false;
78  }
79  CompoundHybridPolygon2d p1(lanelet.polygon2d());
80  CompoundHybridPolygon2d p2(otherLanelet.polygon2d());
81 
82 #if BOOST_VERSION > 105800
83  using Mask = boost::geometry::de9im::static_mask<'T', '*', '*', '*', '*', '*', '*', '*', '*'>;
84  return boost::geometry::relate(p1, p2, Mask());
85 #else
86  // boost 1.58 did not officially support "relate"
87  using Mask = boost::geometry::detail::relate::static_mask<'T', '*', '*', '*', '*', '*', '*', '*', '*'>;
88  return boost::geometry::detail::relate::relate<Mask>(p1, p2);
89 #endif
90 }
91 
92 template <typename Lanelet1T, typename Lanelet2T>
93 BasicPoints2d intersectCenterlines2d(const Lanelet1T& lanelet, const Lanelet2T& otherLanelet,
94  std::vector<double>* distanceThis, std::vector<double>* distanceOther) {
95  BasicPoints2d intersections;
96  const auto centerline = traits::toHybrid(lanelet.centerline2d());
97  const auto otherCenterline = traits::toHybrid(otherLanelet.centerline2d());
98  boost::geometry::intersection(centerline, otherCenterline, intersections);
99  if (distanceThis != nullptr) {
100  std::transform(intersections.begin(), intersections.end(), std::back_inserter(*distanceThis),
101  [&centerline](const auto& elem) { return toArcCoordinates(centerline, elem).length; });
102  }
103  if (distanceOther != nullptr) {
104  std::transform(intersections.begin(), intersections.end(), std::back_inserter(*distanceOther),
105  [&centerline](const auto& elem) { return toArcCoordinates(centerline, elem).length; });
106  }
107  return intersections;
108 }
109 
110 template <typename Lanelet1T, typename Lanelet2T>
111 IfLL<Lanelet1T, bool> intersects3d(const Lanelet1T& lanelet, const Lanelet2T& otherLanelet, double heightTolerance) {
112  if (intersects2d(lanelet, otherLanelet)) {
113  auto projPts = projectedPoint3d(lanelet.centerline3d(), otherLanelet.centerline3d());
114  return std::abs(projPts.first.z() - projPts.second.z()) < heightTolerance;
115  }
116  return false;
117 }
118 
119 template <typename Lanelet1T, typename Lanelet2T>
120 IfLL<Lanelet1T, bool> overlaps3d(const Lanelet1T& lanelet, const Lanelet2T& otherLanelet, double heightTolerance) {
121  if (overlaps2d(lanelet, otherLanelet)) {
122  auto projPts = projectedPoint3d(lanelet.centerline3d(), otherLanelet.centerline3d());
123  return std::abs(projPts.first.z() - projPts.second.z()) < heightTolerance;
124  }
125  return false;
126 }
127 
128 template <typename Lanelet1T, typename Lanelet2T>
129 IfLL<Lanelet1T, IfLL<Lanelet2T, bool>> leftOf(const Lanelet1T& left, const Lanelet2T& right) {
130  return left.rightBound() == right.leftBound();
131 }
132 
133 template <typename Lanelet1T, typename Lanelet2T>
134 IfLL<Lanelet1T, IfLL<Lanelet2T, bool>> rightOf(const Lanelet1T& right, const Lanelet2T& left) {
135  return leftOf(left, right);
136 }
137 
138 template <typename Lanelet1T, typename Lanelet2T>
139 IfLL<Lanelet1T, IfLL<Lanelet2T, bool>> follows(const Lanelet1T& prev, const Lanelet2T& next) {
140  return !prev.leftBound().empty() && !prev.rightBound().empty() && !next.leftBound().empty() &&
141  !next.rightBound().empty() && prev.leftBound().back() == next.leftBound().front() &&
142  prev.rightBound().back() == next.rightBound().front();
143 }
144 
145 template <typename Lanelet1T, typename Lanelet2T>
147  const Lanelet2T& other,
148  bool allowInverted) {
149  if (leftOf(other, ll)) {
150  return ll.leftBound();
151  }
152  if (rightOf(other, ll)) {
153  return ll.rightBound();
154  }
155  if (allowInverted) {
156  if (leftOf(other.invert(), ll)) {
157  return ll.leftBound();
158  }
159  if (rightOf(other.invert(), ll)) {
160  return ll.rightBound();
161  }
162  }
163  return {};
164 }
165 
166 template <typename LaneletT>
167 Velocity maxCurveSpeed(const LaneletT& /*lanelet*/, const BasicPoint2d& /*position*/,
168  const Acceleration& /*maxLateralAcceleration*/) {
170  throw LaneletError("This is not yet implemented!");
171 }
172 
173 template <typename LaneletT>
174 double approximatedLength2d(const LaneletT& lanelet) {
175  double length = 0.;
176  auto line = lanelet.leftBound2d();
177  auto step = std::max(size_t{1}, line.size() / 10);
178  for (auto i1 = size_t{}, i2 = step; i2 < line.size(); i1 += step, i2 += step) {
179  length += distance(line[i1], line[i2]);
180  if (i2 + step >= line.size()) {
181  length += distance(line[i2], line[line.size() - 1]);
182  }
183  }
184  return length;
185 }
186 
187 template <typename LaneletT>
188 double length2d(const LaneletT& lanelet) {
189  return double(length(lanelet.centerline2d()));
190 }
191 
192 template <typename LaneletT>
193 double length3d(const LaneletT& lanelet) {
194  return double(length(lanelet.centerline3d()));
195 }
196 
197 } // namespace geometry
198 } // namespace lanelet
lanelet::geometry::length2d
double length2d(const LaneletT &lanelet)
calculate length of centerline in 2d
Definition: geometry/impl/Lanelet.h:188
lanelet::geometry::intersects2d
IfAr< Area1T, bool > intersects2d(const Area1T &area, const Area2T &otherArea)
test whether two areas intersect in 2d.
Definition: geometry/impl/Area.h:35
lanelet::Velocity
units::MPSQuantity Velocity
Definition: Forward.h:210
lanelet::geometry::boundingBox3d
IfAr< AreaT, BoundingBox3d > boundingBox3d(const AreaT &area)
calculates 3d bounding box
Definition: geometry/impl/Area.h:30
lanelet::Acceleration
units::MPS2Quantity Acceleration
Definition: Forward.h:211
lanelet::geometry::follows
bool follows(const ConstLanelet &prev, const ConstArea &next)
Test whether area follows lanelet.
Definition: geometry/impl/Area.h:68
lanelet::geometry::length3d
double length3d(const LaneletT &lanelet)
calculate length of centerline in 3d
Definition: geometry/impl/Lanelet.h:193
lanelet
Definition: Attribute.h:13
lanelet::geometry::inside
IfAr< AreaT, bool > inside(const AreaT &area, const BasicPoint2d &point)
Checks whether a point is within or at the border of a area.
Definition: geometry/impl/Area.h:20
lanelet::IfLL
std::enable_if_t< traits::isLaneletT< T >(), RetT > IfLL
Definition: primitives/Lanelet.h:387
lanelet::BasicPoint3d
Eigen::Vector3d BasicPoint3d
a simple 3d-point
Definition: primitives/Point.h:19
lanelet::traits::toHybrid
constexpr auto toHybrid(const LineStringT ls)
Definition: primitives/LineString.h:765
p2
BasicPoint p2
Definition: LineStringGeometry.cpp:167
lanelet::geometry::intersects3d
IfLL< Lanelet1T, bool > intersects3d(const Lanelet1T &lanelet, const Lanelet2T &otherLanelet, double heightTolerance)
test whether two lanelets intersect in 2d.
Definition: geometry/impl/Lanelet.h:111
lanelet::geometry::internal::GetGeometry< T, IfLL< T, void > >::twoD
static auto twoD(const T &geometry)
Definition: geometry/impl/Lanelet.h:19
Polygon.h
lanelet::geometry::approximatedLength2d
double approximatedLength2d(const LaneletT &lanelet)
approximates length by sampling points along left bound
Definition: geometry/impl/Lanelet.h:174
lanelet::geometry::distanceToCenterline2d
double distanceToCenterline2d(const LaneletT &lanelet, const BasicPoint2d &point)
calculates distance in 2d to the centerline of a lanelet.
Definition: geometry/impl/Lanelet.h:30
lanelet::BoundingBox2d
Axis-Aligned bounding box in 2d.
Definition: primitives/BoundingBox.h:23
lanelet::geometry::distanceToCenterline3d
double distanceToCenterline3d(const LaneletT &lanelet, const BasicPoint3d &point)
calculates distance in 3d to centerline of a lanelet.
Definition: geometry/impl/Lanelet.h:35
lanelet::BoundingBox2d::extend
BoundingBox2d & extend(const Eigen::MatrixBase< Derived > &p)
Definition: primitives/BoundingBox.h:178
lanelet::utils::detail::transform
auto transform(Container &&c, Func f)
Definition: Utilities.h:120
lanelet::BoundingBox3d
Eigen::AlignedBox3d BoundingBox3d
Convenience type for an axis aligned bounding box in 3d.
Definition: primitives/BoundingBox.h:283
lanelet::geometry::determineCommonLine
Optional< ConstLineString3d > determineCommonLine(const ConstArea &ar, const ConstLanelet &ll)
Definition: geometry/impl/Area.h:128
lanelet::CompoundHybridPolygon2d
Combines multiple linestrings to one polygon in 2d that returns BasicPoint2d.
Definition: CompoundPolygon.h:71
p1
BasicPoint p1
Definition: LineStringGeometry.cpp:166
lanelet::geometry::intersectCenterlines2d
BasicPoints2d intersectCenterlines2d(const Lanelet1T &lanelet, const Lanelet2T &otherLanelet, std::vector< double > *distanceThis, std::vector< double > *distanceOther)
calculates points of intersection between centerlines in 2d.
Definition: geometry/impl/Lanelet.h:93
lanelet::geometry::internal::GetGeometry< T, IfLL< T, void > >::threeD
static auto threeD(const T &geometry)
Definition: geometry/impl/Lanelet.h:20
lanelet::geometry::overlaps2d
IfAr< AreaT, bool > overlaps2d(const AreaT &area, const AreaT &otherArea)
Definition: geometry/impl/Area.h:43
lanelet::BasicPoint2d
Eigen::Matrix< double, 2, 1, Eigen::DontAlign > BasicPoint2d
a simple 2d-point
Definition: primitives/Point.h:20
lanelet::geometry::overlaps3d
IfAr< AreaT, bool > overlaps3d(const AreaT &area, const AreaT &otherArea, double heightTolerance)
Definition: geometry/impl/Area.h:48
lanelet::geometry::boundingBox2d
IfAr< AreaT, BoundingBox2d > boundingBox2d(const AreaT &area)
calculates an up-right 2d bounding box
Definition: geometry/impl/Area.h:25
lanelet::geometry::leftOf
bool leftOf(const ConstLanelet &right, const ConstArea &left)
Test whether area is left of lanelet.
Definition: geometry/impl/Area.h:62
lanelet::geometry::maxCurveSpeed
Velocity maxCurveSpeed(const LaneletT &, const BasicPoint2d &, const Acceleration &)
calculates the maximum velocity without exceding a maximum lateral acceleration.
Definition: geometry/impl/Lanelet.h:167
Lanelet.h
lanelet::LaneletError
Generic lanelet error class. All errors lanelet2 will throw derive from this type.
Definition: Exceptions.h:11
LineString.h
distance
Optional< double > distance
Definition: LineStringGeometry.cpp:168
lanelet::geometry::rightOf
bool rightOf(const ConstLanelet &left, const ConstArea &area)
Test whether area is right of lanelet.
Definition: geometry/impl/Area.h:66
lanelet::BasicPoints2d
std::vector< Eigen::Vector2d, Eigen::aligned_allocator< Eigen::Vector2d > > BasicPoints2d
multiple simple 2d-points
Definition: primitives/Point.h:22
lanelet::geometry::projectedPoint3d
IfLS< LineString3dT, std::pair< BasicPoint3d, BasicPoint3d > > projectedPoint3d(const LineString3dT &l1, const LineString3dT &l2)
Computes the projected points on the two linestrings for the shortest distance.
Definition: geometry/impl/LineString.h:796


lanelet2_core
Author(s): Fabian Poggenhans
autogenerated on Thu Mar 6 2025 03:25:52