-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMathLinear.h
2125 lines (1865 loc) · 64.8 KB
/
MathLinear.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)2019 Berk Atabek ( atabek dot berk at hotmail dot com )
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// File : MathLinear.h
// Desc : Small size linear math library for 3D graphics.
//
// [Classes]
// vec2d : 2D vector data structure
// vec3d : 3D vector data structure
// vec4d : 4D vector data structure
// mat3x3 : 3x3 Matrix
// mat4x4 : 4x4 Matrix
// ray : Parametric line segment
// plane : 3D plane structure
// rectangle: 2D bounding rectangle
// AABB : Axis-Aligned-Bounding-Box
// frustum : view frustum for visibility testing
// colorRGB : RGB triplet color structure
//
// Note : All matrix transformations are right handed and row major.
// In order to use them with OpenGL pipeline users need to use
// transpose() method of corresponding matrix class.
//
// Date : 11/19
//===========================================================================
#ifndef MATHLINEAR_H
#define MATHLINEAR_H
#include <assert.h>
#include <math.h>
#include <string.h>
#include <iostream>
namespace MATHLINEAR { // start of namespace MATHLINEAR
//===========================================================================
//
// Common math symbollic constants
//
//===========================================================================
#define ML_PI ((float)3.141592654f) // Pi
#define ML_2PI ((float)6.283185308f) // 2*Pi
#define ML_HLFPI ((float)1.570796327f) // Pi/2
#define ML_PIBY4 ((float)0.785398163f) // Pi/4
#define ML_1BYPI ((float)0.318309886f) // 1/Pi
#define ML_BIGNUM ((unsigned long)1e6) // 1000000
#define ML_EPSILON ((float)1e-6f) // 0.000001
#define ML_e ((float)2.718281828f) // e
#define ML_LOG2e ((float)1.442695040f) // log2(e)
#define ML_LOG10e ((float)0.434294481f) // log10(e)
#define ML_LN2 ((float)0.693147180f) // ln(2)
#define ML_LN10 ((float)2.302585092f) // ln(10)
#define ML_SQRT2 ((float)1.414213562f) // Sqrt(2)
#define ML_INVSQRT2 ((float)0.707106781f) // 1/Sqrt(2)
//===========================================================================
//
// Some useful macros
//
//===========================================================================
#define ML_DegToRad(degree) ((float)(degree) * (ML_PI / 180.0f))
#define ML_RadToDeg(radian) ((float)(radian) * (180.0f / ML_PI))
#define ML_MIN2(x, y) ((x) < (y) ? (x) : (y))
#define ML_MAX2(x, y) ((x) > (y) ? (x) : (y))
#define ML_MIN3(x, y, z) (ML_MIN(ML_MIN((x), (y)), (z)))
#define ML_MAX3(x, y, z) (ML_MAX(ML_MAX((x), (y)), (z)))
#define ML_CLAMP(x, a, b) \
((x) < (a) ? (a) \
: (x) > (b) ? (b) : (x)) // clamps value 'x' in the range [a,b].
#define ML_SATURATE(x) \
(ML_CLAMP((x), 0, 1)) // clamps value 'x' in the range [0,1].
#define ML_FRAND() \
(rand() / (float)RAND_MAX) // random number within the [0,1] range.
#define ML_FRAND_RANGE(x, y) \
((x) + ((y) - (x)) * ML_FRAND()) // random number within [x,y] range.
#define ML_POW2(x) \
((a) & (a - 1) == 0 ? 1 : 0) // tests whether the number is a power of 2
//===========================================================================
//
// 2D Vector Class
//
//===========================================================================
struct vec2d {
// Constructors
inline explicit vec2d() { /* Empty constructor is preferred due to perf.
reasons */
}
vec2d(float _x, float _y) {
x = _x;
y = _y;
}
explicit vec2d(const float *v) {
assert(v);
x = v[0];
y = v[1];
}
vec2d(const vec2d &v) {
x = v.x;
y = v.y;
}
// Unary overloaded operators
vec2d operator++(int a) { return vec2d(x++, y++); }
vec2d operator--() { return vec2d(x--, y--); }
vec2d operator-() const { return vec2d(-x, -y); } // vector invert
// Binary overloaded operators
vec2d operator+(const vec2d &v) const { return vec2d(x + v.x, y + v.y); }
vec2d operator-(const vec2d &v) const { return vec2d(x - v.x, y - v.y); }
// Multipl. by a scalar from rhs.
vec2d operator*(const float scalar) const {
return vec2d(scalar * x, scalar * y);
}
// Multipl. by a scalar from lhs.
friend vec2d operator*(const float scalar, const vec2d &v) {
return vec2d(scalar * v.x, scalar * v.y);
}
vec2d operator/(const float scalar) const {
assert(scalar > ML_EPSILON);
float InvScalar = 1.0f / scalar;
return vec2d(x * InvScalar, y * InvScalar);
}
// Assignment operators
vec2d &operator+=(const vec2d &v) {
x += v.x;
y += v.y;
return *this;
}
vec2d &operator-=(const vec2d &v) {
x -= v.x;
y -= v.y;
return *this;
}
vec2d &operator*=(const float scalar) {
x *= scalar;
y *= scalar;
return *this;
}
vec2d &operator/=(const float scalar) {
assert(scalar);
x /= scalar;
y /= scalar;
return *this;
}
vec2d &operator=(const vec2d &v) {
x = v.x;
y = v.y;
return *this;
}
// Casting operators
operator float *() { return (float *)&x; }
operator const float *() const { return (const float *)&x; }
// Equality operators
bool operator==(const vec2d &v) const { return (x == v.x && y == v.y); }
bool operator!=(const vec2d &v) const { return (x != v.x || y != v.y); }
bool operator>=(const vec2d &v) const { return (x >= v.x && y >= v.y); }
bool operator<=(const vec2d &v) const { return (x <= v.x && y <= v.y); }
bool operator>(const vec2d &v) const { return (x > v.x && y > v.y); }
bool operator<(const vec2d &v) const { return (x < v.x && y < v.y); }
// Set values
void set(float vx, float vy) {
x = vx;
y = vy;
}
void set(const float *v) {
assert(v);
x = v[0];
y = v[1];
}
void setToZeroVector() { x = y = 0.0f; }
// console I/O
inline friend std::ostream &operator<<(std::ostream &out, const vec2d &v) {
return out << "(" << v.x << "," << v.y << ")" << '\n';
}
inline friend std::istream &operator>>(std::istream &in, vec2d &v) {
return in >> v.x >> v.y;
}
// Dot product
inline float dot(const vec2d &v) const { return x * v.x + y * v.y; }
// Cross product
inline float cross(const vec2d &v) const { return x * v.y - y * v.x; }
// Magnitude
inline float length() const { return sqrtf(dot(*this)); }
// Magnitude squared
inline float lengthSqr() const { return dot(*this); }
// Distance between two vectors
inline float distance(const vec2d &v) const { return ((*this) - v).length(); }
// Normalize
inline void normalize() {
float mag = this->length();
if (mag > ML_EPSILON) {
this->x *= 1.0f / mag;
this->y *= 1.0f / mag;
}
}
// Return angle(radian) between two vectors
inline float angleBtw(const vec2d &v) {
float invDen = this->length() * v.length();
assert(invDen > ML_EPSILON);
// clamp the arccos range to [-1,1]
return acos(ML_CLAMP(dot(v) / invDen, -1.0f, 1.0f));
}
// Reflection vector R = v2 - 2*V1*dot(v1*v2)
inline vec2d reflection(const vec2d &v) const {
return vec2d((*this) - 2.0f * v * dot(v));
}
// Linear interpolation between two vectors i.e. V1*(1-s) + V2*s
inline vec2d Lerp(const vec2d &v, const float s) const {
return vec2d(v * s + (1 - s) * (*this));
}
// 2D Coordinates
union {
struct {
float x;
float y;
};
float v[2];
};
};
//===========================================================================
//
// 3D Vector
//
//===========================================================================
struct vec3d {
// Constructors
explicit vec3d() { /* Empty constructor is preferred due to perf. reasons */
}
vec3d(float _x, float _y, float _z) {
x = _x;
y = _y;
z = _z;
}
vec3d(const vec2d &v, float scalar) : x(v.x), y(v.y), z(scalar) {}
explicit vec3d(const float *v) {
assert(v);
x = v[0];
y = v[1];
z = v[2];
}
vec3d(const vec3d &v) {
x = v.x;
y = v.y;
z = v.z;
}
// Unary overloaded operators
vec3d operator++(int a) { return vec3d(x++, y++, z++); }
vec3d operator--() { return vec3d(x--, y--, z--); }
vec3d operator-() const { return vec3d(-x, -y, -z); } // vector invert
// Binary overloaded operators
vec3d operator+(const vec3d &v) const {
return vec3d(x + v.x, y + v.y, z + v.z);
}
vec3d operator-(const vec3d &v) const {
return vec3d(x - v.x, y - v.y, z - v.z);
}
// Multipl. by a scalar from rhs.
vec3d operator*(const float scalar) const {
return vec3d(scalar * x, scalar * y, scalar * z);
}
// Multipl. by a scalar from lhs.
friend vec3d operator*(const float scalar, const vec3d &v) {
return vec3d(scalar * v.x, scalar * v.y, scalar * v.z);
}
vec3d operator/(const float scalar) const {
assert(scalar);
float InvScalar = 1.0f / scalar;
return vec3d(x * InvScalar, y * InvScalar, z * InvScalar);
}
// Assignment operators
vec3d &operator+=(const vec3d &v) {
x += v.x;
y += v.y;
z += v.z;
return *this;
}
vec3d &operator-=(const vec3d &v) {
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
vec3d &operator*=(const float scalar) {
x *= scalar;
y *= scalar;
z *= scalar;
return *this;
}
vec3d &operator/=(const float scalar) {
assert(scalar);
x /= scalar;
y /= scalar;
z /= scalar;
return *this;
}
vec3d &operator=(const vec3d &v) {
x = v.x;
y = v.y;
z = v.z;
return *this;
}
// Casting operators
operator float *() { return (float *)&x; }
operator const float *() const { return (const float *)&x; }
// Equality operators
bool operator==(const vec3d &v) const {
return (x == v.x && y == v.y && z == v.z);
}
bool operator!=(const vec3d &v) const {
return (x != v.x || y != v.y || z != v.z);
}
bool operator>=(const vec3d &v) const {
return (x >= v.x && y >= v.y && z >= v.z);
}
bool operator<=(const vec3d &v) const {
return (x <= v.x && y <= v.y && y <= v.z);
}
bool operator>(const vec3d &v) const {
return (x > v.x && y > v.y && z > v.z);
}
bool operator<(const vec3d &v) const {
return (x < v.x && y < v.y && z < v.z);
}
// Set values
void set(float vx, float vy, float vz) {
x = vx;
y = vy;
z = vz;
}
void set(const float *v) {
assert(v);
x = v[0];
y = v[1];
z = v[2];
}
void setToZeroVector() { x = y = z = 0.0f; }
// console I/O
inline friend std::ostream &operator<<(std::ostream &out, const vec3d &v) {
return out << "(" << v.x << "," << v.y << "," << v.z << ")" << '\n';
}
inline friend std::istream &operator>>(std::istream &in, vec3d &v) {
return in >> v.x >> v.y >> v.z;
}
// Dot product
inline float dot(const vec3d &v) const { return x * v.x + y * v.y + z * v.z; }
// Cross product
inline vec3d cross(const vec3d &v) const {
return vec3d(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
}
// Magnitude
inline float length() const { return sqrtf(dot(*this)); }
// Magnitude squared
inline float lengthSqr() const { return dot(*this); }
// Distance between two vectors
inline float distance(const vec3d &v) const { return ((*this) - v).length(); }
// Normalize
inline void normalize() {
float mag = this->length();
if (mag > ML_EPSILON) {
this->x *= 1.0f / mag;
this->y *= 1.0f / mag;
this->z *= 1.0f / mag;
}
}
// Return angle(radian) between two vectors
inline float angleBtw(const vec3d &v) {
float invDen = this->length() * v.length();
assert(invDen > ML_EPSILON);
// clamp the arccos range to [-1,1]
return acos(ML_CLAMP(dot(v) / invDen, -1.0f, 1.0f));
}
// Reflection vector R = v2 - 2*v1*dot(v1*v2)
inline vec3d reflection(const vec3d &v) const {
return vec3d((*this) - 2.0f * v * dot(v));
}
// Linear interpolation between vectors
inline vec3d Lerp(const vec3d &v, const float s) const {
return vec3d((*this) + (v - (*this)) * s);
}
// 3D Coordinates
union {
struct {
float x;
float y;
float z;
};
float v[3];
};
};
//===========================================================================
//
// 4D Vector
//
//===========================================================================
struct vec4d {
// Constructors
explicit vec4d() { /* Empty constructor is preferred due to perf. reasons */
}
vec4d(float _x, float _y, float _z, float _w = 1.0f)
: x(_x), y(_y), z(_z), w(_w) {}
vec4d(const vec2d &v1, const vec2d &v2)
: x(v1.x), y(v1.y), z(v2.x), w(v2.y) {}
vec4d(const vec3d &v, float _w) : x(v.x), y(v.y), z(v.z), w(_w) {}
explicit vec4d(const float *v) {
assert(v);
x = v[0];
y = v[1];
z = v[2];
w = v[3];
}
vec4d(const vec4d &v) {
x = v.x;
y = v.y;
z = v.z;
w = v.w;
}
// Unary overloaded operators
vec4d operator++(int a) { return vec4d(x++, y++, z++, w++); }
vec4d operator--() { return vec4d(x--, y--, z--, w--); }
vec4d operator-() const { return vec4d(-x, -y, -z, -w); } // vector invert
// Binary overloaded operators
vec4d operator+(const vec4d &v) const {
return vec4d(x + v.x, y + v.y, z + v.z, w + v.w);
}
vec4d operator-(const vec4d &v) const {
return vec4d(x - v.x, y - v.y, z - v.z, w - v.w);
}
// Multipl. by a scalar from rhs.
vec4d operator*(const float scalar) const {
return vec4d(scalar * x, scalar * y, scalar * z, scalar * w);
}
// Multipl. by a scalar from lhs.
friend vec4d operator*(const float scalar, const vec4d &v) {
return vec4d(scalar * v.x, scalar * v.y, scalar * v.z, scalar * v.w);
}
vec4d operator/(const float scalar) const {
assert(scalar > ML_EPSILON);
float InvScalar = 1.0f / scalar;
return vec4d(x * InvScalar, y * InvScalar, z * InvScalar, w * InvScalar);
}
// Assignment operators
vec4d &operator+=(const vec4d &v) {
x += v.x;
y += v.y;
z += v.z;
w += v.w;
return *this;
}
vec4d &operator-=(const vec4d &v) {
x -= v.x;
y -= v.y;
z -= v.z;
w -= v.w;
return *this;
}
vec4d &operator*=(const float scalar) {
x *= scalar;
y *= scalar;
z *= scalar;
w *= scalar;
return *this;
}
vec4d &operator/=(const float scalar) {
assert(scalar);
x /= scalar;
y /= scalar;
z /= scalar;
w /= scalar;
return *this;
}
vec4d &operator=(const vec4d &v) {
x = v.x;
y = v.y;
z = v.z;
w = v.w;
return *this;
}
// Casting operators
operator float *() { return (float *)&x; }
operator const float *() const { return (const float *)&x; }
// Equality operators
bool operator==(const vec4d &v) const {
return (x == v.x && y == v.y && z == v.z && w == v.w);
}
bool operator!=(const vec4d &v) const {
return (x != v.x || y != v.y || z != v.z || w != v.w);
}
bool operator>=(const vec4d &v) const {
return (x >= v.x && y >= v.y && z >= v.z && w >= v.w);
}
bool operator<=(const vec4d &v) const {
return (x <= v.x && y <= v.y && y <= v.z && w <= v.w);
}
bool operator>(const vec4d &v) const {
return (x > v.x && y > v.y && z > v.z && w > v.w);
}
bool operator<(const vec4d &v) const {
return (x < v.x && y < v.y && z < v.z && w < v.w);
}
// Set values
void set(float vx, float vy, float vz, float vw) {
x = vx;
y = vy;
z = vz;
w = vw;
}
void set(const float *v) {
assert(v);
x = v[0];
y = v[1];
z = v[2];
w = v[3];
}
void setToZeroVector() { x = y = z = w = 0.0f; }
// console I/O
inline friend std::ostream &operator<<(std::ostream &out, const vec4d &v) {
return out << "(" << v.x << "," << v.y << "," << v.z << "," << v.w << ")"
<< '\n';
}
inline friend std::istream &operator>>(std::istream &in, vec4d &v) {
return in >> v.x >> v.y >> v.z >> v.w;
}
// Dot product
inline float dot(const vec4d &v) const {
return x * v.x + y * v.y + z * v.z + w * v.w;
}
// Cross product
// inline vec4d cross(const vec4d &v) const { return vec4d(); }
// Magnitude
inline float length() const { return sqrtf(dot(*this)); }
// Magnitude squared
inline float lengthSqr() const { return dot(*this); }
// Distance between two vectors
inline float distance(const vec4d &v) const { return ((*this) - v).length(); }
// Normalize
inline void normalize() {
float mag = this->length();
if (mag > ML_EPSILON) {
this->x *= 1.0f / mag;
this->y *= 1.0f / mag;
this->z *= 1.0f / mag;
this->w *= 1.0f / mag;
}
}
// Return angle(radian) between two vectors
inline float angleBtw(const vec4d &v) {
float invDen = this->length() * v.length();
assert(invDen > ML_EPSILON);
// clamp the arccos range to [-1,1]
return acos(ML_CLAMP(dot(v) / invDen, -1.0f, 1.0f));
}
// Reflection vector R = v2 - 2*V1*dot(v1*v2)
inline vec4d reflection(const vec4d &v) const {
return vec4d((*this) - 2.0f * v * dot(v));
}
// Linear interpolation between vectors
inline vec4d Lerp(const vec4d &v, const float s) const {
return vec4d(*this + (v - *this) * s);
}
// 4D Coordinates
union {
struct {
float x;
float y;
float z;
float w;
};
float v[4];
};
};
//===========================================================================
//
// 3x3 Matrix Class
//
//===========================================================================
struct mat3x3 {
// Constructors
explicit mat3x3() { /* Empty constructor is preferred due to perf. reasons */
}
mat3x3(float _m11, float _m12, float _m13, float _m21, float _m22, float _m23,
float _m31, float _m32, float _m33) {
m11 = _m11;
m12 = _m12;
m13 = _m13;
m21 = _m21;
m22 = _m22;
m23 = _m23;
m31 = _m31;
m32 = _m32;
m33 = _m33;
}
explicit mat3x3(const float *marray) {
assert(marray);
memcpy(this, marray, sizeof(float) * 9);
}
mat3x3(const vec3d &r1, const vec3d &r2, const vec3d &r3) {
memcpy(&m11, &r1, sizeof(float) * 3);
memcpy(&m21, &r2, sizeof(float) * 3);
memcpy(&m31, &r3, sizeof(float) * 3);
}
mat3x3(const mat3x3 &mat) {
m11 = mat.m11;
m12 = mat.m12;
m13 = mat.m13;
m21 = mat.m21;
m22 = mat.m22;
m23 = mat.m23;
m31 = mat.m31;
m32 = mat.m32;
m33 = mat.m33;
}
// Set Matrix row values
inline void set(float _m11, float _m12, float _m13, float _m21, float _m22,
float _m23, float _m31, float _m32, float _m33) {
m11 = _m11;
m12 = _m12;
m13 = _m13;
m21 = _m21;
m22 = _m22;
m23 = _m23;
m31 = _m31;
m32 = _m32;
m33 = _m33;
}
inline void set(const vec3d &r1, const vec3d &r2, const vec3d &r3) {
memcpy(&m11, &r1, sizeof(vec3d));
memcpy(&m21, &r2, sizeof(vec3d));
memcpy(&m31, &r3, sizeof(vec3d));
}
inline void setFromArray(const float *m) {
memcpy(this, m, sizeof(float) * 9);
}
inline vec3d getRow(unsigned row) const {
assert(row >= 1 && row <= 3);
return vec3d(m[3 * (row - 1) + 0], m[3 * (row - 1) + 1],
m[3 * (row - 1) + 2]);
}
inline vec3d getCol(unsigned col) const {
assert(col >= 1 && col <= 3);
return vec3d(m[3 * 0 + col - 1], m[3 * 1 + col - 1], m[3 * 2 + col - 1]);
}
inline void identity() {
m11 = m22 = m33 = 1.0f;
m12 = m13 = m21 = m23 = m31 = m32 = 0.0f;
}
// Unary minus operator
inline mat3x3 operator-() const {
return mat3x3(-m11, -m12, -m13, -m21, -m22, -m23, -m31, -m32, -m33);
}
// Binary Operators
inline mat3x3 operator+(const mat3x3 &mat) const {
return mat3x3(m11 + mat.m11, m12 + mat.m12, m13 + mat.m13, m21 + mat.m21,
m22 + mat.m22, m23 + mat.m23, m31 + mat.m31, m32 + mat.m32,
m33 + mat.m33);
}
inline mat3x3 operator-(const mat3x3 &mat) const {
return mat3x3(m11 - mat.m11, m12 - mat.m12, m13 - mat.m13, m21 - mat.m21,
m22 - mat.m22, m23 - mat.m23, m31 - mat.m31, m32 - mat.m32,
m33 - mat.m33);
}
// Multiplication with a scalar from rhs.
inline mat3x3 operator*(const float scalar) const {
return mat3x3(m11 * scalar, m12 * scalar, m13 * scalar, m21 * scalar,
m22 * scalar, m23 * scalar, m31 * scalar, m32 * scalar,
m33 * scalar);
}
// Multiplication with a scalar from lhs.
friend mat3x3 operator*(const float scalar, const mat3x3 &mat) {
return mat3x3(scalar * mat.m11, scalar * mat.m12, scalar * mat.m13,
scalar * mat.m21, scalar * mat.m22, scalar * mat.m23,
scalar * mat.m31, scalar * mat.m32, scalar * mat.m33);
}
// matrix multiplication
mat3x3 operator*(const mat3x3 &mat) const {
mat3x3 temp;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
temp.m[3 * i + j] = 0.0f;
for (int k = 0; k < 3; ++k)
temp.m[i * 3 + j] += m[3 * i + k] * mat.m[3 * k + j];
}
}
return temp;
}
// Matrix-Vector multiplication: M*v
vec3d operator*(const vec3d &v) {
return vec3d(m11 * v.x + m12 * v.y + m13 * v.z,
m21 * v.x + m22 * v.y + m23 * v.z,
m31 * v.x + m32 * v.y + m33 * v.z);
}
// Vector-Matrix multiplication: v*M
inline friend vec3d operator*(const vec3d &v, const mat3x3 &m) {
return vec3d(v.x * m.m11 + v.y * m.m21 + v.z * m.m31,
v.x * m.m12 + v.y * m.m22 + v.z * m.m32,
v.x * m.m13 + v.y * m.m23 + v.z * m.m33);
}
mat3x3 operator/(const float scalar) const {
assert(scalar);
float invs = 1.0f / scalar;
return mat3x3(m11 * invs, m12 * invs, m13 * invs, m21 * invs, m22 * invs,
m23 * invs, m31 * invs, m32 * invs, m33 * invs);
}
// Assignment operators
mat3x3 &operator+=(const mat3x3 &mat) { return *this = *this + mat; }
mat3x3 &operator-=(const mat3x3 &mat) { return *this = *this - mat; }
mat3x3 &operator*=(const float scalar) { return *this = *this * scalar; }
mat3x3 &operator*=(const mat3x3 &mat) { return *this = *this * mat; }
mat3x3 &operator/=(const float scalar) {
assert(scalar);
return *this = *this / scalar;
}
// Casting operators
operator float *() { return (float *)&m11; }
operator const float *() const { return (const float *)&m11; }
// array index operators
float &operator()(unsigned int Row, unsigned int Col) {
// Check bounds first
assert((Row <= 3 && Row >= 1) && (Col <= 3 && Col >= 1));
return *(&this->m11 + (Row - 1) * 3 + (Col - 1));
}
float operator()(unsigned int Row, unsigned int Col) const {
// Check bounds first
assert((Row <= 3 && Row >= 1) && (Col <= 3 && Col >= 1));
return *(&this->m11 + (Row - 1) * 3 + (Col - 1));
}
// array subscript operator
float operator[](unsigned int i) {
assert(i >= 1 && i <= 9);
return *(&this->m11 + i - 1);
}
// Equality operators
bool operator==(const mat3x3 &mat) const {
return 0 == memcmp(this, &mat, sizeof(mat3x3));
}
bool operator!=(const mat3x3 &mat) const {
return 0 != memcmp(this, &mat, sizeof(mat3x3));
}
// Prints matrix data to the default output stream
friend std::ostream &operator<<(std::ostream &out, const mat3x3 &mat) {
return out << "|" << mat.m11 << " " << mat.m12 << " " << mat.m13
<< "|\n"
"|"
<< mat.m21 << " " << mat.m22 << " " << mat.m23
<< "|\n"
"|"
<< mat.m31 << " " << mat.m32 << " " << mat.m33 << "|\n";
}
float trace() const { return m11 + m22 + m33; }
void transposeSelf() {
float temp;
temp = m21;
m21 = m12;
m12 = temp;
temp = m31;
m31 = m13;
m13 = temp;
temp = m32;
m32 = m23;
m23 = temp;
}
mat3x3 getTranspose() const {
return mat3x3(m11, m21, m31, m12, m22, m32, m13, m23, m33);
}
void inverseSelf() {
float det = determinant();
assert(fabs(det) > ML_EPSILON && "Error: Singular matrix...");
float invDet = 1.0f / det;
// Calculate cofactor terms
float cof11 = m22 * m33 - m23 * m32;
float cof21 = m23 * m31 - m21 * m33;
float cof31 = m21 * m32 - m22 * m31;
float cof12 = m13 * m32 - m12 * m33;
float cof22 = m11 * m33 - m13 * m31;
float cof32 = m12 * m31 - m11 * m32;
float cof13 = m12 * m23 - m13 * m22;
float cof23 = m13 * m21 - m11 * m23;
float cof33 = m11 * m22 - m12 * m21;
m11 = cof11 * invDet;
m12 = cof12 * invDet;
m13 = cof13 * invDet;
m21 = cof21 * invDet;
m22 = cof22 * invDet;
m23 = cof23 * invDet;
m31 = cof31 * invDet;
m32 = cof32 * invDet;
m33 = cof33 * invDet;
}
// returns an inverse matrix copy w/o changing original matrix data.
mat3x3 getInverse() const {
float det = determinant();
assert(fabs(det) > ML_EPSILON && "Error: Singular matrix...");
float invDet = 1.0f / det;
// Calculate cofactor terms
float cof11 = m22 * m33 - m23 * m32;
float cof21 = m23 * m31 - m21 * m33;
float cof31 = m21 * m32 - m22 * m31;
float cof12 = m13 * m32 - m12 * m33;
float cof22 = m11 * m33 - m13 * m31;
float cof32 = m12 * m31 - m11 * m32;
float cof13 = m12 * m23 - m13 * m22;
float cof23 = m13 * m21 - m11 * m23;
float cof33 = m11 * m22 - m12 * m21;
return mat3x3(cof11 * invDet, cof12 * invDet, cof13 * invDet,
cof21 * invDet, cof22 * invDet, cof23 * invDet,
cof31 * invDet, cof32 * invDet, cof33 * invDet);
}
float determinant() const {
// calculate sub determinants
float det11 = m22 * m33 - m23 * m32;
float det12 = -(m21 * m33 - m23 * m31);
float det13 = m21 * m32 - m22 * m31;
return m11 * det11 + m12 * det12 + m13 * det13;
}
// Transformations
void makeTranslateMatrix(float x, float y, float z) {
identity();
m13 = x;
m23 = y;
m33 = z;
}
void makeTranslateMatrix(const vec3d &t) {
makeTranslateMatrix(t.x, t.y, t.z);
}
void makeRotXMatrix(float angle) {
float c = cos(angle), s = sin(angle);
identity();
m22 = c;
m23 = -s;
m32 = s;
m33 = c;
}
void makeRotYMatrix(float angle) {
float c = cos(angle), s = sin(angle);
identity();
m11 = c;
m13 = s;
m31 = -s;
m33 = c;
}
void makeRotZMatrix(float angle) {
float c = cos(angle), s = sin(angle);
identity();
m11 = c;
m12 = -s;
m21 = s;
m22 = c;
}
void makeRotAxis(float angle, const vec3d &v) {
float s = sin(ML_DegToRad(angle)), c = cos(ML_DegToRad(angle)),
onemc = 1.0f - c;
float xy, xz, yz;
vec3d axis(v);
// guarantee unit rot. axis
if (1.0f != axis.length()) axis.normalize();
xy = axis.x * axis.y;
xz = axis.x * axis.z;
yz = axis.y * axis.z;
m11 = axis.x * axis.x * onemc + c;
m12 = xy * onemc - axis.z * s;
m13 = xz * onemc + axis.y * s;
m21 = xy * onemc + axis.z * s;
m22 = axis.y * axis.y * onemc + c;
m23 = yz * onemc - axis.x * s;
m31 = xz * onemc - axis.y * s;
m32 = yz * onemc + axis.x * s;
m33 = axis.z * axis.z * onemc + c;
}
void makeScaleMatrix(float sx, float sy, float sz) {
identity();
m11 = sx;
m22 = sy;
m33 = sz;
}
// Query methods