-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinear_algebros.c
565 lines (391 loc) · 13 KB
/
linear_algebros.c
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#include "linear_algebros.h"
#include <math.h>
/*-------- Conversions ----------*/
float linalgDeg2Rad(float angle) {
return angle * pi / 180.0f;
}
float linalgRad2Deg(float angle) {
return angle * 180.0f / pi;
}
/*-------- Vec3 Operations ----------*/
vec3 linalgMakeVec3(float x, float y, float z) {
vec3 result;
result.data[0] = x;
result.data[1] = y;
result.data[2] = z;
result.data[3] = 0;
return result;
}
float linalgDotVec3(vec3 a, vec3 b) {
return a.data[0] * b.data[0] + a.data[1] * b.data[1] + a.data[2] * b.data[2];
}
vec3 linalgCross(vec3 a, vec3 b) {
vec3 result;
result.data[0] = a.data[1] * b.data[2] - a.data[2] * b.data[1];
result.data[1] = a.data[2] * b.data[0] - a.data[0] * b.data[2];
result.data[2] = a.data[0] * b.data[1] - a.data[1] * b.data[0];
result.data[3] = 0;
return result;
}
vec3 linalgNormalizeVec3(vec3 a) {
float invMagnitude = 1.0f / sqrtf(a.data[0] * a.data[0] + a.data[1] * a.data[1] + a.data[2] * a.data[2]);
vec3 result;
result.vector = _mm_mul_ps(a.vector, _mm_set1_ps(invMagnitude));
return result;
}
vec3 linalgSubVec3(vec3 a, vec3 b) {
vec3 result;
result.vector = _mm_sub_ps(a.vector, b.vector);
return result;
}
vec3 linalgAddVec3(vec3 a, vec3 b) {
vec3 result;
result.vector = _mm_add_ps(a.vector, b.vector);
return result;
}
vec3 linalgMulVec3(vec3 a, float scalar) {
vec3 result;
result.vector = _mm_mul_ps(a.vector, _mm_set1_ps(scalar));
return result;
}
float linalgAngleBetweenVectors3(vec3 a, vec3 b) {
float denominator = sqrtf(linalgDotVec3(a, a) * linalgDotVec3(b, b));
float dot = linalgDotVec3(a, b);
return linalgRad2Deg(acosf(dot / denominator));
}
vec3 linalgProject(vec3 incoming, vec3 basis) {
return linalgMulVec3(basis, linalgDotVec3(incoming, basis) / linalgDotVec3(basis, basis));
}
vec3 linalgReject(vec3 incoming, vec3 basis) {
return linalgSubVec3(incoming, linalgProject(incoming, basis));
}
vec3 linalgReflect(vec3 incident, vec3 normal) {
vec3 result;
// reflected = incident − 2(incident.normal)normal
result.vector = _mm_fmadd_ps(
normal.vector,
_mm_set1_ps(-2.0f * linalgDotVec3(incident, normal)),
incident.vector
);
return result;
}
vec3 linalgLerpVec3(vec3 a, vec3 b, float t) {
vec3 result;
result.vector = _mm_fmadd_ps(
_mm_sub_ps(b.vector, a.vector),
_mm_set1_ps(t),
a.vector
);
return result;
}
vec3 linalgSlerpVec3(vec3 a, vec3 b, float t) {
if (t < 0.1f) {
return linalgLerpVec3(a, b, t);
}
float angle = linalgAngleBetweenVectors3(a, b);
float denominator = sinf(linalgDeg2Rad(angle));
vec3 result;
result.vector = _mm_fmadd_ps(
a.vector,
_mm_set1_ps(sinf(1 - t) * angle / denominator),
_mm_mul_ps(b.vector, _mm_set1_ps(sinf(t * angle) / denominator))
);
return result;
}
vec3 linalgNlerpVec3(vec3 a, vec3 b, float t) {
return linalgNormalizeVec3(linalgLerpVec3(a, b, t));
}
bool linalgCloseVec3(vec3 a, vec3 b) {
vec3 displacement = linalgSubVec3(a, b);
return linalgDotVec3(displacement, displacement) < eps;
}
/*-------- Vector4 Operations ----------*/
float linalgDotVec4(vec4 a, vec4 b) {
return a.data[0] * b.data[0] + a.data[1] * b.data[1] + a.data[2] * b.data[2] + a.data[3] * b.data[3];
}
vec4 linalgNormalizeVec4(vec4 a) {
float invMagnitude = 1.0f / sqrtf(
a.data[0] * a.data[0] + a.data[1] * a.data[1] + a.data[2] * a.data[2] + a.data[3] * a.data[3]
);
vec4 result;
result.vector = _mm_mul_ps(a.vector, _mm_set1_ps(invMagnitude));
return result;
}
/*-------- Matrix4 Operations ----------*/
mat4 linalgMakeIdentity4() {
mat4 result;
result.column[0] = _mm_setr_ps(1, 0, 0, 0);
result.column[1] = _mm_setr_ps(0, 1, 0, 0);
result.column[2] = _mm_setr_ps(0, 0, 1, 0);
result.column[3] = _mm_setr_ps(0, 0, 0, 1);
return result;
}
mat4 linalgMakePerspectiveProjection(
float fovy, float aspect, float near, float far) {
float yMax = near * tanf(linalgDeg2Rad(fovy / 2));
float xMax = yMax * aspect;
/*
The matrix is:
[E 0 A 0]
[0 F B 0]
[0 0 C D]
[0 0 -1 0]
Given by:
float left{ -xMax };
float right{ -xMax };
float top{ -yMax };
float bottom{ yMax };
float A{ (right + left) / (right - left) };
float B{ (top + bottom) / (top - bottom) };
float C{ -(far + near) / (far - near) };
float D{ -2.0f * far * near / (far - near) };
float E{ 2.0f * near / (right - left) };
float F{ 2.0f * near / (top - bottom) };
(In practice this simplifies out quite a bit though.)
*/
float C = -(far + near) / (far - near);
float D = -2.0f * far * near / (far - near);
float E = near / xMax;
float F = near / yMax;
mat4 result;
result.column[0] = _mm_setr_ps(E, 0, 0, 0);
result.column[1] = _mm_setr_ps(0, F, 0, 0);
result.column[2] = _mm_setr_ps(0, 0, C, -1);
result.column[3] = _mm_setr_ps(0, 0, D, 0);
return result;
}
mat4 linalgMakeLookAt(vec3 eye, vec3 target, vec3 up) {
vec3 forwards = linalgNormalizeVec3(linalgSubVec3(target, eye));
vec3 right = linalgNormalizeVec3(linalgCross(forwards, up));
up = linalgNormalizeVec3(linalgCross(right, forwards));
forwards = linalgMulVec3(forwards, -1);
mat4 result;
result.column[0] = _mm_setr_ps(right.data[0], up.data[0], forwards.data[0], 0);
result.column[1] = _mm_setr_ps(right.data[1], up.data[1], forwards.data[1], 0);
result.column[2] = _mm_setr_ps(right.data[2], up.data[2], forwards.data[2], 0);
result.column[3] = _mm_setr_ps(-linalgDotVec3(right, eye), -linalgDotVec3(up, eye), -linalgDotVec3(forwards, eye), 1);
return result;
}
mat4 linalgMakeTranslation(vec3 translation) {
mat4 result;
result.column[0] = _mm_setr_ps(1, 0, 0, 0);
result.column[1] = _mm_setr_ps(0, 1, 0, 0);
result.column[2] = _mm_setr_ps(0, 0, 1, 0);
result.column[3] = _mm_setr_ps(translation.data[0], translation.data[1], translation.data[2], 1);
return result;
}
mat4 linalgMakeXRotation(float angle) {
angle = linalgDeg2Rad(angle);
float cT = cosf(angle);
float sT = sinf(angle);
mat4 result;
result.column[0] = _mm_setr_ps(1, 0, 0, 0);
result.column[1] = _mm_setr_ps(0, cT, -sT, 0);
result.column[2] = _mm_setr_ps(0, sT, cT, 0);
result.column[3] = _mm_setr_ps(0, 0, 0, 1);
return result;
}
mat4 linalgMakeYRotation(float angle) {
angle = linalgDeg2Rad(angle);
float cT = cosf(angle);
float sT = sinf(angle);
mat4 result;
result.column[0] = _mm_setr_ps( cT, 0, sT, 0);
result.column[1] = _mm_setr_ps( 0, 1, 0, 0);
result.column[2] = _mm_setr_ps(-sT, 0, cT, 0);
result.column[3] = _mm_setr_ps( 0, 0, 0, 1);
return result;
}
mat4 linalgMakeZRotation(float angle) {
angle = linalgDeg2Rad(angle);
float cT = cosf(angle);
float sT = sinf(angle);
mat4 result;
result.column[0] = _mm_setr_ps(cT, -sT, 0, 0);
result.column[1] = _mm_setr_ps(sT, cT, 0, 0);
result.column[2] = _mm_setr_ps( 0, 0, 1, 0);
result.column[3] = _mm_setr_ps( 0, 0, 0, 1);
return result;
}
vec4 linalgMulMat4Vec4(mat4 m, vec4 v) {
vec4 result;
result.vector = _mm_fmadd_ps(_mm_set1_ps(v.data[0]), m.column[0],
_mm_fmadd_ps(_mm_set1_ps(v.data[1]), m.column[1],
_mm_fmadd_ps(_mm_set1_ps(v.data[2]), m.column[2],
_mm_mul_ps(_mm_set1_ps(v.data[3]), m.column[3])
)
)
);
return result;
}
mat4 linalgMulMat4Mat4(mat4 m1, mat4 m2) {
mat4 result;
result.column_vector[0] = linalgMulMat4Vec4(m2, m1.column_vector[0]);
result.column_vector[1] = linalgMulMat4Vec4(m2, m1.column_vector[1]);
result.column_vector[2] = linalgMulMat4Vec4(m2, m1.column_vector[2]);
result.column_vector[3] = linalgMulMat4Vec4(m2, m1.column_vector[3]);
return result;
}
mat4 linalgAddMat4(mat4 m1, mat4 m2) {
mat4 m3;
m3.chunk[0] = _mm256_add_ps(m1.chunk[0], m2.chunk[0]);
m3.chunk[1] = _mm256_add_ps(m1.chunk[1], m2.chunk[1]);
return m3;
}
mat4 linalgMulMat4Scalar(mat4 matrix, float scalar) {
mat4 m3;
__m256 scale = _mm256_set1_ps(scalar);
m3.chunk[0] = _mm256_mul_ps(matrix.chunk[0], scale);
m3.chunk[1] = _mm256_mul_ps(matrix.chunk[1], scale);
return m3;
}
mat4 linalgLerpMat4(mat4 m1, mat4 m2, float t) {
mat4 m3;
__m256 scale = _mm256_set1_ps(t);
m3.chunk[0] = _mm256_fmadd_ps(
_mm256_sub_ps(m2.chunk[0], m1.chunk[0]),
scale,
m1.chunk[0]
);
m3.chunk[1] = _mm256_fmadd_ps(
_mm256_sub_ps(m2.chunk[1], m1.chunk[1]),
scale,
m1.chunk[1]
);
return m3;
}
mat4 linalgTranspose(mat4 matrix) {
mat4 transposed;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
transposed.data[i + 4 * j] = matrix.data[j + 4 * i];
}
}
return transposed;
}
mat4 linalgTransformInverse(mat4 matrix) {
//Get the scale factors
float a = sqrtf(linalgDotVec4(matrix.column_vector[0], matrix.column_vector[0]));
float b = sqrtf(linalgDotVec4(matrix.column_vector[1], matrix.column_vector[1]));
float c = sqrtf(linalgDotVec4(matrix.column_vector[2], matrix.column_vector[2]));
//Get the rotation vectors, apply inverse scaling
vec4 X = linalgNormalizeVec4(matrix.column_vector[0]);
X.vector = _mm_mul_ps(X.vector, _mm_set1_ps(1 / a));
vec4 Y = linalgNormalizeVec4(matrix.column_vector[1]);
Y.vector = _mm_mul_ps(Y.vector, _mm_set1_ps(1 / b));
vec4 Z = linalgNormalizeVec4(matrix.column_vector[2]);
Z.vector = _mm_mul_ps(Z.vector, _mm_set1_ps(1 / c));
vec4 T = linalgNormalizeVec4(matrix.column_vector[3]);
T.vector = _mm_mul_ps(T.vector, _mm_set1_ps(-1));
mat4 inverse;
//Column adjustments
inverse.column[0] = _mm_setr_ps(X.data[0], Y.data[0], Z.data[0], 0);
inverse.column[1] = _mm_setr_ps(X.data[1], Y.data[1], Z.data[1], 0);
inverse.column[2] = _mm_setr_ps(X.data[2], Y.data[2], Z.data[2], 0);
inverse.column[3] = _mm_setr_ps(linalgDotVec4(X, T), linalgDotVec4(Y, T), linalgDotVec4(Z, T), 1);
return inverse;
}
/*-------- Quaternion Operations ----------*/
quat linalgMakeQuaternionFromComponents(float x, float y, float z, float w) {
quat q;
q.vector = _mm_setr_ps(x, y, z, w);
return q;
}
quat linalgMakeQuaternionFromRotation(float angle, vec3 axis) {
quat q;
axis = linalgNormalizeVec3(axis);
float s = sinf(linalgDeg2Rad(angle / 2));
float c = cosf(linalgDeg2Rad(angle / 2));
q.vector = _mm_mul_ps(axis.vector, _mm_set1_ps(s));
q.data[3] = c;
return q;
}
quat linalgMakeRotationFromVec2Vec(vec3 a, vec3 b) {
a = linalgNormalizeVec3(a);
b = linalgNormalizeVec3(b);
// a and b might be antiparallel
if (linalgClose(a, linalgMulVec3(b, -1.0f))) {
//we want to rotate around a to get to b,
//pick the least dominant component as the rotation direction
vec3 ortho = linalgMakeVec3(1, 0, 0);
if (fabsf(a.data[1]) < fabs(a.data[0])) {
ortho = linalgMakeVec3(0, 1, 0);
}
if (fabsf(a.data[2]) < min(fabs(a.data[0]), fabs(a.data[1]))) {
ortho = linalgMakeVec3(0, 0, 1);
}
vec3 axis = linalgNormalizeVec3(linalgCross(a, ortho));
return linalgMakeQuaternionFromComponents(
axis.data[0],
axis.data[1],
axis.data[2],
0.0f
);
}
//Construct the regular quaternion
vec3 halfVec = linalgNormalizeVec3(linalgAddVec3(a, b));
vec3 axis = linalgCross(a, halfVec);
return linalgMakeQuaternionFromComponents(
axis.data[0],
axis.data[1],
axis.data[2],
linalgDotVec3(a, halfVec)
);
}
vec3 linalgGetAxisFromQuaternion(quat q) {
return linalgNormalizeVec3(linalgMakeVec3(q.data[0], q.data[1], q.data[2]));
}
float linalgGetAngleFromQuaternion(quat q) {
return linalgRad2Deg(2.0f * acosf(q.data[3]));
}
quat linalgAddQuat(quat q1, quat q2) {
quat result;
result.vector = _mm_add_ps(q1.vector, q2.vector);
return result;
}
quat linalgSubQuat(quat q1, quat q2) {
quat result;
result.vector = _mm_sub_ps(q1.vector, q2.vector);
return result;
}
quat linalgMulQuat(quat q, float scalar) {
quat result;
result.vector = _mm_mul_ps(q.vector, _mm_set1_ps(scalar));
return result;
}
float linalgDotQuat(quat q1, quat q2) {
return q1.data[0] * q2.data[0]
+ q1.data[1] * q2.data[1]
+ q1.data[2] * q2.data[2]
+ q1.data[3] * q2.data[3];
}
bool linalgCloseQuat(quat q1, quat q2) {
quat displacement = linalgSubQuat(q2, q1);
return linalgDotQuat(displacement, displacement) < eps;
}
bool linalgQuatSameOrientation(quat q1, quat q2) {
quat displacement = linalgSubQuat(q1, q2);
if (linalgDotQuat(displacement, displacement) < eps) {
return true;
}
displacement = linalgAddQuat(q1, q2);
if (linalgDotQuat(displacement, displacement) < eps) {
return true;
}
return false;
}
quat linalgNormalizeQuat(quat q) {
float scalar = 1 / sqrtf(linalgDotQuat(q, q));
return linalgMulQuat(q, scalar);
}
quat linalgGetConjQuat(quat q) {
return linalgMakeQuaternionFromComponents(
-q.data[0],
-q.data[1],
-q.data[2],
q.data[3]
);
}
quat linalgInvQuat(quat q) {
return linalgMulQuat(linalgGetConjQuat(q), 1 / linalgDotQuat(q, q));
}