28 #ifndef BERSERK_TQUAT_H
29 #define BERSERK_TQUAT_H
76 *
this =
TQuat({1, 0, 0}, roll) *
77 TQuat({0, 1, 0}, yaw) *
78 TQuat({0, 0, 1}, pitch);
85 T trace = v[0] + v[5] + v[10];
98 q[3] =
static_cast<T>(s * 0.5);
101 q[0] = (v[9] - v[6]) * t;
102 q[1] = (v[2] - v[8]) * t;
103 q[2] = (v[4] - v[1]) * t;
108 if (v[5] > v[0]) i = 1;
109 if (v[10] > v[4 * i + i]) i = 2;
111 static const int32 NEXT[3] = {1, 2, 0};
115 auto s = (
T)
MathUtils::Sqrt(v[i * 4 + i] - (v[j * 4 + j] + v[k * 4 + k]) + 1.0);
120 t =
static_cast<T>(0.5 / s);
123 q[3] = (v[k * 4 + j] - v[j * 4 + k]) * t;
124 q[j] = (v[j * 4 + i] - v[i * 4 + j]) * t;
125 q[k] = (v[k * 4 + i] - v[i * 4 + k]) * t;
129 vec = {q[0], q[1], q[2]};
260 v[0] = 1 - 2 * (yy + zz);
261 v[1] = 2 * (xy - zw);
262 v[2] = 2 * (xz + yw);
264 v[4] = 2 * (xy + zw);
265 v[5] = 1 - 2 * (xx + zz);
266 v[6] = 2 * (yz - xw);
268 v[8] = 2 * (xz - yw);
269 v[9] = 2 * (yz + xw);
270 v[10] = 1 - 2 * (xx + yy);
287 axis =
vec({0, 1, 0});
307 return TQuat(axis, angle);
329 return lerp(t, a, b);
345 return Lerp(t, a, b);
380 for (
int i = 0; i < 3; i++) {
409 for (
uint32 i = 0; i < 3; i++) {
415 return TQuat(m).Inverse();
444 inline std::ostream &
operator<<(std::ostream &ostream,
const BRK_NS::TQuat<T> &quat) {
445 ostream <<
"(" << quat.scalar <<
"," << quat.vec[0] <<
"," << quat.vec[1] <<
"," << quat.vec[2] <<
")";
#define BRK_NS_END
Definition: Config.hpp:48
#define BRK_NS
Definition: Config.hpp:50
std::ostream & operator<<(std::ostream &ostream, const BRK_NS::TQuat< T > &quat)
Definition: TQuat.hpp:444
std::int32_t int32
Definition: Typedefs.hpp:43
std::size_t size_t
Definition: Typedefs.hpp:49
std::uint32_t uint32
Definition: Typedefs.hpp:44
Crc32 hash builder.
Definition: Crc32.hpp:68
Crc32Hash GetHash() const
Definition: Crc32.hpp:86
Crc32Builder & Hash(const void *buffer, size_t size)
Definition: Crc32.hpp:78
static float Lerp(float t, float left, float right)
Definition: MathUtils.hpp:156
static float Sqrt(float a)
Definition: MathUtils.hpp:106
static float Acos(float a)
Definition: MathUtils.hpp:100
static float Cos(float a)
Definition: MathUtils.hpp:91
static BRK_API const float THRESH_FLOAT32
Definition: MathUtils.hpp:53
static BRK_API const float THRESH_ZERO_NORM_SQUARED
Definition: MathUtils.hpp:56
static float Max(float a, float b)
Definition: MathUtils.hpp:85
static float Sin(float a)
Definition: MathUtils.hpp:88
Generic matrix of M x N size for space of type T.
Definition: TMatMxN.hpp:56
T values[M *N]
Definition: TMatMxN.hpp:377
General quaternion math class.
Definition: TQuat.hpp:52
Vec RotateReverse(const Vec &v) const
Definition: TQuat.hpp:218
static TQuat Slerp(T t, const TQuat &a, const TQuat &b)
Definition: TQuat.hpp:325
static TQuat LookAt(const TVecN< T, 3 > &direction, const TVecN< T, 3 > &up)
Definition: TQuat.hpp:373
Mat AsMatrix() const
Definition: TQuat.hpp:240
void Zero()
Definition: TQuat.hpp:144
Crc32Hash Hash() const
Definition: TQuat.hpp:290
static T Dot(const TQuat &a, const TQuat &b)
Definition: TQuat.hpp:310
TQuat & operator=(const TQuat &other)=default
static TQuat Rotation(const Vec &axis, T angle)
Definition: TQuat.hpp:306
TQuat & Normalize()
Definition: TQuat.hpp:182
TQuat & operator*=(T a)
Definition: TQuat.hpp:156
TQuat & operator/=(T a)
Definition: TQuat.hpp:162
TQuat operator*(const TQuat &other) const
Definition: TQuat.hpp:149
TQuat(const TQuat &other)=default
static TQuat Lerp(T t, const TQuat &a, const TQuat &b)
Definition: TQuat.hpp:318
T GetAngle() const
Definition: TQuat.hpp:236
void Identity()
Definition: TQuat.hpp:139
static T Angle(const TQuat &a, const TQuat &b)
Definition: TQuat.hpp:314
Vec GetAxisZ() const
Definition: TQuat.hpp:232
bool operator!=(const TQuat &other) const
Definition: TQuat.hpp:301
Vec GetAxisX() const
Definition: TQuat.hpp:224
TQuat(T s, const Vec &v)
Definition: TQuat.hpp:59
TQuat(T s, T x, T y, T z)
Definition: TQuat.hpp:64
TQuat(const Vec &axis, T angle)
Definition: TQuat.hpp:70
bool operator==(const TQuat &other) const
Definition: TQuat.hpp:297
TQuat(TQuat &&other) noexcept=default
Vec vec
Definition: TQuat.hpp:420
Vec Rotate(const Vec &v) const
Definition: TQuat.hpp:212
TQuat Conjugate() const
Definition: TQuat.hpp:208
T scalar
Definition: TQuat.hpp:419
Vec GetAxisY() const
Definition: TQuat.hpp:228
TQuat Inverse() const
Definition: TQuat.hpp:195
T Length2() const
Definition: TQuat.hpp:168
TQuat & operator=(TQuat &&other) noexcept=default
T Length() const
Definition: TQuat.hpp:172
TQuat Normalized() const
Definition: TQuat.hpp:177
TQuat(const Mat &mat)
Definition: TQuat.hpp:81
void GetAxisAngle(Vec &axis, T &angle) const
Definition: TQuat.hpp:280
static TQuat Slerp(T t, T ang, const TQuat &a, const TQuat &b)
Definition: TQuat.hpp:343
static TQuat FaceAt(const TVecN< T, 3 > &direction, const TVecN< T, 3 > &up)
Definition: TQuat.hpp:402
TQuat(T roll, T yaw, T pitch)
Definition: TQuat.hpp:75
T Length2() const
Definition: TVecN.hpp:505
TVecN & Zero()
Definition: TVecN.hpp:538
static TVecN Cross(const TVecN &a, const TVecN &b)
Definition: TVecN.hpp:375
TVecN Normalized() const
Definition: TVecN.hpp:520
static T Dot(const TVecN &a, const TVecN &b)
Definition: TVecN.hpp:354
static TVecN Lerp(float t, const TVecN &a, const TVecN &b)
Definition: TVecN.hpp:409
Definition: GLDevice.cpp:46
Definition: TQuat.hpp:431
std::size_t operator()(const BRK_NS::TQuat< T > &quat) const
Definition: TQuat.hpp:436