/
Matrix4.h
1158 lines (1005 loc) · 36.4 KB
/
Matrix4.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
#pragma once
/// \file
/// \brief Matrix data types and related operations.
#include "math/Vector3.h"
#include "math/Vector4.h"
#include "math/pi.h"
class Quaternion;
typedef unsigned char ClipResult;
const ClipResult c_CLIP_PASS = 0x00; // 000000
const ClipResult c_CLIP_LT_X = 0x01; // 000001
const ClipResult c_CLIP_GT_X = 0x02; // 000010
const ClipResult c_CLIP_LT_Y = 0x04; // 000100
const ClipResult c_CLIP_GT_Y = 0x08; // 001000
const ClipResult c_CLIP_LT_Z = 0x10; // 010000
const ClipResult c_CLIP_GT_Z = 0x20; // 100000
const ClipResult c_CLIP_FAIL = 0x3F; // 111111
/**
* A 4x4 matrix stored in double-precision floating-point.
*
* The elements of this matrix are stored columnwise in memory:
*
* | 0 4 8 12 |
* | 1 5 9 13 |
* | 2 6 10 14 |
* | 3 7 11 15 |
*
* or, alternatively, as the 4 columns are regarded as 4 vectors named x, y, z, t:
*
* | xx yx zx tx |
* | xy yy zy ty |
* | xz yz zz tz |
* | xw yw zw tw |
*/
class Matrix4
{
// Elements of the 4x4 matrix. These appear to be treated COLUMNWISE, i.e.
// elements [0] through [3] are the first column, [4] through [7] are the
// second column, etc.
double _m[16];
private:
// Initialising constructor, elements are passed in column-wise order
Matrix4(double xx_, double xy_, double xz_, double xw_,
double yx_, double yy_, double yz_, double yw_,
double zx_, double zy_, double zz_, double zw_,
double tx_, double ty_, double tz_, double tw_);
public:
/// Construct a matrix with uninitialised values.
Matrix4() { }
/* NAMED CONSTRUCTORS FOR SPECIFIC MATRICES */
/**
* \brief
* Obtain the identity matrix.
*/
static const Matrix4& getIdentity();
/**
* \brief
* Get a matrix representing the given 3D translation.
*
* @param translation
* Vector3 representing the translation in 3D space.
*/
static Matrix4 getTranslation(const Vector3& translation);
/**
* greebo: Attempts to parse the rotation from the given string, which is
* a whitespace-separated chain of nine floating point values, as used
* in entity spawnargs.
*
* Example: "0 1 0 -1 0 0 0 0 1"
*
* Returns: the parsed (translation-free) matrix. In case of parser errors
* the identity matrix is returned.
*/
static Matrix4 getRotation(const std::string& rotationString);
/**
* greebo: Returns the rotation matrix defined by two three-component
* vectors.
* The rotational axis is defined by the normalised cross product of those
* two vectors, the angle can be retrieved from the dot product.
*/
static Matrix4 getRotation(const Vector3& a, const Vector3& b);
/**
* greebo: Returns the rotation matrix defined by an arbitrary axis
* and an angle.
*
* Important: the axis vector must be normalised.
*/
static Matrix4 getRotation(const Vector3& axis, const double angle);
/**
* Constructs a pure-rotation matrix from the given quaternion.
*/
static Matrix4 getRotation(const Quaternion& quaternion);
/**
* Constructs a pure-rotation matrix from the given quaternion, quantised.
*/
static Matrix4 getRotationQuantised(const Quaternion& quaternion);
/**
* Constructs a pure-rotation matrix about the x axis from sin and cosine of an angle.
*/
static Matrix4 getRotationAboutXForSinCos(double s, double c);
/**
* Constructs a pure-rotation matrix about the x axis from an angle in radians
*/
static Matrix4 getRotationAboutX(double angle);
/**
* Constructs a pure-rotation matrix about the x axis from an angle in degrees.
*/
static Matrix4 getRotationAboutXDegrees(double angle);
/**
* Constructs a pure-rotation matrix about the y axis from sin and cosine of an angle.
*/
static Matrix4 getRotationAboutYForSinCos(double s, double c);
/**
* Constructs a pure-rotation matrix about the y axis from an angle in radians
*/
static Matrix4 getRotationAboutY(double angle);
/**
* Constructs a pure-rotation matrix about the y axis from an angle in degrees.
*/
static Matrix4 getRotationAboutYDegrees(double angle);
/**
* Constructs a pure-rotation matrix about the z axis from sin and cosine of an angle.
*/
static Matrix4 getRotationAboutZForSinCos(double s, double c);
/**
* Constructs a pure-rotation matrix about the z axis from an angle in radians
*/
static Matrix4 getRotationAboutZ(double angle);
/**
* Constructs a pure-rotation matrix about the z axis from an angle in degrees.
*/
static Matrix4 getRotationAboutZDegrees(double angle);
/**
* Constructs a pure-rotation matrix from a set of euler angles (radians) in the order (x, y, z).
*/
static Matrix4 getRotationForEulerXYZ(const Vector3& euler);
/**
* Constructs a pure-rotation matrix from a set of euler angles (degrees) in the order (x, y, z).
*/
static Matrix4 getRotationForEulerXYZDegrees(const Vector3& euler);
/**
* Constructs a pure-rotation matrix from a set of euler angles (radians) in the order (y, z, x).
*/
static Matrix4 getRotationForEulerYZX(const Vector3& euler);
/**
* Constructs a pure-rotation matrix from a set of euler angles (degrees) in the order (y, z, x).
*/
static Matrix4 getRotationForEulerYZXDegrees(const Vector3& euler);
/**
* Constructs a pure-rotation matrix from a set of euler angles (radians) in the order (x, z, y).
*/
static Matrix4 getRotationForEulerXZY(const Vector3& euler);
/**
* Constructs a pure-rotation matrix from a set of euler angles (degrees) in the order (x, z, y).
*/
static Matrix4 getRotationForEulerXZYDegrees(const Vector3& euler);
/**
* Constructs a pure-rotation matrix from a set of euler angles (radians) in the order (y, x, z).
*/
static Matrix4 getRotationForEulerYXZ(const Vector3& euler);
/**
* Constructs a pure-rotation matrix from a set of euler angles (degrees) in the order (y, x, z).
*/
static Matrix4 getRotationForEulerYXZDegrees(const Vector3& euler);
/**
* Constructs a pure-rotation matrix from a set of euler angles (radians) in the order (z, x, y).
*/
static Matrix4 getRotationForEulerZXY(const Vector3& euler);
/**
* Constructs a pure-rotation matrix from a set of euler angles (degrees) in the order (z, x, y).
*/
static Matrix4 getRotationForEulerZXYDegrees(const Vector3& euler);
/**
* Constructs a pure-rotation matrix from a set of euler angles (radians) in the order (z, y, x).
*/
static Matrix4 getRotationForEulerZYX(const Vector3& euler);
/**
* Constructs a pure-rotation matrix from a set of euler angles (degrees) in the order (z, y, x).
*/
static Matrix4 getRotationForEulerZYXDegrees(const Vector3& euler);
/**
* \brief
* Get a matrix representing the given scale in 3D space.
*
* \param scale
* Vector3 representing the scale.
*/
static Matrix4 getScale(const Vector3& scale);
/**
* Returns a perspective projection matrix for the six given frustum planes. The result is the projection
* matrix as constructed by openGL when calling the glFrustum() function.
*/
static Matrix4 getProjectionForFrustum(double left, double right, double bottom, double top, double nearval, double farval);
/**
* \brief
* Construct a matrix containing the given elements.
*
* The elements are specified column-wise, starting with the left-most
* column.
*/
static Matrix4 byColumns(double xx, double xy, double xz, double xw,
double yx, double yy, double yz, double yw,
double zx, double zy, double zz, double zw,
double tx, double ty, double tz, double tw);
/**
* \brief
* Construct a matrix containing the given elements.
*
* The elements are specified row-wise, starting with the top row.
*/
static Matrix4 byRows(double xx, double yx, double zx, double tx,
double xy, double yy, double zy, double ty,
double xz, double yz, double zz, double tz,
double xw, double yw, double zw, double tw);
enum Handedness
{
RIGHTHANDED = 0,
LEFTHANDED = 1,
};
/**
* Return matrix elements
* \{
*/
double& xx() { return _m[0]; }
const double& xx() const { return _m[0]; }
double& xy() { return _m[1]; }
const double& xy() const { return _m[1]; }
double& xz() { return _m[2]; }
const double& xz() const { return _m[2]; }
double& xw() { return _m[3]; }
const double& xw() const { return _m[3]; }
double& yx() { return _m[4]; }
const double& yx() const { return _m[4]; }
double& yy() { return _m[5]; }
const double& yy() const { return _m[5]; }
double& yz() { return _m[6]; }
const double& yz() const { return _m[6]; }
double& yw() { return _m[7]; }
const double& yw() const { return _m[7]; }
double& zx() { return _m[8]; }
const double& zx() const { return _m[8]; }
double& zy() { return _m[9]; }
const double& zy() const { return _m[9]; }
double& zz() { return _m[10]; }
const double& zz() const { return _m[10]; }
double& zw() { return _m[11]; }
const double& zw() const { return _m[11]; }
double& tx() { return _m[12]; }
const double& tx() const { return _m[12]; }
double& ty() { return _m[13]; }
const double& ty() const { return _m[13]; }
double& tz() { return _m[14]; }
const double& tz() const { return _m[14]; }
double& tw() { return _m[15]; }
const double& tw() const { return _m[15]; }
/**
* \}
*/
/**
* Return columns of the matrix as vectors.
* \{
*/
Vector4& x()
{
return reinterpret_cast<Vector4&>(xx());
}
const Vector4& x() const
{
return reinterpret_cast<const Vector4&>(xx());
}
Vector4& y()
{
return reinterpret_cast<Vector4&>(yx());
}
const Vector4& y() const
{
return reinterpret_cast<const Vector4&>(yx());
}
Vector4& z()
{
return reinterpret_cast<Vector4&>(zx());
}
const Vector4& z() const
{
return reinterpret_cast<const Vector4&>(zx());
}
Vector4& t()
{
return reinterpret_cast<Vector4&>(tx());
}
const Vector4& t() const
{
return reinterpret_cast<const Vector4&>(tx());
}
/**
* \}
*/
/**
* Cast to double* for use with GL functions that accept a double
* array, also provides operator[].
*/
operator double* ()
{
return _m;
}
/**
* Cast to const double* to provide operator[] for const objects.
*/
operator const double* () const
{
return _m;
}
/**
* \brief
* Transpose this matrix in-place.
*/
void transpose();
/**
* \brief
* Return a transposed copy of this matrix.
*/
Matrix4 getTransposed() const;
/**
* \brief
* Return the affine inverse of this transformation matrix.
*/
Matrix4 getInverse() const;
/**
* Affine invert this matrix in-place.
*/
void invert();
/**
* \brief
* Return the full inverse of this matrix.
*/
Matrix4 getFullInverse() const;
/**
* Invert this matrix in-place.
*/
void invertFull();
/**
* \brief
* Returns the given 3-component point transformed by this matrix.
*
* The point is assumed to have a W component of 1.
*/
template<typename Element>
BasicVector3<Element> transformPoint(const BasicVector3<Element>& point) const;
/**
* Returns the given 3-component direction transformed by this matrix.
* The given vector is treated as direction so it won't receive a translation, just like
* a 4-component vector with its w-component set to 0 would be transformed.
*/
template<typename Element>
BasicVector3<Element> transformDirection(const BasicVector3<Element>& direction) const;
/**
* \brief Use this matrix to transform the provided vector and return a new
* vector containing the result.
*
* \param vector4
* The 4-element vector to transform.
*/
template<typename Element>
BasicVector4<Element> transform(const BasicVector4<Element>& vector4) const;
/**
* \brief
* Return the result of this matrix post-multiplied by another matrix.
*/
Matrix4 getMultipliedBy(const Matrix4& other) const;
/**
* \brief
* Post-multiply this matrix by another matrix, in-place.
*/
void multiplyBy(const Matrix4& other);
/**
* Returns this matrix pre-multiplied by the other
*/
Matrix4 getPremultipliedBy(const Matrix4& other) const;
/**
* Pre-multiplies this matrix by other in-place.
*/
void premultiplyBy(const Matrix4& other);
/**
* \brief
* Add a translation component to the transformation represented by this
* matrix.
*
* Equivalent to multiplyBy(Matrix4::getTranslation(translation));
*/
void translateBy(const Vector3& translation);
/**
* \brief
* Add a translation component to the transformation represented by this
* matrix.
*
* Equivalent to getMultipliedBy(Matrix4::getTranslation(translation));
*/
Matrix4 getTranslatedBy(const Vector3& translation) const;
/**
* Returns this matrix concatenated with the rotation transform produced by the given quat.
* The concatenated rotation occurs before the transformation of this matrix.
*
* Equivalent to getMultipliedBy(getRotation(rotation));
*/
Matrix4 getRotatedBy(const Quaternion& rotation) const;
/**
* Concatenates this matrix with the rotation transform produced by the given quat.
* The concatenated rotation occurs before the transformation of this matrix.
*/
void rotateBy(const Quaternion& rotation);
/**
* Concatenates this matrix with the pivoted rotation transform produced by the given quat.
* The concatenated rotation occurs before the transformation of this matrix.
*/
void rotateBy(const Quaternion& rotation, const Vector3& pivot);
/**
* \brief
* Add a scale component to the transformation represented by this matrix.
*
* Equivalent to multiplyBy(Matrix4::getScale(scale));
*/
void scaleBy(const Vector3& scale);
/**
* \brief
* Add a pivoted scale transformation to this matrix.
*/
void scaleBy(const Vector3& scale, const Vector3& pivot);
/**
* Equality operator, Returns true if this and the other are exactly element-wise equal.
*/
bool operator==(const Matrix4& other) const;
/**
* Inequality operator.
*/
bool operator!=(const Matrix4& other) const;
/**
* Returns true if self and other are element-wise equal within epsilon.
*/
bool isEqual(const Matrix4& other, double epsilon) const;
/**
* Returns true if this and the given matrix are exactly element-wise equal.
* This and the other matrix must be affine.
*/
bool isAffineEqual(const Matrix4& other) const;
/**
* Returns RIGHTHANDED if this is right-handed, else returns LEFTHANDED.
*/
Handedness getHandedness() const;
/**
* Returns true if this matrix is affine.
*/
bool isAffine() const;
/**
* Returns this matrix post-multiplied by the other.
* This and the other matrix must be affine.
*/
Matrix4 getAffineMultipliedBy(const Matrix4& other) const;
/**
* Post-multiplies this matrix by the other in-place.
* This and the other matrix must be affine.
*/
void affineMultiplyBy(const Matrix4& other);
/**
* Returns this matrix pre-multiplied by the other.
* This matrix and the other must be affine.
*/
Matrix4 getAffinePremultipliedBy(const Matrix4& other) const;
/**
* Pre-multiplies this matrix by the other in-place.
* This and the other matrix must be affine.
*/
void affinePremultiplyBy(const Matrix4& other);
/// Return the 3-element translation component of this matrix
const Vector3& translation() const;
/**
* Concatenates this with the rotation transform produced
* by euler angles (degrees) in the order (x, y, z).
* The concatenated rotation occurs before self.
*/
void rotateByEulerXYZDegrees(const Vector3& euler);
/**
* Concatenates this with the pivoted rotation transform produced
* by euler angles (degrees) in the order (x, y, z).
* The concatenated rotation occurs before self.
*/
void rotateByEulerXYZDegrees(const Vector3& euler, const Vector3& pivot);
/**
* Returns this matrix concatenated with the rotation transform produced by the given
* euler angles (degrees) in the order (y, x, z). The concatenated rotation occurs before this matrix.
*/
Matrix4 getRotatedByEulerYXZDegrees(const Vector3& euler) const;
/**
* Concatenates this with the rotation transform produced
* by euler angles (degrees) in the order (y, x, z).
* The concatenated rotation occurs before self.
*/
void rotateByEulerYXZDegrees(const Vector3& euler);
/**
* Returns this matrix concatenated with the rotation transform produced by the given
* euler angles (degrees) in the order (z, x, y). The concatenated rotation occurs before this matrix.
*/
Matrix4 getRotatedByEulerZXYDegrees(const Vector3& euler) const;
/**
* Concatenates this with the rotation transform produced
* by euler angles (degrees) in the order (z, x, y).
* The concatenated rotation occurs before self.
*/
void rotateByEulerZXYDegrees(const Vector3& euler);
/**
* Calculates and returns a set of euler angles in radians that produce
* the rotation component of this matrix when applied in the order (x, y, z).
* This matrix must be affine and orthonormal (unscaled) to produce a meaningful result.
*/
Vector3 getEulerAnglesXYZ() const;
/**
* Calculates and returns a set of euler angles in degrees that produce
* the rotation component of this matrix when applied in the order (x, y, z).
* This matrix must be affine and orthonormal (unscaled) to produce a meaningful result.
*/
Vector3 getEulerAnglesXYZDegrees() const;
/**
* Calculates and returns a set of euler angles in radians that produce
* the rotation component of this matrix when applied in the order (y, x, z).
* This matrix must be affine and orthonormal (unscaled) to produce a meaningful result.
*/
Vector3 getEulerAnglesYXZ() const;
/**
* Calculates and returns a set of euler angles in degrees that produce
* the rotation component of this matrix when applied in the order (y, x, z).
* This matrix must be affine and orthonormal (unscaled) to produce a meaningful result.
*/
Vector3 getEulerAnglesYXZDegrees() const;
/**
* Calculates and returns a set of euler angles in radians that produce
* the rotation component of this matrix when applied in the order (z, x, y).
* This matrix must be affine and orthonormal (unscaled) to produce a meaningful result.
*/
Vector3 getEulerAnglesZXY() const;
/**
* Calculates and returns a set of euler angles in degrees that produce
* the rotation component of this matrix when applied in the order (z, x, y).
* This matrix must be affine and orthonormal (unscaled) to produce a meaningful result.
*/
Vector3 getEulerAnglesZXYDegrees() const;
/**
* Calculates and returns a set of euler angles in radians that produce
* the rotation component of this matrix when applied in the order (z, y, x).
* This matrix must be affine and orthonormal (unscaled) to produce a meaningful result.
*/
Vector3 getEulerAnglesZYX() const;
/**
* Calculates and returns a set of euler angles in degrees that produce
* the rotation component of this matrix when applied in the order (z, y, x).
* This matrix must be affine and orthonormal (unscaled) to produce a meaningful result.
*/
Vector3 getEulerAnglesZYXDegrees() const;
/**
* Calculates and returns the (x, y, z) scale values that produce the scale component of this matrix.
* This matrix must be affine and orthogonal to produce a meaningful result.
*/
Vector3 getScale() const;
/**
* Transforms and clips the line formed by p0, p1 by this canonical matrix.
* Stores the resulting line in clipped.
*
* @returns: the number of points in the resulting line.
*/
std::size_t clipLine(const Vector3& p0, const Vector3& p1, Vector4 clipped[2]) const;
/**
* Clips point by this canonical matrix and stores the result in clipped.
* Returns a bitmask indicating which clip-planes the point was outside.
*/
ClipResult clipPoint(const Vector3& point, Vector4& clipped) const;
/**
* Transforms and clips the triangle formed by p0, p1, p2 by this canonical matrix.
* Stores the resulting polygon in clipped.
* Returns the number of points in the resulting polygon.
*/
std::size_t clipTriangle(const Vector3& p0, const Vector3& p1, const Vector3& p2, Vector4 clipped[9]) const;
};
// ===========================================================================
// Operators
// ===========================================================================
/**
* \brief
* Multiply a 4-component vector by this matrix.
*
* Equivalent to m.transform(v).
*/
template<typename T>
BasicVector4<T> operator* (const Matrix4& m, const BasicVector4<T>& v)
{
return m.transform(v);
}
/**
* \brief
* Multiply a 3-component vector by this matrix.
*
* The vector is upgraded to a 4-component vector with a W component of 1, i.e.
* equivalent to m.transformPoint(v).
*/
template<typename T>
BasicVector3<T> operator* (const Matrix4& m, const BasicVector3<T>& v)
{
return m.transformPoint(v);
}
// =========================================================================================
// Inlined member definitions
// =========================================================================================
// Construct a matrix with given column elements
inline Matrix4 Matrix4::byColumns(double xx, double xy, double xz, double xw,
double yx, double yy, double yz, double yw,
double zx, double zy, double zz, double zw,
double tx, double ty, double tz, double tw)
{
return Matrix4(xx, xy, xz, xw,
yx, yy, yz, yw,
zx, zy, zz, zw,
tx, ty, tz, tw);
}
// Construct a matrix with given row elements
inline Matrix4 Matrix4::byRows(double xx, double yx, double zx, double tx,
double xy, double yy, double zy, double ty,
double xz, double yz, double zz, double tz,
double xw, double yw, double zw, double tw)
{
return Matrix4(xx, xy, xz, xw,
yx, yy, yz, yw,
zx, zy, zz, zw,
tx, ty, tz, tw);
}
// Post-multiply this with other
inline Matrix4 Matrix4::getMultipliedBy(const Matrix4& other) const
{
return Matrix4::byColumns(
other[0] * _m[0] + other[1] * _m[4] + other[2] * _m[8] + other[3] * _m[12],
other[0] * _m[1] + other[1] * _m[5] + other[2] * _m[9] + other[3] * _m[13],
other[0] * _m[2] + other[1] * _m[6] + other[2] * _m[10]+ other[3] * _m[14],
other[0] * _m[3] + other[1] * _m[7] + other[2] * _m[11]+ other[3] * _m[15],
other[4] * _m[0] + other[5] * _m[4] + other[6] * _m[8] + other[7] * _m[12],
other[4] * _m[1] + other[5] * _m[5] + other[6] * _m[9] + other[7] * _m[13],
other[4] * _m[2] + other[5] * _m[6] + other[6] * _m[10]+ other[7] * _m[14],
other[4] * _m[3] + other[5] * _m[7] + other[6] * _m[11]+ other[7] * _m[15],
other[8] * _m[0] + other[9] * _m[4] + other[10]* _m[8] + other[11]* _m[12],
other[8] * _m[1] + other[9] * _m[5] + other[10]* _m[9] + other[11]* _m[13],
other[8] * _m[2] + other[9] * _m[6] + other[10]* _m[10]+ other[11]* _m[14],
other[8] * _m[3] + other[9] * _m[7] + other[10]* _m[11]+ other[11]* _m[15],
other[12]* _m[0] + other[13]* _m[4] + other[14]* _m[8] + other[15]* _m[12],
other[12]* _m[1] + other[13]* _m[5] + other[14]* _m[9] + other[15]* _m[13],
other[12]* _m[2] + other[13]* _m[6] + other[14]* _m[10]+ other[15]* _m[14],
other[12]* _m[3] + other[13]* _m[7] + other[14]* _m[11]+ other[15]* _m[15]
);
}
inline Matrix4 Matrix4::getPremultipliedBy(const Matrix4& other) const
{
return other.getMultipliedBy(*this);
}
inline Matrix4 Matrix4::getRotationAboutX(double angle)
{
return getRotationAboutXForSinCos(sin(angle), cos(angle));
}
inline Matrix4 Matrix4::getRotationAboutXDegrees(double angle)
{
return getRotationAboutX(degrees_to_radians(angle));
}
inline Matrix4 Matrix4::getRotationAboutY(double angle)
{
return getRotationAboutYForSinCos(sin(angle), cos(angle));
}
inline Matrix4 Matrix4::getRotationAboutYDegrees(double angle)
{
return getRotationAboutY(degrees_to_radians(angle));
}
inline Matrix4 Matrix4::getRotationAboutZ(double angle)
{
return getRotationAboutZForSinCos(sin(angle), cos(angle));
}
inline Matrix4 Matrix4::getRotationAboutZDegrees(double angle)
{
return getRotationAboutZ(degrees_to_radians(angle));
}
inline Matrix4 Matrix4::getProjectionForFrustum(double left, double right, double bottom, double top, double nearval, double farval)
{
return Matrix4::byColumns(
(2*nearval) / (right-left),
0,
0,
0,
0,
(2*nearval) / (top-bottom),
0,
0,
(right+left) / (right-left),
(top+bottom) / (top-bottom),
-(farval+nearval) / (farval-nearval),
-1,
0,
0,
-(2*farval*nearval) / (farval-nearval),
0
);
}
inline bool Matrix4::operator==(const Matrix4& other) const
{
return xx() == other.xx() && xy() == other.xy() && xz() == other.xz() && xw() == other.xw()
&& yx() == other.yx() && yy() == other.yy() && yz() == other.yz() && yw() == other.yw()
&& zx() == other.zx() && zy() == other.zy() && zz() == other.zz() && zw() == other.zw()
&& tx() == other.tx() && ty() == other.ty() && tz() == other.tz() && tw() == other.tw();
}
inline bool Matrix4::operator!=(const Matrix4& other) const
{
return !operator==(other);
}
inline bool Matrix4::isEqual(const Matrix4& other, double epsilon) const
{
return float_equal_epsilon(xx(), other.xx(), epsilon)
&& float_equal_epsilon(xy(), other.xy(), epsilon)
&& float_equal_epsilon(xz(), other.xz(), epsilon)
&& float_equal_epsilon(xw(), other.xw(), epsilon)
&& float_equal_epsilon(yx(), other.yx(), epsilon)
&& float_equal_epsilon(yy(), other.yy(), epsilon)
&& float_equal_epsilon(yz(), other.yz(), epsilon)
&& float_equal_epsilon(yw(), other.yw(), epsilon)
&& float_equal_epsilon(zx(), other.zx(), epsilon)
&& float_equal_epsilon(zy(), other.zy(), epsilon)
&& float_equal_epsilon(zz(), other.zz(), epsilon)
&& float_equal_epsilon(zw(), other.zw(), epsilon)
&& float_equal_epsilon(tx(), other.tx(), epsilon)
&& float_equal_epsilon(ty(), other.ty(), epsilon)
&& float_equal_epsilon(tz(), other.tz(), epsilon)
&& float_equal_epsilon(tw(), other.tw(), epsilon);
}
inline bool Matrix4::isAffineEqual(const Matrix4& other) const
{
return xx() == other.xx() &&
xy() == other.xy() &&
xz() == other.xz() &&
yx() == other.yx() &&
yy() == other.yy() &&
yz() == other.yz() &&
zx() == other.zx() &&
zy() == other.zy() &&
zz() == other.zz() &&
tx() == other.tx() &&
ty() == other.ty() &&
tz() == other.tz();
}
inline Matrix4::Handedness Matrix4::getHandedness() const
{
return (x().getVector3().crossProduct(y().getVector3()).dot(z().getVector3()) < 0.0f) ? LEFTHANDED : RIGHTHANDED;
}
inline void Matrix4::premultiplyBy(const Matrix4& other)
{
*this = getPremultipliedBy(other);
}
inline bool Matrix4::isAffine() const
{
return xw() == 0 && yw() == 0 && zw() == 0 && tw() == 1;
}
inline Matrix4 Matrix4::getAffineMultipliedBy(const Matrix4& other) const
{
return Matrix4::byColumns(
other.xx() * xx() + other.xy() * yx() + other.xz() * zx(),
other.xx() * xy() + other.xy() * yy() + other.xz() * zy(),
other.xx() * xz() + other.xy() * yz() + other.xz() * zz(),
0,
other.yx() * xx() + other.yy() * yx() + other.yz() * zx(),
other.yx() * xy() + other.yy() * yy() + other.yz() * zy(),
other.yx() * xz() + other.yy() * yz() + other.yz() * zz(),
0,
other.zx() * xx() + other.zy() * yx() + other.zz()* zx(),
other.zx() * xy() + other.zy() * yy() + other.zz()* zy(),
other.zx() * xz() + other.zy() * yz() + other.zz()* zz(),
0,
other.tx()* xx() + other.ty()* yx() + other.tz()* zx() + tx(),
other.tx()* xy() + other.ty()* yy() + other.tz()* zy() + ty(),
other.tx()* xz() + other.ty()* yz() + other.tz()* zz()+ tz(),
1
);
}
inline void Matrix4::affineMultiplyBy(const Matrix4& other)
{
*this = getAffineMultipliedBy(other);
}
inline Matrix4 Matrix4::getAffinePremultipliedBy(const Matrix4& other) const
{
return other.getAffineMultipliedBy(*this);
}
inline void Matrix4::affinePremultiplyBy(const Matrix4& other)
{
*this = getAffinePremultipliedBy(other);
}
template<typename Element>
BasicVector3<Element> Matrix4::transformPoint(const BasicVector3<Element>& point) const
{
return BasicVector3<Element>(
static_cast<Element>(xx() * point[0] + yx() * point[1] + zx() * point[2] + tx()),
static_cast<Element>(xy() * point[0] + yy() * point[1] + zy() * point[2] + ty()),
static_cast<Element>(xz() * point[0] + yz() * point[1] + zz() * point[2] + tz())
);
}
template<typename Element>
BasicVector3<Element> Matrix4::transformDirection(const BasicVector3<Element>& direction) const
{
return BasicVector3<Element>(
static_cast<Element>(xx() * direction[0] + yx() * direction[1] + zx() * direction[2]),
static_cast<Element>(xy() * direction[0] + yy() * direction[1] + zy() * direction[2]),
static_cast<Element>(xz() * direction[0] + yz() * direction[1] + zz() * direction[2])
);
}
template<typename Element>
BasicVector4<Element> Matrix4::transform(const BasicVector4<Element>& vector4) const
{
return BasicVector4<Element>(
static_cast<Element>(_m[0] * vector4[0] + _m[4] * vector4[1] + _m[8] * vector4[2] + _m[12] * vector4[3]),
static_cast<Element>(_m[1] * vector4[0] + _m[5] * vector4[1] + _m[9] * vector4[2] + _m[13] * vector4[3]),
static_cast<Element>(_m[2] * vector4[0] + _m[6] * vector4[1] + _m[10] * vector4[2] + _m[14] * vector4[3]),
static_cast<Element>(_m[3] * vector4[0] + _m[7] * vector4[1] + _m[11] * vector4[2] + _m[15] * vector4[3])
);
}
inline void Matrix4::invert()
{
*this = getInverse();
}
inline void Matrix4::invertFull()
{
*this = getFullInverse();
}
inline const Vector3& Matrix4::translation() const
{
return t().getVector3();
}
inline Matrix4 Matrix4::getTranslatedBy(const Vector3& translation) const
{
return getMultipliedBy(Matrix4::getTranslation(translation));
}
inline Matrix4 Matrix4::getRotatedBy(const Quaternion& rotation) const
{
return getMultipliedBy(getRotation(rotation));
}
inline void Matrix4::rotateBy(const Quaternion& rotation)
{
*this = getRotatedBy(rotation);
}
inline void Matrix4::rotateBy(const Quaternion& rotation, const Vector3& pivot)
{
translateBy(pivot);
rotateBy(rotation);
translateBy(-pivot);
}
inline void Matrix4::rotateByEulerXYZDegrees(const Vector3& euler)
{
multiplyBy(getRotationForEulerXYZDegrees(euler));
}
inline void Matrix4::rotateByEulerXYZDegrees(const Vector3& euler, const Vector3& pivot)
{
translateBy(pivot);
rotateByEulerXYZDegrees(euler);
translateBy(-pivot);
}
inline Matrix4 Matrix4::getRotatedByEulerYXZDegrees(const Vector3& euler) const
{
return getMultipliedBy(getRotationForEulerYXZDegrees(euler));
}
/// \brief Concatenates \p self with the rotation transform produced by \p euler angles (degrees) in the order (y, x, z).