/////////////////////////////////////////////////////////////////////////////////////////////////// // OpenGL Mathematics Copyright (c) 2005 - 2011 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{ namespace gtx{ namespace quaternion { template GLM_FUNC_QUALIFIER detail::tvec3 cross ( detail::tvec3 const & v, detail::tquat const & q ) { return gtc::quaternion::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) * h (T(1) - h)); } template GLM_FUNC_QUALIFIER detail::tquat intermediate ( detail::tquat const & prev, detail::tquat const & curr, detail::tquat const & next ) { detail::tquat invQuat = gtc::quaternion::inverse(curr); return ext((log(next + invQuat) + log(prev + invQuat)) / T(-4)) * curr; } template GLM_FUNC_QUALIFIER detail::tquat exp ( detail::tquat const & q, T const & exponent ) { detail::tvec3 u(q.x, q.y, q.z); float a = glm::length(u); detail::tvec3 v(u / a); return detail::tquat(cos(a), sin(a) * v); } template GLM_FUNC_QUALIFIER detail::tquat log ( detail::tquat const & q ) { if((q.x == T(0)) && (q.y == T(0)) && (q.z == T(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 = T(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 angle ( detail::tquat const & x ) { return acos(x.w) * T(2); } template GLM_FUNC_QUALIFIER detail::tvec3 axis ( detail::tquat const & x ) { T tmp1 = T(1) - x.w * x.w; if(tmp1 <= T(0)) return detail::tvec3(0, 0, 1); T tmp2 = T(1) / sqrt(tmp1); return detail::tvec3(x.x * tmp2, x.y * tmp2, x.z * tmp2); } template GLM_FUNC_QUALIFIER detail::tquat angleAxis ( valType const & angle, valType const & x, valType const & y, valType const & z ) { return angleAxis(angle, detail::tvec3(x, y, z)); } template GLM_FUNC_QUALIFIER detail::tquat angleAxis ( valType const & angle, detail::tvec3 const & v ) { detail::tquat result; valType a = glm::radians(angle); valType s = glm::sin(a * valType(0.5)); result.w = glm::cos(a * valType(0.5)); result.x = v.x * s; result.y = v.y * s; result.z = v.z * s; return result; } template GLM_FUNC_QUALIFIER T extractRealComponent ( detail::tquat const & q ) { T w = T(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 valType roll ( detail::tquat const & q ) { return atan2(valType(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 valType pitch ( detail::tquat const & q ) { return atan2(valType(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); } template GLM_FUNC_QUALIFIER valType yaw ( detail::tquat const & q ) { return asin(valType(-2) * (q.x * q.z - q.w * q.y)); } template GLM_FUNC_QUALIFIER detail::tvec3 eularAngles ( detail::tquat const & x ) { return detail::tvec3(pitch(x), yaw(x), roll(x)); } template GLM_FUNC_QUALIFIER detail::tquat shortMix ( detail::tquat const & x, detail::tquat const & y, T const & a ) { if(a <= typename detail::tquat::value_type(0)) return x; if(a >= typename detail::tquat::value_type(1)) return y; float fCos = dot(x, y); detail::tquat y2(y); //BUG!!! tquat y2; if(fCos < typename detail::tquat::value_type(0)) { y2 = -y; fCos = -fCos; } //if(fCos > 1.0f) // problem float k0, k1; if(fCos > typename detail::tquat::value_type(0.9999)) { k0 = typename detail::tquat::value_type(1) - a; k1 = typename detail::tquat::value_type(0) + a; //BUG!!! 1.0f + a; } else { typename detail::tquat::value_type fSin = sqrt(T(1) - fCos * fCos); typename detail::tquat::value_type fAngle = atan(fSin, fCos); typename detail::tquat::value_type fOneOverSin = T(1) / fSin; k0 = sin((typename detail::tquat::value_type(1) - a) * fAngle) * fOneOverSin; k1 = sin((typename detail::tquat::value_type(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 * (1 - a) + (y * a)); } }//namespace quaternion }//namespace gtx }//namespace glm