/// @ref gtx_quaternion #include #include "../gtc/constants.hpp" namespace glm { template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua quat_identity() { return qua(static_cast(1), static_cast(0), static_cast(0), static_cast(0)); } template GLM_FUNC_QUALIFIER vec<3, T, Q> cross(vec<3, T, Q> const& v, qua const& q) { return inverse(q) * v; } template GLM_FUNC_QUALIFIER vec<3, T, Q> cross(qua const& q, vec<3, T, Q> const& v) { return q * v; } template GLM_FUNC_QUALIFIER qua squad ( qua const& q1, qua const& q2, qua const& s1, qua const& s2, T const& h) { return mix(mix(q1, q2, h), mix(s1, s2, h), static_cast(2) * (static_cast(1) - h) * h); } template GLM_FUNC_QUALIFIER qua intermediate ( qua const& prev, qua const& curr, qua const& next ) { qua invQuat = inverse(curr); return exp((log(next * invQuat) + log(prev * invQuat)) / static_cast(-4)) * curr; } template GLM_FUNC_QUALIFIER vec<3, T, Q> rotate(qua const& q, vec<3, T, Q> const& v) { return q * v; } template GLM_FUNC_QUALIFIER vec<4, T, Q> rotate(qua const& q, vec<4, T, Q> const& v) { return q * v; } template GLM_FUNC_QUALIFIER T extractRealComponent(qua const& q) { T w = static_cast(1) - q.x * q.x - q.y * q.y - q.z * q.z; if(w < T(0)) return T(0); else return -sqrt(w); } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR T length2(qua const& q) { return q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w; } template GLM_FUNC_QUALIFIER qua shortMix(qua const& x, qua const& y, T const& a) { if(a <= static_cast(0)) return x; if(a >= static_cast(1)) return y; T fCos = dot(x, y); qua y2(y); //BUG!!! qua y2; if(fCos < static_cast(0)) { y2 = -y; fCos = -fCos; } //if(fCos > 1.0f) // problem T k0, k1; if(fCos > (static_cast(1) - epsilon())) { k0 = static_cast(1) - a; k1 = static_cast(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((static_cast(1) - a) * fAngle) * fOneOverSin; k1 = sin((static_cast(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 fastMix(qua const& x, qua const& y, T const& a) { return glm::normalize(x * (static_cast(1) - a) + (y * a)); } template GLM_FUNC_QUALIFIER qua rotation(vec<3, T, Q> const& orig, vec<3, T, Q> const& dest) { T cosTheta = dot(orig, dest); vec<3, T, Q> rotationAxis; if(cosTheta >= static_cast(1) - epsilon()) { // orig and dest point in the same direction return quat_identity(); } if(cosTheta < static_cast(-1) + epsilon()) { // special case when vectors in opposite directions : // there is no "ideal" rotation axis // So guess one; any will do as long as it's perpendicular to start // This implementation favors a rotation around the Up axis (Y), // since it's often what you want to do. rotationAxis = cross(vec<3, T, Q>(0, 0, 1), orig); if(length2(rotationAxis) < epsilon()) // bad luck, they were parallel, try again! rotationAxis = cross(vec<3, T, Q>(1, 0, 0), orig); rotationAxis = normalize(rotationAxis); return angleAxis(pi(), rotationAxis); } // Implementation from Stan Melax's Game Programming Gems 1 article rotationAxis = cross(orig, dest); T s = sqrt((T(1) + cosTheta) * static_cast(2)); T invs = static_cast(1) / s; return qua( s * static_cast(0.5f), rotationAxis.x * invs, rotationAxis.y * invs, rotationAxis.z * invs); } }//namespace glm