Add smart cast functions and ctor with scalar conversion (explicit)

to all classes of the Geometry module. By smart I mean that if current
type == new type, then it returns a const reference to *this => zero overhead
This commit is contained in:
Gael Guennebaud 2008-10-25 22:38:22 +00:00
parent 568a7e8eba
commit e5b8a59cfa
9 changed files with 361 additions and 224 deletions

View File

@ -169,4 +169,9 @@ template<typename ExpressionType, int RowsOrSize=Dynamic, int Cols=Dynamic> stru
typedef Block<ExpressionType, RowsOrSize, Cols> Type;
};
template<typename CurrentType, typename NewType> struct ei_cast_return_type
{
typedef typename ei_meta_if<ei_is_same_type<CurrentType,NewType>::ret,const CurrentType&,NewType>::ret type;
};
#endif // EIGEN_XPRHELPER_H

View File

@ -132,6 +132,23 @@ public:
template<typename Derived>
AngleAxis& fromRotationMatrix(const MatrixBase<Derived>& m);
Matrix3 toRotationMatrix(void) const;
/** \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>
typename ei_cast_return_type<AngleAxis,AngleAxis<NewScalarType> >::type cast() const
{ return typename ei_cast_return_type<AngleAxis,AngleAxis<NewScalarType> >::type(*this); }
/** Copy constructor with scalar type conversion */
template<typename OtherScalarType>
explicit AngleAxis(const AngleAxis<OtherScalarType>& other)
{
m_axis = other.axis().template cast<OtherScalarType>();
m_angle = other.angle();
}
};
/** \ingroup GeometryModule

View File

@ -238,6 +238,24 @@ class Hyperplane
return *this;
}
/** \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>
typename ei_cast_return_type<Hyperplane,
Hyperplane<NewScalarType,AmbientDimAtCompileTime> >::type cast() const
{
return typename ei_cast_return_type<Hyperplane,
Hyperplane<NewScalarType,AmbientDimAtCompileTime> >::type(*this);
}
/** Copy constructor with scalar type conversion */
template<typename OtherScalarType>
explicit Hyperplane(const Hyperplane<OtherScalarType,AmbientDimAtCompileTime>& other)
{ m_coeffs = other.coeffs().template cast<OtherScalarType>(); }
protected:
Coefficients m_coeffs;

View File

@ -101,6 +101,27 @@ class ParametrizedLine
Scalar intersection(const Hyperplane<_Scalar, _AmbientDim>& hyperplane);
/** \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>
typename ei_cast_return_type<ParametrizedLine,
ParametrizedLine<NewScalarType,AmbientDimAtCompileTime> >::type cast() const
{
return typename ei_cast_return_type<ParametrizedLine,
ParametrizedLine<NewScalarType,AmbientDimAtCompileTime> >::type(*this);
}
/** Copy constructor with scalar type conversion */
template<typename OtherScalarType>
explicit ParametrizedLine(const ParametrizedLine<OtherScalarType,AmbientDimAtCompileTime>& other)
{
m_origin = other.origin().template cast<OtherScalarType>();
m_direction = other.direction().template cast<OtherScalarType>();
}
protected:
VectorType m_origin, m_direction;

View File

@ -195,6 +195,22 @@ public:
template<typename Derived>
Vector3 operator* (const MatrixBase<Derived>& vec) const;
/** \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>
typename ei_cast_return_type<Quaternion,Quaternion<NewScalarType> >::type cast() const
{ return typename ei_cast_return_type<Quaternion,Quaternion<NewScalarType> >::type(*this); }
/** Copy constructor with scalar type conversion */
template<typename OtherScalarType>
explicit Quaternion(const Quaternion<OtherScalarType>& other)
{
m_coeffs = other.coeffs().template cast<OtherScalarType>();
}
};
/** \ingroup GeometryModule

View File

@ -100,6 +100,22 @@ public:
*/
inline Rotation2D slerp(Scalar t, const Rotation2D& other) const
{ return m_angle * (1-t) + other.angle() * t; }
/** \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>
typename ei_cast_return_type<Rotation2D,Rotation2D<NewScalarType> >::type cast() const
{ return typename ei_cast_return_type<Rotation2D,Rotation2D<NewScalarType> >::type(*this); }
/** Copy constructor with scalar type conversion */
template<typename OtherScalarType>
explicit Rotation2D(const Rotation2D<OtherScalarType>& other)
{
m_angle = other.angle();
}
};
/** \ingroup GeometryModule

View File

@ -128,6 +128,20 @@ public:
return *this;
}
/** \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>
typename ei_cast_return_type<Scaling,Scaling<NewScalarType,Dim> >::type cast() const
{ return typename ei_cast_return_type<Scaling,Scaling<NewScalarType,Dim> >::type(*this); }
/** Copy constructor with scalar type conversion */
template<typename OtherScalarType>
explicit Scaling(const Scaling<OtherScalarType,Dim>& other)
{ m_coeffs = other.coeffs().template cast<OtherScalarType>(); }
};
/** \addtogroup GeometryModule */

View File

@ -241,9 +241,25 @@ public:
inline const MatrixType inverse(TransformTraits traits = Affine) const;
/** \returns a const pointer to the column major internal matrix */
const Scalar* data() const { return m_matrix.data(); }
/** \returns a non-const pointer to the column major internal matrix */
Scalar* data() { return m_matrix.data(); }
/** \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>
typename ei_cast_return_type<Transform,Transform<NewScalarType,Dim> >::type cast() const
{ return typename ei_cast_return_type<Transform,Transform<NewScalarType,Dim> >::type(*this); }
/** Copy constructor with scalar type conversion */
template<typename OtherScalarType>
explicit Transform(const Transform<OtherScalarType,Dim>& other)
{ m_matrix = other.matrix().template cast<OtherScalarType>(); }
protected:
};

View File

@ -131,6 +131,20 @@ public:
return *this;
}
/** \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>
typename ei_cast_return_type<Translation,Translation<NewScalarType,Dim> >::type cast() const
{ return typename ei_cast_return_type<Translation,Translation<NewScalarType,Dim> >::type(*this); }
/** Copy constructor with scalar type conversion */
template<typename OtherScalarType>
explicit Translation(const Translation<OtherScalarType,Dim>& other)
{ m_coeffs = other.vector().template cast<OtherScalarType>(); }
};
/** \addtogroup GeometryModule */