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 #ifndef EIGEN_ALIGNEDBOX_H
00026 #define EIGEN_ALIGNEDBOX_H
00027
00040 template <typename _Scalar, int _AmbientDim>
00041 class AlignedBox
00042 {
00043 public:
00044 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim==Dynamic ? Dynamic : _AmbientDim+1)
00045 enum { AmbientDimAtCompileTime = _AmbientDim };
00046 typedef _Scalar Scalar;
00047 typedef typename NumTraits<Scalar>::Real RealScalar;
00048 typedef Matrix<Scalar,AmbientDimAtCompileTime,1> VectorType;
00049
00051 inline explicit AlignedBox()
00052 { if (AmbientDimAtCompileTime!=Dynamic) setNull(); }
00053
00055 inline explicit AlignedBox(int _dim) : m_min(_dim), m_max(_dim)
00056 { setNull(); }
00057
00059 inline AlignedBox(const VectorType& _min, const VectorType& _max) : m_min(_min), m_max(_max) {}
00060
00062 inline explicit AlignedBox(const VectorType& p) : m_min(p), m_max(p) {}
00063
00064 ~AlignedBox() {}
00065
00067 inline int dim() const { return AmbientDimAtCompileTime==Dynamic ? m_min.size()-1 : AmbientDimAtCompileTime; }
00068
00070 inline bool isNull() const { return (m_min.cwise() > m_max).any(); }
00071
00073 inline void setNull()
00074 {
00075 m_min.setConstant( std::numeric_limits<Scalar>::max());
00076 m_max.setConstant(-std::numeric_limits<Scalar>::max());
00077 }
00078
00080 inline const VectorType& min() const { return m_min; }
00082 inline VectorType& min() { return m_min; }
00084 inline const VectorType& max() const { return m_max; }
00086 inline VectorType& max() { return m_max; }
00087
00089 inline bool contains(const VectorType& p) const
00090 { return (m_min.cwise()<=p).all() && (p.cwise()<=m_max).all(); }
00091
00093 inline bool contains(const AlignedBox& b) const
00094 { return (m_min.cwise()<=b.min()).all() && (b.max().cwise()<=m_max).all(); }
00095
00097 inline AlignedBox& extend(const VectorType& p)
00098 { m_min = m_min.cwise().min(p); m_max = m_max.cwise().max(p); return *this; }
00099
00101 inline AlignedBox& extend(const AlignedBox& b)
00102 { m_min = m_min.cwise().min(b.m_min); m_max = m_max.cwise().max(b.m_max); return *this; }
00103
00105 inline AlignedBox& clamp(const AlignedBox& b)
00106 { m_min = m_min.cwise().max(b.m_min); m_max = m_max.cwise().min(b.m_max); return *this; }
00107
00109 inline AlignedBox& translate(const VectorType& t)
00110 { m_min += t; m_max += t; return *this; }
00111
00116 inline Scalar squaredExteriorDistance(const VectorType& p) const;
00117
00122 inline Scalar exteriorDistance(const VectorType& p) const
00123 { return ei_sqrt(squaredExteriorDistance(p)); }
00124
00130 template<typename NewScalarType>
00131 inline typename ei_cast_return_type<AlignedBox,
00132 AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type cast() const
00133 {
00134 return typename ei_cast_return_type<AlignedBox,
00135 AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type(*this);
00136 }
00137
00139 template<typename OtherScalarType>
00140 inline explicit AlignedBox(const AlignedBox<OtherScalarType,AmbientDimAtCompileTime>& other)
00141 {
00142 m_min = other.min().template cast<Scalar>();
00143 m_max = other.max().template cast<Scalar>();
00144 }
00145
00150 bool isApprox(const AlignedBox& other, typename NumTraits<Scalar>::Real prec = precision<Scalar>()) const
00151 { return m_min.isApprox(other.m_min, prec) && m_max.isApprox(other.m_max, prec); }
00152
00153 protected:
00154
00155 VectorType m_min, m_max;
00156 };
00157
00158 template<typename Scalar,int AmbiantDim>
00159 inline Scalar AlignedBox<Scalar,AmbiantDim>::squaredExteriorDistance(const VectorType& p) const
00160 {
00161 Scalar dist2 = 0.;
00162 Scalar aux;
00163 for (int k=0; k<dim(); ++k)
00164 {
00165 if ((aux = (p[k]-m_min[k]))<0.)
00166 dist2 += aux*aux;
00167 else if ( (aux = (m_max[k]-p[k]))<0. )
00168 dist2 += aux*aux;
00169 }
00170 return dist2;
00171 }
00172
00173 #endif // EIGEN_ALIGNEDBOX_H