1
// This file is part of Eigen, a lightweight C++ template library
2
// for linear algebra. Eigen itself is part of the KDE project.
4
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
5
// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
7
// Eigen is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 3 of the License, or (at your option) any later version.
12
// Alternatively, you can redistribute it and/or
13
// modify it under the terms of the GNU General Public License as
14
// published by the Free Software Foundation; either version 2 of
15
// the License, or (at your option) any later version.
17
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20
// GNU General Public License for more details.
22
// You should have received a copy of the GNU Lesser General Public
23
// License and a copy of the GNU General Public License along with
24
// Eigen. If not, see <http://www.gnu.org/licenses/>.
26
// no include guard, we'll include this twice from All.h from Eigen2Support, and it's internal anyway
29
/** \geometry_module \ingroup Geometry_Module
31
* \class ParametrizedLine
33
* \brief A parametrized line
35
* A parametrized line is defined by an origin point \f$ \mathbf{o} \f$ and a unit
36
* direction vector \f$ \mathbf{d} \f$ such that the line corresponds to
37
* the set \f$ l(t) = \mathbf{o} + t \mathbf{d} \f$, \f$ l \in \mathbf{R} \f$.
39
* \param _Scalar the scalar type, i.e., the type of the coefficients
40
* \param _AmbientDim the dimension of the ambient space, can be a compile time value or Dynamic.
42
template <typename _Scalar, int _AmbientDim>
43
class ParametrizedLine
46
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim)
47
enum { AmbientDimAtCompileTime = _AmbientDim };
48
typedef _Scalar Scalar;
49
typedef typename NumTraits<Scalar>::Real RealScalar;
50
typedef Matrix<Scalar,AmbientDimAtCompileTime,1> VectorType;
52
/** Default constructor without initialization */
53
inline explicit ParametrizedLine() {}
55
/** Constructs a dynamic-size line with \a _dim the dimension
56
* of the ambient space */
57
inline explicit ParametrizedLine(int _dim) : m_origin(_dim), m_direction(_dim) {}
59
/** Initializes a parametrized line of direction \a direction and origin \a origin.
60
* \warning the vector direction is assumed to be normalized.
62
ParametrizedLine(const VectorType& origin, const VectorType& direction)
63
: m_origin(origin), m_direction(direction) {}
65
explicit ParametrizedLine(const Hyperplane<_Scalar, _AmbientDim>& hyperplane);
67
/** Constructs a parametrized line going from \a p0 to \a p1. */
68
static inline ParametrizedLine Through(const VectorType& p0, const VectorType& p1)
69
{ return ParametrizedLine(p0, (p1-p0).normalized()); }
71
~ParametrizedLine() {}
73
/** \returns the dimension in which the line holds */
74
inline int dim() const { return m_direction.size(); }
76
const VectorType& origin() const { return m_origin; }
77
VectorType& origin() { return m_origin; }
79
const VectorType& direction() const { return m_direction; }
80
VectorType& direction() { return m_direction; }
82
/** \returns the squared distance of a point \a p to its projection onto the line \c *this.
85
RealScalar squaredDistance(const VectorType& p) const
87
VectorType diff = p-origin();
88
return (diff - diff.eigen2_dot(direction())* direction()).squaredNorm();
90
/** \returns the distance of a point \a p to its projection onto the line \c *this.
91
* \sa squaredDistance()
93
RealScalar distance(const VectorType& p) const { return ei_sqrt(squaredDistance(p)); }
95
/** \returns the projection of a point \a p onto the line \c *this. */
96
VectorType projection(const VectorType& p) const
97
{ return origin() + (p-origin()).eigen2_dot(direction()) * direction(); }
99
Scalar intersection(const Hyperplane<_Scalar, _AmbientDim>& hyperplane);
101
/** \returns \c *this with scalar type casted to \a NewScalarType
103
* Note that if \a NewScalarType is equal to the current scalar type of \c *this
104
* then this function smartly returns a const reference to \c *this.
106
template<typename NewScalarType>
107
inline typename internal::cast_return_type<ParametrizedLine,
108
ParametrizedLine<NewScalarType,AmbientDimAtCompileTime> >::type cast() const
110
return typename internal::cast_return_type<ParametrizedLine,
111
ParametrizedLine<NewScalarType,AmbientDimAtCompileTime> >::type(*this);
114
/** Copy constructor with scalar type conversion */
115
template<typename OtherScalarType>
116
inline explicit ParametrizedLine(const ParametrizedLine<OtherScalarType,AmbientDimAtCompileTime>& other)
118
m_origin = other.origin().template cast<Scalar>();
119
m_direction = other.direction().template cast<Scalar>();
122
/** \returns \c true if \c *this is approximately equal to \a other, within the precision
123
* determined by \a prec.
125
* \sa MatrixBase::isApprox() */
126
bool isApprox(const ParametrizedLine& other, typename NumTraits<Scalar>::Real prec = precision<Scalar>()) const
127
{ return m_origin.isApprox(other.m_origin, prec) && m_direction.isApprox(other.m_direction, prec); }
131
VectorType m_origin, m_direction;
134
/** Constructs a parametrized line from a 2D hyperplane
136
* \warning the ambient space must have dimension 2 such that the hyperplane actually describes a line
138
template <typename _Scalar, int _AmbientDim>
139
inline ParametrizedLine<_Scalar, _AmbientDim>::ParametrizedLine(const Hyperplane<_Scalar, _AmbientDim>& hyperplane)
141
EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(VectorType, 2)
142
direction() = hyperplane.normal().unitOrthogonal();
143
origin() = -hyperplane.normal()*hyperplane.offset();
146
/** \returns the parameter value of the intersection between \c *this and the given hyperplane
148
template <typename _Scalar, int _AmbientDim>
149
inline _Scalar ParametrizedLine<_Scalar, _AmbientDim>::intersection(const Hyperplane<_Scalar, _AmbientDim>& hyperplane)
151
return -(hyperplane.offset()+origin().eigen2_dot(hyperplane.normal()))
152
/(direction().eigen2_dot(hyperplane.normal()));