| 1 | namespace glm |
| 2 | { |
| 3 | template<typename T, qualifier Q> |
| 4 | GLM_FUNC_QUALIFIER qua<T, Q> mix(qua<T, Q> const& x, qua<T, Q> const& y, T a) |
| 5 | { |
| 6 | static_assert(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT, "'mix' only accept floating-point inputs"); |
| 7 | |
| 8 | T const cosTheta = dot(x, y); |
| 9 | |
| 10 | // Perform a linear interpolation when cosTheta is close to 1 to avoid side effect of sin(angle) becoming a zero denominator |
| 11 | if(cosTheta > static_cast<T>(1) - epsilon<T>()) |
| 12 | { |
| 13 | // Linear interpolation |
| 14 | return qua<T, Q>::wxyz( |
| 15 | mix(x.w, y.w, a), |
| 16 | mix(x.x, y.x, a), |
| 17 | mix(x.y, y.y, a), |
| 18 | mix(x.z, y.z, a)); |
| 19 | } |
| 20 | else |
| 21 | { |
| 22 | // Essential Mathematics, page 467 |
| 23 | T angle = acos(cosTheta); |
| 24 | return (sin((static_cast<T>(1) - a) * angle) * x + sin(a * angle) * y) / sin(angle); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | template<typename T, qualifier Q> |
| 29 | GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua<T, Q> lerp(qua<T, Q> const& x, qua<T, Q> const& y, T a) |
| 30 | { |
| 31 | static_assert(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT, "'lerp' only accept floating-point inputs"); |
| 32 | |
| 33 | // Lerp is only defined in [0, 1] |
| 34 | assert(a >= static_cast<T>(0)); |
| 35 | assert(a <= static_cast<T>(1)); |
| 36 | |
| 37 | return x * (static_cast<T>(1) - a) + (y * a); |
| 38 | } |
| 39 | |
| 40 | template<typename T, qualifier Q> |
| 41 | GLM_FUNC_QUALIFIER qua<T, Q> slerp(qua<T, Q> const& x, qua<T, Q> const& y, T a) |
| 42 | { |
| 43 | static_assert(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT, "'slerp' only accept floating-point inputs"); |
| 44 | |
| 45 | qua<T, Q> z = y; |
| 46 | |
| 47 | T cosTheta = dot(x, y); |
| 48 | |
| 49 | // If cosTheta < 0, the interpolation will take the long way around the sphere. |
| 50 | // To fix this, one quat must be negated. |
| 51 | if(cosTheta < static_cast<T>(0)) |
| 52 | { |
| 53 | z = -y; |
| 54 | cosTheta = -cosTheta; |
| 55 | } |
| 56 | |
| 57 | // Perform a linear interpolation when cosTheta is close to 1 to avoid side effect of sin(angle) becoming a zero denominator |
| 58 | if(cosTheta > static_cast<T>(1) - epsilon<T>()) |
| 59 | { |
| 60 | // Linear interpolation |
| 61 | return qua<T, Q>::wxyz( |
| 62 | mix(x.w, z.w, a), |
| 63 | mix(x.x, z.x, a), |
| 64 | mix(x.y, z.y, a), |
| 65 | mix(x.z, z.z, a)); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | // Essential Mathematics, page 467 |
| 70 | T angle = acos(cosTheta); |
| 71 | return (sin((static_cast<T>(1) - a) * angle) * x + sin(a * angle) * z) / sin(angle); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | template<typename T, typename S, qualifier Q> |
| 76 | GLM_FUNC_QUALIFIER qua<T, Q> slerp(qua<T, Q> const& x, qua<T, Q> const& y, T a, S k) |
| 77 | { |
| 78 | static_assert(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT, "'slerp' only accept floating-point inputs"); |
| 79 | static_assert(std::numeric_limits<S>::is_integer, "'slerp' only accept integer for spin count"); |
| 80 | |
| 81 | qua<T, Q> z = y; |
| 82 | |
| 83 | T cosTheta = dot(x, y); |
| 84 | |
| 85 | // If cosTheta < 0, the interpolation will take the long way around the sphere. |
| 86 | // To fix this, one quat must be negated. |
| 87 | if (cosTheta < static_cast<T>(0)) |
| 88 | { |
| 89 | z = -y; |
| 90 | cosTheta = -cosTheta; |
| 91 | } |
| 92 | |
| 93 | // Perform a linear interpolation when cosTheta is close to 1 to avoid side effect of sin(angle) becoming a zero denominator |
| 94 | if (cosTheta > static_cast<T>(1) - epsilon<T>()) |
| 95 | { |
| 96 | // Linear interpolation |
| 97 | return qua<T, Q>::wxyz( |
| 98 | mix(x.w, z.w, a), |
| 99 | mix(x.x, z.x, a), |
| 100 | mix(x.y, z.y, a), |
| 101 | mix(x.z, z.z, a)); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | // Graphics Gems III, page 96 |
| 106 | T angle = acos(cosTheta); |
| 107 | T phi = angle + static_cast<T>(k) * glm::pi<T>(); |
| 108 | return (sin(angle - a * phi)* x + sin(a * phi) * z) / sin(angle); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | template<typename T, qualifier Q> |
| 113 | GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua<T, Q> conjugate(qua<T, Q> const& q) |
| 114 | { |
| 115 | return qua<T, Q>::wxyz(q.w, -q.x, -q.y, -q.z); |
| 116 | } |
| 117 | |
| 118 | template<typename T, qualifier Q> |
| 119 | GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua<T, Q> inverse(qua<T, Q> const& q) |
| 120 | { |
| 121 | return conjugate(q) / dot(q, q); |
| 122 | } |
| 123 | |
| 124 | template<typename T, qualifier Q> |
| 125 | GLM_FUNC_QUALIFIER vec<4, bool, Q> isnan(qua<T, Q> const& q) |
| 126 | { |
| 127 | static_assert(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT, "'isnan' only accept floating-point inputs"); |
| 128 | |
| 129 | return vec<4, bool, Q>(isnan(q.x), isnan(q.y), isnan(q.z), isnan(q.w)); |
| 130 | } |
| 131 | |
| 132 | template<typename T, qualifier Q> |
| 133 | GLM_FUNC_QUALIFIER vec<4, bool, Q> isinf(qua<T, Q> const& q) |
| 134 | { |
| 135 | static_assert(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT, "'isinf' only accept floating-point inputs"); |
| 136 | |
| 137 | return vec<4, bool, Q>(isinf(q.x), isinf(q.y), isinf(q.z), isinf(q.w)); |
| 138 | } |
| 139 | }//namespace glm |
| 140 | |
| 141 | #if GLM_CONFIG_SIMD == GLM_ENABLE |
| 142 | # include "quaternion_common_simd.inl" |
| 143 | #endif |
| 144 | |