00001
00002
00003
00004
00005
00006 #ifndef RC_VECTORMATH_H
00007 #define RC_VECTORMATH_H
00008
00009 #define max2(a, b) (((a) > (b)) ? (a) : (b))
00010
00011 #define min2(a, b) (((a) < (b)) ? (a) : (b))
00012
00013
00014
00015
00016 #define fPI 3.1415926535f
00017 #define f2PI (2.0f * 3.1415926535f)
00018 #define fPIinv (1.0f / 3.1415926535f)
00019
00020 #define fMin 1.17549e-038f
00021 #define fMax 3.40282e+038f
00022
00023
00024
00025 #define sclamp(s, a, b) ((s) > (b) ? (b) : ((s) < (a) ? (a) : (s)))
00026
00027 #define ssaturate(s) sclamp((s), 0.0f, 1.0f)
00028
00029 #define sswap(a, b) T tmp##a##b = (a); (a) = (b); (b) = tmp##a##b
00030
00031 #define toRad(a) ((a / 360.0f) * f2PI)
00032
00033 #define toDeg(a) ((a * 360.0f) / f2PI)
00034
00035
00036
00037 template<class T> class vec2;
00038 template<class T> class vec3;
00039 template<class T> class vec4;
00040
00041 template<class T> class mat2;
00042 template<class T> class mat3;
00043 template<class T> class mat4;
00044
00045 template<class T>
00046 class vec2
00047 {
00048 public:
00049
00050
00051 union {
00052 struct {
00053 union {
00054 T x;
00055 T u;
00056 };
00057 union {
00058 T y;
00059 T v;
00060 };
00061 };
00062
00063 T vec[2];
00064 };
00065
00066
00067 vec2();
00068
00069 vec2(
00070 const vec2<T> &v
00071 );
00072
00073 vec2(
00074 T nx,
00075 T ny
00076 );
00077
00078
00079
00080
00081 vec2<T> &operator=(
00082 const vec2<T> &v
00083 );
00084
00085 vec2<T> &operator=(
00086 const T v
00087 );
00088
00089
00090
00091 bool operator==(
00092 const vec2<T> &v
00093 );
00094
00095 bool operator!=(
00096 const vec2<T> &v
00097 );
00098
00099
00100
00101 vec2<T> operator+(
00102 const vec2<T> &v
00103 );
00104
00105 void operator+=(
00106 const vec2<T> &v
00107 );
00108
00109 vec2<T> operator+(
00110 const T v
00111 );
00112
00113 void operator+=(
00114 const T v
00115 );
00116
00117 vec2<T> operator+();
00118
00119
00120
00121 vec2<T> operator-(
00122 const vec2<T> &v
00123 );
00124
00125 void operator-=(
00126 const vec2<T> &v
00127 );
00128
00129 vec2<T> operator-(
00130 const T v
00131 );
00132
00133 void operator-=(
00134 const T v
00135 );
00136
00137 vec2<T> operator-();
00138
00139
00140
00141 vec2<T> operator*(
00142 const vec2<T> &v
00143 );
00144
00145 void operator*=(
00146 const vec2<T> &v
00147 );
00148
00149 vec2<T> operator*(
00150 const T v
00151 );
00152
00153 void operator*=(
00154 const T v
00155 );
00156
00157
00158
00159 T dot(
00160 const vec2<T> &v
00161 );
00162
00163 T operator^(
00164 const vec2<T> &v
00165 );
00166
00167
00168
00169 vec2<T> operator/(
00170 const vec2<T> &v
00171 );
00172
00173 void operator/=(
00174 const vec2<T> &v
00175 );
00176
00177 vec2<T> operator/(
00178 const T v
00179 );
00180
00181 void operator/=(
00182 const T v
00183 );
00184
00185
00186
00187 T minElement();
00188
00189 T maxElement();
00190
00191 void clamp(
00192 const T min,
00193 const T max
00194 );
00195
00196 void clamp(
00197 const vec2<T> &min,
00198 const vec2<T> &max
00199 );
00200
00201 void saturate();
00202
00203 void normalize();
00204
00205 T length();
00206
00207 T length2();
00208
00209 vec2<T> reflection(
00210 const vec2<T> &v
00211 );
00212
00213 vec2<T> refraction(
00214 const vec2<T> &v,
00215 const T r
00216 );
00217
00218 };
00219
00220
00221
00222 template<class T>
00223 class vec3
00224 {
00225 public:
00226
00227
00228 union {
00229 struct {
00230 union {
00231 T x;
00232 T u;
00233 T r;
00234 };
00235 union {
00236 T y;
00237 T v;
00238 T g;
00239 };
00240 union {
00241 T z;
00242 T w;
00243 T b;
00244 };
00245 };
00246
00247 T vec[3];
00248 };
00249
00250
00251 vec3();
00252
00253 vec3(
00254 const vec3<T> &v
00255 );
00256
00257 vec3(
00258 T nx,
00259 T ny,
00260 T nz
00261 );
00262
00263 vec3(
00264 T n
00265 );
00266
00267 vec3(
00268 T nxyz[3]
00269 );
00270
00271
00272
00273
00274
00275 vec3<T> &operator=(
00276 const vec3<T> &v
00277 );
00278
00279 vec3<T> &operator=(
00280 const T v
00281 );
00282
00283
00284 bool operator==(
00285 const vec3<T> &v
00286 );
00287
00288 bool operator!=(
00289 const vec3<T> &v
00290 );
00291
00292
00293
00294 vec3<T> operator+(
00295 const vec3<T> &v
00296 );
00297
00298 void operator+=(
00299 const vec3<T> &v
00300 );
00301
00302 vec3<T> operator+(
00303 const T v
00304 );
00305
00306 void operator+=(
00307 const T v
00308 );
00309
00310 vec3<T> operator+();
00311
00312
00313
00314 vec3<T> operator-(
00315 const vec3<T> &v
00316 );
00317
00318 void operator-=(
00319 const vec3<T> &v
00320 );
00321
00322 vec3<T> operator-(
00323 const T v
00324 );
00325
00326 void operator-=(
00327 const T v
00328 );
00329
00330 vec3<T> operator-();
00331
00332
00333
00334 vec3<T> operator*(
00335 const vec3<T> &v
00336 );
00337
00338 void operator*=(
00339 const vec3<T> &v
00340 );
00341
00342 vec3<T> operator*(
00343 const T v
00344 );
00345
00346 void operator*=(
00347 const T v
00348 );
00349
00350
00351
00352 T dot(
00353 const vec3<T> &v
00354 );
00355
00356 T operator^(
00357 const vec3<T> &v
00358 );
00359
00360
00361
00362 vec3<T> cross(
00363 const vec3<T> &v
00364 );
00365
00366 vec3<T> operator%(
00367 const vec3<T> &v
00368 );
00369
00370
00371
00372 vec3<T> operator/(
00373 const vec3<T> &v
00374 );
00375
00376 void operator/=(
00377 const vec3<T> &v
00378 );
00379
00380 vec3<T> operator/(
00381 const T v
00382 );
00383
00384 void operator/=(
00385 const T v
00386 );
00387
00388
00389
00390 T minElement();
00391
00392 T maxElement();
00393
00394 void clamp(
00395 const T min,
00396 const T max
00397 );
00398
00399 void clamp(
00400 const vec3<T> &min,
00401 const vec3<T> &max
00402 );
00403
00404 void saturate();
00405
00406 void normalize();
00407
00408 T length();
00409
00410 T length2();
00411
00412 vec3<T> reflection(
00413 const vec3<T> &v
00414 );
00415
00416 vec3<T> refraction(
00417 const vec3<T> &v,
00418 const T r
00419 );
00420
00421 vec4<T> xyz0();
00422
00423 vec4<T> xyz1();
00424
00425 vec2<T> xy();
00426
00427 };
00428
00429
00430
00431 template<class T>
00432 class vec4
00433 {
00434 public:
00435
00436
00437 union {
00438 struct {
00439 union {
00440 T x;
00441 T r;
00442 };
00443 union {
00444 T y;
00445 T g;
00446 };
00447 union {
00448 T z;
00449 T b;
00450 };
00451 union {
00452 T w;
00453 T a;
00454 };
00455 };
00456
00457 T vec[4];
00458 };
00459
00460
00461 vec4();
00462
00463 vec4(
00464 const vec4<T> &v
00465 );
00466
00467 vec4(
00468 T nx,
00469 T ny,
00470 T nz,
00471 T nw
00472 );
00473
00474
00475
00476
00477
00478 vec4<T> &operator=(
00479 const vec4<T> &v
00480 );
00481
00482 vec4<T> &operator=(
00483 const T v
00484 );
00485
00486
00487 bool operator==(
00488 const vec4<T> &v
00489 );
00490
00491 bool operator!=(
00492 const vec4<T> &v
00493 );
00494
00495
00496
00497 vec4<T> operator+(
00498 const vec4<T> &v
00499 );
00500
00501 void operator+=(
00502 const vec4<T> &v
00503 );
00504
00505 vec4<T> operator+(
00506 const T v
00507 );
00508
00509 void operator+=(
00510 const T v
00511 );
00512
00513 vec4<T> operator+();
00514
00515
00516
00517 vec4<T> operator-(
00518 const vec4<T> &v
00519 );
00520
00521 void operator-=(
00522 const vec4<T> &v
00523 );
00524
00525 vec4<T> operator-(
00526 const T v
00527 );
00528
00529 void operator-=(
00530 const T v
00531 );
00532
00533 vec4<T> operator-();
00534
00535
00536
00537 vec4<T> operator*(
00538 const vec4<T> &v
00539 );
00540
00541 void operator*=(
00542 const vec4<T> &v
00543 );
00544
00545 vec4<T> operator*(
00546 const T v
00547 );
00548
00549 void operator*=(
00550 const T v
00551 );
00552
00553
00554
00555 T dot(
00556 const vec4<T> &v
00557 );
00558
00559 T operator^(
00560 const vec4<T> &v
00561 );
00562
00563
00564
00565 vec4<T> operator/(
00566 const vec4<T> &v
00567 );
00568
00569 void operator/=(
00570 const vec4<T> &v
00571 );
00572
00573 vec4<T> operator/(
00574 const T v
00575 );
00576
00577 void operator/=(
00578 const T v
00579 );
00580
00581
00582
00583 T minElement();
00584
00585 T maxElement();
00586
00587 void clamp(
00588 const T min,
00589 const T max
00590 );
00591
00592 void clamp(
00593 const vec4<T> &min,
00594 const vec4<T> &max
00595 );
00596
00597 void saturate();
00598
00599 void normalize();
00600
00601 T length();
00602
00603 T length2();
00604
00605 vec4<T> reflection(
00606 const vec4<T> &v
00607 );
00608
00609 vec4<T> refraction(
00610 const vec4<T> &v,
00611 const T r
00612 );
00613
00614 vec3<T> xyz();
00615
00616 vec2<T> xy();
00617
00618 };
00619
00620
00621
00622
00623
00624
00625 template<class T>
00626 class mat2
00627 {
00628 public:
00629 friend class vec2<T>;
00630
00631
00632 union {
00633 struct {
00634 T a;
00635 T b;
00636
00637 T c;
00638 T d;
00639 };
00640
00641 T vec[4];
00642 T mat[2][2];
00643 };
00644
00645
00646
00647 mat2();
00648
00649 mat2(
00650 const mat2<T> &v
00651 );
00652
00653 mat2(
00654 T na,
00655 T nb,
00656 T nc,
00657 T nd
00658 );
00659
00660
00661
00662
00663 mat2<T> &operator=(
00664 const mat2<T> &v
00665 );
00666
00667
00668
00669 bool operator==(
00670 const mat2<T> &v
00671 );
00672
00673 bool operator!=(
00674 const mat2<T> &v
00675 );
00676
00677 bool isIdentity();
00678
00679
00680
00681 mat2<T> operator+(
00682 const mat2<T> &v
00683 );
00684
00685 void operator+=(
00686 const mat2<T> &v
00687 );
00688
00689 mat2<T> operator+(
00690 const T v
00691 );
00692
00693 void operator+=(
00694 const T v
00695 );
00696
00697 mat2<T> operator+();
00698
00699
00700
00701 mat2<T> operator-(
00702 const mat2<T> &v
00703 );
00704
00705 void operator-=(
00706 const mat2<T> &v
00707 );
00708
00709 mat2<T> operator-(
00710 const T v
00711 );
00712
00713 void operator-=(
00714 const T v
00715 );
00716
00717 mat2<T> operator-();
00718
00719
00720
00721 T iprod(
00722 const mat2<T> &v,
00723 const u32 row,
00724 const u32 col
00725 );
00726
00727
00728
00729 mat2<T> operator*(
00730 const mat2<T> &v
00731 );
00732
00733 void operator*=(
00734 const mat2<T> &v
00735 );
00736
00737 mat2<T> operator*(
00738 const T v
00739 );
00740
00741 void operator*=(
00742 const T v
00743 );
00744
00745 vec2<T> operator*(
00746 const vec2<T> &v
00747 );
00748
00749
00750
00751 mat2<T> operator/(
00752 const T v
00753 );
00754
00755 void operator/=(
00756 const T v
00757 );
00758
00759
00760
00761 mat2<T> operator&(
00762 const mat2<T> &v
00763 );
00764
00765 void operator&=(
00766 const mat2<T> &v
00767 );
00768
00769
00770
00771 mat2<T> operator/(
00772 const mat2<T> &v
00773 );
00774
00775 void operator/=(
00776 const mat2<T> &v
00777 );
00778
00779
00780
00781 void identity();
00782
00783 void transpose();
00784
00785 T det();
00786
00787 void inv();
00788
00789 T trace();
00790
00791 vec2<T> eigenvalues();
00792
00793 mat2<T> eigenvectors(
00794 const vec2<T> eig
00795 );
00796 vec2<T> getRow(
00797 u32 row
00798 );
00799
00800 void setRow(
00801 u32 row,
00802 vec2<T> vec
00803 );
00804
00805 vec2<T> getCol(
00806 u32 col
00807 );
00808
00809 void setCol(
00810 u32 col,
00811 vec2<T> vec
00812 );
00813
00814
00815 };
00816
00817
00818
00819 template<class T>
00820 class mat3
00821 {
00822 public:
00823 friend class vec3<T>;
00824
00825
00826 union {
00827 struct {
00828 T a;
00829 T b;
00830 T c;
00831
00832 T d;
00833 T e;
00834 T f;
00835
00836 T g;
00837 T h;
00838 T i;
00839 };
00840
00841
00842 T vec[9];
00843 T mat[3][3];
00844 };
00845
00846
00847
00848 mat3();
00849
00850 mat3(
00851 const mat3<T> &v
00852 );
00853
00854 mat3(
00855 T na,
00856 T nb,
00857 T nc,
00858
00859 T nd,
00860 T ne,
00861 T nf,
00862
00863 T ng,
00864 T nh,
00865 T ni
00866 );
00867
00868 mat3(
00869 const vec3<T> &row0,
00870 const vec3<T> &row1,
00871 const vec3<T> &row2
00872 );
00873
00874
00875
00876
00877 mat3<T> &operator=(
00878 const mat3<T> &v
00879 );
00880
00881
00882
00883 bool operator==(
00884 const mat3<T> &v
00885 );
00886
00887 bool operator!=(
00888 const mat3<T> &v
00889 );
00890
00891 bool isIdentity();
00892
00893
00894
00895 mat3<T> operator+(
00896 const mat3<T> &v
00897 );
00898
00899 void operator+=(
00900 const mat3<T> &v
00901 );
00902
00903 mat3<T> operator+(
00904 const T v
00905 );
00906
00907 void operator+=(
00908 const T v
00909 );
00910
00911 mat3<T> operator+();
00912
00913
00914
00915 mat3<T> operator-(
00916 const mat3<T> &v
00917 );
00918
00919 void operator-=(
00920 const mat3<T> &v
00921 );
00922
00923 mat3<T> operator-(
00924 const T v
00925 );
00926
00927 void operator-=(
00928 const T v
00929 );
00930
00931 mat3<T> operator-();
00932
00933
00934
00935 T iprod(
00936 const mat3<T> &v,
00937 const u32 row,
00938 const u32 col
00939 );
00940
00941
00942
00943 mat3<T> operator*(
00944 const mat3<T> &v
00945 );
00946
00947 void operator*=(
00948 const mat3<T> &v
00949 );
00950
00951 mat3<T> operator*(
00952 const T v
00953 );
00954
00955 void operator*=(
00956 const T v
00957 );
00958
00959 vec3<T> operator*(
00960 const vec3<T> &v
00961 );
00962
00963
00964
00965 mat3<T> operator/(
00966 const T v
00967 );
00968
00969 void operator/=(
00970 const T v
00971 );
00972
00973
00974
00975 mat3<T> operator&(
00976 const mat3<T> &v
00977 );
00978
00979 void operator&=(
00980 const mat3<T> &v
00981 );
00982
00983
00984
00985 mat3<T> operator/(
00986 const mat3<T> &v
00987 );
00988
00989 void operator/=(
00990 const mat3<T> &v
00991 );
00992
00993
00994
00995 void identity();
00996
00997 void transpose();
00998
00999 T det();
01000
01001 void inv();
01002
01003 T trace();
01004
01005 vec3<T> getRow(
01006 u32 row
01007 );
01008
01009 void setRow(
01010 u32 row,
01011 vec3<T> vec
01012 );
01013
01014 vec3<T> getCol(
01015 u32 col
01016 );
01017
01018 void setCol(
01019 u32 col,
01020 vec3<T> vec
01021 );
01022
01023
01024 };
01025
01026
01027
01028 template<class T>
01029 class mat4
01030 {
01031 public:
01032 friend class vec4<T>;
01033
01034
01035 union {
01036 struct {
01037 T a;
01038 T b;
01039 T c;
01040 T d;
01041
01042 T e;
01043 T f;
01044 T g;
01045 T h;
01046
01047 T i;
01048 T j;
01049 T k;
01050 T l;
01051
01052 T m;
01053 T n;
01054 T o;
01055 T p;
01056 };
01057 struct {
01058 T R00;
01059 T R10;
01060 T R20;
01061 T T0;
01062
01063 T R01;
01064 T R11;
01065 T R21;
01066 T T1;
01067
01068 T R02;
01069 T R12;
01070 T R22;
01071 T T2;
01072
01073 T N0;
01074 T N1;
01075 T N2;
01076 T N3;
01077 };
01078
01079 T vec[16];
01080 T mat[4][4];
01081
01082 };
01083
01084
01085
01086 mat4();
01087
01088 mat4(
01089 const mat4<T> &v
01090 );
01091
01092 mat4(
01093 T na,
01094 T nb,
01095 T nc,
01096 T nd,
01097
01098 T ne,
01099 T nf,
01100 T ng,
01101 T nh,
01102
01103 T ni,
01104 T nj,
01105 T nk,
01106 T nl,
01107
01108 T nm,
01109 T nn,
01110 T no,
01111 T np
01112 );
01113
01114
01115
01116
01117 mat4<T> &operator=(
01118 const mat4<T> &v
01119 );
01120
01121
01122
01123 bool operator==(
01124 const mat4<T> &v
01125 );
01126
01127 bool operator!=(
01128 const mat4<T> &v
01129 );
01130
01131 bool isIdentity();
01132
01133
01134
01135 mat4<T> operator+(
01136 const mat4<T> &v
01137 );
01138
01139 void operator+=(
01140 const mat4<T> &v
01141 );
01142
01143 mat4<T> operator+(
01144 const T v
01145 );
01146
01147 void operator+=(
01148 const T v
01149 );
01150
01151 mat4<T> operator+();
01152
01153
01154
01155 mat4<T> operator-(
01156 const mat4<T> &v
01157 );
01158
01159 void operator-=(
01160 const mat4<T> &v
01161 );
01162
01163 mat4<T> operator-(
01164 const T v
01165 );
01166
01167 void operator-=(
01168 const T v
01169 );
01170
01171 mat4<T> operator-();
01172
01173
01174
01175 T iprod(
01176 const mat4<T> &v,
01177 const u32 row,
01178 const u32 col
01179 );
01180
01181
01182
01183 mat4<T> operator*(
01184 const mat4<T> &v
01185 );
01186
01187 void operator*=(
01188 const mat4<T> &v
01189 );
01190
01191 mat4<T> operator*(
01192 const T v
01193 );
01194
01195 void operator*=(
01196 const T v
01197 );
01198
01199 vec4<T> operator*(
01200 const vec4<T> &v
01201 );
01202
01203 mat4<T> affineMul(
01204 const mat4<T> &v
01205 );
01206
01207 vec4<T> affineMul(
01208 const vec4<T> &v
01209 );
01210
01211 vec3<T> affineMul(
01212 const vec3<T> &v
01213 );
01214
01215
01216 mat4<T> operator/(
01217 const T v
01218 );
01219
01220 void operator/=(
01221 const T v
01222 );
01223
01224
01225
01226 mat4<T> operator&(
01227 const mat4<T> &v
01228 );
01229
01230 void operator&=(
01231 const mat4<T> &v
01232 );
01233
01234
01235
01236 mat4<T> operator/(
01237 const mat4<T> &v
01238 );
01239
01240 void operator/=(
01241 const mat4<T> &v
01242 );
01243
01244
01245
01246 void identity();
01247
01248 void transpose();
01249
01250 T det();
01251
01252 void inv();
01253
01254 T trace();
01255
01256 void print();
01257
01258 mat3<T> rotationMatrix();
01259
01260 vec4<T> getRow(
01261 u32 row
01262 );
01263
01264 void setRow(
01265 u32 row,
01266 vec4<T> vec
01267 );
01268
01269 vec4<T> getCol(
01270 u32 col
01271 );
01272
01273 void setCol(
01274 u32 col,
01275 vec4<T> vec
01276 );
01277
01278 private:
01279 T det2(
01280 T n0,
01281 T n1,
01282 T n2,
01283 T n3
01284 );
01285
01286 T affineiprod0(
01287 const mat4<T> &v,
01288 const u32 row,
01289 const u32 col
01290 );
01291
01292 T affineiprod1(
01293 const mat4<T> &v,
01294 const u32 row,
01295 const u32 col
01296 );
01297
01298 };
01299
01300
01301
01302 typedef vec2<float> vec2f;
01303 typedef vec2<double> vec2d;
01304
01305 typedef vec3<float> vec3f;
01306 typedef vec3<double> vec3d;
01307
01308 typedef vec4<float> vec4f;
01309 typedef vec4<double> vec4d;
01310
01311
01312 typedef mat2<float> mat2f;
01313 typedef mat2<double> mat2d;
01314
01315 typedef mat3<float> mat3f;
01316 typedef mat3<double> mat3d;
01317
01318 typedef mat4<float> mat4f;
01319 typedef mat4<double> mat4d;
01320
01321
01322
01323
01324 static const mat4f ident4f(1.0f, 0.0f, 0.0f, 0.0f,
01325 0.0f, 1.0f, 0.0f, 0.0f,
01326 0.0f, 0.0f, 1.0f, 0.0f,
01327 0.0f, 0.0f, 0.0f, 1.0f);
01328
01329 static const mat3f ident3f(1.0f, 0.0f, 0.0f,
01330 0.0f, 1.0f, 0.0f,
01331 0.0f, 0.0f, 1.0f);
01332
01333 #define RC_VMATH
01334 #include "VectorMath.cpp"
01335
01336
01337 #endif
01338