1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_ALIGNEDBOX_H
11#define EIGEN_ALIGNEDBOX_H
12
13namespace Eigen {
14
15/** \geometry_module \ingroup Geometry_Module
16 *
17 *
18 * \class AlignedBox
19 *
20 * \brief An axis aligned box
21 *
22 * \tparam _Scalar the type of the scalar coefficients
23 * \tparam _AmbientDim the dimension of the ambient space, can be a compile time value or Dynamic.
24 *
25 * This class represents an axis aligned box as a pair of the minimal and maximal corners.
26 * \warning The result of most methods is undefined when applied to an empty box. You can check for empty boxes using isEmpty().
27 * \sa alignedboxtypedefs
28 */
29template <typename _Scalar, int _AmbientDim>
30class AlignedBox
31{
32public:
33EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim)
34 enum { AmbientDimAtCompileTime = _AmbientDim };
35 typedef _Scalar Scalar;
36 typedef NumTraits<Scalar> ScalarTraits;
37 typedef Eigen::Index Index; ///< \deprecated since Eigen 3.3
38 typedef typename ScalarTraits::Real RealScalar;
39 typedef typename ScalarTraits::NonInteger NonInteger;
40 typedef Matrix<Scalar,AmbientDimAtCompileTime,1> VectorType;
41 typedef CwiseBinaryOp<internal::scalar_sum_op<Scalar>, const VectorType, const VectorType> VectorTypeSum;
42
43 /** Define constants to name the corners of a 1D, 2D or 3D axis aligned bounding box */
44 enum CornerType
45 {
46 /** 1D names @{ */
47 Min=0, Max=1,
48 /** @} */
49
50 /** Identifier for 2D corner @{ */
51 BottomLeft=0, BottomRight=1,
52 TopLeft=2, TopRight=3,
53 /** @} */
54
55 /** Identifier for 3D corner @{ */
56 BottomLeftFloor=0, BottomRightFloor=1,
57 TopLeftFloor=2, TopRightFloor=3,
58 BottomLeftCeil=4, BottomRightCeil=5,
59 TopLeftCeil=6, TopRightCeil=7
60 /** @} */
61 };
62
63
64 /** Default constructor initializing a null box. */
65 EIGEN_DEVICE_FUNC inline AlignedBox()
66 { if (AmbientDimAtCompileTime!=Dynamic) setEmpty(); }
67
68 /** Constructs a null box with \a _dim the dimension of the ambient space. */
69 EIGEN_DEVICE_FUNC inline explicit AlignedBox(Index _dim) : m_min(_dim), m_max(_dim)
70 { setEmpty(); }
71
72 /** Constructs a box with extremities \a _min and \a _max.
73 * \warning If either component of \a _min is larger than the same component of \a _max, the constructed box is empty. */
74 template<typename OtherVectorType1, typename OtherVectorType2>
75 EIGEN_DEVICE_FUNC inline AlignedBox(const OtherVectorType1& _min, const OtherVectorType2& _max) : m_min(_min), m_max(_max) {}
76
77 /** Constructs a box containing a single point \a p. */
78 template<typename Derived>
79 EIGEN_DEVICE_FUNC inline explicit AlignedBox(const MatrixBase<Derived>& p) : m_min(p), m_max(m_min)
80 { }
81
82 EIGEN_DEVICE_FUNC ~AlignedBox() {}
83
84 /** \returns the dimension in which the box holds */
85 EIGEN_DEVICE_FUNC inline Index dim() const { return AmbientDimAtCompileTime==Dynamic ? m_min.size() : Index(AmbientDimAtCompileTime); }
86
87 /** \deprecated use isEmpty() */
88 EIGEN_DEVICE_FUNC inline bool isNull() const { return isEmpty(); }
89
90 /** \deprecated use setEmpty() */
91 EIGEN_DEVICE_FUNC inline void setNull() { setEmpty(); }
92
93 /** \returns true if the box is empty.
94 * \sa setEmpty */
95 EIGEN_DEVICE_FUNC inline bool isEmpty() const { return (m_min.array() > m_max.array()).any(); }
96
97 /** Makes \c *this an empty box.
98 * \sa isEmpty */
99 EIGEN_DEVICE_FUNC inline void setEmpty()
100 {
101 m_min.setConstant( ScalarTraits::highest() );
102 m_max.setConstant( ScalarTraits::lowest() );
103 }
104
105 /** \returns the minimal corner */
106 EIGEN_DEVICE_FUNC inline const VectorType& (min)() const { return m_min; }
107 /** \returns a non const reference to the minimal corner */
108 EIGEN_DEVICE_FUNC inline VectorType& (min)() { return m_min; }
109 /** \returns the maximal corner */
110 EIGEN_DEVICE_FUNC inline const VectorType& (max)() const { return m_max; }
111 /** \returns a non const reference to the maximal corner */
112 EIGEN_DEVICE_FUNC inline VectorType& (max)() { return m_max; }
113
114 /** \returns the center of the box */
115 EIGEN_DEVICE_FUNC inline const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(VectorTypeSum, RealScalar, quotient)
116 center() const
117 { return (m_min+m_max)/RealScalar(2); }
118
119 /** \returns the lengths of the sides of the bounding box.
120 * Note that this function does not get the same
121 * result for integral or floating scalar types: see
122 */
123 EIGEN_DEVICE_FUNC inline const CwiseBinaryOp< internal::scalar_difference_op<Scalar,Scalar>, const VectorType, const VectorType> sizes() const
124 { return m_max - m_min; }
125
126 /** \returns the volume of the bounding box */
127 EIGEN_DEVICE_FUNC inline Scalar volume() const
128 { return sizes().prod(); }
129
130 /** \returns an expression for the bounding box diagonal vector
131 * if the length of the diagonal is needed: diagonal().norm()
132 * will provide it.
133 */
134 EIGEN_DEVICE_FUNC inline CwiseBinaryOp< internal::scalar_difference_op<Scalar,Scalar>, const VectorType, const VectorType> diagonal() const
135 { return sizes(); }
136
137 /** \returns the vertex of the bounding box at the corner defined by
138 * the corner-id corner. It works only for a 1D, 2D or 3D bounding box.
139 * For 1D bounding boxes corners are named by 2 enum constants:
140 * BottomLeft and BottomRight.
141 * For 2D bounding boxes, corners are named by 4 enum constants:
142 * BottomLeft, BottomRight, TopLeft, TopRight.
143 * For 3D bounding boxes, the following names are added:
144 * BottomLeftCeil, BottomRightCeil, TopLeftCeil, TopRightCeil.
145 */
146 EIGEN_DEVICE_FUNC inline VectorType corner(CornerType corner) const
147 {
148 EIGEN_STATIC_ASSERT(_AmbientDim <= 3, THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE);
149
150 VectorType res;
151
152 Index mult = 1;
153 for(Index d=0; d<dim(); ++d)
154 {
155 if( mult & corner ) res[d] = m_max[d];
156 else res[d] = m_min[d];
157 mult *= 2;
158 }
159 return res;
160 }
161
162 /** \returns a random point inside the bounding box sampled with
163 * a uniform distribution */
164 EIGEN_DEVICE_FUNC inline VectorType sample() const
165 {
166 VectorType r(dim());
167 for(Index d=0; d<dim(); ++d)
168 {
169 if(!ScalarTraits::IsInteger)
170 {
171 r[d] = m_min[d] + (m_max[d]-m_min[d])
172 * internal::random<Scalar>(Scalar(0), Scalar(1));
173 }
174 else
175 r[d] = internal::random(m_min[d], m_max[d]);
176 }
177 return r;
178 }
179
180 /** \returns true if the point \a p is inside the box \c *this. */
181 template<typename Derived>
182 EIGEN_DEVICE_FUNC inline bool contains(const MatrixBase<Derived>& p) const
183 {
184 typename internal::nested_eval<Derived,2>::type p_n(p.derived());
185 return (m_min.array()<=p_n.array()).all() && (p_n.array()<=m_max.array()).all();
186 }
187
188 /** \returns true if the box \a b is entirely inside the box \c *this. */
189 EIGEN_DEVICE_FUNC inline bool contains(const AlignedBox& b) const
190 { return (m_min.array()<=(b.min)().array()).all() && ((b.max)().array()<=m_max.array()).all(); }
191
192 /** \returns true if the box \a b is intersecting the box \c *this.
193 * \sa intersection, clamp */
194 EIGEN_DEVICE_FUNC inline bool intersects(const AlignedBox& b) const
195 { return (m_min.array()<=(b.max)().array()).all() && ((b.min)().array()<=m_max.array()).all(); }
196
197 /** Extends \c *this such that it contains the point \a p and returns a reference to \c *this.
198 * \sa extend(const AlignedBox&) */
199 template<typename Derived>
200 EIGEN_DEVICE_FUNC inline AlignedBox& extend(const MatrixBase<Derived>& p)
201 {
202 typename internal::nested_eval<Derived,2>::type p_n(p.derived());
203 m_min = m_min.cwiseMin(p_n);
204 m_max = m_max.cwiseMax(p_n);
205 return *this;
206 }
207
208 /** Extends \c *this such that it contains the box \a b and returns a reference to \c *this.
209 * \sa merged, extend(const MatrixBase&) */
210 EIGEN_DEVICE_FUNC inline AlignedBox& extend(const AlignedBox& b)
211 {
212 m_min = m_min.cwiseMin(b.m_min);
213 m_max = m_max.cwiseMax(b.m_max);
214 return *this;
215 }
216
217 /** Clamps \c *this by the box \a b and returns a reference to \c *this.
218 * \note If the boxes don't intersect, the resulting box is empty.
219 * \sa intersection(), intersects() */
220 EIGEN_DEVICE_FUNC inline AlignedBox& clamp(const AlignedBox& b)
221 {
222 m_min = m_min.cwiseMax(b.m_min);
223 m_max = m_max.cwiseMin(b.m_max);
224 return *this;
225 }
226
227 /** Returns an AlignedBox that is the intersection of \a b and \c *this
228 * \note If the boxes don't intersect, the resulting box is empty.
229 * \sa intersects(), clamp, contains() */
230 EIGEN_DEVICE_FUNC inline AlignedBox intersection(const AlignedBox& b) const
231 {return AlignedBox(m_min.cwiseMax(b.m_min), m_max.cwiseMin(b.m_max)); }
232
233 /** Returns an AlignedBox that is the union of \a b and \c *this.
234 * \note Merging with an empty box may result in a box bigger than \c *this.
235 * \sa extend(const AlignedBox&) */
236 EIGEN_DEVICE_FUNC inline AlignedBox merged(const AlignedBox& b) const
237 { return AlignedBox(m_min.cwiseMin(b.m_min), m_max.cwiseMax(b.m_max)); }
238
239 /** Translate \c *this by the vector \a t and returns a reference to \c *this. */
240 template<typename Derived>
241 EIGEN_DEVICE_FUNC inline AlignedBox& translate(const MatrixBase<Derived>& a_t)
242 {
243 const typename internal::nested_eval<Derived,2>::type t(a_t.derived());
244 m_min += t;
245 m_max += t;
246 return *this;
247 }
248
249 /** \returns the squared distance between the point \a p and the box \c *this,
250 * and zero if \a p is inside the box.
251 * \sa exteriorDistance(const MatrixBase&), squaredExteriorDistance(const AlignedBox&)
252 */
253 template<typename Derived>
254 EIGEN_DEVICE_FUNC inline Scalar squaredExteriorDistance(const MatrixBase<Derived>& p) const;
255
256 /** \returns the squared distance between the boxes \a b and \c *this,
257 * and zero if the boxes intersect.
258 * \sa exteriorDistance(const AlignedBox&), squaredExteriorDistance(const MatrixBase&)
259 */
260 EIGEN_DEVICE_FUNC inline Scalar squaredExteriorDistance(const AlignedBox& b) const;
261
262 /** \returns the distance between the point \a p and the box \c *this,
263 * and zero if \a p is inside the box.
264 * \sa squaredExteriorDistance(const MatrixBase&), exteriorDistance(const AlignedBox&)
265 */
266 template<typename Derived>
267 EIGEN_DEVICE_FUNC inline NonInteger exteriorDistance(const MatrixBase<Derived>& p) const
268 { EIGEN_USING_STD_MATH(sqrt) return sqrt(NonInteger(squaredExteriorDistance(p))); }
269
270 /** \returns the distance between the boxes \a b and \c *this,
271 * and zero if the boxes intersect.
272 * \sa squaredExteriorDistance(const AlignedBox&), exteriorDistance(const MatrixBase&)
273 */
274 EIGEN_DEVICE_FUNC inline NonInteger exteriorDistance(const AlignedBox& b) const
275 { EIGEN_USING_STD_MATH(sqrt) return sqrt(NonInteger(squaredExteriorDistance(b))); }
276
277 /** \returns \c *this with scalar type casted to \a NewScalarType
278 *
279 * Note that if \a NewScalarType is equal to the current scalar type of \c *this
280 * then this function smartly returns a const reference to \c *this.
281 */
282 template<typename NewScalarType>
283 EIGEN_DEVICE_FUNC inline typename internal::cast_return_type<AlignedBox,
284 AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type cast() const
285 {
286 return typename internal::cast_return_type<AlignedBox,
287 AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type(*this);
288 }
289
290 /** Copy constructor with scalar type conversion */
291 template<typename OtherScalarType>
292 EIGEN_DEVICE_FUNC inline explicit AlignedBox(const AlignedBox<OtherScalarType,AmbientDimAtCompileTime>& other)
293 {
294 m_min = (other.min)().template cast<Scalar>();
295 m_max = (other.max)().template cast<Scalar>();
296 }
297
298 /** \returns \c true if \c *this is approximately equal to \a other, within the precision
299 * determined by \a prec.
300 *
301 * \sa MatrixBase::isApprox() */
302 EIGEN_DEVICE_FUNC bool isApprox(const AlignedBox& other, const RealScalar& prec = ScalarTraits::dummy_precision()) const
303 { return m_min.isApprox(other.m_min, prec) && m_max.isApprox(other.m_max, prec); }
304
305protected:
306
307 VectorType m_min, m_max;
308};
309
310
311
312template<typename Scalar,int AmbientDim>
313template<typename Derived>
314EIGEN_DEVICE_FUNC inline Scalar AlignedBox<Scalar,AmbientDim>::squaredExteriorDistance(const MatrixBase<Derived>& a_p) const
315{
316 typename internal::nested_eval<Derived,2*AmbientDim>::type p(a_p.derived());
317 Scalar dist2(0);
318 Scalar aux;
319 for (Index k=0; k<dim(); ++k)
320 {
321 if( m_min[k] > p[k] )
322 {
323 aux = m_min[k] - p[k];
324 dist2 += aux*aux;
325 }
326 else if( p[k] > m_max[k] )
327 {
328 aux = p[k] - m_max[k];
329 dist2 += aux*aux;
330 }
331 }
332 return dist2;
333}
334
335template<typename Scalar,int AmbientDim>
336EIGEN_DEVICE_FUNC inline Scalar AlignedBox<Scalar,AmbientDim>::squaredExteriorDistance(const AlignedBox& b) const
337{
338 Scalar dist2(0);
339 Scalar aux;
340 for (Index k=0; k<dim(); ++k)
341 {
342 if( m_min[k] > b.m_max[k] )
343 {
344 aux = m_min[k] - b.m_max[k];
345 dist2 += aux*aux;
346 }
347 else if( b.m_min[k] > m_max[k] )
348 {
349 aux = b.m_min[k] - m_max[k];
350 dist2 += aux*aux;
351 }
352 }
353 return dist2;
354}
355
356/** \defgroup alignedboxtypedefs Global aligned box typedefs
357 *
358 * \ingroup Geometry_Module
359 *
360 * Eigen defines several typedef shortcuts for most common aligned box types.
361 *
362 * The general patterns are the following:
363 *
364 * \c AlignedBoxSizeType where \c Size can be \c 1, \c 2,\c 3,\c 4 for fixed size boxes or \c X for dynamic size,
365 * and where \c Type can be \c i for integer, \c f for float, \c d for double.
366 *
367 * For example, \c AlignedBox3d is a fixed-size 3x3 aligned box type of doubles, and \c AlignedBoxXf is a dynamic-size aligned box of floats.
368 *
369 * \sa class AlignedBox
370 */
371
372#define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
373/** \ingroup alignedboxtypedefs */ \
374typedef AlignedBox<Type, Size> AlignedBox##SizeSuffix##TypeSuffix;
375
376#define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
377EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 1, 1) \
378EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
379EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
380EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
381EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X)
382
383EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int, i)
384EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float, f)
385EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double, d)
386
387#undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
388#undef EIGEN_MAKE_TYPEDEFS
389
390} // end namespace Eigen
391
392#endif // EIGEN_ALIGNEDBOX_H
393