From a9abee3d7b57a094358ffbb460799d70fed7ef8b Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 10 May 2016 18:16:34 -0400 Subject: windows build --- include/glm/gtx/quaternion.inl | 283 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 283 insertions(+) create mode 100644 include/glm/gtx/quaternion.inl (limited to 'include/glm/gtx/quaternion.inl') diff --git a/include/glm/gtx/quaternion.inl b/include/glm/gtx/quaternion.inl new file mode 100644 index 0000000..b405cc5 --- /dev/null +++ b/include/glm/gtx/quaternion.inl @@ -0,0 +1,283 @@ +/////////////////////////////////////////////////////////////////////////////////// +/// OpenGL Mathematics (glm.g-truc.net) +/// +/// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net) +/// Permission is hereby granted, free of charge, to any person obtaining a copy +/// of this software and associated documentation files (the "Software"), to deal +/// in the Software without restriction, including without limitation the rights +/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +/// copies of the Software, and to permit persons to whom the Software is +/// furnished to do so, subject to the following conditions: +/// +/// The above copyright notice and this permission notice shall be included in +/// all copies or substantial portions of the Software. +/// +/// Restrictions: +/// By making use of the Software for military purposes, you choose to make +/// a Bunny unhappy. +/// +/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +/// THE SOFTWARE. +/// +/// @ref gtx_quaternion +/// @file glm/gtx/quaternion.inl +/// @date 2005-12-21 / 2011-06-07 +/// @author Christophe Riccio +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include "../gtc/constants.hpp" + +namespace glm +{ + template + GLM_FUNC_QUALIFIER tvec3 cross + ( + tvec3 const & v, + tquat const & q + ) + { + return inverse(q) * v; + } + + template + GLM_FUNC_QUALIFIER tvec3 cross + ( + tquat const & q, + tvec3 const & v + ) + { + return q * v; + } + + template + GLM_FUNC_QUALIFIER tquat squad + ( + tquat const & q1, + tquat const & q2, + tquat const & s1, + tquat 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 tquat intermediate + ( + tquat const & prev, + tquat const & curr, + tquat const & next + ) + { + tquat invQuat = inverse(curr); + return exp((log(next + invQuat) + log(prev + invQuat)) / static_cast(-4)) * curr; + } + + template + GLM_FUNC_QUALIFIER tquat exp + ( + tquat const & q + ) + { + tvec3 u(q.x, q.y, q.z); + T Angle = glm::length(u); + if (Angle < epsilon()) + return tquat(); + + tvec3 v(u / Angle); + return tquat(cos(Angle), sin(Angle) * v); + } + + template + GLM_FUNC_QUALIFIER tquat log + ( + tquat const & q + ) + { + tvec3 u(q.x, q.y, q.z); + T Vec3Len = length(u); + + if (Vec3Len < epsilon()) + { + if(q.w > static_cast(0)) + return tquat(log(q.w), static_cast(0), static_cast(0), static_cast(0)); + else if(q.w < static_cast(0)) + return tquat(log(-q.w), pi(), static_cast(0), static_cast(0)); + else + return tquat(std::numeric_limits::infinity(), std::numeric_limits::infinity(), std::numeric_limits::infinity(), std::numeric_limits::infinity()); + } + else + { + T QuatLen = sqrt(Vec3Len * Vec3Len + q.w * q.w); + T t = atan(Vec3Len, T(q.w)) / Vec3Len; + return tquat(log(QuatLen), t * q.x, t * q.y, t * q.z); + } + } + + template + GLM_FUNC_QUALIFIER tquat pow(tquat const & x, T const & y) + { + //Raising to the power of 0 should yield 1 + //Needed to prevent a division by 0 error later on + if(y > -epsilon() && y < epsilon()) + return tquat(1,0,0,0); + + //To deal with non-unit quaternions + T magnitude = sqrt(x.x * x.x + x.y * x.y + x.z * x.z + x.w *x.w); + + //Equivalent to raising a real number to a power + //Needed to prevent a division by 0 error later on + if(abs(x.w / magnitude) > static_cast(1) - epsilon() && abs(x.w / magnitude) < static_cast(1) + epsilon()) + return tquat(pow(x.w, y),0,0,0); + + T Angle = acos(x.w / magnitude); + T NewAngle = Angle * y; + T Div = sin(NewAngle) / sin(Angle); + T Mag = pow(magnitude, y-1); + + return tquat(cos(NewAngle) * magnitude * Mag, x.x * Div * Mag, x.y * Div * Mag, x.z * Div * Mag); + } + + template + GLM_FUNC_QUALIFIER tvec3 rotate + ( + tquat const & q, + tvec3 const & v + ) + { + return q * v; + } + + template + GLM_FUNC_QUALIFIER tvec4 rotate + ( + tquat const & q, + tvec4 const & v + ) + { + return q * v; + } + + template + GLM_FUNC_QUALIFIER T extractRealComponent + ( + tquat 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 T length2 + ( + tquat const & q + ) + { + return q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w; + } + + template + GLM_FUNC_QUALIFIER tquat shortMix + ( + tquat const & x, + tquat const & y, + T const & a + ) + { + if(a <= static_cast(0)) return x; + if(a >= static_cast(1)) return y; + + T fCos = dot(x, y); + tquat y2(y); //BUG!!! tquat 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 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 tquat fastMix + ( + tquat const & x, + tquat const & y, + T const & a + ) + { + return glm::normalize(x * (static_cast(1) - a) + (y * a)); + } + + template + GLM_FUNC_QUALIFIER tquat rotation + ( + tvec3 const & orig, + tvec3 const & dest + ) + { + T cosTheta = dot(orig, dest); + tvec3 rotationAxis; + + if(cosTheta >= static_cast(1) - epsilon()) + return quat(); + + 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(tvec3(0, 0, 1), orig); + if(length2(rotationAxis) < epsilon()) // bad luck, they were parallel, try again! + rotationAxis = cross(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) * static_cast(2)); + T invs = static_cast(1) / s; + + return tquat( + s * static_cast(0.5f), + rotationAxis.x * invs, + rotationAxis.y * invs, + rotationAxis.z * invs); + } + +}//namespace glm -- cgit v1.2.3