Main MRPT website > C++ reference
MRPT logo

Hyperplane.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 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_HYPERPLANE_H
00027 #define EIGEN_HYPERPLANE_H
00028 
00029 /** \geometry_module \ingroup Geometry_Module
00030   *
00031   * \class Hyperplane
00032   *
00033   * \brief A hyperplane
00034   *
00035   * A hyperplane is an affine subspace of dimension n-1 in a space of dimension n.
00036   * For example, a hyperplane in a plane is a line; a hyperplane in 3-space is a plane.
00037   *
00038   * \param _Scalar the scalar type, i.e., the type of the coefficients
00039   * \param _AmbientDim the dimension of the ambient space, can be a compile time value or Dynamic.
00040   *             Notice that the dimension of the hyperplane is _AmbientDim-1.
00041   *
00042   * This class represents an hyperplane as the zero set of the implicit equation
00043   * \f$ n \cdot x + d = 0 \f$ where \f$ n \f$ is a unit normal vector of the plane (linear part)
00044   * and \f$ d \f$ is the distance (offset) to the origin.
00045   */
00046 template <typename _Scalar, int _AmbientDim>
00047 class Hyperplane
00048 {
00049 public:
00050   EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim==Dynamic ? Dynamic : _AmbientDim+1)
00051   enum { AmbientDimAtCompileTime = _AmbientDim };
00052   typedef _Scalar Scalar;
00053   typedef typename NumTraits<Scalar>::Real RealScalar;
00054   typedef DenseIndex Index;
00055   typedef Matrix<Scalar,AmbientDimAtCompileTime,1> VectorType;
00056   typedef Matrix<Scalar,Index(AmbientDimAtCompileTime)==Dynamic
00057                         ? Dynamic
00058                         : Index(AmbientDimAtCompileTime)+1,1> Coefficients;
00059   typedef Block<Coefficients,AmbientDimAtCompileTime,1> NormalReturnType;
00060   typedef const Block<const Coefficients,AmbientDimAtCompileTime,1> ConstNormalReturnType;
00061 
00062   /** Default constructor without initialization */
00063   inline explicit Hyperplane() {}
00064 
00065   /** Constructs a dynamic-size hyperplane with \a _dim the dimension
00066     * of the ambient space */
00067   inline explicit Hyperplane(Index _dim) : m_coeffs(_dim+1) {}
00068 
00069   /** Construct a plane from its normal \a n and a point \a e onto the plane.
00070     * \warning the vector normal is assumed to be normalized.
00071     */
00072   inline Hyperplane(const VectorType& n, const VectorType& e)
00073     : m_coeffs(n.size()+1)
00074   {
00075     normal() = n;
00076     offset() = -n.dot(e);
00077   }
00078 
00079   /** Constructs a plane from its normal \a n and distance to the origin \a d
00080     * such that the algebraic equation of the plane is \f$ n \cdot x + d = 0 \f$.
00081     * \warning the vector normal is assumed to be normalized.
00082     */
00083   inline Hyperplane(const VectorType& n, Scalar d)
00084     : m_coeffs(n.size()+1)
00085   {
00086     normal() = n;
00087     offset() = d;
00088   }
00089 
00090   /** Constructs a hyperplane passing through the two points. If the dimension of the ambient space
00091     * is greater than 2, then there isn't uniqueness, so an arbitrary choice is made.
00092     */
00093   static inline Hyperplane Through(const VectorType& p0, const VectorType& p1)
00094   {
00095     Hyperplane result(p0.size());
00096     result.normal() = (p1 - p0).unitOrthogonal();
00097     result.offset() = -p0.dot(result.normal());
00098     return result;
00099   }
00100 
00101   /** Constructs a hyperplane passing through the three points. The dimension of the ambient space
00102     * is required to be exactly 3.
00103     */
00104   static inline Hyperplane Through(const VectorType& p0, const VectorType& p1, const VectorType& p2)
00105   {
00106     EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(VectorType, 3)
00107     Hyperplane result(p0.size());
00108     result.normal() = (p2 - p0).cross(p1 - p0).normalized();
00109     result.offset() = -p0.dot(result.normal());
00110     return result;
00111   }
00112 
00113   /** Constructs a hyperplane passing through the parametrized line \a parametrized.
00114     * If the dimension of the ambient space is greater than 2, then there isn't uniqueness,
00115     * so an arbitrary choice is made.
00116     */
00117   // FIXME to be consitent with the rest this could be implemented as a static Through function ??
00118   explicit Hyperplane(const ParametrizedLine<Scalar, AmbientDimAtCompileTime>& parametrized)
00119   {
00120     normal() = parametrized.direction().unitOrthogonal();
00121     offset() = -parametrized.origin().dot(normal());
00122   }
00123 
00124   ~Hyperplane() {}
00125 
00126   /** \returns the dimension in which the plane holds */
00127   inline Index dim() const { return AmbientDimAtCompileTime==Dynamic ? m_coeffs.size()-1 : Index(AmbientDimAtCompileTime); }
00128 
00129   /** normalizes \c *this */
00130   void normalize(void)
00131   {
00132     m_coeffs /= normal().norm();
00133   }
00134 
00135   /** \returns the signed distance between the plane \c *this and a point \a p.
00136     * \sa absDistance()
00137     */
00138   inline Scalar signedDistance(const VectorType& p) const { return normal().dot(p) + offset(); }
00139 
00140   /** \returns the absolute distance between the plane \c *this and a point \a p.
00141     * \sa signedDistance()
00142     */
00143   inline Scalar absDistance(const VectorType& p) const { return internal::abs(signedDistance(p)); }
00144 
00145   /** \returns the projection of a point \a p onto the plane \c *this.
00146     */
00147   inline VectorType projection(const VectorType& p) const { return p - signedDistance(p) * normal(); }
00148 
00149   /** \returns a constant reference to the unit normal vector of the plane, which corresponds
00150     * to the linear part of the implicit equation.
00151     */
00152   inline ConstNormalReturnType normal() const { return ConstNormalReturnType(m_coeffs,0,0,dim(),1); }
00153 
00154   /** \returns a non-constant reference to the unit normal vector of the plane, which corresponds
00155     * to the linear part of the implicit equation.
00156     */
00157   inline NormalReturnType normal() { return NormalReturnType(m_coeffs,0,0,dim(),1); }
00158 
00159   /** \returns the distance to the origin, which is also the "constant term" of the implicit equation
00160     * \warning the vector normal is assumed to be normalized.
00161     */
00162   inline const Scalar& offset() const { return m_coeffs.coeff(dim()); }
00163 
00164   /** \returns a non-constant reference to the distance to the origin, which is also the constant part
00165     * of the implicit equation */
00166   inline Scalar& offset() { return m_coeffs(dim()); }
00167 
00168   /** \returns a constant reference to the coefficients c_i of the plane equation:
00169     * \f$ c_0*x_0 + ... + c_{d-1}*x_{d-1} + c_d = 0 \f$
00170     */
00171   inline const Coefficients& coeffs() const { return m_coeffs; }
00172 
00173   /** \returns a non-constant reference to the coefficients c_i of the plane equation:
00174     * \f$ c_0*x_0 + ... + c_{d-1}*x_{d-1} + c_d = 0 \f$
00175     */
00176   inline Coefficients& coeffs() { return m_coeffs; }
00177 
00178   /** \returns the intersection of *this with \a other.
00179     *
00180     * \warning The ambient space must be a plane, i.e. have dimension 2, so that \c *this and \a other are lines.
00181     *
00182     * \note If \a other is approximately parallel to *this, this method will return any point on *this.
00183     */
00184   VectorType intersection(const Hyperplane& other)
00185   {
00186     EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(VectorType, 2)
00187     Scalar det = coeffs().coeff(0) * other.coeffs().coeff(1) - coeffs().coeff(1) * other.coeffs().coeff(0);
00188     // since the line equations ax+by=c are normalized with a^2+b^2=1, the following tests
00189     // whether the two lines are approximately parallel.
00190     if(internal::isMuchSmallerThan(det, Scalar(1)))
00191     {   // special case where the two lines are approximately parallel. Pick any point on the first line.
00192         if(internal::abs(coeffs().coeff(1))>internal::abs(coeffs().coeff(0)))
00193             return VectorType(coeffs().coeff(1), -coeffs().coeff(2)/coeffs().coeff(1)-coeffs().coeff(0));
00194         else
00195             return VectorType(-coeffs().coeff(2)/coeffs().coeff(0)-coeffs().coeff(1), coeffs().coeff(0));
00196     }
00197     else
00198     {   // general case
00199         Scalar invdet = Scalar(1) / det;
00200         return VectorType(invdet*(coeffs().coeff(1)*other.coeffs().coeff(2)-other.coeffs().coeff(1)*coeffs().coeff(2)),
00201                           invdet*(other.coeffs().coeff(0)*coeffs().coeff(2)-coeffs().coeff(0)*other.coeffs().coeff(2)));
00202     }
00203   }
00204 
00205   /** Applies the transformation matrix \a mat to \c *this and returns a reference to \c *this.
00206     *
00207     * \param mat the Dim x Dim transformation matrix
00208     * \param traits specifies whether the matrix \a mat represents an Isometry
00209     *               or a more generic Affine transformation. The default is Affine.
00210     */
00211   template<typename XprType>
00212   inline Hyperplane& transform(const MatrixBase<XprType>& mat, TransformTraits traits = Affine)
00213   {
00214     if (traits==Affine)
00215       normal() = mat.inverse().transpose() * normal();
00216     else if (traits==Isometry)
00217       normal() = mat * normal();
00218     else
00219     {
00220       eigen_assert("invalid traits value in Hyperplane::transform()");
00221     }
00222     return *this;
00223   }
00224 
00225   /** Applies the transformation \a t to \c *this and returns a reference to \c *this.
00226     *
00227     * \param t the transformation of dimension Dim
00228     * \param traits specifies whether the transformation \a t represents an Isometry
00229     *               or a more generic Affine transformation. The default is Affine.
00230     *               Other kind of transformations are not supported.
00231     */
00232   inline Hyperplane& transform(const Transform<Scalar,AmbientDimAtCompileTime,Affine>& t,
00233                                 TransformTraits traits = Affine)
00234   {
00235     transform(t.linear(), traits);
00236     offset() -= normal().dot(t.translation());
00237     return *this;
00238   }
00239 
00240   /** \returns \c *this with scalar type casted to \a NewScalarType
00241     *
00242     * Note that if \a NewScalarType is equal to the current scalar type of \c *this
00243     * then this function smartly returns a const reference to \c *this.
00244     */
00245   template<typename NewScalarType>
00246   inline typename internal::cast_return_type<Hyperplane,
00247            Hyperplane<NewScalarType,AmbientDimAtCompileTime> >::type cast() const
00248   {
00249     return typename internal::cast_return_type<Hyperplane,
00250                     Hyperplane<NewScalarType,AmbientDimAtCompileTime> >::type(*this);
00251   }
00252 
00253   /** Copy constructor with scalar type conversion */
00254   template<typename OtherScalarType>
00255   inline explicit Hyperplane(const Hyperplane<OtherScalarType,AmbientDimAtCompileTime>& other)
00256   { m_coeffs = other.coeffs().template cast<Scalar>(); }
00257 
00258   /** \returns \c true if \c *this is approximately equal to \a other, within the precision
00259     * determined by \a prec.
00260     *
00261     * \sa MatrixBase::isApprox() */
00262   bool isApprox(const Hyperplane& other, typename NumTraits<Scalar>::Real prec = NumTraits<Scalar>::dummy_precision()) const
00263   { return m_coeffs.isApprox(other.m_coeffs, prec); }
00264 
00265 protected:
00266 
00267   Coefficients m_coeffs;
00268 };
00269 
00270 #endif // EIGEN_HYPERPLANE_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011