Quaternion slerp overload which interpolates with extra spins

Signed-off-by: Karol Kontny <barolek@gmail.com>
This commit is contained in:
Kontny Karol
2019-11-14 15:51:20 +01:00
committed by Karol Kontny
parent 6bd53cc9e5
commit 31d01b525e
3 changed files with 131 additions and 0 deletions

View File

@@ -76,6 +76,21 @@ namespace glm
template<typename T, qualifier Q>
GLM_FUNC_DECL qua<T, Q> slerp(qua<T, Q> const& x, qua<T, Q> const& y, T a);
/// Spherical linear interpolation of two quaternions with multiple spins over rotation axis.
/// The interpolation always take the short path when the spin count is positive and long path
/// when count is negative. Rotation is performed at constant speed.
///
/// @param x A quaternion
/// @param y A quaternion
/// @param a Interpolation factor. The interpolation is defined beyond the range [0, 1].
/// @param k Additional spin count. If Value is negative interpolation will be on "long" path.
///
/// @tparam T A floating-point scalar type
/// @tparam S An integer scalar type
/// @tparam Q A value from qualifier enum
template<typename T, typename S, qualifier Q>
GLM_FUNC_DECL qua<T, Q> slerp(qua<T, Q> const& x, qua<T, Q> const& y, T a, S k);
/// Returns the q conjugate.
///
/// @tparam T A floating-point scalar type

View File

@@ -72,6 +72,43 @@ namespace glm
}
}
template<typename T, typename S, qualifier Q>
GLM_FUNC_QUALIFIER qua<T, Q> slerp(qua<T, Q> const& x, qua<T, Q> const& y, T a, S k)
{
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'slerp' only accept floating-point inputs");
GLM_STATIC_ASSERT(std::numeric_limits<S>::is_integer, "'slerp' only accept integer for spin count");
qua<T, Q> z = y;
T cosTheta = dot(x, y);
// If cosTheta < 0, the interpolation will take the long way around the sphere.
// To fix this, one quat must be negated.
if (cosTheta < static_cast<T>(0))
{
z = -y;
cosTheta = -cosTheta;
}
// Perform a linear interpolation when cosTheta is close to 1 to avoid side effect of sin(angle) becoming a zero denominator
if (cosTheta > static_cast<T>(1) - epsilon<T>())
{
// Linear interpolation
return qua<T, Q>(
mix(x.w, z.w, a),
mix(x.x, z.x, a),
mix(x.y, z.y, a),
mix(x.z, z.z, a));
}
else
{
// Graphics Gems III, page 96
T angle = acos(cosTheta);
T phi = angle + k * glm::pi<T>();
return (sin(angle - a * phi)* x + sin(a * phi) * z) / sin(angle);
}
}
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER qua<T, Q> conjugate(qua<T, Q> const& q)
{