Topaz 5.0
Topaz Game Engine
Loading...
Searching...
No Matches
matrix.hpp
1#ifndef TOPAZ_CORE_MATRIX_HPP
2#define TOPAZ_CORE_MATRIX_HPP
3#include <array>
4#include <concepts>
5
6namespace tz
7{
13 template<typename T, int N>
14 requires std::integral<T> || std::floating_point<T>
15 struct matrix
16 {
18 static constexpr matrix<T, N> zero()
19 {
20 return matrix<T, N>::filled(T{0});
21 }
22
24 static constexpr matrix<T, N> iden()
25 {
26 auto ret = matrix<T, N>::zero();
27 for(std::size_t i = 0; i < N; i++)
28 {
29 ret.mat[i * N + i] = T{1};
30 }
31 return ret;
32 }
33
35 static constexpr matrix<T, N> filled(T t)
36 {
37 matrix<T, N> ret;
38 std::fill(ret.mat.begin(), ret.mat.end(), t);
39 return ret;
40 }
41
42 matrix<T, N>() = default;
43 matrix(const matrix<T, N>& cpy) = default;
44 matrix(matrix<T, N>&& move) = default;
45 matrix& operator=(const matrix<T, N>& rhs) = default;
46 matrix& operator=(matrix<T, N>&& rhs) = default;
47
48 const T& operator[](std::size_t idx) const;
49 T& operator[](std::size_t idx);
50
51 const T& operator()(std::size_t x, std::size_t y) const;
52 T& operator()(std::size_t x, std::size_t y);
53
58
59 // matrix-scalar
60 matrix<T, N>& operator+=(T scalar);
61 matrix<T, N> operator+(T scalar) const{auto cpy = *this; return cpy += scalar;}
62 matrix<T, N>& operator-=(T scalar);
63 matrix<T, N> operator-(T scalar) const{auto cpy = *this; return cpy -= scalar;}
64 matrix<T, N>& operator*=(T scalar);
65 matrix<T, N> operator*(T scalar) const{auto cpy = *this; return cpy *= scalar;}
66 matrix<T, N>& operator/=(T scalar);
67 matrix<T, N> operator/(T scalar) const{auto cpy = *this; return cpy /= scalar;}
68
69 // matrix-matrix
70 matrix<T, N>& operator*=(const matrix<T, N>& rhs);
71 matrix<T, N> operator*(const matrix<T, N>& rhs) const{auto cpy = *this; return cpy *= rhs;}
72
73 bool operator==(const matrix<T, N>& rhs) const = default;
74
75 private:
76 std::array<T, N*N> mat;
77 };
78
79 using m2i = matrix<int, 2>;
80 using m3i = matrix<int, 3>;
81 using m4i = matrix<int, 4>;
82 using m2u = matrix<unsigned int, 2>;
83 using m3u = matrix<unsigned int, 3>;
84 using m4u = matrix<unsigned int, 4>;
85 using m2f = matrix<float, 2>;
86 using m3f = matrix<float, 3>;
87 using m4f = matrix<float, 4>;
88 using m2d = matrix<double, 2>;
89 using m3d = matrix<double, 3>;
90 using m4d = matrix<double, 4>;
91}
92
93#endif // TOPAZ_CORE_MATRIX_HPP
Represents the generic matrix.
Definition matrix.hpp:16
static constexpr matrix< T, N > filled(T t)
Retrieve a matrix filled with the given value.
Definition matrix.hpp:35
static constexpr matrix< T, N > zero()
Retrieve a matrix filled with zeroes.
Definition matrix.hpp:18
matrix< T, N > inverse() const
Create a matrix that causes the inverse transformation. Such that the product of this and the result ...
static constexpr matrix< T, N > iden()
Retrieve an identity matrix that represents no transform.
Definition matrix.hpp:24
matrix< T, N > transpose() const
Retrieve a copy of this matrix but with its rows and columns swapped.