#include "../trigonometric.hpp" #include "../exponential.hpp" #include "../ext/quaternion_geometric.hpp" #include namespace glm{ namespace detail { template struct genTypeTrait > { static const genTypeEnum GENTYPE = GENTYPE_QUAT; }; template struct compute_dot, T, Aligned> { GLM_FUNC_QUALIFIER GLM_CONSTEXPR static T call(qua const& a, qua const& b) { vec<4, T, Q> tmp(a.w * b.w, a.x * b.x, a.y * b.y, a.z * b.z); return (tmp.x + tmp.y) + (tmp.z + tmp.w); } }; template struct compute_quat_add { GLM_FUNC_QUALIFIER GLM_CONSTEXPR static qua call(qua const& q, qua const& p) { return qua(q.w + p.w, q.x + p.x, q.y + p.y, q.z + p.z); } }; template struct compute_quat_sub { GLM_FUNC_QUALIFIER GLM_CONSTEXPR static qua call(qua const& q, qua const& p) { return qua(q.w - p.w, q.x - p.x, q.y - p.y, q.z - p.z); } }; template struct compute_quat_mul_scalar { GLM_FUNC_QUALIFIER GLM_CONSTEXPR static qua call(qua const& q, T s) { return qua(q.w * s, q.x * s, q.y * s, q.z * s); } }; template struct compute_quat_div_scalar { GLM_FUNC_QUALIFIER GLM_CONSTEXPR static qua call(qua const& q, T s) { return qua(q.w / s, q.x / s, q.y / s, q.z / s); } }; template struct compute_quat_mul_vec4 { GLM_FUNC_QUALIFIER GLM_CONSTEXPR static vec<4, T, Q> call(qua const& q, vec<4, T, Q> const& v) { return vec<4, T, Q>(q * vec<3, T, Q>(v), v.w); } }; }//namespace detail // -- Component accesses -- template GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & qua::operator[](typename qua::length_type i) { assert(i >= 0 && i < this->length()); # ifdef GLM_FORCE_QUAT_DATA_WXYZ return (&w)[i]; # else return (&x)[i]; # endif } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& qua::operator[](typename qua::length_type i) const { assert(i >= 0 && i < this->length()); # ifdef GLM_FORCE_QUAT_DATA_WXYZ return (&w)[i]; # else return (&x)[i]; # endif } // -- Implicit basic constructors -- # if GLM_CONFIG_DEFAULTED_FUNCTIONS == GLM_DISABLE template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua::qua() # if GLM_CONFIG_CTOR_INIT != GLM_CTOR_INIT_DISABLE # ifdef GLM_FORCE_QUAT_DATA_WXYZ : w(1), x(0), y(0), z(0) # else : x(0), y(0), z(0), w(1) # endif # endif {} template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua::qua(qua const& q) # ifdef GLM_FORCE_QUAT_DATA_WXYZ : w(q.w), x(q.x), y(q.y), z(q.z) # else : x(q.x), y(q.y), z(q.z), w(q.w) # endif {} # endif template template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua::qua(qua const& q) # ifdef GLM_FORCE_QUAT_DATA_WXYZ : w(q.w), x(q.x), y(q.y), z(q.z) # else : x(q.x), y(q.y), z(q.z), w(q.w) # endif {} // -- Explicit basic constructors -- template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua::qua(T s, vec<3, T, Q> const& v) # ifdef GLM_FORCE_QUAT_DATA_WXYZ : w(s), x(v.x), y(v.y), z(v.z) # else : x(v.x), y(v.y), z(v.z), w(s) # endif {} template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua::qua(T _w, T _x, T _y, T _z) # ifdef GLM_FORCE_QUAT_DATA_WXYZ : w(_w), x(_x), y(_y), z(_z) # else : x(_x), y(_y), z(_z), w(_w) # endif {} // -- Conversion constructors -- template template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua::qua(qua const& q) # ifdef GLM_FORCE_QUAT_DATA_WXYZ : w(static_cast(q.w)), x(static_cast(q.x)), y(static_cast(q.y)), z(static_cast(q.z)) # else : x(static_cast(q.x)), y(static_cast(q.y)), z(static_cast(q.z)), w(static_cast(q.w)) # endif {} //template //GLM_FUNC_QUALIFIER qua::qua //( // valType const& pitch, // valType const& yaw, // valType const& roll //) //{ // vec<3, valType> eulerAngle(pitch * valType(0.5), yaw * valType(0.5), roll * valType(0.5)); // vec<3, valType> c = glm::cos(eulerAngle * valType(0.5)); // vec<3, valType> 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 qua::qua(vec<3, T, Q> const& u, vec<3, T, Q> const& v) { T norm_u_norm_v = sqrt(dot(u, u) * dot(v, v)); T real_part = norm_u_norm_v + dot(u, v); vec<3, T, Q> t; if(real_part < static_cast(1.e-6f) * norm_u_norm_v) { // If u and v are exactly opposite, rotate 180 degrees // around an arbitrary orthogonal axis. Axis normalisation // can happen later, when we normalise the quaternion. real_part = static_cast(0); t = abs(u.x) > abs(u.z) ? vec<3, T, Q>(-u.y, u.x, static_cast(0)) : vec<3, T, Q>(static_cast(0), -u.z, u.y); } else { // Otherwise, build quaternion the standard way. t = cross(u, v); } *this = normalize(qua(real_part, t.x, t.y, t.z)); } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua::qua(vec<3, T, Q> const& eulerAngle) { vec<3, T, Q> c = glm::cos(eulerAngle * T(0.5)); vec<3, T, Q> 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 qua::qua(mat<3, 3, T, Q> const& m) { *this = quat_cast(m); } template GLM_FUNC_QUALIFIER qua::qua(mat<4, 4, T, Q> const& m) { *this = quat_cast(m); } # if GLM_HAS_EXPLICIT_CONVERSION_OPERATORS template GLM_FUNC_QUALIFIER qua::operator mat<3, 3, T, Q>() const { return mat3_cast(*this); } template GLM_FUNC_QUALIFIER qua::operator mat<4, 4, T, Q>() const { return mat4_cast(*this); } # endif//GLM_HAS_EXPLICIT_CONVERSION_OPERATORS // -- Unary arithmetic operators -- # if GLM_CONFIG_DEFAULTED_FUNCTIONS == GLM_DISABLE template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua & qua::operator=(qua const& q) { this->w = q.w; this->x = q.x; this->y = q.y; this->z = q.z; return *this; } # endif template template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua & qua::operator=(qua 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 GLM_CONSTEXPR qua & qua::operator+=(qua const& q) { return (*this = detail::compute_quat_add::value>::call(*this, qua(q))); } template template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua & qua::operator-=(qua const& q) { return (*this = detail::compute_quat_sub::value>::call(*this, qua(q))); } template template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua & qua::operator*=(qua const& r) { qua const p(*this); qua 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 GLM_CONSTEXPR qua & qua::operator*=(U s) { return (*this = detail::compute_quat_mul_scalar::value>::call(*this, static_cast(s))); } template template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua & qua::operator/=(U s) { return (*this = detail::compute_quat_div_scalar::value>::call(*this, static_cast(s))); } // -- Unary bit operators -- template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua operator+(qua const& q) { return q; } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua operator-(qua const& q) { return qua(-q.w, -q.x, -q.y, -q.z); } // -- Binary operators -- template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua operator+(qua const& q, qua const& p) { return qua(q) += p; } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua operator-(qua const& q, qua const& p) { return qua(q) -= p; } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua operator*(qua const& q, qua const& p) { return qua(q) *= p; } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<3, T, Q> operator*(qua const& q, vec<3, T, Q> const& v) { vec<3, T, Q> const QuatVector(q.x, q.y, q.z); vec<3, T, Q> const uv(glm::cross(QuatVector, v)); vec<3, T, Q> const uuv(glm::cross(QuatVector, uv)); return v + ((uv * q.w) + uuv) * static_cast(2); } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<3, T, Q> operator*(vec<3, T, Q> const& v, qua const& q) { return glm::inverse(q) * v; } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<4, T, Q> operator*(qua const& q, vec<4, T, Q> const& v) { return detail::compute_quat_mul_vec4::value>::call(q, v); } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<4, T, Q> operator*(vec<4, T, Q> const& v, qua const& q) { return glm::inverse(q) * v; } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua operator*(qua const& q, T const& s) { return qua( q.w * s, q.x * s, q.y * s, q.z * s); } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua operator*(T const& s, qua const& q) { return q * s; } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua operator/(qua const& q, T const& s) { return qua( q.w / s, q.x / s, q.y / s, q.z / s); } // -- Boolean operators -- template GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool operator==(qua const& q1, qua const& q2) { return q1.x == q2.x && q1.y == q2.y && q1.z == q2.z && q1.w == q2.w; } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool operator!=(qua const& q1, qua const& q2) { return q1.x != q2.x || q1.y != q2.y || q1.z != q2.z || q1.w != q2.w; } }//namespace glm #if GLM_CONFIG_SIMD == GLM_ENABLE # include "type_quat_simd.inl" #endif