-
Notifications
You must be signed in to change notification settings - Fork 8
/
m_quat.h
84 lines (63 loc) · 1.56 KB
/
m_quat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef M_QUAT_HDR
#define M_QUAT_HDR
#include "m_vec.h"
#include "m_trig.h"
#ifdef __SSE2__
#include <emmintrin.h>
#endif
namespace m {
struct mat4;
struct quat : vec4 {
constexpr quat();
constexpr quat(const m::vec4 &v);
constexpr quat(const float (&vals)[4]);
constexpr quat(float x, float y, float z, float w);
quat(float angle, const vec3 &vec);
constexpr quat conjugate() const;
// get all 3 axis of the quaternion
void getOrient(vec3 *direction, vec3 *up, vec3 *side) const;
// get matrix of this quaternion
mat4 getMatrix() const;
friend quat operator*(const quat &l, const vec3 &v);
friend quat operator*(const quat &l, const quat &r);
friend quat operator*(const quat &l, float k);
quat normalize() const;
#ifdef __SSE2__
constexpr quat(__m128 v);
#endif
};
inline constexpr quat::quat() = default;
inline constexpr quat::quat(const m::vec4 &vec)
: vec4(vec)
{
}
inline constexpr quat::quat(const float (&vals)[4])
: vec4(vals)
{
}
inline constexpr quat::quat(float x, float y, float z, float w)
: vec4(x, y, z, w)
{
}
#ifdef __SSE2__
inline constexpr quat::quat(__m128 v)
: vec4(v)
{
}
#endif
inline quat::quat(float angle, const vec3 &vec) {
const vec2 sc = m::sincos(angle * 0.5f);
const vec3 normal = vec.normalized();
x = sc.x * normal.x;
y = sc.x * normal.y;
z = sc.x * normal.z;
w = sc.y;
}
inline constexpr quat quat::conjugate() const {
return quat(-x, -y, -z, -w);
}
inline quat quat::normalize() const {
return *this * (1.0f / abs());
}
}
#endif