123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- #pragma once
- #include <Mathematics/Vector.h>
- #include <Mathematics/Matrix.h>
- #include <Mathematics/ChebyshevRatio.h>
- namespace WwiseGTE
- {
- template <typename Real>
- class Quaternion
- {
- public:
-
-
-
-
- Quaternion() = default;
- Quaternion(Real x, Real y, Real z, Real w)
- {
- mTuple[0] = x;
- mTuple[1] = y;
- mTuple[2] = z;
- mTuple[3] = w;
- }
-
- inline Real const& operator[](int i) const
- {
- return mTuple[i];
- }
- inline Real& operator[](int i)
- {
- return mTuple[i];
- }
-
- inline bool operator==(Quaternion const& q) const
- {
- return mTuple == q.mTuple;
- }
- inline bool operator!=(Quaternion const& q) const
- {
- return mTuple != q.mTuple;
- }
- inline bool operator<(Quaternion const& q) const
- {
- return mTuple < q.mTuple;
- }
- inline bool operator<=(Quaternion const& q) const
- {
- return mTuple <= q.mTuple;
- }
- inline bool operator>(Quaternion const& q) const
- {
- return mTuple > q.mTuple;
- }
- inline bool operator>=(Quaternion const& q) const
- {
- return mTuple >= q.mTuple;
- }
-
-
- static Quaternion Zero()
- {
- return Quaternion((Real)0, (Real)0, (Real)0, (Real)0);
- }
-
- static Quaternion I()
- {
- return Quaternion((Real)1, (Real)0, (Real)0, (Real)0);
- }
-
- static Quaternion J()
- {
- return Quaternion((Real)0, (Real)1, (Real)0, (Real)0);
- }
-
- static Quaternion K()
- {
- return Quaternion((Real)0, (Real)0, (Real)1, (Real)0);
- }
-
- static Quaternion Identity()
- {
- return Quaternion((Real)0, (Real)0, (Real)0, (Real)1);
- }
- protected:
- std::array<Real, 4> mTuple;
- };
-
- template <typename Real>
- Quaternion<Real> operator+(Quaternion<Real> const& q)
- {
- return q;
- }
- template <typename Real>
- Quaternion<Real> operator-(Quaternion<Real> const& q)
- {
- Quaternion<Real> result;
- for (int i = 0; i < 4; ++i)
- {
- result[i] = -q[i];
- }
- return result;
- }
-
- template <typename Real>
- Quaternion<Real> operator+(Quaternion<Real> const& q0, Quaternion<Real> const& q1)
- {
- Quaternion<Real> result = q0;
- return result += q1;
- }
- template <typename Real>
- Quaternion<Real> operator-(Quaternion<Real> const& q0, Quaternion<Real> const& q1)
- {
- Quaternion<Real> result = q0;
- return result -= q1;
- }
- template <typename Real>
- Quaternion<Real> operator*(Quaternion<Real> const& q, Real scalar)
- {
- Quaternion<Real> result = q;
- return result *= scalar;
- }
- template <typename Real>
- Quaternion<Real> operator*(Real scalar, Quaternion<Real> const& q)
- {
- Quaternion<Real> result = q;
- return result *= scalar;
- }
- template <typename Real>
- Quaternion<Real> operator/(Quaternion<Real> const& q, Real scalar)
- {
- Quaternion<Real> result = q;
- return result /= scalar;
- }
- template <typename Real>
- Quaternion<Real>& operator+=(Quaternion<Real>& q0, Quaternion<Real> const& q1)
- {
- for (int i = 0; i < 4; ++i)
- {
- q0[i] += q1[i];
- }
- return q0;
- }
- template <typename Real>
- Quaternion<Real>& operator-=(Quaternion<Real>& q0, Quaternion<Real> const& q1)
- {
- for (int i = 0; i < 4; ++i)
- {
- q0[i] -= q1[i];
- }
- return q0;
- }
- template <typename Real>
- Quaternion<Real>& operator*=(Quaternion<Real>& q, Real scalar)
- {
- for (int i = 0; i < 4; ++i)
- {
- q[i] *= scalar;
- }
- return q;
- }
- template <typename Real>
- Quaternion<Real>& operator/=(Quaternion<Real>& q, Real scalar)
- {
- if (scalar != (Real)0)
- {
- for (int i = 0; i < 4; ++i)
- {
- q[i] /= scalar;
- }
- }
- else
- {
- for (int i = 0; i < 4; ++i)
- {
- q[i] = (Real)0;
- }
- }
- return q;
- }
-
- template <typename Real>
- Real Dot(Quaternion<Real> const& q0, Quaternion<Real> const& q1)
- {
- Real dot = q0[0] * q1[0];
- for (int i = 1; i < 4; ++i)
- {
- dot += q0[i] * q1[i];
- }
- return dot;
- }
- template <typename Real>
- Real Length(Quaternion<Real> const& q)
- {
- return std::sqrt(Dot(q, q));
- }
- template <typename Real>
- Real Normalize(Quaternion<Real>& q)
- {
- Real length = std::sqrt(Dot(q, q));
- if (length > (Real)0)
- {
- q /= length;
- }
- else
- {
- for (int i = 0; i < 4; ++i)
- {
- q[i] = (Real)0;
- }
- }
- return length;
- }
-
-
-
-
-
-
-
-
- template <typename Real>
- Quaternion<Real> operator*(Quaternion<Real> const& q0, Quaternion<Real> const& q1)
- {
-
-
-
-
-
-
- return Quaternion<Real>
- (
- +q0[0] * q1[3] + q0[1] * q1[2] - q0[2] * q1[1] + q0[3] * q1[0],
- -q0[0] * q1[2] + q0[1] * q1[3] + q0[2] * q1[0] + q0[3] * q1[1],
- +q0[0] * q1[1] - q0[1] * q1[0] + q0[2] * q1[3] + q0[3] * q1[2],
- -q0[0] * q1[0] - q0[1] * q1[1] - q0[2] * q1[2] + q0[3] * q1[3]
- );
- }
-
-
-
- template <typename Real>
- Quaternion<Real> Inverse(Quaternion<Real> const& q)
- {
- Real sqrLen = Dot(q, q);
- if (sqrLen > (Real)0)
- {
- Quaternion<Real> inverse = Conjugate(q) / sqrLen;
- return inverse;
- }
- else
- {
- return Quaternion<Real>::Zero();
- }
- }
-
- template <typename Real>
- Quaternion<Real> Conjugate(Quaternion<Real> const& q)
- {
- return Quaternion<Real>(-q[0], -q[1], -q[2], +q[3]);
- }
-
-
- template <typename Real>
- Vector<4, Real> Rotate(Quaternion<Real> const& q, Vector<4, Real> const& v)
- {
- Quaternion<Real> input(v[0], v[1], v[2], (Real)0);
- Quaternion<Real> output = q * input * Conjugate(q);
- Vector<4, Real> u{ output[0], output[1], output[2], (Real)0 };
- return u;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <typename Real>
- Quaternion<Real> Slerp(Real t, Quaternion<Real> const& q0, Quaternion<Real> const& q1)
- {
- Real cosA = Dot(q0, q1);
- Real sign;
- if (cosA >= (Real)0)
- {
- sign = (Real)1;
- }
- else
- {
- cosA = -cosA;
- sign = (Real)-1;
- }
- Real f0, f1;
- ChebyshevRatio<Real>::Get(t, cosA, f0, f1);
- return q0 * f0 + q1 * (sign * f1);
- }
-
-
-
-
-
-
-
-
-
-
-
- template <typename Real>
- Quaternion<Real> SlerpR(Real t, Quaternion<Real> const& q0, Quaternion<Real> const& q1)
- {
- Real f0, f1;
- ChebyshevRatio<Real>::Get(t, Dot(q0, q1), f0, f1);
- return q0 * f0 + q1 * f1;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <typename Real>
- Quaternion<Real> SlerpRP(Real t, Quaternion<Real> const& q0, Quaternion<Real> const& q1, Real cosA)
- {
- Real f0, f1;
- ChebyshevRatio<Real>::Get(t, cosA, f0, f1);
- return q0 * f0 + q1 * f1;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <typename Real>
- Quaternion<Real> SlerpRPH(Real t, Quaternion<Real> const& q0, Quaternion<Real> const& q1,
- Quaternion<Real> const& qh, Real cosAH)
- {
- Real f0, f1;
- Real twoT = static_cast<Real>(2) * t;
- if (twoT <= static_cast<Real>(1))
- {
- ChebyshevRatio<Real>::Get(twoT, cosAH, f0, f1);
- return q0 * f0 + qh * f1;
- }
- else
- {
- ChebyshevRatio<Real>::Get(twoT - static_cast<Real>(1), cosAH, f0, f1);
- return qh * f0 + q1 * f1;
- }
- }
- }
|