primitives/BoundingBox.h
Go to the documentation of this file.
1 #pragma once
2 
3 #pragma GCC diagnostic push
4 #if defined __GNUC__ && (__GNUC__ >= 6)
5 #pragma GCC diagnostic ignored "-Wignored-attributes"
6 #pragma GCC diagnostic ignored "-Wint-in-bool-context"
7 #endif
8 #include <Eigen/Geometry>
9 
12 #pragma GCC diagnostic pop
13 
14 namespace lanelet {
15 
24  public:
26  using Scalar = double;
27  using ScalarTraits = Eigen::NumTraits<Scalar>;
28  using Index = Eigen::Index;
29  using RealScalar = ScalarTraits::Real;
30  using NonInteger = ScalarTraits::NonInteger;
32 
34  enum CornerType {
36  Min = 0,
37  Max = 1,
43  TopLeft = 2,
44  TopRight = 3,
57  };
58 
60  inline BoundingBox2d() {
61  if (AmbientDimAtCompileTime != Eigen::Dynamic) {
62  setEmpty();
63  }
64  }
65 
67  inline explicit BoundingBox2d(Index dim) : min_(dim), max_(dim) { setEmpty(); }
68 
72  template <typename OtherVectorType1, typename OtherVectorType2>
73  inline BoundingBox2d(OtherVectorType1 min, OtherVectorType2 max) : min_(std::move(min)), max_(std::move(max)) {}
74 
76  template <typename Derived>
77  inline explicit BoundingBox2d(const Eigen::MatrixBase<Derived>& p) : min_(p), max_(min_) {}
78 
79  inline BoundingBox2d(const Eigen::AlignedBox<double, 2>& other) : min_(other.min()), max_(other.max()) {} // NOLINT
80 
82  inline Index dim() const {
83  return AmbientDimAtCompileTime == Eigen::Dynamic ? min_.size() : Index(AmbientDimAtCompileTime);
84  }
85 
87  inline bool isNull() const { return isEmpty(); }
88 
90  inline void setNull() { setEmpty(); }
91 
94  inline bool isEmpty() const { return (min_.array() > max_.array()).any(); }
95 
98  inline void setEmpty() {
99  min_.setConstant(ScalarTraits::highest());
100  max_.setConstant(ScalarTraits::lowest());
101  }
102 
104  inline const VectorType&(min)() const { return min_; }
106  inline VectorType&(min)() { return min_; }
108  inline const VectorType&(max)() const { return max_; }
110  inline VectorType&(max)() { return max_; }
111 
113  inline BasicPoint2d center() const { return (min_ + max_) / 2; }
114 
119  inline BasicPoint2d sizes() const { return max_ - min_; }
120 
122  inline Scalar volume() const { return sizes().prod(); }
123 
128  inline BasicPoint2d diagonal() const { return sizes(); }
129 
140  VectorType res;
141 
142  Index mult = 1;
143  for (Index d = 0; d < dim(); ++d) {
144  if ((mult & corner) != 0) {
145  res[d] = max_[d];
146  } else {
147  res[d] = min_[d];
148  }
149  mult *= 2;
150  }
151  return res;
152  }
153 
156  inline VectorType sample() const { return Eigen::AlignedBox<double, 2>(min_, max_).sample(); }
157 
159  template <typename Derived>
160  inline bool contains(const Eigen::MatrixBase<Derived>& p) const {
161  return Eigen::AlignedBox<double, 2>(min_, max_).contains(p);
162  }
163 
165  inline bool contains(const BoundingBox2d& b) const {
166  return (min_.array() <= (b.min)().array()).all() && ((b.max)().array() <= max_.array()).all();
167  }
168 
171  inline bool intersects(const BoundingBox2d& b) const {
172  return (min_.array() <= (b.max)().array()).all() && ((b.min)().array() <= max_.array()).all();
173  }
174 
177  template <typename Derived>
178  inline BoundingBox2d& extend(const Eigen::MatrixBase<Derived>& p) {
179  auto pN(p.derived().eval());
180  min_ = min_.cwiseMin(pN);
181  max_ = max_.cwiseMax(pN);
182  return *this;
183  }
184 
187  inline BoundingBox2d& extend(const BoundingBox2d& b) {
188  min_ = min_.cwiseMin(b.min_);
189  max_ = max_.cwiseMax(b.max_);
190  return *this;
191  }
192 
196  inline BoundingBox2d& clamp(const BoundingBox2d& b) {
197  min_ = min_.cwiseMax(b.min_);
198  max_ = max_.cwiseMin(b.max_);
199  return *this;
200  }
201 
205  inline BoundingBox2d intersection(const BoundingBox2d& b) const {
206  return {min_.cwiseMax(b.min_), max_.cwiseMin(b.max_)};
207  }
208 
212  inline BoundingBox2d merged(const BoundingBox2d& b) const { return {min_.cwiseMin(b.min_), max_.cwiseMax(b.max_)}; }
213 
215  template <typename Derived>
216  inline BoundingBox2d& translate(const Eigen::MatrixBase<Derived>& aT) {
217  const auto t(aT.derived().eval());
218  min_ += t;
219  max_ += t;
220  return *this;
221  }
222 
227  template <typename Derived>
228  inline Scalar squaredExteriorDistance(const Eigen::MatrixBase<Derived>& p) const {
229  return Eigen::AlignedBox<double, 2>(min_, max_).squaredExteriorDistance(p);
230  }
231 
237  using EigenBox = Eigen::AlignedBox<double, 2>;
238  return EigenBox(min_, max_).squaredExteriorDistance(EigenBox(b.min_, b.max_));
239  }
240 
245  template <typename Derived>
246  inline NonInteger exteriorDistance(const Eigen::MatrixBase<Derived>& p) const {
247  return std::sqrt(NonInteger(squaredExteriorDistance(p)));
248  }
249 
254  inline NonInteger exteriorDistance(const BoundingBox2d& b) const {
255  using std::sqrt;
256  return sqrt(NonInteger(squaredExteriorDistance(b)));
257  }
258 
260  template <typename OtherScalarType>
261  inline explicit BoundingBox2d(const Eigen::AlignedBox<OtherScalarType, AmbientDimAtCompileTime>& other) {
262  min_ = (other.min)().template cast<Scalar>();
263  max_ = (other.max)().template cast<Scalar>();
264  }
265 
270  bool isApprox(const BoundingBox2d& other, const RealScalar& prec = ScalarTraits::dummy_precision()) const {
271  return min_.isApprox(other.min_, prec) && max_.isApprox(other.max_, prec);
272  }
273 
274  private:
276 };
277 
283 using BoundingBox3d = Eigen::AlignedBox3d;
284 
285 namespace traits {
286 template <>
293 };
294 template <>
301 };
302 
303 inline BoundingBox3d to3D(const BoundingBox2d& primitive) { return {to3D(primitive.min()), to3D(primitive.max())}; }
304 inline BoundingBox2d to2D(const BoundingBox3d& primitive) { return {to2D(primitive.min()), to2D(primitive.max())}; }
305 } // namespace traits
306 } // namespace lanelet
lanelet::BoundingBox2d::exteriorDistance
NonInteger exteriorDistance(const Eigen::MatrixBase< Derived > &p) const
Definition: primitives/BoundingBox.h:246
lanelet::BoundingBox2d::Scalar
double Scalar
Definition: primitives/BoundingBox.h:26
lanelet::BoundingBox2d::NonInteger
ScalarTraits::NonInteger NonInteger
Definition: primitives/BoundingBox.h:30
lanelet::BoundingBox2d::BottomLeftFloor
@ BottomLeftFloor
Definition: primitives/BoundingBox.h:48
lanelet::BoundingBox2d::TopLeft
@ TopLeft
Definition: primitives/BoundingBox.h:43
lanelet::traits::to2D
BoundingBox2d to2D(const BoundingBox3d &primitive)
Definition: primitives/BoundingBox.h:304
lanelet
Definition: Attribute.h:13
Point.h
lanelet::BoundingBox2d::Min
@ Min
Definition: primitives/BoundingBox.h:36
lanelet::BoundingBox2d::Index
Eigen::Index Index
Definition: primitives/BoundingBox.h:28
lanelet::traits::BoundingBoxTag
Identifies PointPrimitives.
Definition: Traits.h:7
lanelet::BoundingBox2d::CornerType
CornerType
Definition: primitives/BoundingBox.h:34
lanelet::BoundingBox2d::diagonal
BasicPoint2d diagonal() const
Definition: primitives/BoundingBox.h:128
p
BasicPoint p
Definition: LineStringGeometry.cpp:196
lanelet::BoundingBox2d::isEmpty
bool isEmpty() const
Definition: primitives/BoundingBox.h:94
lanelet::BoundingBox2d::BottomLeftCeil
@ BottomLeftCeil
Definition: primitives/BoundingBox.h:52
lanelet::BoundingBox2d::Max
@ Max
Definition: primitives/BoundingBox.h:37
lanelet::BoundingBox2d::center
BasicPoint2d center() const
Definition: primitives/BoundingBox.h:113
lanelet::BoundingBox2d::RealScalar
ScalarTraits::Real RealScalar
Definition: primitives/BoundingBox.h:29
lanelet::BoundingBox2d::BoundingBox2d
BoundingBox2d(OtherVectorType1 min, OtherVectorType2 max)
Definition: primitives/BoundingBox.h:73
lanelet::BoundingBox2d::setNull
void setNull()
Definition: primitives/BoundingBox.h:90
lanelet::BoundingBox2d::TopRightFloor
@ TopRightFloor
Definition: primitives/BoundingBox.h:51
lanelet::BoundingBox2d::BoundingBox2d
BoundingBox2d(Index dim)
Definition: primitives/BoundingBox.h:67
lanelet::BoundingBox2d::min_
VectorType min_
Definition: primitives/BoundingBox.h:275
lanelet::traits::PrimitiveTraits< BoundingBox2d >::ThreeDType
BoundingBox3d ThreeDType
Definition: primitives/BoundingBox.h:291
lanelet::BoundingBox2d::TopRight
@ TopRight
Definition: primitives/BoundingBox.h:44
lanelet::BoundingBox2d::extend
BoundingBox2d & extend(const BoundingBox2d &b)
Definition: primitives/BoundingBox.h:187
lanelet::BoundingBox2d::VectorType
BasicPoint2d VectorType
Definition: primitives/BoundingBox.h:31
lanelet::BoundingBox2d::squaredExteriorDistance
Scalar squaredExteriorDistance(const BoundingBox2d &b) const
Definition: primitives/BoundingBox.h:236
lanelet::BoundingBox2d::BoundingBox2d
BoundingBox2d(const Eigen::MatrixBase< Derived > &p)
Definition: primitives/BoundingBox.h:77
lanelet::BoundingBox2d::BoundingBox2d
BoundingBox2d()
Definition: primitives/BoundingBox.h:60
lanelet::BoundingBox2d::corner
VectorType corner(CornerType corner) const
Definition: primitives/BoundingBox.h:139
lanelet::BoundingBox2d
Axis-Aligned bounding box in 2d.
Definition: primitives/BoundingBox.h:23
lanelet::BoundingBox2d::extend
BoundingBox2d & extend(const Eigen::MatrixBase< Derived > &p)
Definition: primitives/BoundingBox.h:178
lanelet::BoundingBox2d::contains
bool contains(const BoundingBox2d &b) const
Definition: primitives/BoundingBox.h:165
lanelet::BoundingBox2d::TopLeftCeil
@ TopLeftCeil
Definition: primitives/BoundingBox.h:54
lanelet::BoundingBox2d::min
VectorType &() min()
Definition: primitives/BoundingBox.h:106
lanelet::BoundingBox2d::sizes
BasicPoint2d sizes() const
Definition: primitives/BoundingBox.h:119
lanelet::BoundingBox2d::isApprox
bool isApprox(const BoundingBox2d &other, const RealScalar &prec=ScalarTraits::dummy_precision()) const
Definition: primitives/BoundingBox.h:270
lanelet::BoundingBox2d::BottomLeft
@ BottomLeft
Definition: primitives/BoundingBox.h:41
lanelet::BoundingBox3d
Eigen::AlignedBox3d BoundingBox3d
Convenience type for an axis aligned bounding box in 3d.
Definition: primitives/BoundingBox.h:283
lanelet::BoundingBox2d::intersects
bool intersects(const BoundingBox2d &b) const
Definition: primitives/BoundingBox.h:171
lanelet::BoundingBox2d::intersection
BoundingBox2d intersection(const BoundingBox2d &b) const
Definition: primitives/BoundingBox.h:205
lanelet::BoundingBox2d::clamp
BoundingBox2d & clamp(const BoundingBox2d &b)
Definition: primitives/BoundingBox.h:196
lanelet::BoundingBox2d::BottomRight
@ BottomRight
Definition: primitives/BoundingBox.h:42
lanelet::BoundingBox2d::BoundingBox2d
BoundingBox2d(const Eigen::AlignedBox< OtherScalarType, AmbientDimAtCompileTime > &other)
Definition: primitives/BoundingBox.h:261
lanelet::BoundingBox2d::setEmpty
void setEmpty()
Definition: primitives/BoundingBox.h:98
lanelet::traits::PrimitiveTraits< BoundingBox3d >::ConstType
BoundingBox3d ConstType
Definition: primitives/BoundingBox.h:296
lanelet::BoundingBox2d::squaredExteriorDistance
Scalar squaredExteriorDistance(const Eigen::MatrixBase< Derived > &p) const
Definition: primitives/BoundingBox.h:228
lanelet::BoundingBox2d::isNull
bool isNull() const
Definition: primitives/BoundingBox.h:87
lanelet::BoundingBox2d::max
VectorType &() max()
Definition: primitives/BoundingBox.h:110
lanelet::BoundingBox2d::exteriorDistance
NonInteger exteriorDistance(const BoundingBox2d &b) const
Definition: primitives/BoundingBox.h:254
lanelet::BasicPoint2d
Eigen::Matrix< double, 2, 1, Eigen::DontAlign > BasicPoint2d
a simple 2d-point
Definition: primitives/Point.h:20
lanelet::BoundingBox2d::min
const VectorType &() min() const
Definition: primitives/BoundingBox.h:104
lanelet::BoundingBox2d::TopRightCeil
@ TopRightCeil
Definition: primitives/BoundingBox.h:55
lanelet::BoundingBox2d::AmbientDimAtCompileTime
@ AmbientDimAtCompileTime
Definition: primitives/BoundingBox.h:25
lanelet::BoundingBox2d::BottomRightCeil
@ BottomRightCeil
Definition: primitives/BoundingBox.h:53
Traits.h
std
Definition: primitives/Area.h:369
lanelet::BoundingBox2d::max_
VectorType max_
Definition: primitives/BoundingBox.h:275
lanelet::BoundingBox2d::sample
VectorType sample() const
Definition: primitives/BoundingBox.h:156
lanelet::BoundingBox2d::ScalarTraits
Eigen::NumTraits< Scalar > ScalarTraits
Definition: primitives/BoundingBox.h:27
lanelet::traits::PrimitiveTraits< BoundingBox3d >::ThreeDType
BoundingBox3d ThreeDType
Definition: primitives/BoundingBox.h:299
lanelet::BoundingBox2d::merged
BoundingBox2d merged(const BoundingBox2d &b) const
Definition: primitives/BoundingBox.h:212
lanelet::BoundingBox2d::BottomRightFloor
@ BottomRightFloor
Definition: primitives/BoundingBox.h:49
lanelet::BoundingBox2d::max
const VectorType &() max() const
Definition: primitives/BoundingBox.h:108
lanelet::BoundingBox2d::TopLeftFloor
@ TopLeftFloor
Definition: primitives/BoundingBox.h:50
lanelet::BoundingBox2d::BoundingBox2d
BoundingBox2d(const Eigen::AlignedBox< double, 2 > &other)
Definition: primitives/BoundingBox.h:79
lanelet::traits::PrimitiveTraits< BoundingBox3d >::MutableType
BoundingBox3d MutableType
Definition: primitives/BoundingBox.h:297
lanelet::BoundingBox2d::contains
bool contains(const Eigen::MatrixBase< Derived > &p) const
Definition: primitives/BoundingBox.h:160
lanelet::BoundingBox2d::dim
Index dim() const
Definition: primitives/BoundingBox.h:82
d
double d
Definition: RegulatoryElementGeometry.cpp:69
lanelet::traits::PrimitiveTraits
Identifies RegulatoryElementPrimitives.
Definition: Traits.h:20
lanelet::BoundingBox2d::volume
Scalar volume() const
Definition: primitives/BoundingBox.h:122
lanelet::BoundingBox2d::translate
BoundingBox2d & translate(const Eigen::MatrixBase< Derived > &aT)
Definition: primitives/BoundingBox.h:216
lanelet::traits::to3D
BoundingBox3d to3D(const BoundingBox2d &primitive)
Definition: primitives/BoundingBox.h:303


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