summaryrefslogtreecommitdiff
path: root/src/math/quat.c
blob: 07215d26b6079a8eb66e2c012a9a3375f3fe9652 (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <math.h>
#include <stdio.h>
#include <string.h>

#include "math.h"
#include "../util/assert.h"
#include "../core/mem.h"

static Vec3 sharedVec3;
static Quat sharedQuat;
static Quat sharedQuat2;
static Euler sharedEuler;

#define shrquat(q) \
do{\
sharedQuat = *q;\
q = &sharedQuat;\
}while(0)

#define shrquat2(q) \
do{\
sharedQuat2 = *q;\
q = &sharedQuat2;\
}while(0)

#define shrvec3(v) \
do{\
sharedVec3 = *v;\
v = &sharedVec3;\
}while(0)

#define shreuler(v) \
do{\
sharedEuler = *v;\
v = &sharedEuler;\
}while(0)

void internal_quat_tostring(Quat* v, char buf[]) {
	ssr_assert(v);
	sprintf(buf, "%8.3f %8.3f %8.3f", v->x, v->y, v->z);
}

void internal_quat_print(Quat* q) {
	ssr_assert(q);
	internal_quat_tostring(q, printbuffer);
	printf("\n%s\n", printbuffer);
}

void internal_euler_tostring(Euler* v, char buf[]) {
	sprintf(buf, "%8.3f %8.3f %8.3f", v->x, v->y, v->z);
}

void internal_euler_print(Euler* v) {
	internal_euler_tostring(v, printbuffer);
	printf("\n%s\n", printbuffer);
}

void internal_euler_deg2rad(Euler* in, Euler* out) {
	ssr_assert(in && out);
	out->x = radians(in->x);
	out->y = radians(in->y);
	out->z = radians(in->z);
}

void internal_euler_rad2deg(Euler* in, Euler* out) {
	ssr_assert(in && out);
	out->x = degree(in->x);
	out->y = degree(in->y);
	out->z = degree(in->z);
}

Quat internal_quat_make(float rx, float ry, float rz) {
	Quat rot; 
	Euler euler = {rx, ry, rz};
	internal_quat_fromeuler(&euler, &rot);
	return rot;
}

void internal_euler_toquat(Euler* euler, Quat* out) {
	ssr_assert(euler && out);
	internal_quat_fromeuler(euler, out);
}

/*这里精度不对*/
bool internal_quat_isidentity(Quat* q) {
	return compare(internal_quat_magnitude(q), 1.f);
}

void internal_quat_fromaxisangle(Vec3* axis, float angle, Quat* out) {
	ssr_assert(compare(internal_vec3_magnitude2(axis), 1.f));
	angle = radians(angle);
	float half = angle * 0.5;
	float s = sin(half);
	out->w = cos(half);
	out->x = s * axis->x; 
	out->y = s * axis->y; 
	out->z = s * axis->z;
}

void internal_quat_fromeuler(Euler* el, Quat* out) {
	ssr_assert(el && out);
	Euler euler;
	internal_euler_deg2rad(el, &euler);
	float cx = cos(euler.x * 0.5f);
	float sx = sin(euler.x * 0.5f);
	float cy = cos(euler.y * 0.5f);
	float sy = sin(euler.y * 0.5f);
	float cz = cos(euler.z * 0.5f);
	float sz = sin(euler.z * 0.5f);
	Quat qx = {sx, 0, 0,cx};
	Quat qy = {0, sy, 0,cy};
	Quat qz = {0,  0,sz,cz};
	/*按ZXY顺序*/
	internal_quat_multiply(&qx, &qz, &qx);
	internal_quat_multiply(&qy, &qx, out);
	ssr_assert(internal_quat_isidentity(out));
}

/*
** 四元数直接对向量进行旋转和先转成矩阵形式再旋转计算量一样
*/
void internal_quat_applytovec3(Quat* q, Vec3* v, Vec3* out) {
	ssr_assert(q && v && out);
	if (v == out) {
		shrvec3(v);
	}
	/* q[v,0]q^-1 */
	/* https://gamedev.stackexchange.com/questions/28395/rotating-vector3-by-a-quaternion */
	float x = q->x * 2.0F;
	float y = q->y * 2.0F;
	float z = q->z * 2.0F;
	float xx = q->x * x;
	float yy = q->y * y;
	float zz = q->z * z;
	float xy = q->x * y;
	float xz = q->x * z;
	float yz = q->y * z;
	float wx = q->w * x;
	float wy = q->w * y;
	float wz = q->w * z;

	/*从这里能看到矩阵形式*/
	out->x = (1.0f - (yy + zz)) * v->x + (xy - wz)          * v->y + (xz + wy)          * v->z;
	out->y = (xy + wz)          * v->x + (1.0f - (xx + zz)) * v->y + (yz - wx)          * v->z;
	out->z = (xz - wy)          * v->x + (yz + wx)          * v->y + (1.0f - (xx + yy)) * v->z; 
}

void internal_quat_tomat4(Quat* q, Mat4* out) {
	ssr_assert(q && out);
	ssr_assert(internal_quat_isidentity(q));

	internal_mat4_setidentity(out);

	float x = q->x * 2.0F; /*从internal_quat_applytovec3能得到矩阵形式*/
	float y = q->y * 2.0F;
	float z = q->z * 2.0F;
	float xx = q->x * x;
	float yy = q->y * y;
	float zz = q->z * z;
	float xy = q->x * y;
	float xz = q->x * z;
	float yz = q->y * z;
	float wx = q->w * x;
	float wy = q->w * y;
	float wz = q->w * z;

	/*和internal_mat4_setaxisangle实际上结果一样*/
	out->l[0] = 1.0f - (yy + zz);
	out->l[1] = xy + wz;
	out->l[2] = xz - wy;

	out->l[4] = xy - wz;
	out->l[5] = 1.0f - (xx + zz);
	out->l[6] = yz + wx;

	out->l[8] = xz + wy;
	out->l[9] = yz - wx;
	out->l[10] = 1.0f - (xx + yy);
}

void internal_quat_toeuler(Quat* q, Euler* out) {
	ssr_assert(q && out);
	Mat4 mat; 
	internal_quat_tomat4(q, &mat); /*计算变换矩阵*/
	internal_mat4_toeuler(&mat, out); /*mat是按照RyRxRz顺序(z->y->x)的旋转矩阵*/
}

void internal_quat_normalize(Quat* q, Quat* out) {
	ssr_assert(q && out);
	float mag = internal_quat_magnitude(q);
	internal_quat_scale(q, 1.f/mag, out);
}

void internal_quat_scale(Quat* q, float scale, Quat* out) {
	ssr_assert(q && out);
	out->x = q->x * scale;
	out->y = q->y * scale;
	out->z = q->z * scale;
	out->w = q->w * scale;
}

void internal_quat_negtive(Quat* in, Quat* out) {
	ssr_assert(in && out);
	out->w = -in->w; 
	out->x = -in->x;
	out->y = -in->y;
	out->z = -in->z;
}

void internal_quat_devide(Quat* q, float k, Quat* out) {
	ssr_assert(q && out);
	k = 1 / k;
	out->w = q->w * k;
	out->x = q->x * k;
	out->y = q->y * k;
	out->z = q->z * k;
}

void internal_quat_invert(Quat* q, Quat* out) {
	ssr_assert(q && out);
	float mag2 = internal_quat_magnitude2(q);
	internal_quat_conjugate(q, out);
	internal_quat_devide(out, mag2, out);
	/*实际上如果是单位四元数,共轭就是逆*/
}

bool internal_quat_setlookrotation(Vec3* view, Vec3* up, Quat* out) {
	ssr_assert(view && up && out);
	Mat4 m; 
	internal_mat4_setlookrotation(view, up, &m); /*先以view为准构建正交矩阵*/
	internal_mat4_toquat(&m, out);
	return 1;
}

/*q1 * q2^-1*/
void internal_quat_minus(Quat* q1, Quat* q2, Quat* out) {
	ssr_assert(q1 && q2 && out);
	Quat q2i; internal_quat_invert(q2, &q2i);
	internal_quat_multiply(q1, &q2i, out);
}

/*q1*q2*/
void internal_quat_multiply(Quat* q1, Quat* q2, Quat* out) {
	ssr_assert(q1 && q2 && out);

	float w1 = q1->w, x1 = q1->x, y1 = q1->y, z1 = q1->z,
	      w2 = q2->w, x2 = q2->x, y2 = q2->y, z2 = q2->z;
	out->x = x1 * w2 + x2 * w1 + y1 * z2 - z1 * y2;
	out->y = w1 * y2 + w2 * y1 + z1 * x2 - z2 * x1; 
	out->z = w1 * z2 + w2 * z1 + x1 * y2 - x2 * y1;
	out->w = w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2;
}

/*
** 共轭的几何意义是颠倒旋转轴方向,代表了和q相反的角位移
*/
void internal_quat_conjugate(Quat* in, Quat* out) {
	ssr_assert(in && out);
	out->w = in->w; 
	out->x = -in->x;
	out->y = -in->y;
	out->z = -in->z;
}

float internal_quat_dot(Quat* q1, Quat* q2) {
	ssr_assert(q1 && q2);
	return (q1->x*q2->x + q1->y*q2->y + q1->z*q2->z + q1->w*q2->w);
}

float internal_quat_magnitude(Quat* q) {
	ssr_assert(q);
	return sqrt(internal_quat_dot(q, q));
}

float internal_quat_magnitude2(Quat* q) {
	ssr_assert(q);
	return internal_quat_dot(q, q);
}

void internal_quat_slerp(Quat* q1, Quat* q2, float t, Quat* out) {
	ssr_assert(q1 && q2 && out);
	ssr_assert(internal_quat_isidentity(q1) && internal_quat_isidentity(q2)); /*适用于单位四元数*/
	float dot = internal_quat_dot(q1, q2);
	Quat temp;
	if (dot < 0.0f) {
		dot = -dot;
		temp.x = -q2->x;
		temp.y = -q2->y;
		temp.z = -q2->z;
		temp.w = -q2->w;
	}	else {
		temp = *q2;
	}
	if (dot < 0.95f) {
		float angle = acos(dot);
		float sinadiv, sinat, sinaomt;
		sinadiv = 1.0f / sin(angle);
		sinat = sin(angle*t);
		sinaomt = sin(angle*(1.0f - t));
		out->x = (q1->x*sinaomt + temp.x*sinat)*sinadiv;
		out->y = (q1->y*sinaomt + temp.y*sinat)*sinadiv;
		out->z = (q1->z*sinaomt + temp.z*sinat)*sinadiv;
		out->w = (q1->w*sinaomt + temp.w*sinat)*sinadiv;
	}	else { /*小范围时使用lerp*/
		internal_quat_lerp(q1, &temp, t, out);
	}
}

void internal_quat_lerp(Quat* q1, Quat* q2, float t, Quat* out) {
	ssr_assert(q1 && q2 && out);
	float t2 = 1 - t;
	if (internal_quat_dot(q1, q2) < 0.f) { /*点乘不能是负的,翻转一个四元数的符号,使得落回360°*/
		out->x = q1->x * t2 - t * q2->x;
		out->y = q1->y * t2 - t * q2->y;
		out->z = q1->z * t2 - t * q2->z;
		out->w = q1->w * t2 - t * q2->w;
	}	else {
		out->x = q1->x * t2 + t * q2->x;
		out->y = q1->y * t2 + t * q2->y;
		out->z = q1->z * t2 + t * q2->z;
		out->w = q1->w * t2 + t * q2->w;
	}
	internal_quat_normalize(out, out);
}