summaryrefslogtreecommitdiff
path: root/themes/mathlib.cpp
blob: ee9bb1d01ff07c4e44ef28aa0e06c7b28fd499c4 (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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
// mathlib.cpp
//
// Created by Matt Kosarek
// 
// A file containing some common math functionality that I find
// useful in my projects. I don't like that I don't know what's happening
// in glm, so I wrote most of this myself. All mistakes are my own.
//

#include "mathlib.h"
#include <cstdlib>

// ***************************************
// Vector2
Vector2::Vector2() { }

Vector2::Vector2(float inX, float inY) {
    x = inX;
    y = inY;
}

Vector2 getRandomNormalVector2() {
    Vector2 retval = {
		static_cast<float>(rand()) / static_cast<float>(RAND_MAX),
		static_cast<float>(rand()) / static_cast<float>(RAND_MAX) 
	};

	return retval.normalize();
}

Vector2 Vector2::operator+(Vector2 other) {
    return { x + other.x, y + other.y };
}

Vector2& Vector2::operator+=(Vector2 other) {
    x += other.x;
    y += other.y;
    return *this;
}

Vector2 Vector2::operator-(Vector2 other) {
    return { x - other.x, y - other.y };
}

Vector2 Vector2::operator*(float s) {
    return { x * s, y * s };
}

Vector2 Vector2::operator/(float s) {
    return { x / s, y / s };
}

float Vector2::dot(Vector2 other) {
    return x * other.x + y * other.y;
}

float Vector2::length() {
    return sqrtf(x * x + y * y);
}

Vector2 Vector2::normalize() {
    float len = length();
    float inverseLength = len == 0 ? 1.0 : 1.0 / len;

    return { x * inverseLength, y * inverseLength };
}

Vector2 Vector2::negate() {
    return { -x, -y };
}

Vector2 Vector2::getPerp() {
    return { y, -x };
}

Vector2 Vector2::rotate(float angle) {
    return {
        x * cosf(angle) - y * sinf(angle),
        x * sinf(angle) + y * cosf(angle)
    };
}

Vector2 Vector2::rotateAbout(float angle, Vector2 p) {
    Vector2 prev = { x - p.x, y - p.y }; // Translate the point back to the origin
    Vector2 rot = prev.rotate(angle);    // Do the rotation
    return { rot.x + p.x, rot.y + p.y }; // Translate back to original position
}

void Vector2::printDebug(const char* name) {
    printf("%s=Vector2(%f, %f)\n", name, x, y);
}


// ***************************************
// Vector3
Vector3::Vector3() { };

Vector3::Vector3(float value) {
    x = value;
    y = value;
    z = value;
}

Vector3::Vector3(float inX, float inY, float inZ) {
    x = inX;
    y = inY;
    z = inZ;
}

float Vector3::length() {
    return sqrtf(x * x + y * y + z * z);
}

float Vector3::dot(const Vector3& other) {
	return x * other.x + y * other.y + z * other.z;
}

Vector3 Vector3::scale(float scalar) {
	return {
		x * scalar,
		y * scalar,
		z * scalar
	};
}

Vector3 Vector3::add(const Vector3& other) {
	return {
	    x + other.x,
		y + other.y,
		z + other.z
	};
}

Vector3 Vector3::subtract(const Vector3& other) {
	return {
		x - other.x,
		y - other.y,
		z - other.z
	};
}

Vector3 Vector3::negate() {
	return {
		-x,
		-y,
		-z
	};
}

Vector3 Vector3::cross(const Vector3& other) {
	return {
		y * other.z - z * other.y,
		z * other.x - x * other.z,
		x * other.y - y * other.x
	};
}

Vector3 Vector3::normalize() {
    float len = 1.f / length();
	return {
		x * len,
		y * len,
		z * len
	};
}

Vector3 Vector3::operator+(const Vector3& v2) {
	return add(v2);
}

Vector3& Vector3::operator+=(Vector3 other) {
    x += other.x;
    y += other.y;
    z += other.z;
    return *this;
}

Vector3 Vector3::operator-(const Vector3& v2) {
	return subtract(v2);
}

Vector3 Vector3::operator-() {
	return negate();
}

Vector3 Vector3::operator*(float value) {
	return scale(value);
}

Vector3 Vector3::operator/(const Vector3& v2) {
    return {
        x / v2.x,
        y / v2.y,
        z / v2.z        
    };
}

Vector3 Vector3::operator*(const Vector3& v2) {
	return {
	    x * v2.x,
	    y * v2.y,
	    z * v2.z
	};
}

float Vector3::operator [](int index) {
	switch (index) {
	case 0:
		return x;
	case 1:
		return y;
	case 2:
		return z;
	default:
		return 0;
	}
}


void Vector3::printDebug(const char* name) {
    printf("%s=Vector3(%f, %f, %f)\n", name, x, y, z);
}


// ***************************************
// Vector4
Vector4::Vector4() { }

Vector4::Vector4(float value) {
    x = value;
    y = value;
    z = value;
    w = value;
}

Vector4::Vector4(float inX, float inY, float inZ, float inW) {
    x = inX;
    y = inY;
    z = inZ;
    w = inW;
}

Vector4 Vector4::fromColor(float r, float g, float b, float a) {
    float scale = 1.f / 255.f;
    return { r * scale, g * scale, b * scale, a * scale };
}

Vector4 Vector4::toNormalizedColor() {
    return fromColor(x, y, z, w);
}

float Vector4::length() {
    return sqrtf(x * x + y * y + z * z + w * w);
}

Vector4 Vector4::normalize() {
    float len = length();
    float inverseLength = len == 0 ? 1.0 : 1.0 / len;

    return { x * inverseLength, y * inverseLength, z * inverseLength, w * inverseLength };
}

float Vector4::dot(const Vector4& other) {
    return x * other.x + y * other.y + z * other.z + w * other.w;
}

Vector4 Vector4::scale(float scalar) {
    return {
	    x * scalar,
	    y * scalar,
	    z * scalar,
	    w * scalar
	};
}

Vector4 Vector4::add(const Vector4& other) {
    return {
	    x + other.x,
		y + other.y,
		z + other.z,
		w + other.w
	};
}

Vector4 Vector4::subtract(const Vector4& other) {
    return {
	    x - other.x,
		y - other.y,
		z - other.z,
		w - other.w
	};
}

Vector4 Vector4::negate() {
    return { -x, -y, -z, -w };
}

Vector4 Vector4::cross(const Vector4& other) {
    return {
	    y * other.z - z * other.y,
		z * other.x - x * other.z,
		x * other.y - y * other.x,
		1.f
	};
}

Vector4 Vector4::operator+(const Vector4& v2) {
    return add(v2);
}

Vector4 Vector4::operator-(const Vector4& v2) {
    return subtract(v2);
}

Vector4 Vector4::operator-() {
    return negate();
}

Vector4 Vector4::operator*(float value) {
    return scale(value);
}

Vector4 Vector4::operator*(const Vector4& v2) {
    return {
	    x * v2.x,
	    y * v2.y,
	    z * v2.z,
	    w * v2.w
	};
}

float Vector4::operator[](int index) {
    switch (index) {
	case 0:
		return x;
	case 1:
		return y;
	case 2:
		return z;
    case 3:
        return w;
	default:
		return 0;
	}
}

void Vector4::printDebug(const char* name) {
    printf("%s=Vector4(%f, %f, %f, %f)\n", name, x, y, z, w);
}

// ***************************************
// Mat4x4
Mat4x4 Mat4x4::copy() {
    Mat4x4 result;
    memcpy(result.m, m, sizeof(float) * 16);
    return result;
}

Mat4x4 Mat4x4::scale(Vector3 v) {
    Mat4x4 result = copy();
    result.m[0] = result.m[0] * v.x;
    result.m[5] = result.m[5] *v.y;
    result.m[10] = result.m[10] * v.z;
    return result;
}

Mat4x4 Mat4x4::translate(Vector3 v) {
    Mat4x4 result = copy();
    result.m[12] += v.x;
    result.m[13] += v.y;
    result.m[14] += v.z;
    return result;
}

Mat4x4 Mat4x4::translateByVec2(Vector2 v) {
    Mat4x4 result = copy();
    result.m[12] += v.x;
    result.m[13] += v.y;
    return result;
}

Mat4x4 Mat4x4::rotate2D(float angle) {
    Mat4x4 result = copy();
    result.m[0] = cos(angle);
    result.m[1] = -sin(angle);
    result.m[4] = sin(angle);
    result.m[5] = cos(angle);
    return result;
}

Mat4x4 Mat4x4::getXRotationMatrix(float angleRadians) {
    return {
        { 1, 0, 0, 0,
          0, cos(angleRadians), -sin(angleRadians), 0,
          0, sin(angleRadians), cos(angleRadians), 0,
          0, 0, 0, 1 }
    };
}

Mat4x4 Mat4x4::getYRotationMatrix(float angleRadians) {
    return {
        { cos(angleRadians), 0, sin(angleRadians), 0,
          0, 1, 0, 0,
          -sin(angleRadians), 0, cos(angleRadians), 0,
          0, 0, 0, 1 }
    };
}

Mat4x4 Mat4x4::getZRotationMatrix(float angleRadians) {
    return {
        { cos(angleRadians), -sin(angleRadians), 0, 0,
          sin(angleRadians), cos(angleRadians), 0, 0,
          0, 0, 1, 0,
          0, 0, 0, 1 }
    };
}

Mat4x4 Mat4x4::rotate(float xRadians, float yRadians, float zRadians) {
    Mat4x4 result = copy();
        
    Mat4x4 rotationMatrix;
    if (xRadians != 0) {
        rotationMatrix = getXRotationMatrix(xRadians);
        result = result * rotationMatrix;
    }

    if (yRadians != 0) {
        rotationMatrix = getYRotationMatrix(yRadians);
        result = result * rotationMatrix;
    }

    if (zRadians != 0) {
        rotationMatrix = getZRotationMatrix(zRadians);
        result = result * rotationMatrix;
    }

    return result;
}

Vector2 Mat4x4::multByVec2(Vector2 v) {
    Vector4 vec4 = { v.x, v.y, 0.0, 1.0 };
    return {
        vec4.x * m[0] + vec4.y * m[4] + vec4.z * m[8] + vec4.w * m[12],
        vec4.x * m[1] + vec4.y * m[5] + vec4.z * m[9] + vec4.w * m[13]
    };
}

Vector2 Mat4x4::operator*(Vector2 v) {
    return multByVec2(v);
}

Mat4x4 Mat4x4::multMat4x4(const Mat4x4& other) {
    Mat4x4 result;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            int row = i * 4;
            result.m[row + j] = m[row + 0] * other.m[0 + j] + m[row + 1] * other.m[4 + j] + m[row + 2] * other.m[8 + j] + m[row + 3] * other.m[12 + j];
        }
    }

    return result;
}

