-
Notifications
You must be signed in to change notification settings - Fork 0
/
jm_math.h
2027 lines (1646 loc) · 57.9 KB
/
jm_math.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
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2016 Jeremy F. Montgomery (jeremyfmontgomery@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated
documentation files (the "Software"), to deal in the Software without
restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of
the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#ifndef JM_MATH_H
#define JM_MATH_H
#ifndef NULL
#define NULL 0
#endif
#include <math.h>
// NOTE: You can Use the following defines _before_ include this file
// to change what is included
//
// 1. JM_MATH_IMPLEMENTATION(will include the implementation. Otherwise
// this file acts like a regular
// header)
// 1. JM_MATH_USE_SSE (will include sse functions)
// 2. JM_MATH_USE_AVX (will include avx functions)
// 3. JM_MATH_USE_SSE_TRANSCENDENTALS (Needed for some sse functions.
// Currently requires and external file
// for sin_ps and cos_ps implementations)
// 4. JM_USE_SIMD_ALL (include all simd related functions)
#ifndef JM_TYPES
#define JM_TYPES
#include <stdint.h>
#define internal static
#define local_persist static
#define global static
#define global_variable static
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef i32 b32;
typedef float r32;
typedef double r64;
#endif // JM_TYPES
#ifdef JM_MATH_USE_SIMD_ALL
#define JM_MATH_USE_SSE
#define JM_MATH_USE_AVX
#define JM_MATH_USE_SSE_TRANSCENDENTALS
#endif // JM_MATH_USE_SIMD_ALL
#ifdef JM_MATH_USE_SSE
#include <emmintrin.h>
#include <immintrin.h>
#include <xmmintrin.h>
#endif // JM_MATH_USE_SSE
#ifdef JM_MATH_USE_SSE_TRANSCENDENTALS
#define USE_SSE2
#include "simd_sincos.h"
#endif // JM_MATH_USE_SSE_TRANSCENDENTALS
#define PI_32 3.14159265358979323846f
#define JM_SWAP(x, y, type) \
do { \
type temp = x; \
x = y; \
y = temp; \
} while (0)
#define JM_EPSILON 1e-4
#define JM_APPROX(x, y) (fabs((x) - (y)) <= JM_EPSILON)
#define JM_APPROX_EP(x, y, v) (JM_ABSOLUTE((x) - (y)) < (v))
#define JM_APPROX_V2(a, b) (JM_APPROX((a).x, (b).x) && JM_APPROX((a).y, (b).y))
#define JM_SQR(a) ((a) * (a))
union v2;
union v3;
union v4;
struct mat4x4;
struct aabb_2D;
struct aabb_3D;
// clang-format off
//==========================================================================================================
//
//NOTE: Scalar Declarations
//
//==========================================================================================================
//NOTE: degrees to radians
r32 jmToRadians(r32 angle);
// NOTE(Jeremy): These could be macros but that would present other issues like double eval.
inline i32 jmMin(i32 x, i32 y);
inline i32 jmMax(i32 x, i32 y);
inline i64 jmMin(i64 x, i64 y);
inline i64 jmMax(i64 x, i64 y);
inline r32 jmMin(r32 x, r32 y);
inline r32 jmMax(r32 x, r32 y);
inline r64 jmMin(r64 x, r64 y);
inline r64 jmMax(r64 x, r64 y);
inline r32 jmApproach(r32 goal, r32 current, r32 deltaTime);
inline r32 jmLerp(r32 start, r32 end, r32 percent);
inline r32 jmSinerp(r32 start, r32 end, r32 percent); //NOTE: Eases in
inline r32 jmCoserp(r32 start, r32 end, r32 percent); //NOTE: Eases out
inline i32 jmClamp(i32 current, i32 min, i32 max);
inline r32 jmClamp01(r32 current);
inline r32 jmClampReal(r32 current, r32 min, r32 max);
//NOTE: rounds 0.5 and greater up.
inline i32 jmRoundRealToInt(r32 num);
//NOTE: Remaps value from the range of min0-max0 to the range of min1-max1
inline r32 jmRemap(r32 value, r32 min0, r32 max0, r32 min1, r32 max1);
//NOTE: Mod that handles negatives correctly
inline i32 jmMod(i32 x, i32 factor);
//NOTE: Round a number based on the mod factor passed in(eg. round to the nearest 50)
inline i32 jmModRound(r32 x, i32 mod);
//NOTE: based on degrees
inline r32 jmRotationClamp(r32 rot);
//=========================================================================================================
//
// NOTE: v2 Declarations
//
//=========================================================================================================
union v2 {
struct {
r32 x, y;
};
r32 v[2];
};
global_variable v2 v2Down = {0.0f, -1.0f};
global_variable v2 v2Up = {0.0f, 1.0f};
global_variable v2 v2Right = {1.0f, 0.0f};
global_variable v2 v2Left = {-1.0f, 0.0f};
inline v2 V2(r32 x, r32 y);
// NOTE: sets all components to value
inline v2 V2(r32 value);
// NOTE: result uses input's x and y and discards z
inline v2 V2(v3 a);
// NOTE: sets all components to "value"
inline v2 V2(r32 value);
// TODO: find out why MSVC(2013) fails to optimize without passing by reference on v2 but not v3
inline v2 operator+(v2 a, v2 b);
inline v2 operator-(v2 a, v2 b);
inline v2 operator*(v2 a, r32 scalar);
inline v2 operator-(v2 a);
inline v2 &operator+=(v2 &a, v2 b);
inline v2 &operator-=(v2 &a, v2 b);
inline v2 &operator*=(v2 &a, r32 scalar);
inline b32 operator==(v2 a, v2 b);
inline r32 jmLengthSqr(v2 a);
inline r32 jmLengthSqr(r32 x, r32 y);
inline r32 jmLength(v2 a);
inline r32 jmLength(r32 x, r32 y);
inline v2 jmNormalized(v2 a);
inline v2 jmNormalized(r32 x, r32 y);
inline void jmNormalize(v2 *a);
inline r32 jmDot(v2 a, v2 b);
inline r32 jmDot(r32 lhsX, r32 lhsY, r32 rhsX, r32 rhsY);
inline v2 jmLerp(v2 start, v2 end, r32 percent);
inline v2 jmLerp(r32 startX, r32 startY, r32 endX, r32 endY, r32 percent);
inline v2 jmSinerp(v2 start, v2 end, r32 percent);
inline v2 jmSinerp(r32 startX, r32 startY, r32 endX, r32 endY, r32 percent);
inline v2 jmNlerp(v2 start, v2 end, r32 percent);
inline v2 jmNlerp(r32 startX, r32 startY, r32 endX, r32 endY, r32 percent);
inline v2 jmSlerp(v2 start, v2 end, r32 percent);
inline v2 jmSlerp(r32 startX, r32 startY, r32 endX, r32 endY, r32 percent);
inline r32 jmRadiansBetween(v2 a, v2 b);
inline r32 jmRadiansBetween(r32 lhsX, r32 lhsY, r32 rhsX, r32 rhsY);
inline v2 jmRotateVectorRadians(v2 a, r32 rad);
inline v2 jmRotateVectorRadians(r32 x, r32 y, r32 rad);
inline v2 jmReflect(v2 a, v2 normal);
inline v2 jmReflect(r32 x, r32 y, v2 normal);
// NOTE: clamps a vector within an axis-aligned bounding box with the center of the box as the origin.
inline v2 jmClampInRect(v2 current, v2 min, v2 max);
//=========================================================================================================
//
// NOTE: v3 Declarations
//
//=========================================================================================================
union v3 {
struct {
r32 x, y, z;
};
r32 v[3];
};
global v3 v3Up = {0.0f, 1.0f, 0.0f};
global v3 v3Right = {1.0f, 0.0f, 0.0f};
global v3 v3Forward = {0.0f, 0.0f, 1.0f};
inline v3 V3(r32 x, r32 y, r32 z);
//NOTE: copies a's x and y. Sets z to zero
inline v3 V3(v2 a);
//NOTE: sets all components to value
inline v3 V3(r32 value);
//NOTE: copies a's x and y. Sets z to "z"
inline v3 V3(v2 a, r32 z);
//NOTE: copies a's x, y and z. Discards w
inline v3 V3(v4 a);
inline v3 operator+(v3 a, v3 b);
inline v3 operator-(v3 a, v3 b);
inline v3 operator*(v3 a, r32 scalar);
inline v3 operator-(v3 a);
inline v3 & operator+=(v3 &a, v3 b);
inline v3 & operator-=(v3 &a, v3 b);
inline v3 & operator*=(v3 &a, r32 scalar);
inline r32 jmLength(v3 a);
inline r32 jmLength(r32 x, r32 y, r32 z);
inline r32 jmLengthSqr(v3 a);
inline r32 jmLengthSqr(r32 x, r32 y, r32 z);
inline v3 jmNormalized(v3 a);
inline v3 jmNormalized(r32 x, r32 y, r32 z);
//NOTE: Normalizes in-place instead of copying
inline void jmNormalize(v3 *a);
inline r32 jmDot(v3 lhs, v3 rhs);
inline r32 jmDot(r32 lhsX, r32 lhsY, r32 lhsZ, r32 rhsX, r32 rhsY, r32 rhsZ);
inline v3 jmCross(v3 a, v3 b);
inline v3 jmCross(r32 lhsX, r32 lhsY, r32 lhsZ, r32 rhsX, r32 rhsY, r32 rhsZ);
inline v3 jmReflect(v3 a, v3 normal);
inline v3 jmReflect(r32 lhsX, r32 lhsY, r32 lhsZ, r32 rhsX, r32 rhsY, r32 rhsZ);
inline v3 jmLerp(v3 start, v3 end, r32 percent);
inline v3 jmLerp(r32 startX, r32 startY, r32 startZ, r32 endX, r32 endY, r32 endZ, r32 percent);
//NOTE: Eases in (speeds up as it approaches 1)
inline v3 jmSinerp(v3 start, v3 end, r32 percent);
inline v3 jmSinerp(r32 startX, r32 startY, r32 startZ, r32 endX, r32 endY, r32 endZ, r32 percent);
//NOTE: Eases out (slows down as it approaches 1)
inline v3 jmCoserp(v3 start, v3 end, r32 percent);
inline v3 jmCoserp(r32 startX, r32 startY, r32 startZ, r32 endX, r32 endY, r32 endZ, r32 percent);
inline v3 jmNlerp(v3 start, v3 end, r32 percent);
inline v3 jmNlerp(r32 startX, r32 startY, r32 startZ, r32 endX, r32 endY, r32 endZ, r32 percent);
inline v3 jmSlerp(v3 start, v3 end, r32 percent);
inline v3 jmSlerp(r32 startX, r32 startY, r32 startZ, r32 endX, r32 endY, r32 endZ, r32 percent);
//NOTE: checks if a and b are almost equal. Threshold is based on JM_EPSILON
inline b32 jmApproximately(v3 a, v3 b);
//=========================================================================================================
//
//NOTE: v4 Declarations
//
//=========================================================================================================
union v4 {
struct {
r32 x, y, z, w;
};
struct {
r32 r, g, b, a;
};
r32 v[4];
};
inline v4 V4(r32 x, r32 y, r32 z, r32 w);
inline v4 V4(v3, r32 z);
//NOTE: sets all components to value
inline v4 V4(r32 value);
//NOTE: Extracts color channels from a uint32_t and puts them in a v4
inline v4 V4(uint32_t color);
inline v4 operator*(v4 v, r32 s);
inline v4 operator+(v4 v0, v4 v1);
inline v4 operator-(v4 v0, v4 v1);
inline v4 &operator+=(v4 &v0, v4 v1);
inline v4 &operator-=(v4 &v0, v4 v1);
inline v4 jmLerp(v4 start, v4 end, r32 percent);
//=========================================================================================================
//
//NOTE: mat3x3 Declarations
//
//=========================================================================================================
struct mat3x3 {
r32 v[3][3];
};
//NOTE: returns upper left 3x3 of the input 4x4 matrix
inline mat3x3 Mat3x3(mat4x4 m);
inline mat3x3 Mat3x3(v3 a, v3 b, v3 c);
inline mat3x3 jmTanspose(mat3x3 m);
inline mat3x3 jmInverse(mat3x3 m);
//=========================================================================================================
//
//NOTE: mat4x4 Declarations
//
//=========================================================================================================
struct mat4x4 {
r32 v[4][4];
};
inline mat4x4 jmGetUpper3x3(mat4x4 m);
inline v3 jmGetForward(mat4x4 m);
inline v3 jmGetUp(mat4x4 m);
inline v3 jmGetRight(mat4x4 m);
//NOTE: returns normalized vectors
inline v3 jmGetUnitForward(mat4x4 m);
inline v3 jmGetUnitUp(mat4x4 m);
inline v3 jmGetUnitRight(mat4x4 m);
inline v3 jmGetScale(mat4x4 m);
inline v3 jmGetPosition(mat4x4 m);
inline mat4x4 operator*(mat4x4 a, mat4x4 b);
inline v4 operator*(mat4x4 m, v4 v);
inline mat4x4 jmIdentityMatrix();
inline mat4x4 jmTranspose(mat4x4 m);
inline mat4x4 jmInverse(mat4x4 m);
inline mat4x4 jmScale(mat4x4 m, v3 scale);
inline mat4x4 jmScale(v3 scale);
inline mat4x4 jmScale(r32 scale);
inline mat4x4 jmTranslate(mat4x4 mat, v3 trans);
inline mat4x4 jmTranslate(v3 trans);
inline mat4x4 jmRotate(mat4x4 mat, r32 radians, v3 axis);
inline mat4x4 jmRotate(r32 radians, v3 axis);
inline mat4x4 jmPerspective(r32 fov, r32 aspectRatio, r32 near, r32 far);
inline mat4x4 jmOrthographic(r32 left, r32 right, r32 bottom, r32 top, r32 near, r32 far);
inline mat4x4 jmLookAt(v3 position, v3 target, v3 up);
//=========================================================================================================
//
//NOTE: AABB Declarations (Axis-Aligned Bounding Box)
//
//=========================================================================================================
struct aabb_2D {
v2 min;
v2 max;
};
inline aabb_2D operator+(aabb_2D a, v2 b);
// NOTE: Checks if A contains B
inline b32 jmAABBContains(aabb_2D a, v2 b);
inline b32 jmAABBContains(aabb_2D a, aabb_2D b);
inline b32 jmAABBIntersects(aabb_2D a, aabb_2D b);
internal b32 jmAABBClipLine(i32 d, aabb_2D aabb_2D, v2 v0, v2 v1, r32 *outLow, r32 high);
internal b32 jmAABBLineIntersection(aabb_2D aabb_2D, v2 v0, v2 v1, v2 *outIntersection, r32 *outFraction);
internal b32 jmAABBTraceLine(aabb_2D box, v2 boxPosition, v2 v0, v2 v1, v2 *intersection);
inline v2 jmAABBGetNormalFromIntersection(aabb_2D bounds, v2 worldPosition, v2 intersection);
//NOTE: ensures that min and max points are actually min and max
inline aabb_2D jmAABBCorrect(aabb_2D a);
inline aabb_2D jmAABBRotate90(aabb_2D a);
//TODO: create separate section
struct aabb_3D {
v3 min;
v3 max;
};
//=========================================================================================================
//
//NOTE: SSE Declarations
//
//=========================================================================================================
#ifdef JM_USE_SSE
inline __m128 _vectorcall jmLengthSqrSSE(__m128 x, __m128 y);
inline __m128 _vectorcall jmLengthSSE(__m128 x, __m128 y);
inline __m128 _vectorcall jmLerpSSE(__m128 start, __m128 end, __m128 percent);
inline __m128 _vectorcall jmSinerpSSE(__m128 start, __m128 end, __m128 percent);
inline __m128 _vectorcall jmCoserpSSE(__m128 start, __m128 end, __m128 percent);
inline __m128 _vectorcall jmDotSSE(__m128 lhsX, __m128 lhsY, __m128 rhsX, __m128 rhsY);
inline __m128 _vectorcall jmDotSSE(__m128 lhsX, __m128 lhsY, __m128 lhsZ, __m128 rhsX, __m128 rhsY, __m128 rhsZ);
inline __m128 _vectorcall ZInTriangleSSE(__m128 px1, __m128 py1, __m128 pz1,
__m128 px2, __m128 py2, __m128 pz2,
__m128 px3, __m128 py3, __m128 pz3,
__m128 x, __m128 y);
#ifdef JM_USE_SSE_TRANSCENDENTALS
inline __m128 _vectorcall jmSinerpSSE(__m128 start, __m128 end, __m128 percent);
inline __m128 _vectorcall jmCoserpSSE(__m128 start, __m128 end, __m128 percent);
#endif //JM_USE_SSE_TRANSCENDENTALS
#endif
//=========================================================================================================
//
//NOTE: AVX Declarations
//
//=========================================================================================================
#ifdef JM_MATH_USE_AVX
inline __m256 _vectorcall jmNewtonRaphsonSqrtAVX(__m256 x);
inline __m256 _vectorcall jmLengthNewtRaphAVX(__m256 x, __m256 y);
inline __m256 _vectorcall jmLengthSqrAVX(__m256 x, __m256 y);
inline __m256 _vectorcall jmLengthAVX(__m256 x, __m256 y);
#endif
#ifdef JM_MATH_IMPLEMENTATION
//==========================================================================================================
//
// NOTE: Scalar Implementation
//
//==========================================================================================================
r32
jmToRadians(r32 angle) {
r32 result = (PI_32 / 180) * angle;
return result;
}
// NOTE(Jeremy): These could be macros but that would present other issues like
// double eval
inline i32
jmMin(i32 x, i32 y) {
return (x < y ? x : y);
}
inline i32
jmMax(i32 x, i32 y) {
return (x > y ? x : y);
}
inline i64
jmMin(i64 x, i64 y) {
return (x < y ? x : y);
}
inline i64
jmMax(i64 x, i64 y) {
return (x > y ? x : y);
}
inline r32
jmMin(r32 x, r32 y) {
return fmin(x, y);
}
inline r32
jmMax(r32 x, r32 y) {
return fmax(x, y);
}
inline r64
jmMin(r64 x, r64 y) {
return fmin(x, y);
}
inline r64
jmMax(r64 x, r64 y) {
return fmax(x, y);
}
inline r32
jmApproach(r32 goal, r32 current, r32 deltaTime) {
r32 difference = goal - current;
if (difference > deltaTime) {
return current + deltaTime;
}
if (difference < -deltaTime) {
return current - deltaTime;
}
return goal;
}
inline r32
jmLerp(r32 start, r32 end, r32 percent) {
return start + ((end - start) * percent);
}
inline r32
jmSinerp(r32 start, r32 end, r32 percent) {
return jmLerp(start, end, sinf(percent * PI_32 * 0.5f));
}
inline r32
jmCoserp(r32 start, r32 end, r32 percent) {
return jmLerp(start, end, 1.0f - cosf(percent * PI_32 * 0.5f));
}
inline i32
jmClamp(i32 current, i32 min, i32 max) {
return jmMax(min, jmMin(current, max));
}
inline r32
jmClamp01(r32 current) {
if (current > 1.0f) return 1.0f;
if (current < 0.0f) return 0.0f;
return current;
}
inline r32
jmClampReal(r32 current, r32 min, r32 max) {
return jmMax(min, jmMin(current, max));
}
// TODO: Fix this round function. Seems to give inconsistent results
inline i32
jmRoundRealToInt(r32 num) {
// TODO: replace this with better round.
i32 truncation = (i32)num;
return (num - truncation >= 0.5f) ? truncation + 1 : truncation;
}
inline r32
jmRemap(r32 value, r32 min0, r32 max0, r32 min1, r32 max1) {
return min1 + ((max1 - min1) * ((value - min0) / (max0 - min0)));
}
inline i32
jmMod(i32 x, i32 factor) {
i32 result = x % factor;
if (result < 0) result += factor;
return result;
}
inline i32
jmModRound(r32 x, i32 mod) {
i32 result = (i32)x;
i32 modded = jmMod((i32)x, mod);
result += (modded >= mod / 2) ? (mod - modded) : -modded;
return result;
}
inline r32
jmRotationClamp(r32 rot) {
r32 result = rot;
if (rot > 360.0f)
result -= 360.0f;
else if (rot < 0.0f)
result += 360.0f;
return result;
}
//=========================================================================================================
//
// NOTE: V2 Implementation
//
//=========================================================================================================
inline v2
V2(r32 x, r32 y) {
v2 result = {x, y};
return result;
}
inline v2
V2(r32 a) {
v2 result = {a, a};
return result;
}
inline v2
V2(v3 a) {
return V2(a.x, a.y);
}
inline v2
operator+(v2 a, v2 b) {
v2 result = {a.x + b.x, a.y + b.y};
return result;
}
inline v2
operator-(v2 a, v2 b) {
v2 result = {a.x - b.x, a.y - b.y};
return result;
}
inline v2 operator*(v2 a, r32 scalar) {
v2 result = {a.x * scalar, a.y * scalar};
return result;
}
inline v2
operator-(v2 a) {
v2 result = {-a.x, -a.y};
return result;
}
inline v2 &
operator+=(v2 &a, v2 b) {
a.x += b.x;
a.y += b.y;
return a;
}
inline v2 &
operator-=(v2 &a, v2 b) {
a.x -= b.x;
a.y -= b.y;
return a;
}
inline v2 &
operator*=(v2 &a, r32 scalar) {
a.x *= scalar;
a.y *= scalar;
return a;
}
inline b32
operator==(v2 a, v2 b) {
return (a.x == b.x && a.y == b.y);
}
inline r32
jmLengthSqr(v2 a) {
return a.x * a.x + a.y * a.y;
}
inline r32
jmLengthSqr(r32 x, r32 y) {
return x * x + y * y;
}
inline r32
jmLength(v2 a) {
return sqrtf(jmLengthSqr(a));
}
inline r32
jmLength(r32 x, r32 y) {
return sqrtf(jmLengthSqr(x, y));
}
inline v2
jmNormalized(r32 x, r32 y) {
r32 len = jmLength(x, y);
if (len == 0.0f) return {x, y};
r32 invLen = 1.0f / len;
v2 result = {x * invLen, y * invLen};
return result;
}
inline v2
jmNormalized(v2 a) {
return jmNormalized(a.x, a.y);
}
inline void
jmNormalize(v2 *a) {
*a = jmNormalized(*a);
}
inline r32
jmDot(v2 a, v2 b) {
return a.x * b.x + a.y * b.y;
}
inline r32
jmDot(r32 lhsX, r32 lhsY, r32 rhsX, r32 rhsY) {
return lhsX * rhsX + lhsY * rhsY;
}
inline v2
jmLerp(v2 start, v2 end, r32 percent) {
return start + ((end - start) * percent);
}
inline v2
jmLerp(r32 startX, r32 startY, r32 endX, r32 endY, r32 percent) {
v2 result = {jmLerp(startX, endX, percent), jmLerp(startY, endY, percent)};
return result;
}
inline v2
jmSinerp(v2 start, v2 end, r32 percent) {
return jmLerp(start, end, sinf(percent * PI_32 * 0.5f));
}
inline v2
jmSinerp(r32 startX, r32 startY, r32 endX, r32 endY, r32 percent) {
v2 result = {jmSinerp(startX, endX, percent), jmSinerp(startY, endY, percent)};
return result;
}
inline v2
jmCoserp(v2 start, v2 end, r32 percent) {
return jmLerp(start, end, 1.0f - cosf(percent * PI_32 * 0.5f));
}
inline v2
jmCoserp(r32 startX, r32 startY, r32 endX, r32 endY, r32 percent) {
v2 result = {jmCoserp(startX, endX, percent), jmCoserp(startY, endY, percent)};
return result;
}
inline v2
jmNlerp(v2 start, v2 end, r32 percent) {
return jmNormalized((start + ((end - start) * percent)));
}
inline v2
jmNlerp(r32 startX, r32 startY, r32 endX, r32 endY, r32 percent) {
r32 x = jmLerp(startX, endX, percent);
r32 y = jmLerp(startY, endY, percent);
return jmNormalized(x, y);
}
inline v2
jmSlerp(v2 start, v2 end, r32 percent) {
r32 dotP = jmClampReal(jmDot(jmNormalized(start), jmNormalized(end)), -1.0f, 1.0f);
r32 theta = acos(dotP) * percent;
// TODO: is this supposed to be ((end - start) * dotP) or (end - (start *
// dotP))
v2 relative = jmNormalized(end - (start * dotP));
return (start * cos(theta)) + (relative * sin(theta));
}
inline v2
jmSlerp(r32 startX, r32 startY, r32 endX, r32 endY, r32 percent) {
r32 dotP = jmClampReal(jmDot(jmNormalized(startX, startY), jmNormalized(endX, endY)), -1.0f, 1.0f);
r32 theta = acos(dotP) * percent;
// TODO: is this supposed to be ((end - start) * dotP) or (end - (start *
// dotP))
v2 relative = jmNormalized(endX - (startX * dotP), endY - (startY * dotP));
r32 cosine = cos(theta);
r32 sine = sin(theta);
return {startX * cosine + relative.x * sine, startY * cosine + relative.y * sine};
}
inline r32
jmRadiansBetween(v2 a, v2 b) {
r32 result = atan2(b.y, b.x) - atan2(a.y, a.x);
return result;
}
inline r32
jmRadiansBetween(r32 lhsX, r32 lhsY, r32 rhsX, r32 rhsY) {
r32 result = atan2(rhsY, rhsX) - atan2(lhsY, lhsX);
return result;
}
inline v2
jmRotateVectorRadians(v2 a, r32 rad) {
v2 result;
result.x = (a.x * cos(rad)) - (a.y * sin(rad));
result.y = (a.y * cos(rad)) + (a.x * sin(rad));
return result;
}
inline v2
jmRotateVectorRadians(r32 x, r32 y, r32 rad) {
v2 result;
result.x = (x * cos(rad)) - (y * sin(rad));
result.y = (y * cos(rad)) + (x * sin(rad));
return result;
}
inline v2
jmReflect(v2 a, v2 normal) {
v2 result = a + (normal * (-2 * jmDot(a, normal)));
return result;
}
inline v2
jmReflect(r32 x, r32 y, r32 normX, r32 normY) {
r32 reflectionScale = -2 * jmDot(x, y, normX, normY);
v2 result{x + (normX * reflectionScale), y + (normY * reflectionScale)};
return result;
}
inline v2
jmClampInRect(v2 current, v2 min, v2 max) {
v2 result;
result.x = jmClampReal(current.x, min.x, max.x);
result.y = jmClampReal(current.y, min.y, max.y);
return result;
}
//=========================================================================================================
//
// NOTE: v3 Implementation
//
//=========================================================================================================
inline v3
V3(r32 x, r32 y, r32 z) {
return {x, y, z};
}
inline v3
V3(r32 value) {
return {value, value, value};
}
inline v3
V3(v2 a) {
return {a.x, a.y, 0.0f};
}
inline v3
V3(v2 a, r32 z) {
return {a.x, a.y, z};
}
inline v3
V3(v4 a) {
return {a.x, a.y, a.z};
}
inline v3
operator+(v3 a, v3 b) {
v3 result = {a.x + b.x, a.y + b.y, a.z + b.z};
return result;
}
inline v3
operator-(v3 a, v3 b) {
v3 result = {a.x - b.x, a.y - b.y, a.z - b.z};
return result;
}
inline v3 operator*(v3 a, r32 scalar) {
v3 result = {a.x * scalar, a.y * scalar, a.z * scalar};
return result;
}
inline v3
operator-(v3 a) {
v3 result = {-a.x, -a.y, -a.z};
return result;
}
inline v3 &
operator+=(v3 &a, v3 b) {
a.x += b.x;
a.y += b.y;
a.z += b.z;
return a;
}
inline v3 &
operator-=(v3 &a, v3 b) {
a.x -= b.x;
a.y -= b.y;
a.z -= b.z;
return a;
}
inline v3 &
operator*=(v3 &a, r32 scalar) {
a.x *= scalar;
a.y *= scalar;
a.z *= scalar;
return a;
}
inline r32
jmLength(v3 a) {
return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
}
inline r32
jmLength(r32 x, r32 y, r32 z) {
return sqrtf(x * x + y * y + z * z);
}
inline r32
jmLengthSqr(v3 a) {
return a.x * a.x + a.y * a.y + a.z * a.z;
}
inline r32
jmLengthSqr(r32 x, r32 y, r32 z) {
return x * x + y * y + z * z;
}
inline v3
jmNormalized(v3 a) {
r32 len = jmLength(a);
if (len == 0.0f) return a;
r32 invLen = 1.0f / len;
v3 result = {a.x * invLen, a.y * invLen, a.z * invLen};
return result;
}
inline v3
jmNormalized(r32 x, r32 y, r32 z) {
r32 len = jmLength(x, y, z);
if (len == 0.0f) return {x, y, z};
r32 invLen = 1.0f / len;
v3 result = {x * invLen, y * invLen, z * invLen};
return result;
}
inline void
jmNormalize(v3 *a) {
*a = jmNormalized(*a);
}
inline r32
jmDot(v3 lhs, v3 rhs) {
return (lhs.x * rhs.x) + (lhs.y * rhs.y) + (lhs.z * rhs.z);
}
inline r32