Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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 }