-
Notifications
You must be signed in to change notification settings - Fork 0
/
atyp_Quaternion.h
174 lines (143 loc) · 3.32 KB
/
atyp_Quaternion.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#pragma once
#include <memory>
#include "atyp_Vector3.h"
#include "atyp_Matrix4.h"
class Quaternion
{
union {
struct
{
float x;
float y;
float z;
float w;
};
float data[4];
};
public:
Quaternion()
{
*this = Quaternion::identity();
}
Quaternion(Vector3 src)
{
*this = Quaternion::euler(src);
}
Quaternion(float x, float y, float z, float w)
{
this->x = x;
this->y = y;
this->z = z;
this->w = w;
}
~Quaternion() {}
void Print(){
printf("X: %.4f, Y: %.4f, Z: %.4f, W: %.4f\n", x, y, z, w);
}
static Quaternion identity()
{
return Quaternion(0, 0, 0, 1);
}
float magnitude()
{
return sqrtf(x * x + y * y + z * z + w * w);
}
Quaternion normalized()
{
float magnitude = this->magnitude();
return Quaternion(x / magnitude, y / magnitude, z / magnitude, w / magnitude);
}
static Quaternion euler(float x, float y, float z)
{
float cy = cosf(z * 0.5f);
float sy = sinf(z * 0.5f);
float cp = cosf(y * 0.5f);
float sp = sinf(y * 0.5f);
float cr = cosf(x * 0.5f);
float sr = sinf(x * 0.5f);
return Quaternion(
cy * cp * sr - sy * sp * cr,
sy * cp * sr + cy * sp * cr,
sy * cp * cr - cy * sp * sr,
cy * cp * cr + sy * sp * sr)
.normalized();
}
static Quaternion aroundAngle(Vector3 axis, float angle)
{
// Here we calculate the sin( theta / 2) once for optimization
float factor = sinf((float)angle / 2.0f);
// Calculate the x, y and z of the quaternion
float x = axis.x * factor;
float y = axis.y * factor;
float z = axis.z * factor;
// Calcualte the w value by cos( theta / 2 )
float w = cosf((float)angle / 2.0f);
return Quaternion(x, y, z, w).normalized();
}
//return Quaternion(
// direction.x * sin(rotation / 2),
// direction.y * sin(rotation / 2),
// direction.z * sin(rotation / 2),
// cos(rotation / 2));
static Quaternion euler(Vector3 euler)
{
float x = euler.x;
float y = euler.y;
float z = euler.z;
float cy = cosf(z * 0.5f);
float sy = sinf(z * 0.5f);
float cp = cosf(y * 0.5f);
float sp = sinf(y * 0.5f);
float cr = cosf(x * 0.5f);
float sr = sinf(x * 0.5f);
return Quaternion(
cy * cp * sr - sy * sp * cr,
sy * cp * sr + cy * sp * cr,
sy * cp * cr - cy * sp * sr,
cy * cp * cr + sy * sp * sr)
.normalized();
}
Quaternion operator*(Quaternion b)
{
Quaternion a = *this;
return Quaternion(
a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
a.w * b.x + a.x * b.w + a.y * b.z + a.z * b.y,
a.w * b.y - a.x * b.z - a.y * b.w - a.z * b.x,
a.w * b.z + a.x * b.y + a.y * b.x + a.z * b.w);
}
Quaternion& operator*=(Quaternion b)
{
Quaternion a = *this;
this->x = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;
this->y = a.w * b.x + a.x * b.w + a.y * b.z + a.z * b.y;
this->z = a.w * b.y - a.x * b.z - a.y * b.w - a.z * b.x;
this->w = a.w * b.z + a.x * b.y + a.y * b.x + a.z * b.w;
return *this;
}
Matrix4 toMatrix()
{
Quaternion q = *this;
Matrix4 a = Matrix4(
q.w, q.z, -q.y, q.x,
-q.z, q.w, q.x, q.y,
q.y, -q.x, q.w, q.z,
-q.x, -q.y, -q.z, q.w
);
Matrix4 b = Matrix4(
q.w, q.z, -q.y, -q.x,
-q.z, q.w, q.x, -q.y,
q.y, -q.x, q.w, -q.z,
q.x, q.y, q.z, q.w
);
return a * b;
}
operator Matrix4()
{
return toMatrix();
}
Quaternion operator=(Vector3 &rhs)
{
return *this = Quaternion::euler(rhs);
}
};