1// This file is part of Eigen, a lightweight C++ template library 2// for linear algebra. 3// 4// Copyright (C) 2008 Gael Guennebaud <g.gael@free.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// no include guard, we'll include this twice from All.h from Eigen2Support, and it's internal anyway 11 12namespace Eigen { 13 14/** \geometry_module \ingroup Geometry_Module 15 * \nonstableyet 16 * 17 * \class AlignedBox 18 * 19 * \brief An axis aligned box 20 * 21 * \param _Scalar the type of the scalar coefficients 22 * \param _AmbientDim the dimension of the ambient space, can be a compile time value or Dynamic. 23 * 24 * This class represents an axis aligned box as a pair of the minimal and maximal corners. 25 */ 26template <typename _Scalar, int _AmbientDim> 27class AlignedBox 28{ 29public: 30EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim==Dynamic ? Dynamic : _AmbientDim+1) 31 enum { AmbientDimAtCompileTime = _AmbientDim }; 32 typedef _Scalar Scalar; 33 typedef typename NumTraits<Scalar>::Real RealScalar; 34 typedef Matrix<Scalar,AmbientDimAtCompileTime,1> VectorType; 35 36 /** Default constructor initializing a null box. */ 37 inline AlignedBox() 38 { if (AmbientDimAtCompileTime!=Dynamic) setNull(); } 39 40 /** Constructs a null box with \a _dim the dimension of the ambient space. */ 41 inline explicit AlignedBox(int _dim) : m_min(_dim), m_max(_dim) 42 { setNull(); } 43 44 /** Constructs a box with extremities \a _min and \a _max. */ 45 inline AlignedBox(const VectorType& _min, const VectorType& _max) : m_min(_min), m_max(_max) {} 46 47 /** Constructs a box containing a single point \a p. */ 48 inline explicit AlignedBox(const VectorType& p) : m_min(p), m_max(p) {} 49 50 ~AlignedBox() {} 51 52 /** \returns the dimension in which the box holds */ 53 inline int dim() const { return AmbientDimAtCompileTime==Dynamic ? m_min.size()-1 : AmbientDimAtCompileTime; } 54 55 /** \returns true if the box is null, i.e, empty. */ 56 inline bool isNull() const { return (m_min.cwise() > m_max).any(); } 57 58 /** Makes \c *this a null/empty box. */ 59 inline void setNull() 60 { 61 m_min.setConstant( (std::numeric_limits<Scalar>::max)()); 62 m_max.setConstant(-(std::numeric_limits<Scalar>::max)()); 63 } 64 65 /** \returns the minimal corner */ 66 inline const VectorType& (min)() const { return m_min; } 67 /** \returns a non const reference to the minimal corner */ 68 inline VectorType& (min)() { return m_min; } 69 /** \returns the maximal corner */ 70 inline const VectorType& (max)() const { return m_max; } 71 /** \returns a non const reference to the maximal corner */ 72 inline VectorType& (max)() { return m_max; } 73 74 /** \returns true if the point \a p is inside the box \c *this. */ 75 inline bool contains(const VectorType& p) const 76 { return (m_min.cwise()<=p).all() && (p.cwise()<=m_max).all(); } 77 78 /** \returns true if the box \a b is entirely inside the box \c *this. */ 79 inline bool contains(const AlignedBox& b) const 80 { return (m_min.cwise()<=(b.min)()).all() && ((b.max)().cwise()<=m_max).all(); } 81 82 /** Extends \c *this such that it contains the point \a p and returns a reference to \c *this. */ 83 inline AlignedBox& extend(const VectorType& p) 84 { m_min = (m_min.cwise().min)(p); m_max = (m_max.cwise().max)(p); return *this; } 85 86 /** Extends \c *this such that it contains the box \a b and returns a reference to \c *this. */ 87 inline AlignedBox& extend(const AlignedBox& b) 88 { m_min = (m_min.cwise().min)(b.m_min); m_max = (m_max.cwise().max)(b.m_max); return *this; } 89 90 /** Clamps \c *this by the box \a b and returns a reference to \c *this. */ 91 inline AlignedBox& clamp(const AlignedBox& b) 92 { m_min = (m_min.cwise().max)(b.m_min); m_max = (m_max.cwise().min)(b.m_max); return *this; } 93 94 /** Translate \c *this by the vector \a t and returns a reference to \c *this. */ 95 inline AlignedBox& translate(const VectorType& t) 96 { m_min += t; m_max += t; return *this; } 97 98 /** \returns the squared distance between the point \a p and the box \c *this, 99 * and zero if \a p is inside the box. 100 * \sa exteriorDistance() 101 */ 102 inline Scalar squaredExteriorDistance(const VectorType& p) const; 103 104 /** \returns the distance between the point \a p and the box \c *this, 105 * and zero if \a p is inside the box. 106 * \sa squaredExteriorDistance() 107 */ 108 inline Scalar exteriorDistance(const VectorType& p) const 109 { return ei_sqrt(squaredExteriorDistance(p)); } 110 111 /** \returns \c *this with scalar type casted to \a NewScalarType 112 * 113 * Note that if \a NewScalarType is equal to the current scalar type of \c *this 114 * then this function smartly returns a const reference to \c *this. 115 */ 116 template<typename NewScalarType> 117 inline typename internal::cast_return_type<AlignedBox, 118 AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type cast() const 119 { 120 return typename internal::cast_return_type<AlignedBox, 121 AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type(*this); 122 } 123 124 /** Copy constructor with scalar type conversion */ 125 template<typename OtherScalarType> 126 inline explicit AlignedBox(const AlignedBox<OtherScalarType,AmbientDimAtCompileTime>& other) 127 { 128 m_min = (other.min)().template cast<Scalar>(); 129 m_max = (other.max)().template cast<Scalar>(); 130 } 131 132 /** \returns \c true if \c *this is approximately equal to \a other, within the precision 133 * determined by \a prec. 134 * 135 * \sa MatrixBase::isApprox() */ 136 bool isApprox(const AlignedBox& other, typename NumTraits<Scalar>::Real prec = precision<Scalar>()) const 137 { return m_min.isApprox(other.m_min, prec) && m_max.isApprox(other.m_max, prec); } 138 139protected: 140 141 VectorType m_min, m_max; 142}; 143 144template<typename Scalar,int AmbiantDim> 145inline Scalar AlignedBox<Scalar,AmbiantDim>::squaredExteriorDistance(const VectorType& p) const 146{ 147 Scalar dist2(0); 148 Scalar aux; 149 for (int k=0; k<dim(); ++k) 150 { 151 if ((aux = (p[k]-m_min[k]))<Scalar(0)) 152 dist2 += aux*aux; 153 else if ( (aux = (m_max[k]-p[k]))<Scalar(0)) 154 dist2 += aux*aux; 155 } 156 return dist2; 157} 158 159} // end namespace Eigen 160