Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/eigen3/Eigen/src/Geometry/ParametrizedLine.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
0006 //
0007 // This Source Code Form is subject to the terms of the Mozilla
0008 // Public License v. 2.0. If a copy of the MPL was not distributed
0009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0010 
0011 #ifndef EIGEN_PARAMETRIZEDLINE_H
0012 #define EIGEN_PARAMETRIZEDLINE_H
0013 
0014 namespace Eigen { 
0015 
0016 /** \geometry_module \ingroup Geometry_Module
0017   *
0018   * \class ParametrizedLine
0019   *
0020   * \brief A parametrized line
0021   *
0022   * A parametrized line is defined by an origin point \f$ \mathbf{o} \f$ and a unit
0023   * direction vector \f$ \mathbf{d} \f$ such that the line corresponds to
0024   * the set \f$ l(t) = \mathbf{o} + t \mathbf{d} \f$, \f$ t \in \mathbf{R} \f$.
0025   *
0026   * \tparam _Scalar the scalar type, i.e., the type of the coefficients
0027   * \tparam _AmbientDim the dimension of the ambient space, can be a compile time value or Dynamic.
0028   */
0029 template <typename _Scalar, int _AmbientDim, int _Options>
0030 class ParametrizedLine
0031 {
0032 public:
0033   EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim)
0034   enum {
0035     AmbientDimAtCompileTime = _AmbientDim,
0036     Options = _Options
0037   };
0038   typedef _Scalar Scalar;
0039   typedef typename NumTraits<Scalar>::Real RealScalar;
0040   typedef Eigen::Index Index; ///< \deprecated since Eigen 3.3
0041   typedef Matrix<Scalar,AmbientDimAtCompileTime,1,Options> VectorType;
0042 
0043   /** Default constructor without initialization */
0044   EIGEN_DEVICE_FUNC inline ParametrizedLine() {}
0045   
0046   template<int OtherOptions>
0047   EIGEN_DEVICE_FUNC ParametrizedLine(const ParametrizedLine<Scalar,AmbientDimAtCompileTime,OtherOptions>& other)
0048    : m_origin(other.origin()), m_direction(other.direction())
0049   {}
0050 
0051   /** Constructs a dynamic-size line with \a _dim the dimension
0052     * of the ambient space */
0053   EIGEN_DEVICE_FUNC inline explicit ParametrizedLine(Index _dim) : m_origin(_dim), m_direction(_dim) {}
0054 
0055   /** Initializes a parametrized line of direction \a direction and origin \a origin.
0056     * \warning the vector direction is assumed to be normalized.
0057     */
0058   EIGEN_DEVICE_FUNC ParametrizedLine(const VectorType& origin, const VectorType& direction)
0059     : m_origin(origin), m_direction(direction) {}
0060 
0061   template <int OtherOptions>
0062   EIGEN_DEVICE_FUNC explicit ParametrizedLine(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane);
0063 
0064   /** Constructs a parametrized line going from \a p0 to \a p1. */
0065   EIGEN_DEVICE_FUNC static inline ParametrizedLine Through(const VectorType& p0, const VectorType& p1)
0066   { return ParametrizedLine(p0, (p1-p0).normalized()); }
0067 
0068   EIGEN_DEVICE_FUNC ~ParametrizedLine() {}
0069 
0070   /** \returns the dimension in which the line holds */
0071   EIGEN_DEVICE_FUNC inline Index dim() const { return m_direction.size(); }
0072 
0073   EIGEN_DEVICE_FUNC const VectorType& origin() const { return m_origin; }
0074   EIGEN_DEVICE_FUNC VectorType& origin() { return m_origin; }
0075 
0076   EIGEN_DEVICE_FUNC const VectorType& direction() const { return m_direction; }
0077   EIGEN_DEVICE_FUNC VectorType& direction() { return m_direction; }
0078 
0079   /** \returns the squared distance of a point \a p to its projection onto the line \c *this.
0080     * \sa distance()
0081     */
0082   EIGEN_DEVICE_FUNC RealScalar squaredDistance(const VectorType& p) const
0083   {
0084     VectorType diff = p - origin();
0085     return (diff - direction().dot(diff) * direction()).squaredNorm();
0086   }
0087   /** \returns the distance of a point \a p to its projection onto the line \c *this.
0088     * \sa squaredDistance()
0089     */
0090   EIGEN_DEVICE_FUNC RealScalar distance(const VectorType& p) const { EIGEN_USING_STD(sqrt) return sqrt(squaredDistance(p)); }
0091 
0092   /** \returns the projection of a point \a p onto the line \c *this. */
0093   EIGEN_DEVICE_FUNC VectorType projection(const VectorType& p) const
0094   { return origin() + direction().dot(p-origin()) * direction(); }
0095 
0096   EIGEN_DEVICE_FUNC VectorType pointAt(const Scalar& t) const;
0097   
0098   template <int OtherOptions>
0099   EIGEN_DEVICE_FUNC Scalar intersectionParameter(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const;
0100  
0101   template <int OtherOptions>
0102   EIGEN_DEVICE_FUNC Scalar intersection(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const;
0103   
0104   template <int OtherOptions>
0105   EIGEN_DEVICE_FUNC VectorType intersectionPoint(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const;
0106 
0107   /** Applies the transformation matrix \a mat to \c *this and returns a reference to \c *this.
0108     *
0109     * \param mat the Dim x Dim transformation matrix
0110     * \param traits specifies whether the matrix \a mat represents an #Isometry
0111     *               or a more generic #Affine transformation. The default is #Affine.
0112     */
0113   template<typename XprType>
0114   EIGEN_DEVICE_FUNC inline ParametrizedLine& transform(const MatrixBase<XprType>& mat, TransformTraits traits = Affine)
0115   {
0116     if (traits==Affine)
0117       direction() = (mat * direction()).normalized();
0118     else if (traits==Isometry)
0119       direction() = mat * direction();
0120     else
0121     {
0122       eigen_assert(0 && "invalid traits value in ParametrizedLine::transform()");
0123     }
0124     origin() = mat * origin();
0125     return *this;
0126   }
0127 
0128   /** Applies the transformation \a t to \c *this and returns a reference to \c *this.
0129     *
0130     * \param t the transformation of dimension Dim
0131     * \param traits specifies whether the transformation \a t represents an #Isometry
0132     *               or a more generic #Affine transformation. The default is #Affine.
0133     *               Other kind of transformations are not supported.
0134     */
0135   template<int TrOptions>
0136   EIGEN_DEVICE_FUNC inline ParametrizedLine& transform(const Transform<Scalar,AmbientDimAtCompileTime,Affine,TrOptions>& t,
0137                                                        TransformTraits traits = Affine)
0138   {
0139     transform(t.linear(), traits);
0140     origin() += t.translation();
0141     return *this;
0142   }
0143 
0144 /** \returns \c *this with scalar type casted to \a NewScalarType
0145     *
0146     * Note that if \a NewScalarType is equal to the current scalar type of \c *this
0147     * then this function smartly returns a const reference to \c *this.
0148     */
0149   template<typename NewScalarType>
0150   EIGEN_DEVICE_FUNC inline typename internal::cast_return_type<ParametrizedLine,
0151            ParametrizedLine<NewScalarType,AmbientDimAtCompileTime,Options> >::type cast() const
0152   {
0153     return typename internal::cast_return_type<ParametrizedLine,
0154                     ParametrizedLine<NewScalarType,AmbientDimAtCompileTime,Options> >::type(*this);
0155   }
0156 
0157   /** Copy constructor with scalar type conversion */
0158   template<typename OtherScalarType,int OtherOptions>
0159   EIGEN_DEVICE_FUNC inline explicit ParametrizedLine(const ParametrizedLine<OtherScalarType,AmbientDimAtCompileTime,OtherOptions>& other)
0160   {
0161     m_origin = other.origin().template cast<Scalar>();
0162     m_direction = other.direction().template cast<Scalar>();
0163   }
0164 
0165   /** \returns \c true if \c *this is approximately equal to \a other, within the precision
0166     * determined by \a prec.
0167     *
0168     * \sa MatrixBase::isApprox() */
0169   EIGEN_DEVICE_FUNC bool isApprox(const ParametrizedLine& other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const
0170   { return m_origin.isApprox(other.m_origin, prec) && m_direction.isApprox(other.m_direction, prec); }
0171 
0172 protected:
0173 
0174   VectorType m_origin, m_direction;
0175 };
0176 
0177 /** Constructs a parametrized line from a 2D hyperplane
0178   *
0179   * \warning the ambient space must have dimension 2 such that the hyperplane actually describes a line
0180   */
0181 template <typename _Scalar, int _AmbientDim, int _Options>
0182 template <int OtherOptions>
0183 EIGEN_DEVICE_FUNC inline ParametrizedLine<_Scalar, _AmbientDim,_Options>::ParametrizedLine(const Hyperplane<_Scalar, _AmbientDim,OtherOptions>& hyperplane)
0184 {
0185   EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(VectorType, 2)
0186   direction() = hyperplane.normal().unitOrthogonal();
0187   origin() = -hyperplane.normal()*hyperplane.offset();
0188 }
0189 
0190 /** \returns the point at \a t along this line
0191   */
0192 template <typename _Scalar, int _AmbientDim, int _Options>
0193 EIGEN_DEVICE_FUNC inline typename ParametrizedLine<_Scalar, _AmbientDim,_Options>::VectorType
0194 ParametrizedLine<_Scalar, _AmbientDim,_Options>::pointAt(const _Scalar& t) const
0195 {
0196   return origin() + (direction()*t); 
0197 }
0198 
0199 /** \returns the parameter value of the intersection between \c *this and the given \a hyperplane
0200   */
0201 template <typename _Scalar, int _AmbientDim, int _Options>
0202 template <int OtherOptions>
0203 EIGEN_DEVICE_FUNC inline _Scalar ParametrizedLine<_Scalar, _AmbientDim,_Options>::intersectionParameter(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const
0204 {
0205   return -(hyperplane.offset()+hyperplane.normal().dot(origin()))
0206           / hyperplane.normal().dot(direction());
0207 }
0208 
0209 
0210 /** \deprecated use intersectionParameter()
0211   * \returns the parameter value of the intersection between \c *this and the given \a hyperplane
0212   */
0213 template <typename _Scalar, int _AmbientDim, int _Options>
0214 template <int OtherOptions>
0215 EIGEN_DEVICE_FUNC inline _Scalar ParametrizedLine<_Scalar, _AmbientDim,_Options>::intersection(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const
0216 {
0217   return intersectionParameter(hyperplane);
0218 }
0219 
0220 /** \returns the point of the intersection between \c *this and the given hyperplane
0221   */
0222 template <typename _Scalar, int _AmbientDim, int _Options>
0223 template <int OtherOptions>
0224 EIGEN_DEVICE_FUNC inline typename ParametrizedLine<_Scalar, _AmbientDim,_Options>::VectorType
0225 ParametrizedLine<_Scalar, _AmbientDim,_Options>::intersectionPoint(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const
0226 {
0227   return pointAt(intersectionParameter(hyperplane));
0228 }
0229 
0230 } // end namespace Eigen
0231 
0232 #endif // EIGEN_PARAMETRIZEDLINE_H