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