IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
matrix.hxx
Go to the documentation of this file.
1
3
4#pragma once
6
7namespace ice::math
8{
9
10 template<u32 Rows, u32 Cols, typename T>
11 struct mat
12 {
13 using value_type = T;
14 static constexpr u32 count_rows = Rows;
15 static constexpr u32 count_columns = Cols;
16
18 };
19
20 template<typename T>
21 struct mat<2, 2, T>
22 {
23 using value_type = T;
24 static constexpr u32 count_rows = 3;
25 static constexpr u32 count_columns = 3;
26
28
29 template<typename U>
30 constexpr operator mat<3, 3, U>() noexcept;
31 };
32
33
34 template<typename Mat>
35 constexpr auto identity() noexcept;
36
37
38 using mat2x2 = mat<2, 2, f32>;
39 using mat2 = mat2x2;
40
41 using mat3x3 = mat<3, 3, f32>;
42 using mat3 = mat3x3;
43
44 using mat4x4 = mat<4, 4, f32>;
45 using mat4 = mat4x4;
46
47 template<typename T>
48 template<typename U>
49 constexpr mat<2, 2, T>::operator mat<3, 3, U>() noexcept
50 {
51 mat<3, 3, U> result;
52 result.v[0][0] = v[0][0];
53 result.v[0][1] = v[0][1];
54 result.v[1][0] = v[1][0];
55 result.v[1][1] = v[1][1];
56 result.v[2][2] = 1;
57 return result;
58 }
59
60 template<typename Mat>
61 constexpr auto identity() noexcept
62 {
63 static_assert(
64 Mat::count_columns == Mat::count_rows,
65 "Only even martices can be used with 'core::math::identity()'"
66 );
67
68 Mat result{ };
69 for (u32 col = 0; col < Mat::count_columns; ++col)
70 {
71 result.v[col][col] = typename Mat::value_type{ 1 };
72 }
73 return result;
74 }
75
76
77 static constexpr auto mat2x2_identity = identity<mat<2, 2, f32>>();
78 static constexpr auto mat3x3_identity = identity<mat<3, 3, f32>>();
79 static constexpr auto mat4x4_identity = identity<mat<4, 4, f32>>();
80
81} // namespace ice::math
Definition algorithm.hxx:8
static constexpr auto mat2x2_identity
Definition matrix.hxx:77
mat< 3, 3, f32 > mat3x3
Definition matrix.hxx:41
constexpr auto identity() noexcept
Definition matrix.hxx:61
mat2x2 mat2
Definition matrix.hxx:39
static constexpr auto mat3x3_identity
Definition matrix.hxx:78
mat< 2, 2, f32 > mat2x2
Definition matrix.hxx:38
static constexpr auto mat4x4_identity
Definition matrix.hxx:79
mat< 4, 4, f32 > mat4x4
Definition matrix.hxx:44
mat3x3 mat3
Definition matrix.hxx:42
mat4x4 mat4
Definition matrix.hxx:45
mat(T(&)[Size]) -> mat< Size, 1, T >
std::uint32_t u32
Definition types.hxx:26
float f32
Definition types.hxx:16
static constexpr u32 count_rows
Definition matrix.hxx:24
static constexpr u32 count_columns
Definition matrix.hxx:25
T value_type
Definition matrix.hxx:23
T v[count_columns][count_rows]
Definition matrix.hxx:27
Definition matrix.hxx:12
T value_type
Definition matrix.hxx:13
static constexpr u32 count_rows
Definition matrix.hxx:14
static constexpr u32 count_columns
Definition matrix.hxx:15
f32 v[count_columns][count_rows]
Definition matrix.hxx:17