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
|
#include <BH/Math/Quat.h>
#include <BH/Math/Vec3f.h>
#include <BH/Math/Mat4f.h>
#include <string.h>
#include <math.h>
#define EPSILON 0.00001f
#define PI 3.14159265358979323846f
void BH_Quat4fIdentity(float out[4])
{
static const float ident[4] = {0.0f, 0.0f, 0.0f, 1.0f};
memcpy(out, ident, sizeof(ident));
}
void BH_Quat4fConjugate(const float in[4],
float out[4])
{
out[0] = -in[0];
out[1] = -in[1];
out[2] = -in[2];
out[3] = in[3];
}
void BH_Quat4fInverse(const float in[4],
float out[4])
{
float dot;
dot = BH_Vec4fDot(in, in);
BH_Quat4fConjugate(in, out);
BH_Quat4fScale(out, 1.0f / dot, out);
}
void BH_Quat4fMul(const float a[4],
const float b[4],
float out[4])
{
float tmp1[4], tmp2[4], tmp3[4];
float w;
w = a[3] * b[3] - BH_Vec3fDot(a, b);
BH_Vec4fScale(a, b[3], tmp1);
BH_Vec4fScale(b, a[3], tmp2);
BH_Vec3fCross(a, b, tmp3);
BH_Vec4fAdd(tmp1, tmp2, out);
BH_Vec4fAdd(tmp3, out, out);
out[3] = w;
}
void BH_Quat4fSlerp(const float a[4],
const float b[4],
float t,
float out[4])
{
float angle, denom;
float from[4], to[4];
angle = acosf(BH_Vec4fDot(a, b));
if (fabsf(angle) < EPSILON)
{
BH_Vec4fLerp(a, b, t, out);
return;
}
denom = 1.0f / sinf(angle);
BH_Vec4fScale(a, sinf((1 - t) * angle) * denom, from);
BH_Vec4fScale(b, sinf(t * angle) * denom, to);
BH_Vec4fAdd(from, to, out);
}
void BH_Quat4fFromEuler(float roll,
float pitch,
float yaw,
float out[4])
{
float cr, cp, cy, sr, sp, sy;
cr = cosf(roll / 2.0f);
cp = cosf(pitch / 2.0f);
cy = cosf(yaw / 2.0f);
sr = sinf(roll / 2.0f);
sp = sinf(pitch / 2.0f);
sy = sinf(yaw / 2.0f);
out[0] = sr * cp * cy - cr * sp * sy;
out[1] = cr * sp * cy + sr * cp * sy;
out[2] = cr * cp * sy - sr * sp * cy;
out[3] = cr * cp * cy + sr * sp * sy;
}
void BH_Quat4fFromAxis(const float axis[3],
float angle,
float out[4])
{
float c, s;
c = cosf(angle / 2.0f);
s = sinf(angle / 2.0f);
out[0] = axis[0] * s;
out[1] = axis[1] * s;
out[2] = axis[2] * s;
out[3] = c;
}
void BH_Quat4fToEuler(const float in[4],
float *roll,
float *pitch,
float *yaw)
{
float ww, xw, yw, zw, xx, xy, xz, yy, yz, zz, angle;
xx = in[0] * in[0];
xy = in[0] * in[1];
xz = in[0] * in[2];
xw = in[0] * in[3];
yy = in[1] * in[1];
yz = in[1] * in[2];
yw = in[1] * in[3];
zz = in[2] * in[2];
zw = in[2] * in[3];
ww = in[3] * in[3];
angle = 2.0f * (yw - xz);
if (angle > 1.0f)
angle = 1.0f;
if (angle < -1.0f)
angle = -1.0f;
*pitch = asinf(angle);
if (fabsf(*pitch - (PI / 2.0f)) < EPSILON)
{
*roll = 0.0f;
*yaw = -2.0f * atan2f(in[0], in[3]);
}
else if (fabsf(*pitch - (PI / -2.0f)) < EPSILON)
{
*roll = 0.0f;
*yaw = 2.0f * atan2f(in[0], in[3]);
}
else
{
*roll = atan2f(2.0f * (xw + yz), ww - xx - yy + zz);
*yaw = atan2f(2.0f * (zw + xy), ww + xx - yy - zz);
}
}
void BH_Quat4fToAxis(const float in[4],
float axis[3],
float *angle)
{
*angle = 2.0f * acosf(in[3]);
if (fabsf(*angle) < EPSILON)
{
axis[0] = 1.0f;
axis[1] = 0.0f;
axis[2] = 0.0f;
}
else
{
float tmp;
tmp = sqrtf(1.0f - in[3] * in[3]);
axis[0] = in[0] / tmp;
axis[1] = in[1] / tmp;
axis[2] = in[2] / tmp;
}
}
void BH_Quat4fToMat4f(const float in[4],
float out[16])
{
float xx, xy, xz, xw, yy, yz, yw, zz, zw;
xx = in[0] * in[0];
xy = in[0] * in[1];
xz = in[0] * in[2];
xw = in[0] * in[3];
yy = in[1] * in[1];
yz = in[1] * in[2];
yw = in[1] * in[3];
zz = in[2] * in[2];
zw = in[2] * in[3];
BH_Mat4fIdentity(out);
out[0] = 1.0f - 2.0f * (yy + zz);
out[1] = 2.0f * (xy + zw);
out[2] = 2.0f * (xz - yw);
out[4] = 2.0f * (xy - zw);
out[5] = 1.0f - 2.0f * (xx + zz);
out[6] = 2.0f * (yz + xw);
out[8] = 2.0f * (xz + yw);
out[9] = 2.0f * (yz - xw);
out[10] = 1.0f - 2.0f * (xx + yy);
}
|