Source

ArduinoProjects / Libraries / Math / MathUtil.cpp

Full commit
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
//Author: Jody McAdams
//Website: http://www.jodymcadams.com
//Company Website: http://www.gamejellyonline.com

unsigned long long tmpseed; 
int newseed;
int mlcg,p,q;

#include "MathUtil.h"
#include <math.h>
#include <stdlib.h>
#include <cstdio>

static const f32 g_MathUtil_Epsilon = 0.0001f;

f32 Ease_InOut_Cubic(f32 t, f32 t5, f32 t4, f32 t3, f32 t2, f32 t1)
{
	f32 ts = t*t;
	f32 tc = ts*t;
	return (t5*tc*ts + t4*ts*ts + t3*tc + t2*ts + t1*t);
}

f32 Ease_InOut_Cubic_Simple(f32 t)
{
	f32 ts = t*t;
	f32 tc = ts*t;
	return (-2.0f*tc + 3.0f*ts);
}

// This Function Gets The First Power Of 2 >= The
// Int That We Pass It.
s32 PowerOf2(s32 num)
{
    int rval=1;
    // rval<<=1 Is A Prettier Way Of Writing rval*=2;
    while(rval<num) rval<<=1;
    return rval;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void SwapS32(s32* l, s32* r)
{
	int temp = *l;
	*l = *r;
	*r = temp;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 MaxF(f32 l, f32 r)
{
	return l>r?l:r;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
s32 MaxS32(int l, int r)
{
	return l>r?l:r;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
s32 MinS32(int l, int r)
{
	return l<r?l:r;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
u32 MaxU32(unsigned int l, unsigned int r)
{
	return l>r?l:r;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
u32 MinU32(unsigned int l, unsigned int r)
{
	return l<r?l:r;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 MinF(f32 l, f32 r)
{
	return l<r?l:r;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
s32	  ClampS32(s32 n, s32 clampMin, s32 clampMax)
{
	if(n < clampMin)
	{
		n = clampMin;
	}
	else if(n > clampMax)
	{
		n = clampMax;
	}
	
	return n;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
u32	  ClampU32(u32 n, u32 clampMin, u32 clampMax)
{
	if(n < clampMin)
	{
		n = clampMin;
	}
	else if(n > clampMax)
	{
		n = clampMax;
	}
	
	return n;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 ClampF(f32 n, f32 clampMin, f32 clampMax)
{
	if(n < clampMin)
	{
		n = clampMin;
	}
	else if(n > clampMax)
	{
		n = clampMax;
	}
	
	return n;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
const vec2* AsVec2(const vec3* pVec)
{
	return (vec2*)pVec;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
vec2* AsVec2(vec3* pVec)
{
	return (vec2*)pVec;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 DegreesToRadians(f32 degrees)
{
	return degrees * 0.0174532925f;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  SetVec2(vec2* out_resultVec,f32 x, f32 y)
{
	out_resultVec->x = x;
	out_resultVec->y = y;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 DotVec2(const vec2* vec0, const vec2* vec1)
{
    return vec0->x*vec1->x + vec0->y*vec1->y;
}


//----------------------------------------------------------------------------
//Projects vec0 onto normalized vec1
//----------------------------------------------------------------------------
void ProjectVec2_Norm(vec2* out_resultVec, const vec2* vec0, const vec2* vec1)
{
    const f32 dotResult = DotVec2(vec0, vec1);
    ScaleVec2(out_resultVec, vec0, dotResult);   
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 DotVec3(const vec3* vec0, const vec3* vec1)
{
	return vec0->x*vec1->x + vec0->y*vec1->y + vec0->z*vec1->z;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 Lerp(f32 f0, f32 f1, f32 t)
{
	return f0 + t*(f1-f0);
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  LerpVec2(vec2* out_lerpedPos, vec2* p0, vec2* p1, f32 t)
{
    out_lerpedPos->x = p0->x + t*(p1->x-p0->x);
    out_lerpedPos->y = p0->y + t*(p1->y-p0->y);
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool ApproxEqualsZeroVec3(vec3* pTestvec)
{
	return (pTestvec->x < g_MathUtil_Epsilon && pTestvec->x > -g_MathUtil_Epsilon)
	&& (pTestvec->y < g_MathUtil_Epsilon && pTestvec->y > -g_MathUtil_Epsilon)
	&& (pTestvec->z < g_MathUtil_Epsilon && pTestvec->z > -g_MathUtil_Epsilon);
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  LerpVec3(vec3* out_lerpedPos, const vec3* p0, const vec3* p1, f32 t)
{
    out_lerpedPos->x = p0->x + t*(p1->x-p0->x);
    out_lerpedPos->y = p0->y + t*(p1->y-p0->y);
    out_lerpedPos->z = p0->z + t*(p1->z-p0->z);
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  LerpVec4(vec4* out_lerpedPos, const vec4* p0, const vec4* p1, f32 t)
{
    out_lerpedPos->x = p0->x + t*(p1->x-p0->x);
    out_lerpedPos->y = p0->y + t*(p1->y-p0->y);
    out_lerpedPos->z = p0->z + t*(p1->z-p0->z);
    out_lerpedPos->w = p0->w + t*(p1->w-p0->w);
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  MakeRotatedVec2(vec2* out_resultVec, f32 angleRadians)
{
	out_resultVec->x = -sin(angleRadians);
	out_resultVec->y = cos(angleRadians);
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  MakeRotatedVec2_SinCos(vec2* out_resultVec, f32 sinTheta, f32 cosTheta)
{
    out_resultVec->x = -sinTheta;
	out_resultVec->y = cosTheta;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 MagnitudeVec2(const vec2* vec)
{
	return sqrt(DotVec2(vec,vec));
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 DistVec3_2D(const vec3* vec0, const vec3* vec1)
{
	vec3 vec2D;
	SubVec3(&vec2D, vec0, vec1);
	vec2D.y = 0.0f;
	return MagnitudeSqVec3(&vec2D);
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 DistVec3(const vec3* vec0, const vec3* vec1)
{
	return sqrt(DistSqVec3(vec0,vec1));
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 DistSqVec3(const vec3* vec0, const vec3* vec1)
{
	vec3 distVec;
	SubVec3(&distVec, vec0, vec1);
	return MagnitudeSqVec3(&distVec);
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 MagnitudeSqVec2(const vec2* vec)
{
	return DotVec2(vec,vec);
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 DistSqVec2(const vec2* vec0, const vec2* vec1)
{
	vec2 distVec;
	SubVec2(&distVec, vec0, vec1);
	return MagnitudeSqVec2(&distVec);
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 DistVec2(const vec2* vec0, const vec2* vec1)
{
    return sqrt(DistSqVec2(vec0, vec1));
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool PosIsBehind(const vec3* frontPos, const vec3* frontAt, const vec3* behindPos)
{
	vec3 dirVec;
	SubVec3(&dirVec, behindPos, frontPos);
	
	return DotVec3(frontAt, &dirVec) <= 0.0f;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 MagnitudeSqVec3(const vec3* vec)
{
	return DotVec3(vec,vec);
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 MagnitudeVec3(const vec3* vec)
{
	return sqrt(MagnitudeSqVec3(vec));
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32  NormalizeVec2_Self(vec2* vec)
{
	const f32 length = MagnitudeVec2(vec);
	vec->x/=length;
	vec->y/=length;
    
    return length;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32  TryNormalizeVec2_Self(vec2* vec)
{
    const f32 lengthSq = MagnitudeSqVec2(vec);
    
    if(lengthSq > g_MathUtil_Epsilon)
    {
        const f32 length = sqrt(lengthSq);
        
        vec->x/=length;
        vec->y/=length;
        
        return length;
    }
    
    return 0.0f;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32  NormalizeVec2(vec2* out_resultVec, const vec2* vec)
{
	const f32 length = MagnitudeVec2(vec);
	out_resultVec->x = vec->x/length;
	out_resultVec->y = vec->y/length;
    
    return length;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32  NormalizeVec3(vec3* out_resultVec, const vec3* vec)
{
	const f32 length = MagnitudeVec3(vec);
	out_resultVec->x = vec->x/length;
	out_resultVec->y = vec->y/length;
	out_resultVec->z = vec->z/length;
    
    return length;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 NormalizeVec3_Self(vec3* vec)
{
	const f32 length = MagnitudeVec3(vec);
	vec->x/=length;
	vec->y/=length;
	vec->z/=length;
    
    return length;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32  TryNormalizeVec3(vec3* out_resultVec, const vec3* vec)
{
    const f32 lengthSq = MagnitudeSqVec3(vec);
    
    if(lengthSq > g_MathUtil_Epsilon)
    {
        const f32 length = sqrt(lengthSq);
        
        out_resultVec->x = vec->x/length;
        out_resultVec->y = vec->y/length;
        out_resultVec->z = vec->z/length;
        
        return length;
    }
    
    return 0.0f;
}



//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void ProjectVec3_Norm(vec3* out_resultVec, vec3* vec0, vec3* vec1)
{
    const f32 dotResult = DotVec3(vec0, vec1);
    ScaleVec3(out_resultVec, vec1, dotResult);  
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32  TryNormalizeVec3_Self(vec3* vec)
{
    const f32 lengthSq = MagnitudeSqVec3(vec);
    
    if(lengthSq > g_MathUtil_Epsilon)
    {
        const f32 length = sqrt(lengthSq);
        
        vec->x/=length;
        vec->y/=length;
        vec->z/=length;
        
        return length;
    }
    
    return 0.0f;
}


//----------------------------------------------------------------------------
//TODO: I think this might be wrong
//----------------------------------------------------------------------------
void  RotateVec2(vec2* out_resultVec, const vec2* vec, f32 sinTheta, f32 cosTheta)
{
	const f32 x = vec->x;
	const f32 y = vec->y;
	
	out_resultVec->x = x*cosTheta - y*sinTheta;
	out_resultVec->y = x*sinTheta + y*cosTheta;
}

//----------------------------------------------------------------------------
//TODO: I think this might be wrong
//----------------------------------------------------------------------------
void  RotateVec2_Self(vec2* vec, f32 sinTheta, f32 cosTheta)
{
	const f32 x = vec->x;
	const f32 y = vec->y;
	
	vec->x = x*cosTheta - y*sinTheta;
	vec->y = x*sinTheta + y*cosTheta;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  SubVec2(vec2* out_resultVec, const vec2* p0, const vec2* p1)
{
	out_resultVec->x = p0->x-p1->x;
	out_resultVec->y = p0->y-p1->y;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  SubVec2_Self(vec2* out_resultVec, const vec2* p)
{
	out_resultVec->x -= p->x;
	out_resultVec->y -= p->y;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  SubVec3(vec3* out_resultVec, const vec3* p0, const vec3* p1)
{
	out_resultVec->x = p0->x-p1->x;
	out_resultVec->y = p0->y-p1->y;
	out_resultVec->z = p0->z-p1->z;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  SubVec3_Self(vec3* out_resultVec, const vec3* p0)
{
	out_resultVec->x -= p0->x;
	out_resultVec->y -= p0->y;
	out_resultVec->z -= p0->z;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  ScaleVec2_Self(vec2* vec, f32 scale)
{
	vec->x *= scale;
	vec->y *= scale;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  ScaleVec2(vec2* out_resultVec, const vec2* vec, f32 scale)
{
	out_resultVec->x = vec->x*scale;
	out_resultVec->y = vec->y*scale;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  ScaleVec3_Self(vec3* vec, f32 scale)
{
	vec->x *= scale;
	vec->y *= scale;
	vec->z *= scale;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  ScaleVec3(vec3* out_resultVec, const vec3* vec, f32 scale)
{
	out_resultVec->x = vec->x*scale;
	out_resultVec->y = vec->y*scale;
	out_resultVec->z = vec->z*scale;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  ScaleVec4(vec4* out_resultVec, const vec4* vec, f32 scale)
{
    out_resultVec->x = vec->x*scale;
	out_resultVec->y = vec->y*scale;
	out_resultVec->z = vec->z*scale;
    out_resultVec->w = vec->w*scale;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  ScaleVec4_Self(vec4* out_resultVec, f32 scale)
{
	out_resultVec->x *= scale;
	out_resultVec->y *= scale;
	out_resultVec->z *= scale;
    out_resultVec->w *= scale;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  AddScaledVec2_Self(vec2* out_resultVec, const vec2* vec, f32 scale)
{
	out_resultVec->x += vec->x*scale;
	out_resultVec->y += vec->y*scale;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  AddScaledVec2(vec2* out_resultVec, const vec2* vec0, const vec2* vec1, f32 scale)
{
	out_resultVec->x = vec0->x + vec1->x*scale;
	out_resultVec->y = vec0->y + vec1->y*scale;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  SubScaledVec2_Self(vec2* out_resultVec, vec2* vec, f32 scale)
{
	out_resultVec->x -= vec->x*scale;
	out_resultVec->y -= vec->y*scale;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  SubScaledVec2(vec2* out_resultVec, vec2* vec0, vec2* vec1, f32 scale)
{
	out_resultVec->x = vec0->x - vec1->x*scale;
	out_resultVec->y = vec0->y - vec1->y*scale;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  AddVec2_Self(vec2* out_resultVec, const vec2* vec)
{
	out_resultVec->x += vec->x;
	out_resultVec->y += vec->y;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  AddVec2(vec2* out_resultVec, const vec2* vec0, const vec2* vec1)
{
	out_resultVec->x = vec0->x + vec1->x;
	out_resultVec->y = vec0->y + vec1->y;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  CopyIntVec2(intVec2* out_result, const intVec2* point)
{
	out_result->x = point->x;
	out_result->y = point->y;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  CopyVec2(vec2* out_result, const vec2* point)
{
	out_result->x = point->x;
	out_result->y = point->y;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  CopyVec3(vec3* out_result, const vec3* point)
{
	out_result->x = point->x;
	out_result->y = point->y;
	out_result->z = point->z;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  CopyVec4(vec4* out_result, const vec4* point)
{
	out_result->x = point->x;
	out_result->y = point->y;
	out_result->z = point->z;
	out_result->w = point->w;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  CopyVec3IntoVec4(vec4* out_result, const vec3* point)
{
	out_result->x = point->x;
	out_result->y = point->y;
	out_result->z = point->z;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  ReflectVec2_Self(vec2* vec, const vec2* normal)
{
	ReflectVec2(vec, vec, normal);
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  ReflectVec2(vec2* out_resultVec, vec2* vec, const vec2* normal)
{
	f32 dot = -DotVec2(normal, vec);
	vec2 reflect;
	ScaleVec2(&reflect, normal, 2.0f * dot);
	AddVec2(out_resultVec, vec, &reflect);
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  CreatePlaneFromPointsVec2(struct PlaneVec2* out_resultPlane, vec2* p0, vec2* p1)
{
	//Copy point
	CopyVec2(&out_resultPlane->point, p0);
	
	//Create normal
	SubVec2(&out_resultPlane->normal,p1,p0);
	NormalizeVec2_Self(&out_resultPlane->normal);
	
	const f32 tempX = out_resultPlane->normal.x;
	
	//Rotate 90 degrees (normal faces to the right of the direction)
	out_resultPlane->normal.x = out_resultPlane->normal.y;
	out_resultPlane->normal.y = -tempX;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool  PointInsidePlaneVec2(vec2* point,struct PlaneVec2* plane)
{
	vec2 dirVec;
	SubVec2(&dirVec,point,&plane->point);
	return DotVec2(&dirVec,&plane->normal) < 0.0f;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 AngleBetweenVec2(const vec2* vec0, const vec2* vec1)
{
	return atan2(vec1->y,vec1->x) - atan2(vec0->y,vec0->x);
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 AngleOfVec2(const vec2* vec0)
{
	return AngleBetweenVec2(&vec2_up,vec0);
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
f32 AngleBetweenVec3(vec3* vec0, vec3* vec1)
{
	//TODO: get the angle
	return 0;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  CrossVec3(vec3* out_resultVec, const vec3* vec0, const vec3* vec1)
{
	const f32 x = vec0->y*vec1->z - vec1->y*vec0->z;
	const f32 y = vec0->z*vec1->x - vec1->z*vec0->x;
	const f32 z = vec0->x*vec1->y - vec1->x*vec0->y;
	
	out_resultVec->x = x;
	out_resultVec->y = y;
	out_resultVec->z = z;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  SetVec3(vec3* out_resultVec,f32 x, f32 y, f32 z)
{
	out_resultVec->x = x;
	out_resultVec->y = y;
	out_resultVec->z = z;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  SetVec4(vec4* out_resultVec,f32 x, f32 y, f32 z, f32 w)
{
	out_resultVec->x = x;
	out_resultVec->y = y;
	out_resultVec->z = z;
	out_resultVec->w = w;
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  AddVec3_Self(vec3* out_resultVec, const vec3* vec)
{
	out_resultVec->x += vec->x;
	out_resultVec->y += vec->y;
	out_resultVec->z += vec->z;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  AddVec3(vec3* out_resultVec, const vec3* vec0, const vec3* vec1)
{
	out_resultVec->x = vec0->x+vec1->x;
	out_resultVec->y = vec0->y+vec1->y;
	out_resultVec->z = vec0->z+vec1->z;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  AddVec3ThenScale_Self(vec3* out_resultVec, vec3* vec, f32 scale)
{
    AddVec3_Self(out_resultVec,vec);
    ScaleVec3_Self(out_resultVec, scale);
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  AddVec3ThenScale(vec3* out_resultVec, vec3* vec0, vec3* vec1, f32 scale)
{
    AddVec3(out_resultVec,vec0,vec1);
    ScaleVec3_Self(out_resultVec, scale);
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  AddScaledVec3_Self(vec3* out_resultVec, const vec3* vec, f32 scale)
{
	out_resultVec->x += vec->x*scale;
	out_resultVec->y += vec->y*scale;
	out_resultVec->z += vec->z*scale;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  AddScaledVec3(vec3* out_resultVec, const vec3* vec0, const vec3* vec1, f32 scale)
{
	out_resultVec->x = vec0->x+vec1->x*scale;
	out_resultVec->y = vec0->y+vec1->y*scale;
	out_resultVec->z = vec0->z+vec1->z*scale;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  SubScaledVec3_Self(vec3* out_resultVec, vec3* vec, f32 scale)
{
	out_resultVec->x -= vec->x*scale;
	out_resultVec->y -= vec->y*scale;
	out_resultVec->z -= vec->z*scale;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  SubScaledVec3(vec3* out_resultVec, vec3* vec0, vec3* vec1, f32 scale)
{
	out_resultVec->x = vec0->x-vec1->x*scale;
	out_resultVec->y = vec0->y-vec1->y*scale;
	out_resultVec->z = vec0->z-vec1->z*scale;
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void RotateVec3AboutAxis(vec3* out_resultVec,const vec3* vec0, const vec3* pAxis,f32 sinAngle, f32 cosAngle)
{
    // A: Vector to rotate.
	// R: Axis of rotation.
	// U: R x A, perpendicular to the axis and A.
	// V: A projected onto the axis.
	// L: Component of A perpendicular to the axis.
    
	// get up perpendicular to the axis and input
	vec3 up;
    CrossVec3(&up, pAxis, vec0);
	
	// project intput onto axis
	const f32 projectScale = DotVec3(vec0, pAxis );
	vec3 projected;
    ScaleVec3(&projected, pAxis, projectScale);
    
	// get component of input perpendicular to the axis
	vec3 perp;
    SubVec3(&perp,vec0,&projected);
    
	// build the rotated vector
    AddScaledVec3(out_resultVec, &projected, &perp, cosAngle);
    AddScaledVec3_Self(out_resultVec, &up, sinAngle);
}


void RotateVec3AboutAxis_Self(vec3* out_resultVec,const vec3* pAxis,f32 sinAngle, f32 cosAngle)
{
    // A: Vector to rotate.
	// R: Axis of rotation.
	// U: R x A, perpendicular to the axis and A.
	// V: A projected onto the axis.
	// L: Component of A perpendicular to the axis.
    
	// get up perpendicular to the axis and input
	vec3 up;
    CrossVec3(&up, pAxis, out_resultVec);
    
	// project intput onto axis
	const f32 projectScale = DotVec3(out_resultVec, pAxis );
	vec3 projected;
    ScaleVec3(&projected, pAxis, projectScale);
    
	// get component of input perpendicular to the axis
	vec3 perp;
    SubVec3(&perp,out_resultVec,&projected);
    
	// build the rotated vector
    AddScaledVec3(out_resultVec, &projected, &perp, cosAngle);
    AddScaledVec3_Self(out_resultVec, &up, sinAngle);
}


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void  MakeRandomVec3WithinBounds(vec3* pOut_resultVec, f32 x0, f32 x1, f32 y0, f32 y1, f32 z0, f32 z1)
{
    pOut_resultVec->x = rand_FloatRange(x0, x1);
    pOut_resultVec->y = rand_FloatRange(y0, y1);
    pOut_resultVec->z = rand_FloatRange(z0, z1);
}

//----------------------------------------------------------------------------
//Returns a random float in the range (a,b)
//----------------------------------------------------------------------------
f32 rand_FloatRange(f32 a, f32 b)
{
	return ((b-a)*((f32)rand()/RAND_MAX))+a;
}


//----------------------------------------------------------------------------
//Returns a random int in the range (a,b)
//----------------------------------------------------------------------------
s32 rand_IntRange(s32 a, s32 b)
{
	return a+rand()%((b-a)+1);
}

//----------------------------------------------------------------------------
//Returns a random int in the range (a,b)
//----------------------------------------------------------------------------
bool rand_Bool()
{
	return rand_FloatRange( 0.0f, 1.0f ) >= 0.5f;
}


//The base Catmull-Rom evaluation function which requires 4 points
void CatmullRom_Evaluate_Vec2(vec2* pOut_result, const vec2* p0, const vec2* p1, const vec2* p2, const vec2* p3, f32 t)
{
	const f32 c0 = ((-t + 2.0f) * t - 1.0f) * t * 0.5f;
	const f32 c1 = (((3.0f * t - 5.0f) * t) * t + 2.0f) * 0.5f;
	const f32 c2 = ((-3.0f * t + 4.0f) * t + 1.0f) * t * 0.5f;
	const f32 c3 = ((t - 1.0f) * t * t) * 0.5f;
	
	ScaleVec2(pOut_result, p0, c0);
	AddScaledVec2_Self(pOut_result, p1, c1);
	AddScaledVec2_Self(pOut_result, p2, c2);
	AddScaledVec2_Self(pOut_result, p3, c3);
}


//The base Catmull-Rom evaluation function which requires 4 points
void CatmullRom_Evaluate_Vec3(vec3* pOut_result, const vec3* p0, const vec3* p1, const vec3* p2, const vec3* p3, f32 t)
{
	const f32 c0 = ((-t + 2.0f) * t - 1.0f) * t * 0.5f;
	const f32 c1 = (((3.0f * t - 5.0f) * t) * t + 2.0f) * 0.5f;
	const f32 c2 = ((-3.0f * t + 4.0f) * t + 1.0f) * t * 0.5f;
	const f32 c3 = ((t - 1.0f) * t * t) * 0.5f;
	
	ScaleVec3(pOut_result, p0, c0);
	AddScaledVec3_Self(pOut_result, p1, c1);
	AddScaledVec3_Self(pOut_result, p2, c2);
	AddScaledVec3_Self(pOut_result, p3, c3);
}

//The base Catmull-Rom evaluation function which requires 4 points
f32 CatmullRom_Evaluate_F32(f32 p0, f32 p1, f32 p2, f32 p3, f32 t)
{
	const f32 c0 = ((-t + 2.0f) * t - 1.0f) * t * 0.5f;
	const f32 c1 = (((3.0f * t - 5.0f) * t) * t + 2.0f) * 0.5f;
	const f32 c2 = ((-3.0f * t + 4.0f) * t + 1.0f) * t * 0.5f;
	const f32 c3 = ((t - 1.0f) * t * t) * 0.5f;
	
	return c0 * p0 + c1 * p1 + c2 * p2 + c3 * p3;
}

//Evaluates a curve with any number of points using the Catmull-Rom method
void CatmullRom_EvaluateCurve_Vec2(vec2* pOut_result, const vec2* pPoints, u32 numPoints, f32 t)
{
	const f32 index1F = t * (f32)(numPoints-1);
	const u32 index1 = (u32)index1F;
	
	const f32 remainder = index1F - (f32)index1;
	
	const u32 maxIndex = numPoints-1;
	const u32 index0 = MaxS32(0,index1 - 1);
	const u32 index2 = MinS32(maxIndex,index1 + 1);
	const u32 index3 = MinS32(maxIndex,index1 + 2);
	
	const vec2* p0 = &pPoints[index0];
	const vec2* p1 = &pPoints[index1];
	const vec2* p2 = &pPoints[index2];
	const vec2* p3 = &pPoints[index3];
	
	CatmullRom_Evaluate_Vec2(pOut_result, p0, p1, p2, p3, remainder);
}

//Evaluates a curve with any number of points using the Catmull-Rom method
void CatmullRom_EvaluateCurve_Vec3(vec3* pOut_result, const vec3* pPoints, u32 numPoints, f32 t)
{
	const f32 index1F = t * (f32)(numPoints-1);
	const u32 index1 = (u32)index1F;
	
	const f32 remainder = index1F - (f32)index1;
	
	const u32 index0 = MaxS32(0,index1 - 1);
	const u32 index2 = index1 + 1;
	const u32 index3 = MinS32(numPoints-1,index1 + 2);
	
	const vec3* p0 = &pPoints[index0];
	const vec3* p1 = &pPoints[index1];
	const vec3* p2 = &pPoints[index2];
	const vec3* p3 = &pPoints[index3];
	
	CatmullRom_Evaluate_Vec3(pOut_result, p0, p1, p2, p3, remainder);
}


//Evaluates a curve with any number of points using the Catmull-Rom method
f32 CatmullRom_EvaluateCurve_F32(const f32* pPoints, u32 numPoints, f32 t)
{
	const f32 index1F = t * (f32)(numPoints-1);
	const u32 index1 = (u32)index1F;
	
	const f32 remainder = index1F - (f32)index1;
	
	const u32 index0 = MaxS32(0,index1 - 1);
	const u32 index2 = index1 + 1;
	const u32 index3 = MinS32(numPoints-1,index1 + 2);
	
	return CatmullRom_Evaluate_F32(pPoints[index0], pPoints[index1], pPoints[index2], pPoints[index3], remainder);
}


//Creates the specified number of points along a Catmull-Rom spline (non-uniform)
void CatmullRom_CreatePoints_Vec2(vec2* pOut_pointArray, u32 numPointsDesired, const vec2* pCurvePoints, u32 numCurvePoints)
{	
	const f32 numPointsDesiredMin1 = (f32)(numPointsDesired-1);
	
	for(u32 i=0; i<numPointsDesired; ++i)
	{
		const f32 t = (f32)i/numPointsDesiredMin1;
		CatmullRom_EvaluateCurve_Vec2(&pOut_pointArray[i],pCurvePoints,numCurvePoints,t);
	}
}

//Creates the specified number of points along a Catmull-Rom spline (non-uniform)
void CatmullRom_CreatePoints_Vec3(vec3* pOut_pointArray, u32 numPointsDesired, const vec3* pCurvePoints, u32 numCurvePoints)
{	
	const f32 numPointsDesiredMin1 = (f32)(numPointsDesired-1);
	
	for(u32 i=0; i<numPointsDesired; ++i)
	{
		const f32 t = (f32)i/numPointsDesiredMin1;
		CatmullRom_EvaluateCurve_Vec3(&pOut_pointArray[i],pCurvePoints,numCurvePoints,t);
	}
}


//Creates the specified number of points along a Catmull-Rom spline (uniformly spaced)
void CatmullRom_CreatePoints_Uniform_Vec2(vec2* pOut_pointArray, u32 numPointsDesired, const vec2* pCurvePoints, u32 numCurvePoints, DistanceTableEntry* pDistTable, u32 numDistTableEntries)
{
	const f32 numPointsDesiredMin1 = (f32)(numPointsDesired-1);
	const f32 totalLength = pDistTable[numDistTableEntries-1].distance;
	
	for(u32 i=0; i<numPointsDesired; ++i)
	{
		const f32 distT = (f32)i/(f32)numPointsDesiredMin1;
		const f32 distance = distT * totalLength;
		
		const f32 t = Curve_TValueFromDist(distance,pDistTable,numDistTableEntries);
		CatmullRom_EvaluateCurve_Vec2(&pOut_pointArray[i],pCurvePoints,numCurvePoints,t);
	}
}


//Creates the specified number of points along a Catmull-Rom spline (uniformly spaced)
void CatmullRom_CreatePoints_Uniform_Vec3(vec3* pOut_pointArray, u32 numPointsDesired, const vec3* pCurvePoints, u32 numCurvePoints, DistanceTableEntry* pDistTable, u32 numDistTableEntries)
{
	const f32 numPointsDesiredMin1 = (f32)(numPointsDesired-1);
	const f32 totalLength = pDistTable[numDistTableEntries-1].distance;
	
	for(u32 i=0; i<numPointsDesired; ++i)
	{
		const f32 distT = (f32)i/(f32)numPointsDesiredMin1;
		const f32 distance = distT * totalLength;
		
		const f32 t = Curve_TValueFromDist(distance,pDistTable,numDistTableEntries);
		CatmullRom_EvaluateCurve_Vec3(&pOut_pointArray[i],pCurvePoints,numCurvePoints,t);
	}
}


//Creates a distance table used by Curve_TValueFromDist below
f32 Curve_CreateDistanceTable_Vec3(DistanceTableEntry* pOut_result, const vec3* pPoints, u32 numPoints)
{
	const u32 numPointsMin1 = numPoints-1;
	
	f32 distSoFar = 0.0f;
	
	pOut_result[0].distance = 0.0f;
	pOut_result[0].t = 0.0f;
	
	for(u32 i=1; i<numPoints; ++i)
	{
		const f32 dist = DistVec3(&pPoints[i-1], &pPoints[i]);
		
		distSoFar += dist;
		
		DistanceTableEntry* pEntry = &pOut_result[i];
		pEntry->distance = distSoFar;
		pEntry->t = (f32)i/(f32)numPointsMin1;
	}
	
	return distSoFar;
}


//Creates a distance table used by Curve_TValueFromDist below
f32 Curve_CreateDistanceTable_Vec2(DistanceTableEntry* pOut_result, const vec2* pPoints, u32 numPoints)
{
	const u32 numPointsMin1 = numPoints-1;
	
	f32 distSoFar = 0.0f;
	
	pOut_result[0].distance = 0.0f;
	pOut_result[0].t = 0.0f;
	
	for(u32 i=1; i<numPoints; ++i)
	{
		const f32 dist = DistVec2(&pPoints[i-1], &pPoints[i]);
		
		distSoFar += dist;
		
		DistanceTableEntry* pEntry = &pOut_result[i];
		pEntry->distance = distSoFar;
		pEntry->t = (f32)i/(f32)numPointsMin1;
	}
	
	return distSoFar;
}


//When evaluating a spline, pass in a dist (0 to max) to get a T value to pass in
f32 Curve_TValueFromDist(f32 dist, DistanceTableEntry* pDistTable, u32 numDistTableEntries)
{
	for(s32 i=numDistTableEntries-2; i != -1; --i)
	{
		DistanceTableEntry* pEntry = &pDistTable[i];
		if(dist >= pEntry->distance)
		{
			if(i == numDistTableEntries-1)
			{
				return 1.0f;
			}
			else
			{
				DistanceTableEntry* pNextEntry = &pDistTable[i+1];
				const f32 lerpT = (dist-pEntry->distance)/(pNextEntry->distance-pEntry->distance);
				const f32 t = Lerp(pEntry->t, pNextEntry->t, lerpT);
				
				return t;
			}
		}
	}
	
	return 0.0f;
}

void RGBAToVec4(vec4* pOut_vec,u8 r, u8 g, u8 b, u8 a)
{
	pOut_vec->x = (f32)r/255.0f;
	pOut_vec->y = (f32)g/255.0f;
	pOut_vec->z = (f32)b/255.0f;
	pOut_vec->w = (f32)a/255.0f;
}