SA_Maths
Sapphire Suite's C++ Maths Library
Loading...
Searching...
No Matches
Matrix4.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 Sapphire development team. All Rights Reserved.
2
3#pragma once
4
5#ifndef SAPPHIRE_MATHS_MATRIX4_GUARD
6#define SAPPHIRE_MATHS_MATRIX4_GUARD
7
8#include <limits>
9
10#include <SA/Maths/Debug.hpp>
11#include <SA/Maths/Config.hpp>
12
13#include <SA/Maths/Matrix/Matrix4Base.hpp>
14
17
18#if SA_MATHS_MATRIX4_SIMD
19
20 #include <SA/Support/Intrinsics.hpp>
21
22#endif
23
34namespace SA
35{
36 template <typename T>
37 struct Vec3;
38
39 template <typename T>
40 struct Vec4;
41
42 template <typename T>
43 struct Quat;
44
45 template <typename T, MatrixMajor major>
46 struct Mat3;
47
48
57#if SA_MATHS_MATRIX4_SIMD && SA_INTRISC
58
59 template <typename T, MatrixMajor major = MatrixMajor::Default>
60 struct alignas(32) Mat4 : public Intl::Mat4_Base<T, major>
61
62#else
63
64 template <typename T, MatrixMajor major = MatrixMajor::Default>
65 struct Mat4 : public Intl::Mat4_Base<T, major>
66
67#endif
68 {
69 using Intl::Mat4_Base<T, major>::e00;
70 using Intl::Mat4_Base<T, major>::e01;
71 using Intl::Mat4_Base<T, major>::e02;
72 using Intl::Mat4_Base<T, major>::e03;
73
74 using Intl::Mat4_Base<T, major>::e10;
75 using Intl::Mat4_Base<T, major>::e11;
76 using Intl::Mat4_Base<T, major>::e12;
77 using Intl::Mat4_Base<T, major>::e13;
78
79 using Intl::Mat4_Base<T, major>::e20;
80 using Intl::Mat4_Base<T, major>::e21;
81 using Intl::Mat4_Base<T, major>::e22;
82 using Intl::Mat4_Base<T, major>::e23;
83
84 using Intl::Mat4_Base<T, major>::e30;
85 using Intl::Mat4_Base<T, major>::e31;
86 using Intl::Mat4_Base<T, major>::e32;
87 using Intl::Mat4_Base<T, major>::e33;
88
89//{ Constants
90
99 static const Mat4 Zero;
100
109 static const Mat4 Identity;
110
111//}
112
113//{ Constructors
114
115 using Intl::Mat4_Base<T, major>::Mat4_Base;
116
125 template <typename TIn, MatrixMajor majorIn>
126 constexpr Mat4(const Mat4<TIn, majorIn>& _other) noexcept;
127
136 template <typename TIn, MatrixMajor majorIn>
137 constexpr Mat4(const Mat3<TIn, majorIn>& _other) noexcept;
138
139//{ Equals
140
146 constexpr bool IsZero() const noexcept;
147
153 constexpr bool IsIdentity() const noexcept;
154
155
164 constexpr bool Equals(const Mat4& _other, T _threshold = std::numeric_limits<T>::epsilon()) const noexcept;
165
166
174 constexpr bool operator==(const Mat4& _rhs) const noexcept;
175
183 constexpr bool operator!=(const Mat4& _rhs) const noexcept;
184
185//}
186
187//{ Accessors
188
194 T* Data() noexcept;
195
201 const T* Data() const noexcept;
202
203
211 T& At(uint32_t _index);
212
220 const T& At(uint32_t _index) const;
221
230 T& At(uint32_t _x, uint32_t _y);
231
240 const T& At(uint32_t _x, uint32_t _y) const;
241
242
250 T& operator[](uint32_t _index);
251
259 const T& operator[](uint32_t _index) const;
260
261//}
262
263//{ Transpose
264
270 Mat4& Transpose() noexcept;
271
277 constexpr Mat4 GetTransposed() const noexcept;
278
279//}
280
281//{ Inverse
282
288 T Determinant() const noexcept;
289
296
303
304//}
305
306//{ Lerp
307
319 static Mat4 Lerp(const Mat4& _start, const Mat4& _end, float _alpha) noexcept;
320
332 static Mat4 LerpUnclamped(const Mat4& _start, const Mat4& _end, float _alpha) noexcept;
333
334//}
335
336//{ Transform
337
338
346 static Mat4 MakeTranslation(const Vec3<T>& _transl) noexcept;
347
355 static Mat4 MakeRotation(const Quat<T>& _rot) noexcept;
356
364 static Mat4 MakeScale(const Vec3<T>& _scale) noexcept;
365
375 static Mat4 MakeLookAt(const Vec3<T>& _eye, const Vec3<T>& _target, const Vec3<T>& _up);
376
386 // static Mat4 MakeView(const Vec3<T>& _eye, const Vec3<T>& _forward, const Vec3<T>& _up);
387
397 static Mat4 MakeInverseView(const Vec3<T>& _eye, const Vec3<T>& _forward, const Vec3<T>& _up);
398
409 static Mat4 MakePerspective(T _fov = T(90.0), T _aspect = T(1.0), T _near = T(0.35), T _far = T(10.0)) noexcept;
410
411//}
412
413//{ Operators
414
423 template <typename TIn, MatrixMajor majorIn>
424 Mat4& operator=(const Mat4<TIn, majorIn> _rhs) noexcept;
425
431 constexpr Mat4 operator-() const noexcept;
432
441 template<typename TIn>
442 Mat4 operator*(TIn _scale) const noexcept;
443
452 template<typename TIn>
453 Mat4 operator/(TIn _scale) const;
454
462 Mat4 operator+(const Mat4& _rhs) const noexcept;
463
471 Mat4 operator-(const Mat4& _rhs) const noexcept;
472
480 Mat4 operator*(const Mat4& _rhs) const noexcept;
481
489 Mat4 operator/(const Mat4& _rhs) const;
490
491
501 Vec3<T> operator*(const Vec3<T>&_rhs) const noexcept;
502
510 Vec4<T> operator*(const Vec4<T>&_rhs) const noexcept;
511
512
521 template<typename TIn>
522 Mat4& operator*=(TIn _scale) noexcept;
523
532 template<typename TIn>
533 Mat4& operator/=(TIn _scale);
534
542 Mat4& operator+=(const Mat4& _rhs) noexcept;
543
551 Mat4& operator-=(const Mat4& _rhs) noexcept;
552
560 Mat4& operator*=(const Mat4& _rhs) noexcept;
561
569 Mat4& operator/=(const Mat4& _rhs);
570
571//}
572
573
574#if SA_LOGGING
575
584 std::string ToString() const noexcept;
585
586#endif
587 };
588
589#if SA_MATHS_MATRIX4_SIMD && SA_INTRISC
590
591 // Disable padding struct warning.
592 //SA_PRAGMA_EDWARN_MSVC()
593
594#endif
595
596
607 template <typename TIn, typename T, MatrixMajor major>
608 Mat4<T, major> operator*(TIn _lhs, const Mat4<T, major>& _rhs) noexcept;
609
620 template <typename TIn, typename T, MatrixMajor major>
622
623
624#if SA_LOGGER_IMPL
625
638 template <typename T, MatrixMajor major>
639 std::string ToString(const Mat4<T, major>& _m);
640
641#endif
642
643
644//{ Aliases
645
648
651
654
655
657 template <typename T, MatrixMajor major = MatrixMajor::Default>
659
662
665
668
669
672
675
678
679
682
685
688
689//}
690
692
693#if SA_MATHS_MATRIX4_SIMD && SA_INTRISC_AVX // SIMD int32
694
695//{ Row Major
696
697 template <>
698 template <>
699 RMat4i RMat4i::operator*(int32_t _scale) const noexcept;
700
701#if SA_INTRISC_SVML
702 template <>
703 template <>
704 RMat4i RMat4i::operator/(int32_t _scale) const;
705#endif
706
707 template <>
708 RMat4i RMat4i::operator+(const RMat4i& _rhs) const noexcept;
709
710 template <>
711 RMat4i RMat4i::operator-(const RMat4i& _rhs) const noexcept;
712
713 template <>
714 RMat4i RMat4i::operator*(const RMat4i& _rhs) const noexcept;
715
716
717 template <>
718 Vec3<int32_t> RMat4i::operator*(const Vec3<int32_t>& _rhs) const noexcept;
719
720 template <>
721 Vec4<int32_t> RMat4i::operator*(const Vec4<int32_t>& _rhs) const noexcept;
722
723
724 template <>
725 template <>
726 RMat4i& RMat4i::operator*=(int32_t _scale) noexcept;
727
728#if SA_INTRISC_SVML
729 template <>
730 template <>
731 RMat4i& RMat4i::operator/=(int32_t _scale);
732#endif
733
734 template <>
735 RMat4i& RMat4i::operator+=(const RMat4i& _rhs) noexcept;
736
737 template <>
738 RMat4i& RMat4i::operator-=(const RMat4i& _rhs) noexcept;
739
740//}
741
742//{ Column Major
743
744 template <>
745 template <>
746 CMat4i CMat4i::operator*(int32_t _scale) const noexcept;
747
748#if SA_INTRISC_SVML
749 template <>
750 template <>
751 CMat4i CMat4i::operator/(int32_t _scale) const;
752#endif
753
754 template <>
755 CMat4i CMat4i::operator+(const CMat4i& _rhs) const noexcept;
756
757 template <>
758 CMat4i CMat4i::operator-(const CMat4i& _rhs) const noexcept;
759
760 template <>
761 CMat4i CMat4i::operator*(const CMat4i& _rhs) const noexcept;
762
763
764 template <>
765 Vec3<int32_t> CMat4i::operator*(const Vec3<int32_t>& _rhs) const noexcept;
766
767 template <>
768 Vec4<int32_t> CMat4i::operator*(const Vec4<int32_t>& _rhs) const noexcept;
769
770
771 template <>
772 template <>
773 CMat4i& CMat4i::operator*=(int32_t _scale) noexcept;
774
775#if SA_INTRISC_SVML
776 template <>
777 template <>
778 CMat4i& CMat4i::operator/=(int32_t _scale);
779#endif
780
781 template <>
782 CMat4i& CMat4i::operator+=(const CMat4i& _rhs) noexcept;
783
784 template <>
785 CMat4i& CMat4i::operator-=(const CMat4i& _rhs) noexcept;
786
787//}
788
789#endif
790
791#if SA_MATHS_MATRIX4_SIMD && SA_INTRISC_SSE // SIMD float
792
793//{ Row Major
794
795 template <>
796 float RMat4f::Determinant() const noexcept;
797
798 template <>
799 RMat4f RMat4f::GetInversed() const;
800
801
802 template <>
803 RMat4f RMat4f::MakeRotation(const Quat<float>& _rot) noexcept;
804
805
806 template <>
807 template <>
808 RMat4f RMat4f::operator*(float _scale) const noexcept;
809
810 template <>
811 template <>
812 RMat4f RMat4f::operator/(float _scale) const;
813
814 template <>
815 RMat4f RMat4f::operator+(const RMat4f& _rhs) const noexcept;
816
817 template <>
818 RMat4f RMat4f::operator-(const RMat4f& _rhs) const noexcept;
819
820 template <>
821 RMat4f RMat4f::operator*(const RMat4f& _rhs) const noexcept;
822
823
824 template <>
825 Vec3<float> RMat4f::operator*(const Vec3<float>& _rhs) const noexcept;
826
827 template <>
828 Vec4<float> RMat4f::operator*(const Vec4<float>& _rhs) const noexcept;
829
830
831 template <>
832 template <>
833 RMat4f& RMat4f::operator*=(float _scale) noexcept;
834
835 template <>
836 template <>
837 RMat4f& RMat4f::operator/=(float _scale);
838
839 template <>
840 RMat4f& RMat4f::operator+=(const RMat4f& _rhs) noexcept;
841
842 template <>
843 RMat4f& RMat4f::operator-=(const RMat4f& _rhs) noexcept;
844
845
846 template <>
847 RMat4f operator/(float _lhs, const RMat4f& _rhs);
848
849//}
850
851//{ Column Major
852
853 template <>
854 float CMat4f::Determinant() const noexcept;
855
856 template <>
857 CMat4f CMat4f::GetInversed() const;
858
859
860 template <>
861 CMat4f CMat4f::MakeRotation(const Quat<float>& _rot) noexcept;
862
863
864 template <>
865 template <>
866 CMat4f CMat4f::operator*(float _scale) const noexcept;
867
868 template <>
869 template <>
870 CMat4f CMat4f::operator/(float _scale) const;
871
872 template <>
873 CMat4f CMat4f::operator+(const CMat4f& _rhs) const noexcept;
874
875 template <>
876 CMat4f CMat4f::operator-(const CMat4f& _rhs) const noexcept;
877
878 template <>
879 CMat4f CMat4f::operator*(const CMat4f& _rhs) const noexcept;
880
881
882 template <>
883 Vec3<float> CMat4f::operator*(const Vec3<float>& _rhs) const noexcept;
884
885 template <>
886 Vec4<float> CMat4f::operator*(const Vec4<float>& _rhs) const noexcept;
887
888
889 template <>
890 template <>
891 CMat4f& CMat4f::operator*=(float _scale) noexcept;
892
893 template <>
894 template <>
895 CMat4f& CMat4f::operator/=(float _scale);
896
897 template <>
898 CMat4f& CMat4f::operator+=(const CMat4f& _rhs) noexcept;
899
900 template <>
901 CMat4f& CMat4f::operator-=(const CMat4f& _rhs) noexcept;
902
903
904 template <>
905 CMat4f operator/(float _lhs, const CMat4f& _rhs);
906
907//}
908
909#endif
910
911#if SA_MATHS_MATRIX4_SIMD && SA_INTRISC_AVX // SIMD double
912
913//{ Row Major
914
915 template <>
916 double RMat4d::Determinant() const noexcept;
917
918 template <>
919 RMat4d RMat4d::GetInversed() const;
920
921
922 template <>
923 RMat4d RMat4d::MakeRotation(const Quat<double>& _rot) noexcept;
924
925
926 template <>
927 template <>
928 RMat4d RMat4d::operator*(double _scale) const noexcept;
929
930 template <>
931 template <>
932 RMat4d RMat4d::operator/(double _scale) const;
933
934 template <>
935 RMat4d RMat4d::operator+(const RMat4d& _rhs) const noexcept;
936
937 template <>
938 RMat4d RMat4d::operator-(const RMat4d& _rhs) const noexcept;
939
940 template <>
941 RMat4d RMat4d::operator*(const RMat4d& _rhs) const noexcept;
942
943
944 template <>
945 Vec3<double> RMat4d::operator*(const Vec3<double>& _rhs) const noexcept;
946
947 template <>
948 Vec4<double> RMat4d::operator*(const Vec4<double>& _rhs) const noexcept;
949
950
951 template <>
952 template <>
953 RMat4d& RMat4d::operator*=(double _scale) noexcept;
954
955 template <>
956 template <>
957 RMat4d& RMat4d::operator/=(double _scale);
958
959 template <>
960 RMat4d& RMat4d::operator+=(const RMat4d& _rhs) noexcept;
961
962 template <>
963 RMat4d& RMat4d::operator-=(const RMat4d& _rhs) noexcept;
964
965
966 template <>
967 RMat4d operator/(double _lhs, const RMat4d& _rhs);
968
969//}
970
971//{ Column Major
972
973 template <>
974 double CMat4d::Determinant() const noexcept;
975
976 template <>
977 CMat4d CMat4d::GetInversed() const;
978
979
980 template <>
981 CMat4d CMat4d::MakeRotation(const Quat<double>& _rot) noexcept;
982
983
984 template <>
985 template <>
986 CMat4d CMat4d::operator*(double _scale) const noexcept;
987
988 template <>
989 template <>
990 CMat4d CMat4d::operator/(double _scale) const;
991
992 template <>
993 CMat4d CMat4d::operator+(const CMat4d& _rhs) const noexcept;
994
995 template <>
996 CMat4d CMat4d::operator-(const CMat4d& _rhs) const noexcept;
997
998 template <>
999 CMat4d CMat4d::operator*(const CMat4d& _rhs) const noexcept;
1000
1001
1002 template <>
1003 Vec3<double> CMat4d::operator*(const Vec3<double>& _rhs) const noexcept;
1004
1005 template <>
1006 Vec4<double> CMat4d::operator*(const Vec4<double>& _rhs) const noexcept;
1007
1008
1009 template <>
1010 template <>
1011 CMat4d& CMat4d::operator*=(double _scale) noexcept;
1012
1013 template <>
1014 template <>
1015 CMat4d& CMat4d::operator/=(double _scale);
1016
1017 template <>
1018 CMat4d& CMat4d::operator+=(const CMat4d& _rhs) noexcept;
1019
1020 template <>
1021 CMat4d& CMat4d::operator-=(const CMat4d& _rhs) noexcept;
1022
1023
1024 template <>
1025 CMat4d operator/(double _lhs, const CMat4d& _rhs);
1026
1027//}
1028
1029#endif
1030
1032}
1033
1042#include <SA/Maths/Matrix/Matrix4.inl>
1043
1044#endif // GUARD
Maths specific config file.
Maths module Debug compatibility definition.
Equals method implementation.
Lerp algorithms implementation.
Mat3< T, major > operator*(TIn _lhs, const Mat3< T, major > &_rhs) noexcept
Scale each matrix components by _lhs.
Mat3< T, major > operator/(TIn _lhs, const Mat3< T, major > &_rhs)
Inverse Scale each matrix components by _lhs.
MatrixMajor
Matrix major enum.
Definition MatrixMajor.hpp:24
Matrix 3x3 Sapphire-Maths class.
Definition Matrix3.hpp:69
Matrix 4x4 Sapphire-Maths class.
Definition Matrix4.hpp:68
static Mat4 MakeRotation(const Quat< T > &_rot) noexcept
Make rotation matrix from quaternion.
Mat4 & operator*=(TIn _scale) noexcept
Scale each matrix component by _scale.
Mat4 & Inverse()
Inverse this matrix.
static Mat4 MakeLookAt(const Vec3< T > &_eye, const Vec3< T > &_target, const Vec3< T > &_up)
Make LookAt matrix from center, target and up.
constexpr Mat4 operator-() const noexcept
Getter of the opposite signed matrix.
T * Data() noexcept
Getter of matrix data
Mat4 & operator+=(const Mat4 &_rhs) noexcept
Add term by term matrix values.
constexpr Mat4 GetTransposed() const noexcept
Transpose this matrix.
constexpr Mat4(const Mat4< TIn, majorIn > &_other) noexcept
Value constructor from another Mat4 type.
static Mat4 MakePerspective(T _fov=T(90.0), T _aspect=T(1.0), T _near=T(0.35), T _far=T(10.0)) noexcept
Make perspective matrix .
constexpr bool Equals(const Mat4 &_other, T _threshold=std::numeric_limits< T >::epsilon()) const noexcept
Compare 2 Matrix.
Mat4 & operator-=(const Mat4 &_rhs) noexcept
Subtract term by term matrix values.
Mat4 & Transpose() noexcept
Transpose this matrix.
Mat4 operator/(TIn _scale) const
Inverse Scale each matrix component by _scale.
static const Mat4 Identity
Identity Mat4 constant.
Definition Matrix4.hpp:109
constexpr bool IsZero() const noexcept
Whether this matrix is a zero matrix.
T & At(uint32_t _index)
Getter of Value at index.
static Mat4 MakeTranslation(const Vec3< T > &_transl) noexcept
Make translation matrix from vector3.
Mat4 operator*(TIn _scale) const noexcept
Scale each matrix component by _scale.
Mat4 & operator/=(TIn _scale)
Inverse Scale each matrix component by _scale.
Mat4 operator+(const Mat4 &_rhs) const noexcept
Add term by term matrix values.
static Mat4 Lerp(const Mat4 &_start, const Mat4 &_end, float _alpha) noexcept
Clamped Lerp from _start to _end at _alpha.
static Mat4 MakeInverseView(const Vec3< T > &_eye, const Vec3< T > &_forward, const Vec3< T > &_up)
Make view matrix for camera.
constexpr Mat4(const Mat3< TIn, majorIn > &_other) noexcept
Value constructor from another Mat3 type.
static Mat4 LerpUnclamped(const Mat4 &_start, const Mat4 &_end, float _alpha) noexcept
Unclamped Lerp from _start to _end at _alpha.
static Mat4 MakeScale(const Vec3< T > &_scale) noexcept
Make scale matrix from vector3.
constexpr bool IsIdentity() const noexcept
Whether this matrix is an identity matrix.
Mat4 GetInversed() const
Inverse this matrix.
T Determinant() const noexcept
Compute the determinant of the matrix.
static const Mat4 Zero
Zero Mat4 constant.
Definition Matrix4.hpp:99
Quaternion Sapphire-Maths class.
Definition Quaternion.hpp:53
Vector 3 Sapphire-Maths class.
Definition Vector3.hpp:43
Vector 4 Sapphire-Maths class.
Definition Vector4.hpp:41