diff --git a/glm/ext/quaternion_common.hpp b/glm/ext/quaternion_common.hpp index 2980ed4d..f519d559 100644 --- a/glm/ext/quaternion_common.hpp +++ b/glm/ext/quaternion_common.hpp @@ -76,6 +76,21 @@ namespace glm template GLM_FUNC_DECL qua slerp(qua const& x, qua 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 + GLM_FUNC_DECL qua slerp(qua const& x, qua const& y, T a, S k); + /// Returns the q conjugate. /// /// @tparam T A floating-point scalar type diff --git a/glm/ext/quaternion_common.inl b/glm/ext/quaternion_common.inl index 3b2846f3..0e4a3bb2 100644 --- a/glm/ext/quaternion_common.inl +++ b/glm/ext/quaternion_common.inl @@ -72,6 +72,43 @@ namespace glm } } + template + GLM_FUNC_QUALIFIER qua slerp(qua const& x, qua const& y, T a, S k) + { + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'slerp' only accept floating-point inputs"); + GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'slerp' only accept integer for spin count"); + + qua 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(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(1) - epsilon()) + { + // Linear interpolation + return qua( + 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(); + return (sin(angle - a * phi)* x + sin(a * phi) * z) / sin(angle); + } + } + template GLM_FUNC_QUALIFIER qua conjugate(qua const& q) { diff --git a/test/gtc/gtc_quaternion.cpp b/test/gtc/gtc_quaternion.cpp index 65f3b44a..540ca42d 100644 --- a/test/gtc/gtc_quaternion.cpp +++ b/test/gtc/gtc_quaternion.cpp @@ -194,6 +194,84 @@ int test_quat_slerp() return Error; } +int test_quat_slerp_spins() +{ + int Error = 0; + + float const Epsilon = 0.0001f;//glm::epsilon(); + + float sqrt2 = std::sqrt(2.0f) / 2.0f; + glm::quat id(static_cast(1), static_cast(0), static_cast(0), static_cast(0)); + glm::quat Y90rot(sqrt2, 0.0f, sqrt2, 0.0f); + glm::quat Y180rot(0.0f, 0.0f, 1.0f, 0.0f); + + // Testing a == 0, k == 1 + // Must be id + glm::quat id2 = glm::slerp(id, id, 1.0f, 1); + Error += glm::all(glm::equal(id, id2, Epsilon)) ? 0 : 1; + + // Testing a == 1, k == 2 + // Must be id + glm::quat id3 = glm::slerp(id, id, 1.0f, 2); + Error += glm::all(glm::equal(id, id3, Epsilon)) ? 0 : 1; + + // Testing a == 1, k == 1 + // Must be 90° rotation on Y : 0 0.7 0 0.7 + // Negative quaternion is representing same orientation + glm::quat Y90rot2 = glm::slerp(id, Y90rot, 1.0f, 1); + Error += glm::all(glm::equal(Y90rot, -Y90rot2, Epsilon)) ? 0 : 1; + + // Testing a == 1, k == 2 + // Must be id + glm::quat Y90rot3 = glm::slerp(id, Y90rot, 8.0f / 9.0f, 2); + Error += glm::all(glm::equal(id, Y90rot3, Epsilon)) ? 0 : 1; + + // Testing a == 1, k == 1 + // Must be 90° rotation on Y : 0 0.7 0 0.7 + glm::quat Y90rot4 = glm::slerp(id, Y90rot, 0.2f, 1); + Error += glm::all(glm::equal(Y90rot, Y90rot4, Epsilon)) ? 0 : 1; + + // Testing reverse case + // Must be 45° rotation on Y : 0 0.38 0 0.92 + // Negative quaternion is representing same orientation + glm::quat Ym45rot2 = glm::slerp(Y90rot, id, 0.9f, 1); + glm::quat Ym45rot3 = glm::slerp(Y90rot, id, 0.5f); + Error += glm::all(glm::equal(-Ym45rot2, Ym45rot3, Epsilon)) ? 0 : 1; + + // Testing against full circle around the sphere instead of shortest path + // Must be 45° rotation on Y + // certainly not a 135° rotation + glm::quat Y45rot3 = glm::slerp(id, -Y90rot, 0.5f, 0); + float Y45angle3 = glm::angle(Y45rot3); + Error += glm::equal(Y45angle3, glm::pi() * 0.25f, Epsilon) ? 0 : 1; + Error += glm::all(glm::equal(Ym45rot3, Y45rot3, Epsilon)) ? 0 : 1; + + // Same, but inverted + // Must also be 45° rotation on Y : 0 0.38 0 0.92 + // -0 -0.38 -0 -0.92 is ok too + glm::quat Y45rot4 = glm::slerp(-Y90rot, id, 0.5f, 0); + Error += glm::all(glm::equal(Ym45rot2, Y45rot4, Epsilon)) ? 0 : 1; + + // Testing q1 = q2 k == 2 + // Must be 90° rotation on Y : 0 0.7 0 0.7 + glm::quat Y90rot5 = glm::slerp(Y90rot, Y90rot, 0.5f, 2); + Error += glm::all(glm::equal(Y90rot, Y90rot5, Epsilon)) ? 0 : 1; + + // Testing 180° rotation + // Must be 90° rotation on almost any axis that is on the XZ plane + glm::quat XZ90rot = glm::slerp(id, -Y90rot, 0.5f, 1); + float XZ90angle = glm::angle(XZ90rot); // Must be PI/4 = 0.78; + Error += glm::equal(XZ90angle, glm::pi() * 1.25f, Epsilon) ? 0 : 1; + + // Testing rotation over long arc + // Distance from id to 90° is 270°, so 2/3 of it should be 180° + // Negative quaternion is representing same orientation + glm::quat Neg90rot = glm::slerp(id, Y90rot, 2.0f / 3.0f, -1); + Error += glm::all(glm::equal(Y180rot, -Neg90rot, Epsilon)) ? 0 : 1; + + return Error; +} + static int test_quat_mul_vec() { int Error(0); @@ -260,6 +338,7 @@ int main() Error += test_quat_normalize(); Error += test_quat_euler(); Error += test_quat_slerp(); + Error += test_quat_slerp_spins(); Error += test_identity(); return Error;