2008-09-03 22:35:45 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2008-09-03 22:35:45 +00:00
|
|
|
//
|
2010-06-24 23:21:58 +02:00
|
|
|
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2008-11-24 13:40:43 +00:00
|
|
|
// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2008-09-03 22:35:45 +00:00
|
|
|
//
|
2012-07-13 14:42:47 -04:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2008-09-03 22:35:45 +00:00
|
|
|
|
|
|
|
|
#ifndef EIGEN_PARAMETRIZEDLINE_H
|
|
|
|
|
#define EIGEN_PARAMETRIZEDLINE_H
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
namespace Eigen {
|
|
|
|
|
|
2009-01-29 23:10:16 +00:00
|
|
|
/** \geometry_module \ingroup Geometry_Module
|
2008-09-03 22:35:45 +00:00
|
|
|
*
|
|
|
|
|
* \class ParametrizedLine
|
|
|
|
|
*
|
|
|
|
|
* \brief A parametrized line
|
|
|
|
|
*
|
2008-10-25 00:08:52 +00:00
|
|
|
* A parametrized line is defined by an origin point \f$ \mathbf{o} \f$ and a unit
|
|
|
|
|
* direction vector \f$ \mathbf{d} \f$ such that the line corresponds to
|
2011-06-23 00:36:24 +02:00
|
|
|
* the set \f$ l(t) = \mathbf{o} + t \mathbf{d} \f$, \f$ t \in \mathbf{R} \f$.
|
2008-10-25 00:08:52 +00:00
|
|
|
*
|
2016-01-01 21:45:06 +01:00
|
|
|
* \tparam _Scalar the scalar type, i.e., the type of the coefficients
|
|
|
|
|
* \tparam _AmbientDim the dimension of the ambient space, can be a compile time value or Dynamic.
|
2008-09-03 22:35:45 +00:00
|
|
|
*/
|
2011-01-27 17:17:06 +01:00
|
|
|
template <typename _Scalar, int _AmbientDim, int _Options>
|
2008-09-03 22:35:45 +00:00
|
|
|
class ParametrizedLine
|
|
|
|
|
{
|
2008-10-25 22:38:22 +00:00
|
|
|
public:
|
2009-01-12 16:06:04 +00:00
|
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim)
|
2011-01-27 17:17:06 +01:00
|
|
|
enum {
|
|
|
|
|
AmbientDimAtCompileTime = _AmbientDim,
|
|
|
|
|
Options = _Options
|
|
|
|
|
};
|
2008-10-25 22:38:22 +00:00
|
|
|
typedef _Scalar Scalar;
|
|
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
2015-02-16 15:05:41 +01:00
|
|
|
typedef Eigen::Index Index; ///< \deprecated since Eigen 3.3
|
2011-01-27 17:17:06 +01:00
|
|
|
typedef Matrix<Scalar,AmbientDimAtCompileTime,1,Options> VectorType;
|
2008-10-25 22:38:22 +00:00
|
|
|
|
|
|
|
|
/** Default constructor without initialization */
|
2013-06-12 13:05:23 +02:00
|
|
|
inline ParametrizedLine() {}
|
2011-01-27 17:17:06 +01:00
|
|
|
|
|
|
|
|
template<int OtherOptions>
|
|
|
|
|
ParametrizedLine(const ParametrizedLine<Scalar,AmbientDimAtCompileTime,OtherOptions>& other)
|
|
|
|
|
: m_origin(other.origin()), m_direction(other.direction())
|
|
|
|
|
{}
|
2008-10-25 22:38:22 +00:00
|
|
|
|
|
|
|
|
/** Constructs a dynamic-size line with \a _dim the dimension
|
|
|
|
|
* of the ambient space */
|
2010-05-30 16:00:58 -04:00
|
|
|
inline explicit ParametrizedLine(Index _dim) : m_origin(_dim), m_direction(_dim) {}
|
2008-10-25 22:38:22 +00:00
|
|
|
|
|
|
|
|
/** Initializes a parametrized line of direction \a direction and origin \a origin.
|
|
|
|
|
* \warning the vector direction is assumed to be normalized.
|
|
|
|
|
*/
|
|
|
|
|
ParametrizedLine(const VectorType& origin, const VectorType& direction)
|
|
|
|
|
: m_origin(origin), m_direction(direction) {}
|
|
|
|
|
|
2011-01-27 17:17:06 +01:00
|
|
|
template <int OtherOptions>
|
|
|
|
|
explicit ParametrizedLine(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane);
|
2008-10-25 22:38:22 +00:00
|
|
|
|
|
|
|
|
/** Constructs a parametrized line going from \a p0 to \a p1. */
|
|
|
|
|
static inline ParametrizedLine Through(const VectorType& p0, const VectorType& p1)
|
|
|
|
|
{ return ParametrizedLine(p0, (p1-p0).normalized()); }
|
|
|
|
|
|
|
|
|
|
~ParametrizedLine() {}
|
|
|
|
|
|
|
|
|
|
/** \returns the dimension in which the line holds */
|
2010-05-30 16:00:58 -04:00
|
|
|
inline Index dim() const { return m_direction.size(); }
|
2008-10-25 22:38:22 +00:00
|
|
|
|
|
|
|
|
const VectorType& origin() const { return m_origin; }
|
|
|
|
|
VectorType& origin() { return m_origin; }
|
|
|
|
|
|
|
|
|
|
const VectorType& direction() const { return m_direction; }
|
|
|
|
|
VectorType& direction() { return m_direction; }
|
|
|
|
|
|
|
|
|
|
/** \returns the squared distance of a point \a p to its projection onto the line \c *this.
|
|
|
|
|
* \sa distance()
|
|
|
|
|
*/
|
|
|
|
|
RealScalar squaredDistance(const VectorType& p) const
|
|
|
|
|
{
|
2009-08-03 17:20:45 +02:00
|
|
|
VectorType diff = p - origin();
|
|
|
|
|
return (diff - direction().dot(diff) * direction()).squaredNorm();
|
2008-10-25 22:38:22 +00:00
|
|
|
}
|
|
|
|
|
/** \returns the distance of a point \a p to its projection onto the line \c *this.
|
|
|
|
|
* \sa squaredDistance()
|
|
|
|
|
*/
|
2012-11-06 15:25:50 +01:00
|
|
|
RealScalar distance(const VectorType& p) const { using std::sqrt; return sqrt(squaredDistance(p)); }
|
2008-10-25 22:38:22 +00:00
|
|
|
|
|
|
|
|
/** \returns the projection of a point \a p onto the line \c *this. */
|
|
|
|
|
VectorType projection(const VectorType& p) const
|
2009-08-03 17:20:45 +02:00
|
|
|
{ return origin() + direction().dot(p-origin()) * direction(); }
|
2008-10-25 22:38:22 +00:00
|
|
|
|
2012-06-28 02:08:59 +02:00
|
|
|
VectorType pointAt(const Scalar& t) const;
|
2011-12-10 11:58:38 +01:00
|
|
|
|
|
|
|
|
template <int OtherOptions>
|
|
|
|
|
Scalar intersectionParameter(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const;
|
|
|
|
|
|
2011-01-27 17:17:06 +01:00
|
|
|
template <int OtherOptions>
|
2011-06-27 13:15:01 +02:00
|
|
|
Scalar intersection(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const;
|
2011-12-10 11:58:38 +01:00
|
|
|
|
|
|
|
|
template <int OtherOptions>
|
|
|
|
|
VectorType intersectionPoint(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const;
|
|
|
|
|
|
2008-10-25 22:38:22 +00:00
|
|
|
/** \returns \c *this with scalar type casted to \a NewScalarType
|
|
|
|
|
*
|
|
|
|
|
* Note that if \a NewScalarType is equal to the current scalar type of \c *this
|
|
|
|
|
* then this function smartly returns a const reference to \c *this.
|
|
|
|
|
*/
|
|
|
|
|
template<typename NewScalarType>
|
2010-10-25 10:15:22 -04:00
|
|
|
inline typename internal::cast_return_type<ParametrizedLine,
|
2011-01-27 17:17:06 +01:00
|
|
|
ParametrizedLine<NewScalarType,AmbientDimAtCompileTime,Options> >::type cast() const
|
2008-10-25 22:38:22 +00:00
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
return typename internal::cast_return_type<ParametrizedLine,
|
2011-01-27 17:17:06 +01:00
|
|
|
ParametrizedLine<NewScalarType,AmbientDimAtCompileTime,Options> >::type(*this);
|
2008-10-25 22:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Copy constructor with scalar type conversion */
|
2011-01-27 17:17:06 +01:00
|
|
|
template<typename OtherScalarType,int OtherOptions>
|
|
|
|
|
inline explicit ParametrizedLine(const ParametrizedLine<OtherScalarType,AmbientDimAtCompileTime,OtherOptions>& other)
|
2008-10-25 22:38:22 +00:00
|
|
|
{
|
2008-12-22 20:50:47 +00:00
|
|
|
m_origin = other.origin().template cast<Scalar>();
|
|
|
|
|
m_direction = other.direction().template cast<Scalar>();
|
2008-10-25 22:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
2008-10-25 23:10:21 +00:00
|
|
|
/** \returns \c true if \c *this is approximately equal to \a other, within the precision
|
|
|
|
|
* determined by \a prec.
|
|
|
|
|
*
|
|
|
|
|
* \sa MatrixBase::isApprox() */
|
2016-01-27 18:34:42 +01:00
|
|
|
bool isApprox(const ParametrizedLine& other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const
|
2008-10-25 23:10:21 +00:00
|
|
|
{ return m_origin.isApprox(other.m_origin, prec) && m_direction.isApprox(other.m_direction, prec); }
|
|
|
|
|
|
2008-10-25 22:38:22 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
VectorType m_origin, m_direction;
|
2008-09-03 22:35:45 +00:00
|
|
|
};
|
|
|
|
|
|
2008-10-25 00:08:52 +00:00
|
|
|
/** Constructs a parametrized line from a 2D hyperplane
|
2008-09-03 22:35:45 +00:00
|
|
|
*
|
|
|
|
|
* \warning the ambient space must have dimension 2 such that the hyperplane actually describes a line
|
|
|
|
|
*/
|
2011-01-27 17:17:06 +01:00
|
|
|
template <typename _Scalar, int _AmbientDim, int _Options>
|
|
|
|
|
template <int OtherOptions>
|
|
|
|
|
inline ParametrizedLine<_Scalar, _AmbientDim,_Options>::ParametrizedLine(const Hyperplane<_Scalar, _AmbientDim,OtherOptions>& hyperplane)
|
2008-09-03 22:35:45 +00:00
|
|
|
{
|
2008-12-03 21:01:55 +00:00
|
|
|
EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(VectorType, 2)
|
2008-09-03 22:35:45 +00:00
|
|
|
direction() = hyperplane.normal().unitOrthogonal();
|
|
|
|
|
origin() = -hyperplane.normal()*hyperplane.offset();
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-10 12:17:42 +01:00
|
|
|
/** \returns the point at \a t along this line
|
2011-12-10 11:58:38 +01:00
|
|
|
*/
|
|
|
|
|
template <typename _Scalar, int _AmbientDim, int _Options>
|
|
|
|
|
inline typename ParametrizedLine<_Scalar, _AmbientDim,_Options>::VectorType
|
2012-06-28 02:08:59 +02:00
|
|
|
ParametrizedLine<_Scalar, _AmbientDim,_Options>::pointAt(const _Scalar& t) const
|
2011-12-10 11:58:38 +01:00
|
|
|
{
|
|
|
|
|
return origin() + (direction()*t);
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-10 12:17:42 +01:00
|
|
|
/** \returns the parameter value of the intersection between \c *this and the given \a hyperplane
|
2008-09-03 22:35:45 +00:00
|
|
|
*/
|
2011-01-27 17:17:06 +01:00
|
|
|
template <typename _Scalar, int _AmbientDim, int _Options>
|
|
|
|
|
template <int OtherOptions>
|
2011-12-10 11:58:38 +01:00
|
|
|
inline _Scalar ParametrizedLine<_Scalar, _AmbientDim,_Options>::intersectionParameter(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const
|
2008-09-03 22:35:45 +00:00
|
|
|
{
|
2009-08-03 17:20:45 +02:00
|
|
|
return -(hyperplane.offset()+hyperplane.normal().dot(origin()))
|
|
|
|
|
/ hyperplane.normal().dot(direction());
|
2008-09-03 22:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-10 11:58:38 +01:00
|
|
|
|
2011-12-10 12:17:42 +01:00
|
|
|
/** \deprecated use intersectionParameter()
|
|
|
|
|
* \returns the parameter value of the intersection between \c *this and the given \a hyperplane
|
2011-12-10 11:58:38 +01:00
|
|
|
*/
|
|
|
|
|
template <typename _Scalar, int _AmbientDim, int _Options>
|
|
|
|
|
template <int OtherOptions>
|
|
|
|
|
inline _Scalar ParametrizedLine<_Scalar, _AmbientDim,_Options>::intersection(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const
|
|
|
|
|
{
|
|
|
|
|
return intersectionParameter(hyperplane);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \returns the point of the intersection between \c *this and the given hyperplane
|
|
|
|
|
*/
|
|
|
|
|
template <typename _Scalar, int _AmbientDim, int _Options>
|
|
|
|
|
template <int OtherOptions>
|
|
|
|
|
inline typename ParametrizedLine<_Scalar, _AmbientDim,_Options>::VectorType
|
|
|
|
|
ParametrizedLine<_Scalar, _AmbientDim,_Options>::intersectionPoint(const Hyperplane<_Scalar, _AmbientDim, OtherOptions>& hyperplane) const
|
|
|
|
|
{
|
2011-12-10 12:17:42 +01:00
|
|
|
return pointAt(intersectionParameter(hyperplane));
|
2011-12-10 11:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
2008-09-03 22:35:45 +00:00
|
|
|
#endif // EIGEN_PARAMETRIZEDLINE_H
|