AlignedBox.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_ALIGNEDBOX_H
00026 #define EIGEN_ALIGNEDBOX_H
00027 
00028 namespace Eigen { 
00029 
00042 template <typename _Scalar, int _AmbientDim>
00043 class AlignedBox
00044 {
00045 public:
00046 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim)
00047   enum { AmbientDimAtCompileTime = _AmbientDim };
00048   typedef _Scalar                                   Scalar;
00049   typedef NumTraits<Scalar>                         ScalarTraits;
00050   typedef DenseIndex                                Index;
00051   typedef typename ScalarTraits::Real               RealScalar;
00052   typedef typename ScalarTraits::NonInteger      NonInteger;
00053   typedef Matrix<Scalar,AmbientDimAtCompileTime,1>  VectorType;
00054 
00056   enum CornerType
00057   {
00059     Min=0, Max=1,
00060 
00062     BottomLeft=0, BottomRight=1,
00063     TopLeft=2, TopRight=3,
00064 
00066     BottomLeftFloor=0, BottomRightFloor=1,
00067     TopLeftFloor=2, TopRightFloor=3,
00068     BottomLeftCeil=4, BottomRightCeil=5,
00069     TopLeftCeil=6, TopRightCeil=7
00070   };
00071 
00072 
00074   inline explicit AlignedBox()
00075   { if (AmbientDimAtCompileTime!=Dynamic) setEmpty(); }
00076 
00078   inline explicit AlignedBox(Index _dim) : m_min(_dim), m_max(_dim)
00079   { setEmpty(); }
00080 
00082   template<typename OtherVectorType1, typename OtherVectorType2>
00083   inline AlignedBox(const OtherVectorType1& _min, const OtherVectorType2& _max) : m_min(_min), m_max(_max) {}
00084 
00086   template<typename Derived>
00087   inline explicit AlignedBox(const MatrixBase<Derived>& a_p)
00088   {
00089     const typename internal::nested<Derived,2>::type p(a_p.derived());
00090     m_min = p;
00091     m_max = p;
00092   }
00093 
00094   ~AlignedBox() {}
00095 
00097   inline Index dim() const { return AmbientDimAtCompileTime==Dynamic ? m_min.size()-1 : Index(AmbientDimAtCompileTime); }
00098 
00100   inline bool isNull() const { return isEmpty(); }
00101 
00103   inline void setNull() { setEmpty(); }
00104 
00106   inline bool isEmpty() const { return (m_min.array() > m_max.array()).any(); }
00107 
00109   inline void setEmpty()
00110   {
00111     m_min.setConstant( ScalarTraits::highest() );
00112     m_max.setConstant( ScalarTraits::lowest() );
00113   }
00114 
00116   inline const VectorType& (min)() const { return m_min; }
00118   inline VectorType& (min)() { return m_min; }
00120   inline const VectorType& (max)() const { return m_max; }
00122   inline VectorType& (max)() { return m_max; }
00123 
00125   inline const CwiseUnaryOp<internal::scalar_quotient1_op<Scalar>,
00126                             const CwiseBinaryOp<internal::scalar_sum_op<Scalar>, const VectorType, const VectorType> >
00127   center() const
00128   { return (m_min+m_max)/2; }
00129 
00134   inline const CwiseBinaryOp< internal::scalar_difference_op<Scalar>, const VectorType, const VectorType> sizes() const
00135   { return m_max - m_min; }
00136 
00138   inline Scalar volume() const
00139   { return sizes().prod(); }
00140 
00145   inline CwiseBinaryOp< internal::scalar_difference_op<Scalar>, const VectorType, const VectorType> diagonal() const
00146   { return sizes(); }
00147 
00157   inline VectorType corner(CornerType corner) const
00158   {
00159     EIGEN_STATIC_ASSERT(_AmbientDim <= 3, THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE);
00160 
00161     VectorType res;
00162 
00163     Index mult = 1;
00164     for(Index d=0; d<dim(); ++d)
00165     {
00166       if( mult & corner ) res[d] = m_max[d];
00167       else                res[d] = m_min[d];
00168       mult *= 2;
00169     }
00170     return res;
00171   }
00172 
00175   inline VectorType sample() const
00176   {
00177     VectorType r;
00178     for(Index d=0; d<dim(); ++d)
00179     {
00180       if(!ScalarTraits::IsInteger)
00181       {
00182         r[d] = m_min[d] + (m_max[d]-m_min[d])
00183              * internal::random<Scalar>(Scalar(0), Scalar(1));
00184       }
00185       else
00186         r[d] = internal::random(m_min[d], m_max[d]);
00187     }
00188     return r;
00189   }
00190 
00192   template<typename Derived>
00193   inline bool contains(const MatrixBase<Derived>& a_p) const
00194   {
00195     typename internal::nested<Derived,2>::type p(a_p.derived());
00196     return (m_min.array()<=p.array()).all() && (p.array()<=m_max.array()).all();
00197   }
00198 
00200   inline bool contains(const AlignedBox& b) const
00201   { return (m_min.array()<=(b.min)().array()).all() && ((b.max)().array()<=m_max.array()).all(); }
00202 
00204   template<typename Derived>
00205   inline AlignedBox& extend(const MatrixBase<Derived>& a_p)
00206   {
00207     typename internal::nested<Derived,2>::type p(a_p.derived());
00208     m_min = m_min.cwiseMin(p);
00209     m_max = m_max.cwiseMax(p);
00210     return *this;
00211   }
00212 
00214   inline AlignedBox& extend(const AlignedBox& b)
00215   {
00216     m_min = m_min.cwiseMin(b.m_min);
00217     m_max = m_max.cwiseMax(b.m_max);
00218     return *this;
00219   }
00220 
00222   inline AlignedBox& clamp(const AlignedBox& b)
00223   {
00224     m_min = m_min.cwiseMax(b.m_min);
00225     m_max = m_max.cwiseMin(b.m_max);
00226     return *this;
00227   }
00228 
00230   inline AlignedBox intersection(const AlignedBox& b) const
00231   {return AlignedBox(m_min.cwiseMax(b.m_min), m_max.cwiseMin(b.m_max)); }
00232 
00234   inline AlignedBox merged(const AlignedBox& b) const
00235   { return AlignedBox(m_min.cwiseMin(b.m_min), m_max.cwiseMax(b.m_max)); }
00236 
00238   template<typename Derived>
00239   inline AlignedBox& translate(const MatrixBase<Derived>& a_t)
00240   {
00241     const typename internal::nested<Derived,2>::type t(a_t.derived());
00242     m_min += t;
00243     m_max += t;
00244     return *this;
00245   }
00246 
00251   template<typename Derived>
00252   inline Scalar squaredExteriorDistance(const MatrixBase<Derived>& a_p) const;
00253 
00258   inline Scalar squaredExteriorDistance(const AlignedBox& b) const;
00259 
00264   template<typename Derived>
00265   inline NonInteger exteriorDistance(const MatrixBase<Derived>& p) const
00266   { return internal::sqrt(NonInteger(squaredExteriorDistance(p))); }
00267 
00272   inline NonInteger exteriorDistance(const AlignedBox& b) const
00273   { return internal::sqrt(NonInteger(squaredExteriorDistance(b))); }
00274 
00280   template<typename NewScalarType>
00281   inline typename internal::cast_return_type<AlignedBox,
00282            AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type cast() const
00283   {
00284     return typename internal::cast_return_type<AlignedBox,
00285                     AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type(*this);
00286   }
00287 
00289   template<typename OtherScalarType>
00290   inline explicit AlignedBox(const AlignedBox<OtherScalarType,AmbientDimAtCompileTime>& other)
00291   {
00292     m_min = (other.min)().template cast<Scalar>();
00293     m_max = (other.max)().template cast<Scalar>();
00294   }
00295 
00300   bool isApprox(const AlignedBox& other, RealScalar prec = ScalarTraits::dummy_precision()) const
00301   { return m_min.isApprox(other.m_min, prec) && m_max.isApprox(other.m_max, prec); }
00302 
00303 protected:
00304 
00305   VectorType m_min, m_max;
00306 };
00307 
00308 
00309 
00310 template<typename Scalar,int AmbientDim>
00311 template<typename Derived>
00312 inline Scalar AlignedBox<Scalar,AmbientDim>::squaredExteriorDistance(const MatrixBase<Derived>& a_p) const
00313 {
00314   const typename internal::nested<Derived,2*AmbientDim>::type p(a_p.derived());
00315   Scalar dist2(0);
00316   Scalar aux;
00317   for (Index k=0; k<dim(); ++k)
00318   {
00319     if( m_min[k] > p[k] )
00320     {
00321       aux = m_min[k] - p[k];
00322       dist2 += aux*aux;
00323     }
00324     else if( p[k] > m_max[k] )
00325     {
00326       aux = p[k] - m_max[k];
00327       dist2 += aux*aux;
00328     }
00329   }
00330   return dist2;
00331 }
00332 
00333 template<typename Scalar,int AmbientDim>
00334 inline Scalar AlignedBox<Scalar,AmbientDim>::squaredExteriorDistance(const AlignedBox& b) const
00335 {
00336   Scalar dist2(0);
00337   Scalar aux;
00338   for (Index k=0; k<dim(); ++k)
00339   {
00340     if( m_min[k] > b.m_max[k] )
00341     {
00342       aux = m_min[k] - b.m_max[k];
00343       dist2 += aux*aux;
00344     }
00345     else if( b.m_min[k] > m_max[k] )
00346     {
00347       aux = b.m_min[k] - m_max[k];
00348       dist2 += aux*aux;
00349     }
00350   }
00351   return dist2;
00352 }
00353 
00370 #define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix)    \
00371                                  \
00372 typedef AlignedBox<Type, Size>   AlignedBox##SizeSuffix##TypeSuffix;
00373 
00374 #define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
00375 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 1, 1) \
00376 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
00377 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
00378 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
00379 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X)
00380 
00381 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int,                  i)
00382 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float,                f)
00383 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double,               d)
00384 
00385 #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
00386 #undef EIGEN_MAKE_TYPEDEFS
00387 
00388 } // end namespace Eigen
00389 
00390 #endif // EIGEN_ALIGNEDBOX_H