Rotation2D: fix slerp to take the shortest path, and add convenient method to get the angle in [-pi,pi] or [0,pi]

This commit is contained in:
Gael Guennebaud
2015-07-07 17:27:12 +02:00
parent 3f2101b03b
commit fa17358c4b
2 changed files with 53 additions and 4 deletions

View File

@@ -69,6 +69,20 @@ public:
/** \returns a read-write reference to the rotation angle */
inline Scalar& angle() { return m_angle; }
/** \returns the rotation angle in [0,2pi] */
inline Scalar smallestPositiveAngle() const {
Scalar tmp = fmod(m_angle,Scalar(2)*EIGEN_PI);
return tmp<Scalar(0) ? tmp + Scalar(2)*EIGEN_PI : tmp;
}
/** \returns the rotation angle in [-pi,pi] */
inline Scalar smallestAngle() const {
Scalar tmp = fmod(m_angle,Scalar(2)*EIGEN_PI);
if(tmp>Scalar(EIGEN_PI)) tmp -= Scalar(2)*Scalar(EIGEN_PI);
else if(tmp<-Scalar(EIGEN_PI)) tmp += Scalar(2)*Scalar(EIGEN_PI);
return tmp;
}
/** \returns the inverse rotation */
inline Rotation2D inverse() const { return Rotation2D(-m_angle); }
@@ -93,7 +107,10 @@ public:
* parameter \a t. It is in fact equivalent to a linear interpolation.
*/
inline Rotation2D slerp(const Scalar& t, const Rotation2D& other) const
{ return Rotation2D(m_angle * (1-t) + other.angle() * t); }
{
Scalar dist = Rotation2D(other.m_angle-m_angle).smallestAngle();
return Rotation2D(m_angle + dist*t);
}
/** \returns \c *this with scalar type casted to \a NewScalarType
*
@@ -119,6 +136,7 @@ public:
* \sa MatrixBase::isApprox() */
bool isApprox(const Rotation2D& other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const
{ return internal::isApprox(m_angle,other.m_angle, prec); }
};
/** \ingroup Geometry_Module