Main MRPT website > C++ reference
MRPT logo

ParametrizedLine.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_PARAMETRIZEDLINE_H
00027 #define EIGEN_PARAMETRIZEDLINE_H
00028 
00029 /** \geometry_module \ingroup Geometry_Module
00030   *
00031   * \class ParametrizedLine
00032   *
00033   * \brief A parametrized line
00034   *
00035   * A parametrized line is defined by an origin point \f$ \mathbf{o} \f$ and a unit
00036   * direction vector \f$ \mathbf{d} \f$ such that the line corresponds to
00037   * the set \f$ l(t) = \mathbf{o} + t \mathbf{d} \f$, \f$ l \in \mathbf{R} \f$.
00038   *
00039   * \param _Scalar the scalar type, i.e., the type of the coefficients
00040   * \param _AmbientDim the dimension of the ambient space, can be a compile time value or Dynamic.
00041   */
00042 template <typename _Scalar, int _AmbientDim>
00043 class ParametrizedLine
00044 {
00045 public:
00046   EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim)
00047   enum { AmbientDimAtCompileTime = _AmbientDim };
00048   typedef _Scalar Scalar;
00049   typedef typename NumTraits<Scalar>::Real RealScalar;
00050   typedef DenseIndex Index;
00051   typedef Matrix<Scalar,AmbientDimAtCompileTime,1> VectorType;
00052 
00053   /** Default constructor without initialization */
00054   inline explicit ParametrizedLine() {}
00055 
00056   /** Constructs a dynamic-size line with \a _dim the dimension
00057     * of the ambient space */
00058   inline explicit ParametrizedLine(Index _dim) : m_origin(_dim), m_direction(_dim) {}
00059 
00060   /** Initializes a parametrized line of direction \a direction and origin \a origin.
00061     * \warning the vector direction is assumed to be normalized.
00062     */
00063   ParametrizedLine(const VectorType& origin, const VectorType& direction)
00064     : m_origin(origin), m_direction(direction) {}
00065 
00066   explicit ParametrizedLine(const Hyperplane<_Scalar, _AmbientDim>& hyperplane);
00067 
00068   /** Constructs a parametrized line going from \a p0 to \a p1. */
00069   static inline ParametrizedLine Through(const VectorType& p0, const VectorType& p1)
00070   { return ParametrizedLine(p0, (p1-p0).normalized()); }
00071 
00072   ~ParametrizedLine() {}
00073 
00074   /** \returns the dimension in which the line holds */
00075   inline Index dim() const { return m_direction.size(); }
00076 
00077   const VectorType& origin() const { return m_origin; }
00078   VectorType& origin() { return m_origin; }
00079 
00080   const VectorType& direction() const { return m_direction; }
00081   VectorType& direction() { return m_direction; }
00082 
00083   /** \returns the squared distance of a point \a p to its projection onto the line \c *this.
00084     * \sa distance()
00085     */
00086   RealScalar squaredDistance(const VectorType& p) const
00087   {
00088     VectorType diff = p - origin();
00089     return (diff - direction().dot(diff) * direction()).squaredNorm();
00090   }
00091   /** \returns the distance of a point \a p to its projection onto the line \c *this.
00092     * \sa squaredDistance()
00093     */
00094   RealScalar distance(const VectorType& p) const { return internal::sqrt(squaredDistance(p)); }
00095 
00096   /** \returns the projection of a point \a p onto the line \c *this. */
00097   VectorType projection(const VectorType& p) const
00098   { return origin() + direction().dot(p-origin()) * direction(); }
00099 
00100   Scalar intersection(const Hyperplane<_Scalar, _AmbientDim>& hyperplane);
00101 
00102   /** \returns \c *this with scalar type casted to \a NewScalarType
00103     *
00104     * Note that if \a NewScalarType is equal to the current scalar type of \c *this
00105     * then this function smartly returns a const reference to \c *this.
00106     */
00107   template<typename NewScalarType>
00108   inline typename internal::cast_return_type<ParametrizedLine,
00109            ParametrizedLine<NewScalarType,AmbientDimAtCompileTime> >::type cast() const
00110   {
00111     return typename internal::cast_return_type<ParametrizedLine,
00112                     ParametrizedLine<NewScalarType,AmbientDimAtCompileTime> >::type(*this);
00113   }
00114 
00115   /** Copy constructor with scalar type conversion */
00116   template<typename OtherScalarType>
00117   inline explicit ParametrizedLine(const ParametrizedLine<OtherScalarType,AmbientDimAtCompileTime>& other)
00118   {
00119     m_origin = other.origin().template cast<Scalar>();
00120     m_direction = other.direction().template cast<Scalar>();
00121   }
00122 
00123   /** \returns \c true if \c *this is approximately equal to \a other, within the precision
00124     * determined by \a prec.
00125     *
00126     * \sa MatrixBase::isApprox() */
00127   bool isApprox(const ParametrizedLine& other, typename NumTraits<Scalar>::Real prec = NumTraits<Scalar>::dummy_precision()) const
00128   { return m_origin.isApprox(other.m_origin, prec) && m_direction.isApprox(other.m_direction, prec); }
00129 
00130 protected:
00131 
00132   VectorType m_origin, m_direction;
00133 };
00134 
00135 /** Constructs a parametrized line from a 2D hyperplane
00136   *
00137   * \warning the ambient space must have dimension 2 such that the hyperplane actually describes a line
00138   */
00139 template <typename _Scalar, int _AmbientDim>
00140 inline ParametrizedLine<_Scalar, _AmbientDim>::ParametrizedLine(const Hyperplane<_Scalar, _AmbientDim>& hyperplane)
00141 {
00142   EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(VectorType, 2)
00143   direction() = hyperplane.normal().unitOrthogonal();
00144   origin() = -hyperplane.normal()*hyperplane.offset();
00145 }
00146 
00147 /** \returns the parameter value of the intersection between \c *this and the given hyperplane
00148   */
00149 template <typename _Scalar, int _AmbientDim>
00150 inline _Scalar ParametrizedLine<_Scalar, _AmbientDim>::intersection(const Hyperplane<_Scalar, _AmbientDim>& hyperplane)
00151 {
00152   return -(hyperplane.offset()+hyperplane.normal().dot(origin()))
00153           / hyperplane.normal().dot(direction());
00154 }
00155 
00156 #endif // EIGEN_PARAMETRIZEDLINE_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011