/// @ref gtc_quaternion #include "../trigonometric.hpp" #include "../geometric.hpp" #include "../exponential.hpp" #include "epsilon.hpp" #include namespace glm { // -- Operations -- /* // (x * sin(1 - a) * angle / sin(angle)) + (y * sin(a) * angle / sin(angle)) template GLM_FUNC_QUALIFIER qua mix(qua const& x, qua const& y, T const& a) { if(a <= T(0)) return x; if(a >= T(1)) return y; float fCos = dot(x, y); qua y2(y); //BUG!!! qua y2; if(fCos < T(0)) { y2 = -y; fCos = -fCos; } //if(fCos > 1.0f) // problem float k0, k1; if(fCos > T(0.9999)) { k0 = T(1) - a; k1 = T(0) + a; //BUG!!! 1.0f + a; } else { T fSin = sqrt(T(1) - fCos * fCos); T fAngle = atan(fSin, fCos); T fOneOverSin = static_cast(1) / fSin; k0 = sin((T(1) - a) * fAngle) * fOneOverSin; k1 = sin((T(0) + a) * fAngle) * fOneOverSin; } return qua( k0 * x.w + k1 * y2.w, k0 * x.x + k1 * y2.x, k0 * x.y + k1 * y2.y, k0 * x.z + k1 * y2.z); } template GLM_FUNC_QUALIFIER qua mix2 ( qua const& x, qua const& y, T const& a ) { bool flip = false; if(a <= static_cast(0)) return x; if(a >= static_cast(1)) return y; T cos_t = dot(x, y); if(cos_t < T(0)) { cos_t = -cos_t; flip = true; } T alpha(0), beta(0); if(T(1) - cos_t < 1e-7) beta = static_cast(1) - alpha; else { T theta = acos(cos_t); T sin_t = sin(theta); beta = sin(theta * (T(1) - alpha)) / sin_t; alpha = sin(alpha * theta) / sin_t; } if(flip) alpha = -alpha; return normalize(beta * x + alpha * y); } */ template GLM_FUNC_QUALIFIER qua mix(qua const& x, qua const& y, T a) { T cosTheta = dot(x, y); // Perform a linear interpolation when cosTheta is close to 1 to avoid side effect of sin(angle) becoming a zero denominator if(cosTheta > T(1) - epsilon()) { // Linear interpolation return qua( mix(x.w, y.w, a), mix(x.x, y.x, a), mix(x.y, y.y, a), mix(x.z, y.z, a)); } else { // Essential Mathematics, page 467 T angle = acos(cosTheta); return (sin((T(1) - a) * angle) * x + sin(a * angle) * y) / sin(angle); } } template GLM_FUNC_QUALIFIER qua lerp(qua const& x, qua const& y, T a) { // Lerp is only defined in [0, 1] assert(a >= static_cast(0)); assert(a <= static_cast(1)); return x * (T(1) - a) + (y * a); } template GLM_FUNC_QUALIFIER qua slerp(qua const& x, qua const& y, T a) { 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 < 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 > T(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 { // Essential Mathematics, page 467 T angle = acos(cosTheta); return (sin((T(1) - a) * angle) * x + sin(a * angle) * z) / sin(angle); } } template GLM_FUNC_QUALIFIER qua rotate(qua const& q, T const& angle, vec<3, T, Q> const& v) { vec<3, T, Q> Tmp = v; // Axis of rotation must be normalised T len = glm::length(Tmp); if(abs(len - T(1)) > T(0.001)) { T oneOverLen = static_cast(1) / len; Tmp.x *= oneOverLen; Tmp.y *= oneOverLen; Tmp.z *= oneOverLen; } T const AngleRad(angle); T const Sin = sin(AngleRad * T(0.5)); return q * qua(cos(AngleRad * T(0.5)), Tmp.x * Sin, Tmp.y * Sin, Tmp.z * Sin); //return gtc::quaternion::cross(q, qua(cos(AngleRad * T(0.5)), Tmp.x * fSin, Tmp.y * fSin, Tmp.z * fSin)); } template GLM_FUNC_QUALIFIER vec<3, T, Q> eulerAngles(qua const& x) { return vec<3, T, Q>(pitch(x), yaw(x), roll(x)); } template GLM_FUNC_QUALIFIER T roll(qua const& q) { return static_cast(atan(static_cast(2) * (q.x * q.y + q.w * q.z), q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z)); } template GLM_FUNC_QUALIFIER T pitch(qua const& q) { //return T(atan(T(2) * (q.y * q.z + q.w * q.x), q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z)); T const y = static_cast(2) * (q.y * q.z + q.w * q.x); T const x = q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z; if(all(equal(vec<2, T, Q>(x, y), vec<2, T, Q>(0), epsilon()))) //avoid atan2(0,0) - handle singularity - Matiis return static_cast(static_cast(2) * atan(q.x, q.w)); return static_cast(atan(y, x)); } template GLM_FUNC_QUALIFIER T yaw(qua const& q) { return asin(clamp(static_cast(-2) * (q.x * q.z - q.w * q.y), static_cast(-1), static_cast(1))); } template GLM_FUNC_QUALIFIER mat<3, 3, T, Q> mat3_cast(qua const& q) { mat<3, 3, T, Q> Result(T(1)); T qxx(q.x * q.x); T qyy(q.y * q.y); T qzz(q.z * q.z); T qxz(q.x * q.z); T qxy(q.x * q.y); T qyz(q.y * q.z); T qwx(q.w * q.x); T qwy(q.w * q.y); T qwz(q.w * q.z); Result[0][0] = T(1) - T(2) * (qyy + qzz); Result[0][1] = T(2) * (qxy + qwz); Result[0][2] = T(2) * (qxz - qwy); Result[1][0] = T(2) * (qxy - qwz); Result[1][1] = T(1) - T(2) * (qxx + qzz); Result[1][2] = T(2) * (qyz + qwx); Result[2][0] = T(2) * (qxz + qwy); Result[2][1] = T(2) * (qyz - qwx); Result[2][2] = T(1) - T(2) * (qxx + qyy); return Result; } template GLM_FUNC_QUALIFIER mat<4, 4, T, Q> mat4_cast(qua const& q) { return mat<4, 4, T, Q>(mat3_cast(q)); } template GLM_FUNC_QUALIFIER qua quat_cast(mat<3, 3, T, Q> const& m) { T fourXSquaredMinus1 = m[0][0] - m[1][1] - m[2][2]; T fourYSquaredMinus1 = m[1][1] - m[0][0] - m[2][2]; T fourZSquaredMinus1 = m[2][2] - m[0][0] - m[1][1]; T fourWSquaredMinus1 = m[0][0] + m[1][1] + m[2][2]; int biggestIndex = 0; T fourBiggestSquaredMinus1 = fourWSquaredMinus1; if(fourXSquaredMinus1 > fourBiggestSquaredMinus1) { fourBiggestSquaredMinus1 = fourXSquaredMinus1; biggestIndex = 1; } if(fourYSquaredMinus1 > fourBiggestSquaredMinus1) { fourBiggestSquaredMinus1 = fourYSquaredMinus1; biggestIndex = 2; } if(fourZSquaredMinus1 > fourBiggestSquaredMinus1) { fourBiggestSquaredMinus1 = fourZSquaredMinus1; biggestIndex = 3; } T biggestVal = sqrt(fourBiggestSquaredMinus1 + static_cast(1)) * static_cast(0.5); T mult = static_cast(0.25) / biggestVal; switch(biggestIndex) { case 0: return qua(biggestVal, (m[1][2] - m[2][1]) * mult, (m[2][0] - m[0][2]) * mult, (m[0][1] - m[1][0]) * mult); case 1: return qua((m[1][2] - m[2][1]) * mult, biggestVal, (m[0][1] + m[1][0]) * mult, (m[2][0] + m[0][2]) * mult); case 2: return qua((m[2][0] - m[0][2]) * mult, (m[0][1] + m[1][0]) * mult, biggestVal, (m[1][2] + m[2][1]) * mult); case 3: return qua((m[0][1] - m[1][0]) * mult, (m[2][0] + m[0][2]) * mult, (m[1][2] + m[2][1]) * mult, biggestVal); default: // Silence a -Wswitch-default warning in GCC. Should never actually get here. Assert is just for sanity. assert(false); return qua(1, 0, 0, 0); } } template GLM_FUNC_QUALIFIER qua quat_cast(mat<4, 4, T, Q> const& m4) { return quat_cast(mat<3, 3, T, Q>(m4)); } template GLM_FUNC_QUALIFIER T angle(qua const& x) { return acos(x.w) * static_cast(2); } template GLM_FUNC_QUALIFIER vec<3, T, Q> axis(qua const& x) { T tmp1 = static_cast(1) - x.w * x.w; if(tmp1 <= static_cast(0)) return vec<3, T, Q>(0, 0, 1); T tmp2 = static_cast(1) / sqrt(tmp1); return vec<3, T, Q>(x.x * tmp2, x.y * tmp2, x.z * tmp2); } template GLM_FUNC_QUALIFIER qua angleAxis(T const& angle, vec<3, T, Q> const& v) { qua Result; T const a(angle); T const s = glm::sin(a * static_cast(0.5)); Result.w = glm::cos(a * static_cast(0.5)); Result.x = v.x * s; Result.y = v.y * s; Result.z = v.z * s; return Result; } template GLM_FUNC_QUALIFIER vec<4, bool, Q> isnan(qua const& q) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'isnan' only accept floating-point inputs"); return vec<4, bool, Q>(isnan(q.x), isnan(q.y), isnan(q.z), isnan(q.w)); } template GLM_FUNC_QUALIFIER vec<4, bool, Q> isinf(qua const& q) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'isinf' only accept floating-point inputs"); return vec<4, bool, Q>(isinf(q.x), isinf(q.y), isinf(q.z), isinf(q.w)); } template GLM_FUNC_QUALIFIER vec<4, bool, Q> lessThan(qua const& x, qua const& y) { vec<4, bool, Q> Result; for(length_t i = 0; i < x.length(); ++i) Result[i] = x[i] < y[i]; return Result; } template GLM_FUNC_QUALIFIER vec<4, bool, Q> lessThanEqual(qua const& x, qua const& y) { vec<4, bool, Q> Result; for(length_t i = 0; i < x.length(); ++i) Result[i] = x[i] <= y[i]; return Result; } template GLM_FUNC_QUALIFIER vec<4, bool, Q> greaterThan(qua const& x, qua const& y) { vec<4, bool, Q> Result; for(length_t i = 0; i < x.length(); ++i) Result[i] = x[i] > y[i]; return Result; } template GLM_FUNC_QUALIFIER vec<4, bool, Q> greaterThanEqual(qua const& x, qua const& y) { vec<4, bool, Q> Result; for(length_t i = 0; i < x.length(); ++i) Result[i] = x[i] >= y[i]; return Result; } }//namespace glm #if GLM_CONFIG_SIMD == GLM_ENABLE # include "quaternion_simd.inl" #endif