blob: f062b0a636298afc0e21bdf519c94c26100537e2 [file] [log] [blame]
 /// @ref gtc_quaternion /// @file glm/gtc/quaternion.inl #include "../trigonometric.hpp" #include "../geometric.hpp" #include "../exponential.hpp" #include namespace glm{ namespace detail { template struct compute_dot { static GLM_FUNC_QUALIFIER T call(tquat const& x, tquat const& y) { tvec4 tmp(x.x * y.x, x.y * y.y, x.z * y.z, x.w * y.w); return (tmp.x + tmp.y) + (tmp.z + tmp.w); } }; template struct compute_quat_add { static tquat call(tquat const& q, tquat const& p) { return tquat(q.w + p.w, q.x + p.x, q.y + p.y, q.z + p.z); } }; template struct compute_quat_sub { static tquat call(tquat const& q, tquat const& p) { return tquat(q.w - p.w, q.x - p.x, q.y - p.y, q.z - p.z); } }; template struct compute_quat_mul_scalar { static tquat call(tquat const& q, T s) { return tquat(q.w * s, q.x * s, q.y * s, q.z * s); } }; template struct compute_quat_div_scalar { static tquat call(tquat const& q, T s) { return tquat(q.w / s, q.x / s, q.y / s, q.z / s); } }; template struct compute_quat_mul_vec4 { static tvec4 call(tquat const & q, tvec4 const & v) { return tvec4(q * tvec3(v), v.w); } }; }//namespace detail // -- Component accesses -- template GLM_FUNC_QUALIFIER GLM_CONSTEXPR typename tquat::length_type tquat::length() const { return 4; } template GLM_FUNC_QUALIFIER T & tquat::operator[](typename tquat::length_type i) { assert(i >= 0 && i < this->length()); return (&x)[i]; } template GLM_FUNC_QUALIFIER T const & tquat::operator[](typename tquat::length_type i) const { assert(i >= 0 && i < this->length()); return (&x)[i]; } // -- Implicit basic constructors -- # if !GLM_HAS_DEFAULTED_FUNCTIONS || !defined(GLM_FORCE_NO_CTOR_INIT) template GLM_FUNC_QUALIFIER GLM_CONSTEXPR tquat::tquat() # ifndef GLM_FORCE_NO_CTOR_INIT : x(0), y(0), z(0), w(1) # endif {} # endif # if !GLM_HAS_DEFAULTED_FUNCTIONS template GLM_FUNC_QUALIFIER GLM_CONSTEXPR tquat::tquat(tquat const & q) : x(q.x), y(q.y), z(q.z), w(q.w) {} # endif//!GLM_HAS_DEFAULTED_FUNCTIONS template template GLM_FUNC_QUALIFIER GLM_CONSTEXPR tquat::tquat(tquat const & q) : x(q.x), y(q.y), z(q.z), w(q.w) {} // -- Explicit basic constructors -- template GLM_FUNC_QUALIFIER GLM_CONSTEXPR_CTOR tquat::tquat(ctor) {} template GLM_FUNC_QUALIFIER GLM_CONSTEXPR tquat::tquat(T const & s, tvec3 const & v) : x(v.x), y(v.y), z(v.z), w(s) {} template GLM_FUNC_QUALIFIER GLM_CONSTEXPR tquat::tquat(T const & w, T const & x, T const & y, T const & z) : x(x), y(y), z(z), w(w) {} // -- Conversion constructors -- template template GLM_FUNC_QUALIFIER GLM_CONSTEXPR tquat::tquat(tquat const & q) : x(static_cast(q.x)) , y(static_cast(q.y)) , z(static_cast(q.z)) , w(static_cast(q.w)) {} //template //GLM_FUNC_QUALIFIER tquat::tquat //( // valType const & pitch, // valType const & yaw, // valType const & roll //) //{ // tvec3 eulerAngle(pitch * valType(0.5), yaw * valType(0.5), roll * valType(0.5)); // tvec3 c = glm::cos(eulerAngle * valType(0.5)); // tvec3 s = glm::sin(eulerAngle * valType(0.5)); // // this->w = c.x * c.y * c.z + s.x * s.y * s.z; // this->x = s.x * c.y * c.z - c.x * s.y * s.z; // this->y = c.x * s.y * c.z + s.x * c.y * s.z; // this->z = c.x * c.y * s.z - s.x * s.y * c.z; //} template GLM_FUNC_QUALIFIER tquat::tquat(tvec3 const & u, tvec3 const & v) { tvec3 const LocalW(cross(u, v)); T Dot = detail::compute_dot::value>::call(u, v); tquat q(T(1) + Dot, LocalW.x, LocalW.y, LocalW.z); *this = normalize(q); } template GLM_FUNC_QUALIFIER tquat::tquat(tvec3 const & eulerAngle) { tvec3 c = glm::cos(eulerAngle * T(0.5)); tvec3 s = glm::sin(eulerAngle * T(0.5)); this->w = c.x * c.y * c.z + s.x * s.y * s.z; this->x = s.x * c.y * c.z - c.x * s.y * s.z; this->y = c.x * s.y * c.z + s.x * c.y * s.z; this->z = c.x * c.y * s.z - s.x * s.y * c.z; } template GLM_FUNC_QUALIFIER tquat::tquat(tmat3x3 const & m) { *this = quat_cast(m); } template GLM_FUNC_QUALIFIER tquat::tquat(tmat4x4 const & m) { *this = quat_cast(m); } # if GLM_HAS_EXPLICIT_CONVERSION_OPERATORS template GLM_FUNC_QUALIFIER tquat::operator tmat3x3() { return mat3_cast(*this); } template GLM_FUNC_QUALIFIER tquat::operator tmat4x4() { return mat4_cast(*this); } # endif//GLM_HAS_EXPLICIT_CONVERSION_OPERATORS template GLM_FUNC_QUALIFIER tquat conjugate(tquat const & q) { return tquat(q.w, -q.x, -q.y, -q.z); } template GLM_FUNC_QUALIFIER tquat inverse(tquat const & q) { return conjugate(q) / dot(q, q); } // -- Unary arithmetic operators -- # if !GLM_HAS_DEFAULTED_FUNCTIONS template GLM_FUNC_QUALIFIER tquat & tquat::operator=(tquat const & q) { this->w = q.w; this->x = q.x; this->y = q.y; this->z = q.z; return *this; } # endif//!GLM_HAS_DEFAULTED_FUNCTIONS template template GLM_FUNC_QUALIFIER tquat & tquat::operator=(tquat const & q) { this->w = static_cast(q.w); this->x = static_cast(q.x); this->y = static_cast(q.y); this->z = static_cast(q.z); return *this; } template template GLM_FUNC_QUALIFIER tquat & tquat::operator+=(tquat const& q) { return (*this = detail::compute_quat_add::value>::call(*this, tquat(q))); } template template GLM_FUNC_QUALIFIER tquat & tquat::operator-=(tquat const& q) { return (*this = detail::compute_quat_sub::value>::call(*this, tquat(q))); } template template GLM_FUNC_QUALIFIER tquat & tquat::operator*=(tquat const & r) { tquat const p(*this); tquat const q(r); this->w = p.w * q.w - p.x * q.x - p.y * q.y - p.z * q.z; this->x = p.w * q.x + p.x * q.w + p.y * q.z - p.z * q.y; this->y = p.w * q.y + p.y * q.w + p.z * q.x - p.x * q.z; this->z = p.w * q.z + p.z * q.w + p.x * q.y - p.y * q.x; return *this; } template template GLM_FUNC_QUALIFIER tquat & tquat::operator*=(U s) { return (*this = detail::compute_quat_mul_scalar::value>::call(*this, static_cast(s))); } template template GLM_FUNC_QUALIFIER tquat & tquat::operator/=(U s) { return (*this = detail::compute_quat_div_scalar::value>::call(*this, static_cast(s))); } // -- Unary bit operators -- template GLM_FUNC_QUALIFIER tquat operator+(tquat const & q) { return q; } template GLM_FUNC_QUALIFIER tquat operator-(tquat const & q) { return tquat(-q.w, -q.x, -q.y, -q.z); } // -- Binary operators -- template GLM_FUNC_QUALIFIER tquat operator+(tquat const & q, tquat const & p) { return tquat(q) += p; } template GLM_FUNC_QUALIFIER tquat operator*(tquat const & q, tquat const & p) { return tquat(q) *= p; } template GLM_FUNC_QUALIFIER tvec3 operator*(tquat const & q, tvec3 const & v) { tvec3 const QuatVector(q.x, q.y, q.z); tvec3 const uv(glm::cross(QuatVector, v)); tvec3 const uuv(glm::cross(QuatVector, uv)); return v + ((uv * q.w) + uuv) * static_cast(2); } template GLM_FUNC_QUALIFIER tvec3 operator*(tvec3 const & v, tquat const & q) { return glm::inverse(q) * v; } template GLM_FUNC_QUALIFIER tvec4 operator*(tquat const& q, tvec4 const& v) { return detail::compute_quat_mul_vec4::value>::call(q, v); } template GLM_FUNC_QUALIFIER tvec4 operator*(tvec4 const & v, tquat const & q) { return glm::inverse(q) * v; } template GLM_FUNC_QUALIFIER tquat operator*(tquat const & q, T const & s) { return tquat( q.w * s, q.x * s, q.y * s, q.z * s); } template GLM_FUNC_QUALIFIER tquat operator*(T const & s, tquat const & q) { return q * s; } template GLM_FUNC_QUALIFIER tquat operator/(tquat const & q, T const & s) { return tquat( q.w / s, q.x / s, q.y / s, q.z / s); } // -- Boolean operators -- template GLM_FUNC_QUALIFIER bool operator==(tquat const & q1, tquat const & q2) { return (q1.x == q2.x) && (q1.y == q2.y) && (q1.z == q2.z) && (q1.w == q2.w); } template GLM_FUNC_QUALIFIER bool operator!=(tquat const & q1, tquat const & q2) { return (q1.x != q2.x) || (q1.y != q2.y) || (q1.z != q2.z) || (q1.w != q2.w); } // -- Operations -- template GLM_FUNC_QUALIFIER T length(tquat const & q) { return glm::sqrt(dot(q, q)); } template GLM_FUNC_QUALIFIER tquat normalize(tquat const & q) { T len = length(q); if(len <= T(0)) // Problem return tquat(1, 0, 0, 0); T oneOverLen = T(1) / len; return tquat(q.w * oneOverLen, q.x * oneOverLen, q.y * oneOverLen, q.z * oneOverLen); } template GLM_FUNC_QUALIFIER tquat cross(tquat const & q1, tquat const & q2) { return tquat( q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z, q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y, q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z, q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x); } /* // (x * sin(1 - a) * angle / sin(angle)) + (y * sin(a) * angle / sin(angle)) template GLM_FUNC_QUALIFIER tquat mix(tquat const & x, tquat const & y, T const & a) { if(a <= T(0)) return x; if(a >= T(1)) return y; float fCos = dot(x, y); tquat y2(y); //BUG!!! tquat 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 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 mix2 ( tquat const & x, tquat 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 tquat mix(tquat const & x, tquat 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 tquat( 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 tquat lerp(tquat const & x, tquat 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 tquat slerp(tquat const & x, tquat const & y, T a) { tquat 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 tquat( 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 tquat rotate(tquat const & q, T const & angle, tvec3 const & v) { tvec3 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 * tquat(cos(AngleRad * T(0.5)), Tmp.x * Sin, Tmp.y * Sin, Tmp.z * Sin); //return gtc::quaternion::cross(q, tquat(cos(AngleRad * T(0.5)), Tmp.x * fSin, Tmp.y * fSin, Tmp.z * fSin)); } template GLM_FUNC_QUALIFIER tvec3 eulerAngles(tquat const & x) { return tvec3(pitch(x), yaw(x), roll(x)); } template GLM_FUNC_QUALIFIER T roll(tquat const & q) { return T(atan(T(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(tquat 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)); } template GLM_FUNC_QUALIFIER T yaw(tquat const & q) { return asin(clamp(T(-2) * (q.x * q.z - q.w * q.y), T(-1), T(1))); } template GLM_FUNC_QUALIFIER tmat3x3 mat3_cast(tquat const & q) { tmat3x3 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 tmat4x4 mat4_cast(tquat const & q) { return tmat4x4(mat3_cast(q)); } template GLM_FUNC_QUALIFIER tquat quat_cast(tmat3x3 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 + T(1)) * T(0.5); T mult = static_cast(0.25) / biggestVal; tquat Result(uninitialize); switch(biggestIndex) { case 0: Result.w = biggestVal; Result.x = (m[1][2] - m[2][1]) * mult; Result.y = (m[2][0] - m[0][2]) * mult; Result.z = (m[0][1] - m[1][0]) * mult; break; case 1: Result.w = (m[1][2] - m[2][1]) * mult; Result.x = biggestVal; Result.y = (m[0][1] + m[1][0]) * mult; Result.z = (m[2][0] + m[0][2]) * mult; break; case 2: Result.w = (m[2][0] - m[0][2]) * mult; Result.x = (m[0][1] + m[1][0]) * mult; Result.y = biggestVal; Result.z = (m[1][2] + m[2][1]) * mult; break; case 3: Result.w = (m[0][1] - m[1][0]) * mult; Result.x = (m[2][0] + m[0][2]) * mult; Result.y = (m[1][2] + m[2][1]) * mult; Result.z = biggestVal; break; default: // Silence a -Wswitch-default warning in GCC. Should never actually get here. Assert is just for sanity. assert(false); break; } return Result; } template GLM_FUNC_QUALIFIER tquat quat_cast(tmat4x4 const & m4) { return quat_cast(tmat3x3(m4)); } template GLM_FUNC_QUALIFIER T angle(tquat const & x) { return acos(x.w) * T(2); } template GLM_FUNC_QUALIFIER tvec3 axis(tquat const & x) { T tmp1 = static_cast(1) - x.w * x.w; if(tmp1 <= static_cast(0)) return tvec3(0, 0, 1); T tmp2 = static_cast(1) / sqrt(tmp1); return tvec3(x.x * tmp2, x.y * tmp2, x.z * tmp2); } template GLM_FUNC_QUALIFIER tquat angleAxis(T const & angle, tvec3 const & v) { tquat Result(uninitialize); 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 tvec4 lessThan(tquat const & x, tquat const & y) { tvec4 Result(uninitialize); for(length_t i = 0; i < x.length(); ++i) Result[i] = x[i] < y[i]; return Result; } template GLM_FUNC_QUALIFIER tvec4 lessThanEqual(tquat const & x, tquat const & y) { tvec4 Result(uninitialize); for(length_t i = 0; i < x.length(); ++i) Result[i] = x[i] <= y[i]; return Result; } template GLM_FUNC_QUALIFIER tvec4 greaterThan(tquat const & x, tquat const & y) { tvec4 Result(uninitialize); for(length_t i = 0; i < x.length(); ++i) Result[i] = x[i] > y[i]; return Result; } template GLM_FUNC_QUALIFIER tvec4 greaterThanEqual(tquat const & x, tquat const & y) { tvec4 Result(uninitialize); for(length_t i = 0; i < x.length(); ++i) Result[i] = x[i] >= y[i]; return Result; } template GLM_FUNC_QUALIFIER tvec4 equal(tquat const & x, tquat const & y) { tvec4 Result(uninitialize); for(length_t i = 0; i < x.length(); ++i) Result[i] = x[i] == y[i]; return Result; } template GLM_FUNC_QUALIFIER tvec4 notEqual(tquat const & x, tquat const & y) { tvec4 Result(uninitialize); for(length_t i = 0; i < x.length(); ++i) Result[i] = x[i] != y[i]; return Result; } template GLM_FUNC_QUALIFIER tvec4 isnan(tquat const& q) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'isnan' only accept floating-point inputs"); return tvec4(isnan(q.x), isnan(q.y), isnan(q.z), isnan(q.w)); } template GLM_FUNC_QUALIFIER tvec4 isinf(tquat const& q) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'isinf' only accept floating-point inputs"); return tvec4(isinf(q.x), isinf(q.y), isinf(q.z), isinf(q.w)); } }//namespace glm #if GLM_ARCH != GLM_ARCH_PURE && GLM_HAS_ALIGNED_TYPE # include "quaternion_simd.inl" #endif