-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinear_algebros.h
426 lines (332 loc) · 10.8 KB
/
linear_algebros.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
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
#include <intrin.h>
#include <stdbool.h>
#ifndef __linear_algebros_h__
#define __linear_algebros_h__
typedef struct vec3 {
union {
__m128 vector;
float data[4];
};
} vec3;
typedef struct vec4 {
union {
__m128 vector;
float data[4];
};
} vec4;
typedef struct mat4 {
union {
__m256 chunk[2];
__m128 column[4];
vec4 column_vector[4];
float data[16];
};
} mat4;
typedef struct quaternion {
union {
__m128 vector;
float data[4];
};
} quat;
/*-------- Conversions ----------*/
#define pi 3.14159265359f
#define eps 1e-10f
/**
Convert from degrees to radians.
\param angle the angle to convert (in degrees)
\returns the angle converted to radians
*/
float linalgDeg2Rad(float angle);
/**
Convert from radians to degrees.
\param angle the angle to convert (in radians)
\returns the angle converted to degrees
*/
float linalgRad2Deg(float angle);
/*-------- Vec3 Operations ----------*/
/**
Construct a vec3 from individual floating point components.
*/
vec3 linalgMakeVec3(float x, float y, float z);
/**
Compute the dot product between two vec3s,
as with any dot product, the order doesn't matter.
\param a the first vector
\param b the second vector
\returns the dot product: a.b
*/
float linalgDotVec3(vec3 a, vec3 b);
/**
Compute the cross product between two vec3s
\param a the first vector
\param b the second vector
\returns a new vector storing the cross product: c = axb
*/
vec3 linalgCross(vec3 a, vec3 b);
/**
\param a the vector to normalize, cannot have zero magnitude
\returns a new vec3, being parallel with the input a, having unit length.
*/
vec3 linalgNormalizeVec3(vec3 a);
/**
Compute a vector subtraction.
\param a the first vector
\param b the second vector
\returns a new vector storing the difference: c = a - b
*/
vec3 linalgSubVec3(vec3 a, vec3 b);
/**
Compute a vector addition.
\param a the first vector
\param b the second vector
\returns a new vector storing the sum: c = a + b
*/
vec3 linalgAddVec3(vec3 a, vec3 b);
/**
Compute a vector scalar multiplication.
\param a the vector
\param scalar the scalar
\returns a new vector storing the scaled vector: c = scalar * a
*/
vec3 linalgMulVec3(vec3 a, float scalar);
/**
Get the angle between two vectors.
\param a the first vector, cannot have zero magnitude
\param b the second vector, cannot have zero magnitude
\returns the angle between the vectors a & b, in degrees
*/
float linalgAngleBetweenVectors3(vec3 a, vec3 b);
/**
Get the projection of one vector onto another.
Any vector v can be decomposed with regard to another vector u:
v = v(parallel with u) + v(perpendicular with u)
= projection(v onto u) + rejection(v onto u)
\param incoming the vector to be projected
\param basis the vector onto which to be projected, cannot have zero magnitude
\returns a new vector, parallel with basis, storing the vector projection of incoming onto basis
*/
vec3 linalgProject(vec3 incoming, vec3 basis);
/**
Get the rejection of one vector onto another.
Any vector v can be decomposed with regard to another vector u:
v = v(parallel with u) + v(perpendicular with u)
= projection(v onto u) + rejection(v onto u)
\param incoming the vector to be rejected
\param basis the vector to do the rejecting, cannot have zero magnitude
\returns a new vector, orthogonal to basis, storing the vector rejection of incoming from basis
*/
vec3 linalgReject(vec3 incoming, vec3 basis);
/**
Compute a vector reflection.
\param incident a direction vector incident to (pointing towards) the point of impact.
\param normal the normal vector about which to reflect. Must have unit length.
\returns a new vector representing the direction after reflecting.
*/
vec3 linalgReflect(vec3 incident, vec3 normal);
/**
Linearly interpolate between two vectors.
\param a the first vector
\param b the second vector
\param t the interpolation parameter. Typically between 0 and 1, though this isn't enforced
\returns a new vector, being a linear interpolation between a and b.
*/
vec3 linalgLerpVec3(vec3 a, vec3 b, float t);
/**
Spherical Linear interpolation between two vectors.
lerp will take a straight line between vectors, on the other hand,
slerp interpolates angle-wise, in a rotational sense.
\param a the first vector, should be normalized.
\param b the second vector, should be normalized.
\param t the interpolation parameter. Typically between 0 and 1, though this isn't enforced
\returns a new vector, being a linear interpolation between a and b.
*/
vec3 linalgSlerpVec3(vec3 a, vec3 b, float t);
/**
Normalized Linear interpolation between two vectors.
Normalizing the result of lerp will approximate slerp.
\param a the first vector, should be normalized.
\param b the second vector, should be normalized.
\param t the interpolation parameter. Typically between 0 and 1, though this isn't enforced
\returns a new vector, being a linear interpolation between a and b.
*/
vec3 linalgNlerpVec3(vec3 a, vec3 b, float t);
/**
Indicates whether two vectors are within epsilon of one another.
*/
bool linalgCloseVec3(vec3 a, vec3 b);
/*-------- Vector4 Operations ----------*/
/*
Under the hood, vec3 is really a vec4 with zeroed out w component,
however as convention has it right now, it's being treated as a seperate type.
*/
/**
\returns a normalized copy of the given vector.
*/
vec4 linalgNormalizeVec4(vec4 a);
/**
\returns the dot product result = a.b
*/
float linalgDotVec4(vec4 a, vec4 b);
/*-------- Matrix4 Operations ----------*/
/**
\returns a new 4x4 matrix storing the identity transform.
*/
mat4 linalgMakeIdentity4();
/**
Make a perspective projection matrix.
\param fovy the field of view angle of the frustrum (in degrees)
\param aspect the aspect ratio width/height
\param near the near view distance of the frustrum
\param far the far distance of the frustrum
\returns a new mat4 representing the perspective projection transform
*/
mat4 linalgMakePerspectiveProjection(float fovy, float aspect, float near, float far);
/**
Make a view matrix (translates and rotates the world around the
given reference point)
\param eye the position of the viewer
\param target the position the viewer is looking at
\param up the up direction from the viewer's point of reference
\returns a new mat4 representing the view transform
*/
mat4 linalgMakeLookAt(vec3 eye, vec3 target, vec3 up);
/**
Make a translation transform matrix.
\param translation the displacement to apply
\returns a new mat4 representing the transform
*/
mat4 linalgMakeTranslation(vec3 translation);
/**
Make a rotation around the x-axis.
\param angle the angle to rotate by (in degrees)
\returns a new mat4 representing the transform
*/
mat4 linalgMakeXRotation(float angle);
/**
Make a rotation around the y-axis.
\param angle the angle to rotate by (in degrees)
\returns a new mat4 representing the transform
*/
mat4 linalgMakeYRotation(float angle);
/**
Make a rotation around the z-axis.
\param angle the angle to rotate by (in degrees)
\returns a new mat4 representing the transform
*/
mat4 linalgMakeZRotation(float angle);
/**
Transform a vector by a matrix.
\param m the matrix to apply
\param v the vector to transform
\returns a new vec4 representing the matrix multiplication: result = m*v
*/
vec4 linalgMulMat4Vec4(mat4 m, vec4 v);
/**
Multiply two matrices
\param m1 the original matrix
\param m2 the new matrix to multiply onto m1
\returns a new mat4 representing the matrix multiplication: m3 = m2*m1
*/
mat4 linalgMulMat4Mat4(mat4 m1, mat4 m2);
/**
\returns the matrix sum m3 = m1 + m2
*/
mat4 linalgAddMat4(mat4 m1, mat4 m2);
/**
\returns the scalar multiplication result = scalar * matrix
*/
mat4 linalgMulMat4Scalar(mat4 matrix, float scalar);
/**
Blend (linearly interpolate) two matrices.
\param m1 the start matrix (t = 0)
\param m2 the end matrix (t = 1)
\param t the interpolation parameter
\returns the result m3 = m1 + t * (m2 - m1)
*/
mat4 linalgLerpMat4(mat4 m1, mat4 m2, float t);
/**
\returns a transposed copy of the given matrix
*/
mat4 linalgTranspose(mat4 matrix);
/**
Compute a transform matrix inverse.
General matrix inverses are computationally intense, however
most transform matrices can be expressed in the form:
M = (aX | bY | cZ | I)
where:
a: x axis scaling factor
X: forwards rotation vector
b: y axis scaling factor
Y: right rotation vector
c: z axis scale factor
Z: up rotation vector
I: [0 0 0 1]
Matrices in this form have a closed form inverse.
Source: https://lxjk.github.io/2017/09/03/Fast-4x4-Matrix-Inverse-with-SSE-SIMD-Explained.html
\param matrix the matrix to invert
\returns the inverse
*/
mat4 linalgTransformInverse(mat4 matrix);
/*-------- Quaternion Operations ----------*/
/**
\returns a quaternion made from individual components.
*/
quat linalgMakeQuaternionFromComponents(float x, float y, float z, float w);
/**
Make a quaternion from a rotation operation.
\param angle the rotation angle (in degrees)
\param axis the axis of rotation
\returns the corresponding quaternion
*/
quat linalgMakeQuaternionFromRotation(float angle, vec3 axis);
/**
Make a quaternion tracking a rotation from vector a to vector b.
*/
quat linalgMakeRotationFromVec2Vec(vec3 a, vec3 b);
/**
\returns the quaternion's axis of rotation
*/
vec3 linalgGetAxisFromQuaternion(quat q);
/**
\returns the quaternion's angle, in degrees
*/
float linalgGetAngleFromQuaternion(quat q);
/**
\returns the sum of two quaternions
*/
quat linalgAddQuat(quat q1, quat q2);
/**
\returns the difference of two quaternions
*/
quat linalgSubQuat(quat q1, quat q2);
/**
\returns a scaled copy of the quaternion
*/
quat linalgMulQuat(quat q, float scalar);
/**
\returns the dot product of two quaternions
*/
float linalgDotQuat(quat q1, quat q2);
/**
\returns whether two quaternions are sufficiently close.
*/
bool linalgCloseQuat(quat q1, quat q2);
/**
It's possible for two quaternions to be the same, but mathematically different.
Ie. a rotation in the opposite angle through the opposite axis is actually still the same.
\returns whether two quaternions have the same orientation.
*/
bool linalgQuatSameOrientation(quat q1, quat q2);
/**
\returns a normalized quaternion
*/
quat linalgNormalizeQuat(quat q);
/**
\returns the conjugate quaternion, a rotation of the same angle, around the opposite axis.
*/
quat linalgGetConjQuat(quat q);
/**
\returns the inverse of the given quaternion.
*/
quat linalgInvQuat(quat q);
#endif // !__linear_algebros_h__