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. Eigen itself is part of the KDE project.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <g.gael@free.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 // no include guard, we'll include this twice from All.h from Eigen2Support, and it's internal anyway
00026 
00039 template <typename _Scalar, int _AmbientDim>
00040 class AlignedBox
00041 {
00042 public:
00043 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim==Dynamic ? Dynamic : _AmbientDim+1)
00044   enum { AmbientDimAtCompileTime = _AmbientDim };
00045   typedef _Scalar Scalar;
00046   typedef typename NumTraits<Scalar>::Real RealScalar;
00047   typedef Matrix<Scalar,AmbientDimAtCompileTime,1> VectorType;
00048 
00050   inline explicit AlignedBox()
00051   { if (AmbientDimAtCompileTime!=Dynamic) setNull(); }
00052 
00054   inline explicit AlignedBox(int _dim) : m_min(_dim), m_max(_dim)
00055   { setNull(); }
00056 
00058   inline AlignedBox(const VectorType& _min, const VectorType& _max) : m_min(_min), m_max(_max) {}
00059 
00061   inline explicit AlignedBox(const VectorType& p) : m_min(p), m_max(p) {}
00062 
00063   ~AlignedBox() {}
00064 
00066   inline int dim() const { return AmbientDimAtCompileTime==Dynamic ? m_min.size()-1 : AmbientDimAtCompileTime; }
00067 
00069   inline bool isNull() const { return (m_min.cwise() > m_max).any(); }
00070 
00072   inline void setNull()
00073   {
00074     m_min.setConstant( std::numeric_limits<Scalar>::max());
00075     m_max.setConstant(-std::numeric_limits<Scalar>::max());
00076   }
00077 
00079   inline const VectorType& min() const { return m_min; }
00081   inline VectorType& min() { return m_min; }
00083   inline const VectorType& max() const { return m_max; }
00085   inline VectorType& max() { return m_max; }
00086 
00088   inline bool contains(const VectorType& p) const
00089   { return (m_min.cwise()<=p).all() && (p.cwise()<=m_max).all(); }
00090 
00092   inline bool contains(const AlignedBox& b) const
00093   { return (m_min.cwise()<=b.min()).all() && (b.max().cwise()<=m_max).all(); }
00094 
00096   inline AlignedBox& extend(const VectorType& p)
00097   { m_min = m_min.cwise().min(p); m_max = m_max.cwise().max(p); return *this; }
00098 
00100   inline AlignedBox& extend(const AlignedBox& b)
00101   { m_min = m_min.cwise().min(b.m_min); m_max = m_max.cwise().max(b.m_max); return *this; }
00102 
00104   inline AlignedBox& clamp(const AlignedBox& b)
00105   { m_min = m_min.cwise().max(b.m_min); m_max = m_max.cwise().min(b.m_max); return *this; }
00106 
00108   inline AlignedBox& translate(const VectorType& t)
00109   { m_min += t; m_max += t; return *this; }
00110 
00115   inline Scalar squaredExteriorDistance(const VectorType& p) const;
00116 
00121   inline Scalar exteriorDistance(const VectorType& p) const
00122   { return ei_sqrt(squaredExteriorDistance(p)); }
00123 
00129   template<typename NewScalarType>
00130   inline typename internal::cast_return_type<AlignedBox,
00131            AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type cast() const
00132   {
00133     return typename internal::cast_return_type<AlignedBox,
00134                     AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type(*this);
00135   }
00136 
00138   template<typename OtherScalarType>
00139   inline explicit AlignedBox(const AlignedBox<OtherScalarType,AmbientDimAtCompileTime>& other)
00140   {
00141     m_min = other.min().template cast<Scalar>();
00142     m_max = other.max().template cast<Scalar>();
00143   }
00144 
00149   bool isApprox(const AlignedBox& other, typename NumTraits<Scalar>::Real prec = precision<Scalar>()) const
00150   { return m_min.isApprox(other.m_min, prec) && m_max.isApprox(other.m_max, prec); }
00151 
00152 protected:
00153 
00154   VectorType m_min, m_max;
00155 };
00156 
00157 template<typename Scalar,int AmbiantDim>
00158 inline Scalar AlignedBox<Scalar,AmbiantDim>::squaredExteriorDistance(const VectorType& p) const
00159 {
00160   Scalar dist2 = 0.;
00161   Scalar aux;
00162   for (int k=0; k<dim(); ++k)
00163   {
00164     if ((aux = (p[k]-m_min[k]))<0.)
00165       dist2 += aux*aux;
00166     else if ( (aux = (m_max[k]-p[k]))<0. )
00167       dist2 += aux*aux;
00168   }
00169   return dist2;
00170 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:30:42