Michael Ernst Peter / Eigen
Committer:
ykuroda
Date:
Thu Oct 13 04:07:23 2016 +0000
Revision:
0:13a5d365ba16
First commint, Eigne Matrix Class Library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ykuroda 0:13a5d365ba16 1 // This file is part of Eigen, a lightweight C++ template library
ykuroda 0:13a5d365ba16 2 // for linear algebra.
ykuroda 0:13a5d365ba16 3 //
ykuroda 0:13a5d365ba16 4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
ykuroda 0:13a5d365ba16 5 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
ykuroda 0:13a5d365ba16 6 //
ykuroda 0:13a5d365ba16 7 // This Source Code Form is subject to the terms of the Mozilla
ykuroda 0:13a5d365ba16 8 // Public License v. 2.0. If a copy of the MPL was not distributed
ykuroda 0:13a5d365ba16 9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
ykuroda 0:13a5d365ba16 10
ykuroda 0:13a5d365ba16 11 #ifndef EIGEN_PARAMETRIZEDLINE_H
ykuroda 0:13a5d365ba16 12 #define EIGEN_PARAMETRIZEDLINE_H
ykuroda 0:13a5d365ba16 13
ykuroda 0:13a5d365ba16 14 namespace Eigen {
ykuroda 0:13a5d365ba16 15
ykuroda 0:13a5d365ba16 16 /** \geometry_module \ingroup Geometry_Module
ykuroda 0:13a5d365ba16 17 *
ykuroda 0:13a5d365ba16 18 * \class ParametrizedLine
ykuroda 0:13a5d365ba16 19 *
ykuroda 0:13a5d365ba16 20 * \brief A parametrized line
ykuroda 0:13a5d365ba16 21 *
ykuroda 0:13a5d365ba16 22 * A parametrized line is defined by an origin point \f$ \mathbf{o} \f$ and a unit
ykuroda 0:13a5d365ba16 23 * direction vector \f$ \mathbf{d} \f$ such that the line corresponds to
ykuroda 0:13a5d365ba16 24 * the set \f$ l(t) = \mathbf{o} + t \mathbf{d} \f$, \f$ t \in \mathbf{R} \f$.
ykuroda 0:13a5d365ba16 25 *
ykuroda 0:13a5d365ba16 26 * \param _Scalar the scalar type, i.e., the type of the coefficients
ykuroda 0:13a5d365ba16 27 * \param _AmbientDim the dimension of the ambient space, can be a compile time value or Dynamic.
ykuroda 0:13a5d365ba16 28 */
ykuroda 0:13a5d365ba16 29 template <typename _Scalar, int _AmbientDim, int _Options>
ykuroda 0:13a5d365ba16 30 class ParametrizedLine
ykuroda 0:13a5d365ba16 31 {
ykuroda 0:13a5d365ba16 32 public:
ykuroda 0:13a5d365ba16 33 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim)
ykuroda 0:13a5d365ba16 34 enum {
ykuroda 0:13a5d365ba16 35 AmbientDimAtCompileTime = _AmbientDim,
ykuroda 0:13a5d365ba16 36 Options = _Options
ykuroda 0:13a5d365ba16 37 };
ykuroda 0:13a5d365ba16 38 typedef _Scalar Scalar;
ykuroda 0:13a5d365ba16 39 typedef typename NumTraits<Scalar>::Real RealScalar;
ykuroda 0:13a5d365ba16 40 typedef DenseIndex Index;
ykuroda 0:13a5d365ba16 41 typedef Matrix<Scalar,AmbientDimAtCompileTime,1,Options> VectorType;
ykuroda 0:13a5d365ba16 42
ykuroda 0:13a5d365ba16 43 /** Default constructor without initialization */
ykuroda 0:13a5d365ba16 44 inline ParametrizedLine() {}
ykuroda 0:13a5d365ba16 45
ykuroda 0:13a5d365ba16 46 template<int OtherOptions>
ykuroda 0:13a5d365ba16 47 ParametrizedLine(const ParametrizedLine<Scalar,AmbientDimAtCompileTime,OtherOptions>& other)
ykuroda 0:13a5d365ba16 48 : m_origin(other.origin()), m_direction(other.direction())
ykuroda 0:13a5d365ba16 49 {}
ykuroda 0:13a5d365ba16 50
ykuroda 0:13a5d365ba16 51 /** Constructs a dynamic-size line with \a _dim the dimension
ykuroda 0:13a5d365ba16 52 * of the ambient space */
ykuroda 0:13a5d365ba16 53 inline explicit ParametrizedLine(Index _dim) : m_origin(_dim), m_direction(_dim) {}
ykuroda 0:13a5d365ba16 54
ykuroda 0:13a5d365ba16 55 /** Initializes a parametrized line of direction \a direction and origin \a origin.
ykuroda 0:13a5d365ba16 56 * \warning the vector direction is assumed to be normalized.
ykuroda 0:13a5d365ba16 57 */
ykuroda 0:13a5d365ba16 58 ParametrizedLine(const VectorType& origin, const VectorType& direction)
ykuroda 0:13a5d365ba16 59 : m_origin(origin), m_direction(direction) {}
ykuroda 0:13a5d365ba16 60
ykuroda 0:13a5d365ba16 61 template <int OtherOptions>
ykuroda 0:13a5d365ba16 62 explicit ParametrizedLine(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane);
ykuroda 0:13a5d365ba16 63
ykuroda 0:13a5d365ba16 64 /** Constructs a parametrized line going from \a p0 to \a p1. */
ykuroda 0:13a5d365ba16 65 static inline ParametrizedLine Through(const VectorType& p0, const VectorType& p1)
ykuroda 0:13a5d365ba16 66 { return ParametrizedLine(p0, (p1-p0).normalized()); }
ykuroda 0:13a5d365ba16 67
ykuroda 0:13a5d365ba16 68 ~ParametrizedLine() {}
ykuroda 0:13a5d365ba16 69
ykuroda 0:13a5d365ba16 70 /** \returns the dimension in which the line holds */
ykuroda 0:13a5d365ba16 71 inline Index dim() const { return m_direction.size(); }
ykuroda 0:13a5d365ba16 72
ykuroda 0:13a5d365ba16 73 const VectorType& origin() const { return m_origin; }
ykuroda 0:13a5d365ba16 74 VectorType& origin() { return m_origin; }
ykuroda 0:13a5d365ba16 75
ykuroda 0:13a5d365ba16 76 const VectorType& direction() const { return m_direction; }
ykuroda 0:13a5d365ba16 77 VectorType& direction() { return m_direction; }
ykuroda 0:13a5d365ba16 78
ykuroda 0:13a5d365ba16 79 /** \returns the squared distance of a point \a p to its projection onto the line \c *this.
ykuroda 0:13a5d365ba16 80 * \sa distance()
ykuroda 0:13a5d365ba16 81 */
ykuroda 0:13a5d365ba16 82 RealScalar squaredDistance(const VectorType& p) const
ykuroda 0:13a5d365ba16 83 {
ykuroda 0:13a5d365ba16 84 VectorType diff = p - origin();
ykuroda 0:13a5d365ba16 85 return (diff - direction().dot(diff) * direction()).squaredNorm();
ykuroda 0:13a5d365ba16 86 }
ykuroda 0:13a5d365ba16 87 /** \returns the distance of a point \a p to its projection onto the line \c *this.
ykuroda 0:13a5d365ba16 88 * \sa squaredDistance()
ykuroda 0:13a5d365ba16 89 */
ykuroda 0:13a5d365ba16 90 RealScalar distance(const VectorType& p) const { using std::sqrt; return sqrt(squaredDistance(p)); }
ykuroda 0:13a5d365ba16 91
ykuroda 0:13a5d365ba16 92 /** \returns the projection of a point \a p onto the line \c *this. */
ykuroda 0:13a5d365ba16 93 VectorType projection(const VectorType& p) const
ykuroda 0:13a5d365ba16 94 { return origin() + direction().dot(p-origin()) * direction(); }
ykuroda 0:13a5d365ba16 95
ykuroda 0:13a5d365ba16 96 VectorType pointAt(const Scalar& t) const;
ykuroda 0:13a5d365ba16 97
ykuroda 0:13a5d365ba16 98 template <int OtherOptions>
ykuroda 0:13a5d365ba16 99 Scalar intersectionParameter(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const;
ykuroda 0:13a5d365ba16 100
ykuroda 0:13a5d365ba16 101 template <int OtherOptions>
ykuroda 0:13a5d365ba16 102 Scalar intersection(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const;
ykuroda 0:13a5d365ba16 103
ykuroda 0:13a5d365ba16 104 template <int OtherOptions>
ykuroda 0:13a5d365ba16 105 VectorType intersectionPoint(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const;
ykuroda 0:13a5d365ba16 106
ykuroda 0:13a5d365ba16 107 /** \returns \c *this with scalar type casted to \a NewScalarType
ykuroda 0:13a5d365ba16 108 *
ykuroda 0:13a5d365ba16 109 * Note that if \a NewScalarType is equal to the current scalar type of \c *this
ykuroda 0:13a5d365ba16 110 * then this function smartly returns a const reference to \c *this.
ykuroda 0:13a5d365ba16 111 */
ykuroda 0:13a5d365ba16 112 template<typename NewScalarType>
ykuroda 0:13a5d365ba16 113 inline typename internal::cast_return_type<ParametrizedLine,
ykuroda 0:13a5d365ba16 114 ParametrizedLine<NewScalarType,AmbientDimAtCompileTime,Options> >::type cast() const
ykuroda 0:13a5d365ba16 115 {
ykuroda 0:13a5d365ba16 116 return typename internal::cast_return_type<ParametrizedLine,
ykuroda 0:13a5d365ba16 117 ParametrizedLine<NewScalarType,AmbientDimAtCompileTime,Options> >::type(*this);
ykuroda 0:13a5d365ba16 118 }
ykuroda 0:13a5d365ba16 119
ykuroda 0:13a5d365ba16 120 /** Copy constructor with scalar type conversion */
ykuroda 0:13a5d365ba16 121 template<typename OtherScalarType,int OtherOptions>
ykuroda 0:13a5d365ba16 122 inline explicit ParametrizedLine(const ParametrizedLine<OtherScalarType,AmbientDimAtCompileTime,OtherOptions>& other)
ykuroda 0:13a5d365ba16 123 {
ykuroda 0:13a5d365ba16 124 m_origin = other.origin().template cast<Scalar>();
ykuroda 0:13a5d365ba16 125 m_direction = other.direction().template cast<Scalar>();
ykuroda 0:13a5d365ba16 126 }
ykuroda 0:13a5d365ba16 127
ykuroda 0:13a5d365ba16 128 /** \returns \c true if \c *this is approximately equal to \a other, within the precision
ykuroda 0:13a5d365ba16 129 * determined by \a prec.
ykuroda 0:13a5d365ba16 130 *
ykuroda 0:13a5d365ba16 131 * \sa MatrixBase::isApprox() */
ykuroda 0:13a5d365ba16 132 bool isApprox(const ParametrizedLine& other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const
ykuroda 0:13a5d365ba16 133 { return m_origin.isApprox(other.m_origin, prec) && m_direction.isApprox(other.m_direction, prec); }
ykuroda 0:13a5d365ba16 134
ykuroda 0:13a5d365ba16 135 protected:
ykuroda 0:13a5d365ba16 136
ykuroda 0:13a5d365ba16 137 VectorType m_origin, m_direction;
ykuroda 0:13a5d365ba16 138 };
ykuroda 0:13a5d365ba16 139
ykuroda 0:13a5d365ba16 140 /** Constructs a parametrized line from a 2D hyperplane
ykuroda 0:13a5d365ba16 141 *
ykuroda 0:13a5d365ba16 142 * \warning the ambient space must have dimension 2 such that the hyperplane actually describes a line
ykuroda 0:13a5d365ba16 143 */
ykuroda 0:13a5d365ba16 144 template <typename _Scalar, int _AmbientDim, int _Options>
ykuroda 0:13a5d365ba16 145 template <int OtherOptions>
ykuroda 0:13a5d365ba16 146 inline ParametrizedLine<_Scalar, _AmbientDim,_Options>::ParametrizedLine(const Hyperplane<_Scalar, _AmbientDim,OtherOptions>& hyperplane)
ykuroda 0:13a5d365ba16 147 {
ykuroda 0:13a5d365ba16 148 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(VectorType, 2)
ykuroda 0:13a5d365ba16 149 direction() = hyperplane.normal().unitOrthogonal();
ykuroda 0:13a5d365ba16 150 origin() = -hyperplane.normal()*hyperplane.offset();
ykuroda 0:13a5d365ba16 151 }
ykuroda 0:13a5d365ba16 152
ykuroda 0:13a5d365ba16 153 /** \returns the point at \a t along this line
ykuroda 0:13a5d365ba16 154 */
ykuroda 0:13a5d365ba16 155 template <typename _Scalar, int _AmbientDim, int _Options>
ykuroda 0:13a5d365ba16 156 inline typename ParametrizedLine<_Scalar, _AmbientDim,_Options>::VectorType
ykuroda 0:13a5d365ba16 157 ParametrizedLine<_Scalar, _AmbientDim,_Options>::pointAt(const _Scalar& t) const
ykuroda 0:13a5d365ba16 158 {
ykuroda 0:13a5d365ba16 159 return origin() + (direction()*t);
ykuroda 0:13a5d365ba16 160 }
ykuroda 0:13a5d365ba16 161
ykuroda 0:13a5d365ba16 162 /** \returns the parameter value of the intersection between \c *this and the given \a hyperplane
ykuroda 0:13a5d365ba16 163 */
ykuroda 0:13a5d365ba16 164 template <typename _Scalar, int _AmbientDim, int _Options>
ykuroda 0:13a5d365ba16 165 template <int OtherOptions>
ykuroda 0:13a5d365ba16 166 inline _Scalar ParametrizedLine<_Scalar, _AmbientDim,_Options>::intersectionParameter(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const
ykuroda 0:13a5d365ba16 167 {
ykuroda 0:13a5d365ba16 168 return -(hyperplane.offset()+hyperplane.normal().dot(origin()))
ykuroda 0:13a5d365ba16 169 / hyperplane.normal().dot(direction());
ykuroda 0:13a5d365ba16 170 }
ykuroda 0:13a5d365ba16 171
ykuroda 0:13a5d365ba16 172
ykuroda 0:13a5d365ba16 173 /** \deprecated use intersectionParameter()
ykuroda 0:13a5d365ba16 174 * \returns the parameter value of the intersection between \c *this and the given \a hyperplane
ykuroda 0:13a5d365ba16 175 */
ykuroda 0:13a5d365ba16 176 template <typename _Scalar, int _AmbientDim, int _Options>
ykuroda 0:13a5d365ba16 177 template <int OtherOptions>
ykuroda 0:13a5d365ba16 178 inline _Scalar ParametrizedLine<_Scalar, _AmbientDim,_Options>::intersection(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const
ykuroda 0:13a5d365ba16 179 {
ykuroda 0:13a5d365ba16 180 return intersectionParameter(hyperplane);
ykuroda 0:13a5d365ba16 181 }
ykuroda 0:13a5d365ba16 182
ykuroda 0:13a5d365ba16 183 /** \returns the point of the intersection between \c *this and the given hyperplane
ykuroda 0:13a5d365ba16 184 */
ykuroda 0:13a5d365ba16 185 template <typename _Scalar, int _AmbientDim, int _Options>
ykuroda 0:13a5d365ba16 186 template <int OtherOptions>
ykuroda 0:13a5d365ba16 187 inline typename ParametrizedLine<_Scalar, _AmbientDim,_Options>::VectorType
ykuroda 0:13a5d365ba16 188 ParametrizedLine<_Scalar, _AmbientDim,_Options>::intersectionPoint(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const
ykuroda 0:13a5d365ba16 189 {
ykuroda 0:13a5d365ba16 190 return pointAt(intersectionParameter(hyperplane));
ykuroda 0:13a5d365ba16 191 }
ykuroda 0:13a5d365ba16 192
ykuroda 0:13a5d365ba16 193 } // end namespace Eigen
ykuroda 0:13a5d365ba16 194
ykuroda 0:13a5d365ba16 195 #endif // EIGEN_PARAMETRIZEDLINE_H