/////////////////////////////////////////////////////////////////////////////////////////////////// // OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net) /////////////////////////////////////////////////////////////////////////////////////////////////// // Created : 2005-12-21 // Updated : 2008-11-27 // Licence : This source is under MIT License // File : glm/gtx/quaternion.inl /////////////////////////////////////////////////////////////////////////////////////////////////// #include namespace glm { template GLM_FUNC_QUALIFIER detail::tvec3 cross ( detail::tvec3 const & v, detail::tquat const & q ) { return inverse(q) * v; } template GLM_FUNC_QUALIFIER detail::tvec3 cross ( detail::tquat const & q, detail::tvec3 const & v ) { return q * v; } template GLM_FUNC_QUALIFIER detail::tquat squad ( detail::tquat const & q1, detail::tquat const & q2, detail::tquat const & s1, detail::tquat const & s2, T const & h) { return mix(mix(q1, q2, h), mix(s1, s2, h), T(2) * (T(1) - h) * h); } template GLM_FUNC_QUALIFIER detail::tquat intermediate ( detail::tquat const & prev, detail::tquat const & curr, detail::tquat const & next ) { detail::tquat invQuat = inverse(curr); return exp((log(next + invQuat) + log(prev + invQuat)) / T(-4)) * curr; } template GLM_FUNC_QUALIFIER detail::tquat exp ( detail::tquat const & q ) { detail::tvec3 u(q.x, q.y, q.z); float Angle = glm::length(u); detail::tvec3 v(u / Angle); return detail::tquat(cos(Angle), sin(Angle) * v); } template GLM_FUNC_QUALIFIER detail::tquat log ( detail::tquat const & q ) { if((q.x == static_cast(0)) && (q.y == static_cast(0)) && (q.z == static_cast(0))) { if(q.w > T(0)) return detail::tquat(log(q.w), T(0), T(0), T(0)); else if(q.w < T(0)) return detail::tquat(log(-q.w), T(3.1415926535897932384626433832795), T(0),T(0)); else return detail::tquat(std::numeric_limits::infinity(), std::numeric_limits::infinity(), std::numeric_limits::infinity(), std::numeric_limits::infinity()); } else { T Vec3Len = sqrt(q.x * q.x + q.y * q.y + q.z * q.z); T QuatLen = sqrt(Vec3Len * Vec3Len + q.w * q.w); T t = atan(Vec3Len, T(q.w)) / Vec3Len; return detail::tquat(t * q.x, t * q.y, t * q.z, log(QuatLen)); } } template GLM_FUNC_QUALIFIER detail::tquat pow ( detail::tquat const & x, T const & y ) { if(abs(x.w) > T(0.9999)) return x; float Angle = acos(y); float NewAngle = Angle * y; float Div = sin(NewAngle) / sin(Angle); return detail::tquat( cos(NewAngle), x.x * Div, x.y * Div, x.z * Div); } //template //GLM_FUNC_QUALIFIER detail::tquat sqrt //( // detail::tquat const & q //) //{ // T q0 = static_cast(1) - dot(q, q); // return T(2) * (T(1) + q0) * q; //} template GLM_FUNC_QUALIFIER detail::tvec3 rotate ( detail::tquat const & q, detail::tvec3 const & v ) { return q * v; } template GLM_FUNC_QUALIFIER detail::tvec4 rotate ( detail::tquat const & q, detail::tvec4 const & v ) { return q * v; } template GLM_FUNC_QUALIFIER T extractRealComponent ( detail::tquat const & q ) { T w = static_cast(1.0) - 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 T length2 ( detail::tquat const & q ) { return q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w; } template GLM_FUNC_QUALIFIER detail::tquat shortMix ( detail::tquat const & x, detail::tquat const & y, T const & a ) { if(a <= T(0)) return x; if(a >= T(1)) return y; T fCos = dot(x, y); detail::tquat y2(y); //BUG!!! tquat y2; if(fCos < T(0)) { y2 = -y; fCos = -fCos; } //if(fCos > 1.0f) // problem T k0, k1; if(fCos > T(0.9999)) { 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((T(1) - a) * fAngle) * fOneOverSin; k1 = sin((T(0) + a) * fAngle) * fOneOverSin; } return detail::tquat( 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 detail::tquat fastMix ( detail::tquat const & x, detail::tquat const & y, T const & a ) { return glm::normalize(x * (T(1) - a) + (y * a)); } template GLM_FUNC_QUALIFIER detail::tquat rotation ( detail::tvec3 const & orig, detail::tvec3 const & dest ) { T cosTheta = dot(orig, dest); detail::tvec3 rotationAxis; if(cosTheta < T(-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(detail::tvec3(0, 0, 1), orig); if(length2(rotationAxis) < epsilon()) // bad luck, they were parallel, try again! rotationAxis = cross(detail::tvec3(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) * T(2)); T invs = static_cast(1) / s; return detail::tquat( s * T(0.5f), rotationAxis.x * invs, rotationAxis.y * invs, rotationAxis.z * invs); } }//namespace glm