Mat4x4 Mat4x4::operator*(const Mat4x4& other) {
    return multMat4x4(other);
}

Mat4x4 Mat4x4::getOrthographicMatrix(float left, float right, float bottom, float top) {
    Mat4x4 result;
    result.m[0] = 2.0 / (right - left);
    result.m[5] = 2.0 / (top - bottom);
    result.m[10] = 1.0;
    result.m[12] = -(right + left) / (right - left);
    result.m[13] = -(top + bottom) / (top - bottom);
    return result;
}

Mat4x4 Mat4x4::inverse() {
    Mat4x4 inv;

    inv.m[0] = m[5] * m[10] * m[15] - m[5] * m[11] * m[14] - m[9] * m[6] * m[15] + m[9] * m[7] * m[14] + m[13] * m[6] * m[11] - m[13] * m[7] * m[10];
    inv.m[4] = -m[4] * m[10] * m[15] + m[4] * m[11] * m[14] + m[8] * m[6] * m[15] - m[8] * m[7] * m[14] - m[12] * m[6] * m[11] + m[12] * m[7] * m[10];
    inv.m[8] = m[4] * m[9] * m[15] - m[4] * m[11] * m[13] - m[8] * m[5] * m[15] + m[8] * m[7] * m[13] + m[12] * m[5] * m[11] - m[12] * m[7] * m[9];
    inv.m[12] = -m[4] * m[9] * m[14] + m[4] * m[10] * m[13] + m[8] * m[5] * m[14] - m[8] * m[6] * m[13] - m[12] * m[5] * m[10] + m[12] * m[6] * m[9];
    inv.m[1] = -m[1] * m[10] * m[15] + m[1] * m[11] * m[14] + m[9] * m[2] * m[15] - m[9] * m[3] * m[14] - m[13] * m[2] * m[11] + m[13] * m[3] * m[10];
    inv.m[5] = m[0] * m[10] * m[15] - m[0] * m[11] * m[14] - m[8] * m[2] * m[15] + m[8] * m[3] * m[14] + m[12] * m[2] * m[11] - m[12] * m[3] * m[10];
    inv.m[9] = -m[0] * m[9] * m[15] + m[0] * m[11] * m[13] + m[8] * m[1] * m[15] - m[8] * m[3] * m[13] - m[12] * m[1] * m[11] + m[12] * m[3] * m[9];
    inv.m[13] = m[0] * m[9] * m[14] - m[0] * m[10] * m[13] - m[8] * m[1] * m[14] + m[8] * m[2] * m[13] + m[12] * m[1] * m[10] - m[12] * m[2] * m[9];
    inv.m[2] = m[1] * m[6] * m[15] - m[1] * m[7] * m[14] - m[5] * m[2] * m[15] + m[5] * m[3] * m[14] + m[13] * m[2] * m[7] - m[13] * m[3] * m[6];
    inv.m[6] = -m[0] * m[6] * m[15] + m[0] * m[7] * m[14] + m[4] * m[2] * m[15] - m[4] * m[3] * m[14] - m[12] * m[2] * m[7] + m[12] * m[3] * m[6];
    inv.m[10] = m[0] * m[5] * m[15] - m[0] * m[7] * m[13] - m[4] * m[1] * m[15] + m[4] * m[3] * m[13] + m[12] * m[1] * m[7] - m[12] * m[3] * m[5];
    inv.m[14] = -m[0] * m[5] * m[14] + m[0] * m[6] * m[13] + m[4] * m[1] * m[14] - m[4] * m[2] * m[13] - m[12] * m[1] * m[6] + m[12] * m[2] * m[5];
    inv.m[3] = -m[1] * m[6] * m[11] + m[1] * m[7] * m[10] + m[5] * m[2] * m[11] - m[5] * m[3] * m[10] - m[9] * m[2] * m[7] + m[9] * m[3] * m[6];
    inv.m[7] = m[0] * m[6] * m[11] - m[0] * m[7] * m[10] - m[4] * m[2] * m[11] + m[4] * m[3] * m[10] + m[8] * m[2] * m[7] - m[8] * m[3] * m[6];
    inv.m[11] = -m[0] * m[5] * m[11] + m[0] * m[7] * m[9] + m[4] * m[1] * m[11] - m[4] * m[3] * m[9] - m[8] * m[1] * m[7] + m[8] * m[3] * m[5];
    inv.m[15] = m[0] * m[5] * m[10] - m[0] * m[6] * m[9] - m[4] * m[1] * m[10] + m[4] * m[2] * m[9] + m[8] * m[1] * m[6] - m[8] * m[2] * m[5];

    float det = m[0] * inv.m[0] + m[1] * inv.m[4] + m[2] * inv.m[8] + m[3] * inv.m[12];

    if (det == 0)
        return Mat4x4();

    det = 1.f / det;

    for (int i = 0; i < 16; i++)
        inv.m[i] = inv.m[i] * det;

    return inv;
}

