diff options
-rw-r--r-- | Eigen/Geometry | 1 | ||||
-rw-r--r-- | Eigen/src/Geometry/AlignedBox.h | 179 | ||||
-rw-r--r-- | Eigen/src/Geometry/Hyperplane.h | 4 | ||||
-rw-r--r-- | Eigen/src/Geometry/Scaling.h | 2 | ||||
-rw-r--r-- | Eigen/src/Geometry/Translation.h | 2 | ||||
-rw-r--r-- | test/CMakeLists.txt | 1 | ||||
-rw-r--r-- | test/alignedbox.cpp | 75 |
7 files changed, 260 insertions, 4 deletions
diff --git a/Eigen/Geometry b/Eigen/Geometry index 2a2dfda2b..883c2d769 100644 --- a/Eigen/Geometry +++ b/Eigen/Geometry @@ -33,6 +33,7 @@ namespace Eigen { #include "src/Geometry/Scaling.h" #include "src/Geometry/Hyperplane.h" #include "src/Geometry/ParametrizedLine.h" +#include "src/Geometry/AlignedBox.h" } // namespace Eigen diff --git a/Eigen/src/Geometry/AlignedBox.h b/Eigen/src/Geometry/AlignedBox.h new file mode 100644 index 000000000..a81cc5474 --- /dev/null +++ b/Eigen/src/Geometry/AlignedBox.h @@ -0,0 +1,179 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#ifndef EIGEN_ALIGNEDBOX_H +#define EIGEN_ALIGNEDBOX_H + +/** \geometry_module \ingroup GeometryModule + * + * \class AlignedBox + * + * \brief A box aligned with a axis + * + * \param _Scalar the type of the scalar coefficients + * \param _AmbientDim the dimension of the ambient space, can be a compile time value or Dynamic. + * + * This class represents an axis aligned box as the pair of the minimal and maximal corners. + */ +template <typename _Scalar, int _AmbientDim> +class AlignedBox + #ifdef EIGEN_VECTORIZE + : public ei_with_aligned_operator_new<_Scalar,_AmbientDim==Dynamic ? Dynamic : _AmbientDim+1> + #endif +{ +public: + + enum { AmbientDimAtCompileTime = _AmbientDim }; + typedef _Scalar Scalar; + typedef typename NumTraits<Scalar>::Real RealScalar; + typedef Matrix<Scalar,AmbientDimAtCompileTime,1> VectorType; + + /** Default constructor initializing a null box. */ + inline explicit AlignedBox() + { if (AmbientDimAtCompileTime!=Dynamic) setNull(); } + + /** Constructs a null box with \a _dim the dimension of the ambient space. */ + inline explicit AlignedBox(int _dim) : m_min(_dim), m_max(_dim) + { setNull(); } + + /** Constructs a box with extremities \a _min and \a _max. */ + inline AlignedBox(const VectorType& _min, const VectorType _max) : m_min(_min), m_max(_max) {} + + /** Constructs a box containing a single point \a p. */ + inline explicit AlignedBox(const VectorType& p) : m_min(p), m_max(p) {} + + ~AlignedBox() {} + + /** \returns the dimension in which the box holds */ + inline int dim() const { return AmbientDimAtCompileTime==Dynamic ? m_min.size()-1 : AmbientDimAtCompileTime; } + + /** \returns true if the box is null, i.e, empty. */ + inline bool isNull() const { return (m_min.cwise() > m_max).any(); } + + /** Makes \c *this a null/empty box. */ + inline void setNull() + { + m_min.setConstant( std::numeric_limits<Scalar>::max()); + m_max.setConstant(-std::numeric_limits<Scalar>::max()); + } + + /** \returns the minimal corner */ + inline const VectorType& min() const { return m_min; } + /** \returns a non const reference to the minimal corner */ + inline VectorType& min() { return m_min; } + /** \returns the maximal corner */ + inline const VectorType& max() const { return m_max; } + /** \returns a non const reference to the maximal corner */ + inline VectorType& max() { return m_max; } + + /** \returns true if the point \a p is inside the box \c *this. */ + inline bool contains(const VectorType& p) const + { return (m_min.cwise()<=p).all() && (p.cwise()<=m_max).all(); } + + /** \returns true if the box \a b is entirely inside the box \c *this. */ + inline bool contains(const AlignedBox& b) const + { return (m_min.cwise()<=b.min()).all() && (b.max().cwise()<=m_max).all(); } + + /** Extends \c *this such that it contains the point \a p and returns a reference to \c *this. */ + inline AlignedBox& extend(const VectorType& p) + { m_min = m_min.cwise().min(p); m_max = m_max.cwise().max(p); return *this; } + + /** Extends \c *this such that it contains the box \a b and returns a reference to \c *this. */ + inline AlignedBox& extend(const AlignedBox& b) + { m_min = m_min.cwise().min(b.m_min); m_max = m_max.cwise().max(b.m_max); return *this; } + + /** Clamps \c *this by the box \a b and returns a reference to \c *this. */ + inline AlignedBox& clamp(const AlignedBox& b) + { m_min = m_min.cwise().max(b.m_min); m_max = m_max.cwise().min(b.m_max); return *this; } + + /** \returns the squared distance between the point \a p and the box \c *this, + * and zero if \a p is inside the box. + * \sa exteriorDistance() + */ + inline Scalar squaredExteriorDistance(const VectorType& p) const; + + /** \returns the distance between the point \a p and the box \c *this, + * and zero if \a p is inside the box. + * \sa squaredExteriorDistance() + */ + inline Scalar exteriorDistance(const VectorType& p) const + { return ei_sqrt(squaredExteriorDistance(p)); } + + /** Translate \c *this by the vector \a t and returns a reference to \c *this. */ + inline AlignedBox& translate(const VectorType& t) + { + m_min += t; + m_max += t; + return *this; + } + + /** \returns \c *this with scalar type casted to \a NewScalarType + * + * Note that if \a NewScalarType is equal to the current scalar type of \c *this + * then this function smartly returns a const reference to \c *this. + */ + template<typename NewScalarType> + inline typename ei_cast_return_type<AlignedBox, + AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type cast() const + { + return typename ei_cast_return_type<AlignedBox, + AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type(*this); + } + + /** Copy constructor with scalar type conversion */ + template<typename OtherScalarType> + inline explicit AlignedBox(const AlignedBox<OtherScalarType,AmbientDimAtCompileTime>& other) + { + m_min = other.min().template cast<OtherScalarType>(); + m_max = other.max().template cast<OtherScalarType>(); + } + + /** \returns \c true if \c *this is approximately equal to \a other, within the precision + * determined by \a prec. + * + * \sa MatrixBase::isApprox() */ + bool isApprox(const AlignedBox& other, typename NumTraits<Scalar>::Real prec = precision<Scalar>()) const + { return m_min.isApprox(other.m_min, prec) && m_max.isApprox(other.m_max, prec); } + +protected: + + VectorType m_min, m_max; +}; + +template<typename Scalar,int AmbiantDim> +inline Scalar AlignedBox<Scalar,AmbiantDim>::squaredExteriorDistance(const VectorType& p) const +{ + Scalar dist2 = 0.; + Scalar aux; + for (int k=0; k<dim(); ++k) + { + if ((aux = (p[k]-m_min[k]))<0.) + dist2 += aux*aux; + else if ( (aux = (m_max[k]-p[k]))<0. ) + dist2 += aux*aux; + } + return dist2; +} + +#endif // EIGEN_ALIGNEDBOX_H diff --git a/Eigen/src/Geometry/Hyperplane.h b/Eigen/src/Geometry/Hyperplane.h index a45268f07..4edbcece2 100644 --- a/Eigen/src/Geometry/Hyperplane.h +++ b/Eigen/src/Geometry/Hyperplane.h @@ -203,7 +203,7 @@ public: } } - /** \returns the transformation of \c *this by the transformation matrix \a mat. + /** Applies the transformation matrix \a mat to \c *this and returns a reference to \c *this. * * \param mat the Dim x Dim transformation matrix * \param traits specifies whether the matrix \a mat represents an Isometry @@ -223,7 +223,7 @@ public: return *this; } - /** \returns the transformation of \c *this by the transformation \a t + /** Applies the transformation \a t to \c *this and returns a reference to \c *this. * * \param t the transformation of dimension Dim * \param traits specifies whether the transformation \a t represents an Isometry diff --git a/Eigen/src/Geometry/Scaling.h b/Eigen/src/Geometry/Scaling.h index 8e98bd58a..54b01d8a0 100644 --- a/Eigen/src/Geometry/Scaling.h +++ b/Eigen/src/Geometry/Scaling.h @@ -35,7 +35,7 @@ * \param _Dim the dimension of the space, can be a compile time value or Dynamic * * \note This class is not aimed to be used to store a scaling transformation, - * but rather to make easier the constructions and updates of Transformation object. + * but rather to make easier the constructions and updates of Transform objects. * * \sa class Translation, class Transform */ diff --git a/Eigen/src/Geometry/Translation.h b/Eigen/src/Geometry/Translation.h index 8dce5dd4c..eec829690 100644 --- a/Eigen/src/Geometry/Translation.h +++ b/Eigen/src/Geometry/Translation.h @@ -35,7 +35,7 @@ * \param _Dim the dimension of the space, can be a compile time value or Dynamic * * \note This class is not aimed to be used to store a translation transformation, - * but rather to make easier the constructions and updates of Transformation object. + * but rather to make easier the constructions and updates of Transform objects. * * \sa class Scaling, class Transform */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 39729fe7e..524c3e1aa 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -166,6 +166,7 @@ ei_add_test(svd) ei_add_test(geometry) ei_add_test(hyperplane) ei_add_test(parametrizedline) +ei_add_test(alignedbox) ei_add_test(regression) ei_add_test(sparse ) diff --git a/test/alignedbox.cpp b/test/alignedbox.cpp new file mode 100644 index 000000000..0d295021e --- /dev/null +++ b/test/alignedbox.cpp @@ -0,0 +1,75 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#include "main.h" +#include <Eigen/Geometry> +#include <Eigen/LU> +#include <Eigen/QR> + +template<typename BoxType> void alignedbox(const BoxType& _box) +{ + /* this test covers the following files: + AlignedBox.h + */ + + const int dim = _box.dim(); + typedef typename BoxType::Scalar Scalar; + typedef typename NumTraits<Scalar>::Real RealScalar; + typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType; + + VectorType p0 = VectorType::Random(dim); + VectorType p1 = VectorType::Random(dim); + RealScalar s1 = ei_random<RealScalar>(0,1); + + BoxType b0(dim); + BoxType b1(VectorType::Random(dim),VectorType::Random(dim)); + BoxType b2; + + b0.extend(p0); + b0.extend(p1); + VERIFY(b0.contains(p0*s1+(1.-s1)*p1)); + VERIFY(!b0.contains(p0 + (1+s1)*(p1-p0))); + + (b2 = b0).extend(b1); + VERIFY(b2.contains(b0)); + VERIFY(b2.contains(b1)); + VERIFY_IS_APPROX(b2.clamp(b0), b0); + + // casting + const int Dim = BoxType::AmbientDimAtCompileTime; + typedef typename GetDifferentType<Scalar>::type OtherScalar; + AlignedBox<OtherScalar,Dim> hp1f = b0.template cast<OtherScalar>(); + VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),b0); + AlignedBox<Scalar,Dim> hp1d = b0.template cast<Scalar>(); + VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),b0); +} + +void test_alignedbox() +{ + for(int i = 0; i < g_repeat; i++) { + CALL_SUBTEST( alignedbox(AlignedBox<float,2>()) ); + CALL_SUBTEST( alignedbox(AlignedBox<float,3>()) ); + CALL_SUBTEST( alignedbox(AlignedBox<double,4>()) ); + } +} |