IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
matrix_operations.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/math/matrix.hxx>
7
9{
10
11 template<typename T>
12 constexpr void normalize3_insitu(T(&array)[4]) noexcept
13 {
14 T const square_sum = length2_sequenced(array, std::make_index_sequence<3>{});
15 if (square_sum == 0)
16 {
17 return;
18 }
19
20 f32 const sqrt_inverted = 1.f / sqrt(square_sum);
21 array[0] *= sqrt_inverted;
22 array[1] *= sqrt_inverted;
23 array[2] *= sqrt_inverted;
24 }
25
26} // namespace detail
27
28namespace ice::math
29{
30
31 template<u32 Rows1, u32 Cols1, u32 Rows2, u32 Cols2, typename T, typename U = T>
32 constexpr auto mul(
35 ) noexcept -> mat<Rows1, Cols2, T>;
36
37 template<u32 Rows, u32 Cols, typename T>
38 constexpr auto transpose(mat<Rows, Cols, T> matrix) noexcept -> mat<Cols, Rows, T>;
39
40 template<u32 Rows, u32 Cols, typename T>
41 constexpr bool inverse_insitu(mat<Rows, Cols, T>& m) noexcept
42 {
43 T inv[Rows * Cols], det;
44
45 inv[0] = m.v[0][5] * m.v[0][10] * m.v[0][15] -
46 m.v[0][5] * m.v[0][11] * m.v[0][14] -
47 m.v[0][9] * m.v[0][6] * m.v[0][15] +
48 m.v[0][9] * m.v[0][7] * m.v[0][14] +
49 m.v[0][13] * m.v[0][6] * m.v[0][11] -
50 m.v[0][13] * m.v[0][7] * m.v[0][10];
51
52 inv[4] = -m.v[0][4] * m.v[0][10] * m.v[0][15] +
53 m.v[0][4] * m.v[0][11] * m.v[0][14] +
54 m.v[0][8] * m.v[0][6] * m.v[0][15] -
55 m.v[0][8] * m.v[0][7] * m.v[0][14] -
56 m.v[0][12] * m.v[0][6] * m.v[0][11] +
57 m.v[0][12] * m.v[0][7] * m.v[0][10];
58
59 inv[8] = m.v[0][4] * m.v[0][9] * m.v[0][15] -
60 m.v[0][4] * m.v[0][11] * m.v[0][13] -
61 m.v[0][8] * m.v[0][5] * m.v[0][15] +
62 m.v[0][8] * m.v[0][7] * m.v[0][13] +
63 m.v[0][12] * m.v[0][5] * m.v[0][11] -
64 m.v[0][12] * m.v[0][7] * m.v[0][9];
65
66 inv[12] = -m.v[0][4] * m.v[0][9] * m.v[0][14] +
67 m.v[0][4] * m.v[0][10] * m.v[0][13] +
68 m.v[0][8] * m.v[0][5] * m.v[0][14] -
69 m.v[0][8] * m.v[0][6] * m.v[0][13] -
70 m.v[0][12] * m.v[0][5] * m.v[0][10] +
71 m.v[0][12] * m.v[0][6] * m.v[0][9];
72
73 inv[1] = -m.v[0][1] * m.v[0][10] * m.v[0][15] +
74 m.v[0][1] * m.v[0][11] * m.v[0][14] +
75 m.v[0][9] * m.v[0][2] * m.v[0][15] -
76 m.v[0][9] * m.v[0][3] * m.v[0][14] -
77 m.v[0][13] * m.v[0][2] * m.v[0][11] +
78 m.v[0][13] * m.v[0][3] * m.v[0][10];
79
80 inv[5] = m.v[0][0] * m.v[0][10] * m.v[0][15] -
81 m.v[0][0] * m.v[0][11] * m.v[0][14] -
82 m.v[0][8] * m.v[0][2] * m.v[0][15] +
83 m.v[0][8] * m.v[0][3] * m.v[0][14] +
84 m.v[0][12] * m.v[0][2] * m.v[0][11] -
85 m.v[0][12] * m.v[0][3] * m.v[0][10];
86
87 inv[9] = -m.v[0][0] * m.v[0][9] * m.v[0][15] +
88 m.v[0][0] * m.v[0][11] * m.v[0][13] +
89 m.v[0][8] * m.v[0][1] * m.v[0][15] -
90 m.v[0][8] * m.v[0][3] * m.v[0][13] -
91 m.v[0][12] * m.v[0][1] * m.v[0][11] +
92 m.v[0][12] * m.v[0][3] * m.v[0][9];
93
94 inv[13] = m.v[0][0] * m.v[0][9] * m.v[0][14] -
95 m.v[0][0] * m.v[0][10] * m.v[0][13] -
96 m.v[0][8] * m.v[0][1] * m.v[0][14] +
97 m.v[0][8] * m.v[0][2] * m.v[0][13] +
98 m.v[0][12] * m.v[0][1] * m.v[0][10] -
99 m.v[0][12] * m.v[0][2] * m.v[0][9];
100
101 inv[2] = m.v[0][1] * m.v[0][6] * m.v[0][15] -
102 m.v[0][1] * m.v[0][7] * m.v[0][14] -
103 m.v[0][5] * m.v[0][2] * m.v[0][15] +
104 m.v[0][5] * m.v[0][3] * m.v[0][14] +
105 m.v[0][13] * m.v[0][2] * m.v[0][7] -
106 m.v[0][13] * m.v[0][3] * m.v[0][6];
107
108 inv[6] = -m.v[0][0] * m.v[0][6] * m.v[0][15] +
109 m.v[0][0] * m.v[0][7] * m.v[0][14] +
110 m.v[0][4] * m.v[0][2] * m.v[0][15] -
111 m.v[0][4] * m.v[0][3] * m.v[0][14] -
112 m.v[0][12] * m.v[0][2] * m.v[0][7] +
113 m.v[0][12] * m.v[0][3] * m.v[0][6];
114
115 inv[10] = m.v[0][0] * m.v[0][5] * m.v[0][15] -
116 m.v[0][0] * m.v[0][7] * m.v[0][13] -
117 m.v[0][4] * m.v[0][1] * m.v[0][15] +
118 m.v[0][4] * m.v[0][3] * m.v[0][13] +
119 m.v[0][12] * m.v[0][1] * m.v[0][7] -
120 m.v[0][12] * m.v[0][3] * m.v[0][5];
121
122 inv[14] = -m.v[0][0] * m.v[0][5] * m.v[0][14] +
123 m.v[0][0] * m.v[0][6] * m.v[0][13] +
124 m.v[0][4] * m.v[0][1] * m.v[0][14] -
125 m.v[0][4] * m.v[0][2] * m.v[0][13] -
126 m.v[0][12] * m.v[0][1] * m.v[0][6] +
127 m.v[0][12] * m.v[0][2] * m.v[0][5];
128
129 inv[3] = -m.v[0][1] * m.v[0][6] * m.v[0][11] +
130 m.v[0][1] * m.v[0][7] * m.v[0][10] +
131 m.v[0][5] * m.v[0][2] * m.v[0][11] -
132 m.v[0][5] * m.v[0][3] * m.v[0][10] -
133 m.v[0][9] * m.v[0][2] * m.v[0][7] +
134 m.v[0][9] * m.v[0][3] * m.v[0][6];
135
136 inv[7] = m.v[0][0] * m.v[0][6] * m.v[0][11] -
137 m.v[0][0] * m.v[0][7] * m.v[0][10] -
138 m.v[0][4] * m.v[0][2] * m.v[0][11] +
139 m.v[0][4] * m.v[0][3] * m.v[0][10] +
140 m.v[0][8] * m.v[0][2] * m.v[0][7] -
141 m.v[0][8] * m.v[0][3] * m.v[0][6];
142
143 inv[11] = -m.v[0][0] * m.v[0][5] * m.v[0][11] +
144 m.v[0][0] * m.v[0][7] * m.v[0][9] +
145 m.v[0][4] * m.v[0][1] * m.v[0][11] -
146 m.v[0][4] * m.v[0][3] * m.v[0][9] -
147 m.v[0][8] * m.v[0][1] * m.v[0][7] +
148 m.v[0][8] * m.v[0][3] * m.v[0][5];
149
150 inv[15] = m.v[0][0] * m.v[0][5] * m.v[0][10] -
151 m.v[0][0] * m.v[0][6] * m.v[0][9] -
152 m.v[0][4] * m.v[0][1] * m.v[0][10] +
153 m.v[0][4] * m.v[0][2] * m.v[0][9] +
154 m.v[0][8] * m.v[0][1] * m.v[0][6] -
155 m.v[0][8] * m.v[0][2] * m.v[0][5];
156
157 det = m.v[0][0] * inv[0] + m.v[0][1] * inv[4] + m.v[0][2] * inv[8] + m.v[0][3] * inv[12];
158 if (det == 0)
159 {
160 return false;
161 }
162
163 det = 1.0f / det;
164 for (u32 i = 0; i < 16; i++)
165 {
166 m.v[i >> 2][i & 0x3] = inv[i] * det;
167 }
168
169 return true;
170 }
171
172 template<typename T>
173 constexpr auto ortho_normalize(mat<4, 4, T> matrix) noexcept -> mat<4, 4, T>;
174
175
176 template<u32 Rows1, u32 Cols1, u32 Rows2, u32 Cols2, typename T, typename U>
177 constexpr auto mul(
180 ) noexcept -> mat<Rows1, Cols2, T>
181 {
182 static_assert(
183 Cols1 == Rows2,
184 "[ left_matrix.count_columns != right_matrix.count_rows ] Cannot multiply given matrices."
185 );
186
187 if constexpr (Cols1 == Rows2)
188 {
189 mat<Rows1, Cols2, T> result{};
190 for (u32 col = 0; col < Cols2; ++col)
191 {
192 for (u32 row = 0; row < Rows1; ++row)
193 {
194 for (u32 i = 0; i < Rows2; ++i)
195 {
196 result.v[col][row] += left.v[i][row] * right.v[col][i];
197 }
198 }
199
200 }
201 return result;
202 }
203 else
204 {
205 return { };
206 }
207 }
208
209 template<u32 Rows, u32 Cols, typename T>
210 constexpr auto transpose(mat<Rows, Cols, T> matrix) noexcept -> mat<Cols, Rows, T>
211 {
212 mat<Cols, Rows, T> result{ };
213 for (u32 col = 0; col < Cols; ++col)
214 {
215 for (u32 row = 0; row < Rows; ++row)
216 {
217 result.v[row][col] = matrix.v[col][row];
218 }
219 }
220 return result;
221 }
222
223 template<typename T>
224 constexpr auto ortho_normalize(mat<4, 4, T> matrix) noexcept -> mat<4, 4, T>
225 {
229 return matrix;
230 }
231
232} // namespace ice::math
Definition matrix_operations.hxx:9
constexpr auto length2_sequenced(ice::math::vec< Size, T > vector, std::index_sequence< Values... >) noexcept -> T
Definition vector_operations.hxx:12
constexpr void normalize3_insitu(T(&array)[4]) noexcept
Definition matrix_operations.hxx:12
Definition algorithm.hxx:8
constexpr auto mul(U first, Args... args) noexcept
Definition algorithm.hxx:38
constexpr bool inverse_insitu(mat< Rows, Cols, T > &m) noexcept
Definition matrix_operations.hxx:41
constexpr auto ortho_normalize(mat< 4, 4, T > matrix) noexcept -> mat< 4, 4, T >
Definition matrix_operations.hxx:224
constexpr auto sqrt(f32 val) noexcept -> f32
Definition common.hxx:93
mat(T(&)[Size]) -> mat< Size, 1, T >
constexpr auto transpose(mat< Rows, Cols, T > matrix) noexcept -> mat< Cols, Rows, T >
Definition matrix_operations.hxx:210
std::uint32_t u32
Definition types.hxx:26
float f32
Definition types.hxx:16
Definition matrix.hxx:12
T v[count_columns][count_rows]
Definition matrix.hxx:17