-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquat.h
247 lines (191 loc) · 4.62 KB
/
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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#pragma once
#include "matrix.h"
#define Epsilon() 1e-6f
#define DegToRad(x) ((x) * 3.1415926536f / 180.0f)
#define RadToDeg(x) ((x) * 180.0f / 3.1415926536f)
struct quat
{
quat(): x(0), y(0), z(0), w(1)
{
}
quat(const mat3& m)
{
float trace = m.mat[0] + m.mat[4] + m.mat[8];
if(trace > 0.0)
{
float s = sqrtf(trace + 1.0f);
w = 0.5f * s;
s = 0.5f / s;
x = (m.mat[5] - m.mat[7]) * s;
y = (m.mat[6] - m.mat[2]) * s;
z = (m.mat[1] - m.mat[3]) * s;
}
else
{
static int next[3] = { 1, 2, 0 };
int i = 0;
if(m.mat[4] > m.mat[0])
i = 1;
if(m.mat[8] > m.mat[3 * i + i])
i = 2;
int j = next[i];
int k = next[j];
float s = sqrtf(m.mat[3 * i + i] - m.mat[3 * j + j] - m.mat[3 * k + k] + 1.0f);
((float*)&x)[i] = 0.5f * s;
if(s != 0)
s = 0.5f / s;
w = (m.mat[3 * j + k] - m.mat[3 * k + j]) * s;
((float*)&x)[j] = (m.mat[3 * i + j] + m.mat[3 * j + i]) * s;
((float*)&x)[k] = (m.mat[3 * i + k] + m.mat[3 * k + i]) * s;
}
}
quat(float nx, float ny, float nz, float nw): x(nx), y(ny), z(nz), w(nw)
{
}
bool operator==(const quat& q) const
{
return x == q.x && y == q.y && z == q.z && w == q.w;
}
bool operator!=(const quat& q) const
{
return x != q.x || y != q.y || z != q.z || w != q.w;
}
quat operator*(const quat& q) const
{
quat ret;
ret.x = w * q.x + y * q.z - z * q.y + x * q.w;
ret.y = w * q.y + z * q.x - x * q.z + y * q.w;
ret.z = w * q.z + x * q.y - y * q.x + z * q.w;
ret.w = w * q.w - x * q.x - y * q.y - z * q.z;
return ret;
}
// Create a quaternion that represents rotation around axis "dir" by angle
quat& set(const vec3& dir, float angle)
{
float length = dir.length();
if(length != 0.0f)
{
length = 1.0f / length;
float sinAngle = sinf(DegToRad(angle) / 2.0f);
x = dir.x * length * sinAngle;
y = dir.y * length * sinAngle;
z = dir.z * length * sinAngle;
w = cosf(DegToRad(angle) / 2.0f);
}
else
{
x = y = z = 0.0f;
w = 1.0f;
}
return *this;
}
quat& set(float x, float y, float z, float angle)
{
return set(vec3(x, y, z), angle);
}
// Create a quaternion that represents transformation that rotates vec3(1, 0, 0) into "dir"
void set_from_direction(const vec3& dir)
{
set_from_direction(vec3(1, 0, 0), dir);
}
// Create a quaternion that represents transformation that rotates "from" into "to"
void set_from_direction(const vec3& from, const vec3& to)
{
vec3 f = ::normalize(from);
vec3 t = ::normalize(to);
float cosAngle = dot(f, t);
vec3 axis = cross(f, t);
float value = acosf(cosAngle);
set(axis, RadToDeg(value));
}
void slerp(const quat& q0, const quat& q1, float t)
{
float k0, k1;
float cosomega = q0.x * q1.x + q0.y * q1.y + q0.z * q1.z + q0.w * q1.w;
quat q;
if(cosomega < 0.0f)
{
cosomega = -cosomega;
q.x = -q1.x;
q.y = -q1.y;
q.z = -q1.z;
q.w = -q1.w;
}
else
{
q.x = q1.x;
q.y = q1.y;
q.z = q1.z;
q.w = q1.w;
}
if(1.0 - cosomega > Epsilon())
{
float omega = acosf(cosomega);
float sinomega = sinf(omega);
k0 = sinf((1.0f - t) * omega) / sinomega;
k1 = sinf(t * omega) / sinomega;
}
else
{
k0 = 1.0f - t;
k1 = t;
}
x = q0.x * k0 + q.x * k1;
y = q0.y * k0 + q.y * k1;
z = q0.z * k0 + q.z * k1;
w = q0.w * k0 + q.w * k1;
}
mat3 to_matrix() const
{
mat3 ret;
float x2 = x + x;
float y2 = y + y;
float z2 = z + z;
float xx = x * x2;
float yy = y * y2;
float zz = z * z2;
float xy = x * y2;
float yz = y * z2;
float xz = z * x2;
float wx = w * x2;
float wy = w * y2;
float wz = w * z2;
ret.mat[0] = 1.0f - (yy + zz); ret.mat[3] = xy - wz; ret.mat[6] = xz + wy;
ret.mat[1] = xy + wz; ret.mat[4] = 1.0f - (xx + zz); ret.mat[7] = yz - wx;
ret.mat[2] = xz - wy; ret.mat[5] = yz + wx; ret.mat[8] = 1.0f - (xx + yy);
return ret;
}
void normalize()
{
float magn = float(1.0 / sqrtf(x * x + y * y + z * z + w * w));
x *= magn;
y *= magn;
z *= magn;
w *= magn;
}
quat conjugate()
{
quat ret = *this;
ret.x = -ret.x;
ret.y = -ret.y;
ret.z = -ret.z;
return ret;
}
vec3 transform_vector(const vec3& v)
{
vec3 uv(y * v.z - z * v.y,
z * v.x - x * v.z,
x * v.y - y * v.x);
vec3 uuv(y * uv.z - z * uv.y,
z * uv.x - x * uv.z,
x * uv.y - y * uv.x);
uv *= w;
uv += uuv;
uv *= 2;
return v + uv;
}
float x, y, z, w;
};
#undef Epsilon
#undef DegToRad
#undef RadToDeg