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() {
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 {
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
BoundingBox2d merged(const BoundingBox2d &b) const
BasicPoint p
bool intersects(const BoundingBox2d &b) const
bool contains(const Eigen::MatrixBase< Derived > &p) const
Eigen::NumTraits< Scalar > ScalarTraits
BoundingBox2d & extend(const BoundingBox2d &b)
bool contains(const BoundingBox2d &b) const
ScalarTraits::NonInteger NonInteger
BoundingBox2d(const Eigen::AlignedBox< OtherScalarType, AmbientDimAtCompileTime > &other)
BoundingBox2d & clamp(const BoundingBox2d &b)
const VectorType &() min() const
Scalar squaredExteriorDistance(const Eigen::MatrixBase< Derived > &p) const
BasicPoint2d center() const
Identifies RegulatoryElementPrimitives.
Definition: Traits.h:20
Scalar squaredExteriorDistance(const BoundingBox2d &b) const
ScalarTraits::Real RealScalar
Eigen::AlignedBox3d BoundingBox3d
Convenience type for an axis aligned bounding box in 3d.
NonInteger exteriorDistance(const BoundingBox2d &b) const
const VectorType &() max() const
bool isApprox(const BoundingBox2d &other, const RealScalar &prec=ScalarTraits::dummy_precision()) const
Identifies PointPrimitives.
Definition: Traits.h:7
BoundingBox2d(const Eigen::MatrixBase< Derived > &p)
BoundingBox2d intersection(const BoundingBox2d &b) const
VectorType corner(CornerType corner) const
NonInteger exteriorDistance(const Eigen::MatrixBase< Derived > &p) const
Axis-Aligned bounding box in 2d.
Eigen::Matrix< double, 2, 1, Eigen::DontAlign > BasicPoint2d
a simple 2d-point
BasicPoint2d sizes() const
BasicPoint2d diagonal() const
BoundingBox2d & translate(const Eigen::MatrixBase< Derived > &aT)
BoundingBox2d to2D(const BoundingBox3d &primitive)
BoundingBox3d to3D(const BoundingBox2d &primitive)
BoundingBox2d & extend(const Eigen::MatrixBase< Derived > &p)
BoundingBox2d(const Eigen::AlignedBox< double, 2 > &other)
BoundingBox2d(OtherVectorType1 min, OtherVectorType2 max)


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