Mat4x4 Mat4x4::getPerspectiveProjection(float near, float far, float fieldOfViewRadians, float aspectRatio) {
    float halfFieldOfView = fieldOfViewRadians * 0.5f;
    float top = tan(halfFieldOfView) * near;
    float bottom = -top;
    float right = top * aspectRatio;
    float left = -right;

    return {
        { (2 * near) / (right - left), 0, 0, 0,
          0, (2 * near) / (top - bottom), 0, 0,
          (right + left) / (right - left), (top + bottom) / (top - bottom), -(far + near) / (far - near), -1,
          0, 0, (-2 * far * near) / (far - near), 0 }
    };
}

void Mat4x4::print() {
    printf("[ ");
    for (int idx = 0; idx < 16; idx++) {
        printf("%f, ", m[idx]);
    }
    printf(" ]\n");
}

// ***************************************
// Quaternion

float Quaternion::operator [](int index) {
	switch (index) {
	case 0:
		return x;
	case 1:
		return y;
	case 2:
		return z;
	case 3:
		return w;
	default:
		return 0;
	}
}

Quaternion Quaternion::operator*(const Quaternion& other) const {
	return {
		w * other.w - x * other.x - y * other.y - z * other.z,  // w
        w * other.x + x * other.w + y * other.z - z * other.y,  // i
        w * other.y - x * other.z + y * other.w + z * other.x,  // j
        w * other.z + x * other.y - y * other.x + z * other.w   // k
	};
}

Quaternion Quaternion::operator*(const float& scale) const {
	return {
		w * scale,
		x * scale,
		y * scale,
		z * scale
	};
}

Quaternion Quaternion::operator+(const Quaternion& other) const {
	return {
		w + other.w,
		x + other.x,
		y + other.y,
		z + other.z
	};
}

Quaternion Quaternion::operator-(const Quaternion& other) const {
	return {
		w - other.w,
		x - other.x,
		y - other.y,
		z - other.z
	};
}

const float DOT_THRESHOLD = 0.9999f; 

// Using a slerp here, mostly taken from here: http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/.
// As JBlow says, it's expensive as heck. I will address this if it becomes a problem.
Quaternion Quaternion::interpolate(const Quaternion& other, const float factor) {
	Quaternion newOther = other;
	float dotProduct = dot(other);

	if (dotProduct < 0) {
		newOther = other * -1;
		dotProduct *= -1;
	}

	if (dotProduct > DOT_THRESHOLD) {
		return (*this + ((newOther - *this) * factor)).normalize();
	}

	float thetaZero = acos(dotProduct); // angle between input vectors
	float theta = thetaZero * factor;

	Quaternion v2 = (newOther - (*this * dotProduct)).normalize();

	return (*this * cos(theta)) + (v2 * sin(theta));
}

float Quaternion::length() const {
	return sqrtf(x * x + y * y + z * z + w * w);
}

Quaternion Quaternion::normalize() const {
	float l = length();
	return {
		w / l,
		x / l,
		y / l,
		z / l,
	};
}

Mat4x4 Quaternion::toMatrix() const {
	return {
		{
			1 - 2 * (y * y +  z * z), 
			2 * (x * y + z * w),
			2 * (x * z - y * w),
			0,
			2 * (x * y - w * z),
			1 - 2 * (x * x + z * z),
			2 * (y * z + w * x),
			0,
			2 * (x * z + w * y),
			2 * (y * z - w * x),
			1 - 2 * (x * x + y * y),
			0,
			0,
			0,
			0,
			1
		}
	};
}

float Quaternion::dot(const Quaternion& other) const {
	return w * other.w + x * other.x + y * other.y + z * other.z;
}

Quaternion quaternionFromRotation(Vector3 axis, float angleRadians) {
    float halfAngleRadians = angleRadians / 2.f;
    float cosHalfAngRad = cosf(halfAngleRadians);
    float sinHalfAngRad = sinf(halfAngleRadians);

    return {
        cosHalfAngRad,
        axis.x * sinHalfAngRad,
        axis.y * sinHalfAngRad,
        axis.z * sinHalfAngRad
    };
}