IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
array.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/math/vector.hxx>
6
7namespace ice::math
8{
9
10 template<u32 Size, typename T>
11 struct arr_t;
12
13 template<u32 Size, typename T, typename U = T>
14 constexpr auto to_arr(arr_t<Size, U> arr_val) noexcept -> arr_t<Size, T>;
15
16 template<u32 Size, typename T, typename U = T>
17 constexpr auto to_arr(vec<Size, U> vec_val) noexcept -> arr_t<Size, T>;
18
19 template<u32 Size, typename T>
20 constexpr auto to_arr(vec<Size, T> vec_val) noexcept -> arr_t<Size, T>;
21
22 template<u32 Size, typename T>
23 constexpr auto to_vec(vec<Size, T> vec_val) noexcept -> vec<Size, T>;
24
25
26 template<u32 Size, typename T>
27 struct arr_t { };
28
29 template<typename T>
30 struct arr_t<1, T>
31 {
32 using value_type = T;
33 static constexpr auto count_elements = 1;
34
35 constexpr arr_t() noexcept
36 : v{ }
37 { }
38
39 explicit constexpr arr_t(T value) noexcept
40 : v{ { value } }
41 { }
42
43 union
44 {
46 struct
47 {
48 T x;
49 };
50 };
51 };
52
53 template<typename T>
54 struct arr_t<2, T>
55 {
56 using value_type = T;
57 static constexpr auto count_elements = 2;
58
59 constexpr arr_t() noexcept
60 : v{ }
61 { }
62
63 explicit constexpr arr_t(T value) noexcept
64 : v{ value, value }
65 { }
66
67 constexpr arr_t(T x, T y) noexcept
68 : v{ x, y }
69 { }
70
71 union
72 {
74 struct
75 {
76 T x, y;
77 };
78 };
79 };
80
81 template<typename T>
82 struct arr_t<3, T>
83 {
84 using value_type = T;
85 static constexpr auto count_elements = 3;
86
87 constexpr arr_t() noexcept
88 : v{ }
89 { }
90
91 explicit constexpr arr_t(T value) noexcept
92 : v{ value, value, value }
93 { }
94
95 constexpr arr_t(T x, T y, T z) noexcept
96 : v{ x, y, z }
97 { }
98
99 union
100 {
102 struct
103 {
104 T x, y, z;
105 };
106 };
107 };
108
109 template<typename T>
110 struct arr_t<4, T>
111 {
112 using value_type = T;
113 static constexpr auto count_elements = 4;
114
115 constexpr arr_t() noexcept
116 : v{ }
117 { }
118
119 explicit constexpr arr_t(T value) noexcept
120 : v{ value, value, value, value }
121 { }
122
123 constexpr arr_t(T x, T y, T z, T w) noexcept
124 : v{ x, y, z, w }
125 { }
126
127 union
128 {
130 struct
131 {
132 T x, y, z, w;
133 };
134 };
135 };
136
137
138 template<u32 Size, typename T, typename U>
139 constexpr auto to_arr(arr_t<Size, U> arr_val) noexcept -> arr_t<Size, T>
140 {
141 arr_t<Size, T> result{ };
142 for (u32 idx = 0; idx < Size; ++idx)
143 {
144 result.v[idx] = T{ arr_val.v[idx] };
145 }
146 return result;
147 }
148
149 template<u32 Size, typename T, typename U>
150 constexpr auto to_arr(vec<Size, U> vec_val) noexcept -> arr_t<Size, T>
151 {
152 arr_t<Size, T> result{ };
153 for (u32 idx = 0; idx < Size; ++idx)
154 {
155 result.v[idx] = T{ vec_val.v[0][idx] };
156 }
157 return result;
158 }
159
160 template<u32 Size, typename T>
161 constexpr auto to_arr(vec<Size, T> vec_val) noexcept -> arr_t<Size, T>
162 {
163 return to_arr<Size, T, T>(vec_val);
164 }
165
166 template<u32 Size, typename T>
167 constexpr auto to_vec(arr_t<Size, T> arr_val) noexcept -> vec<Size, T>
168 {
169 vec<Size, T> result{ };
170 for (u32 idx = 0; idx < Size; ++idx)
171 {
172 result.v[0][idx] = arr_val.v[idx];
173 }
174 return result;
175 }
176
177 template<u32 Size, typename T>
179
183
187
191
195
196} // namespace ice::math
Definition algorithm.hxx:8
arr< 2, u32 > arr2u
Definition array.hxx:185
mat< Size, 1, T > vec
Definition vector.hxx:172
arr< 1, u32 > arr1u
Definition array.hxx:181
arr< 4, u32 > arr4u
Definition array.hxx:193
arr< 3, f32 > arr3f
Definition array.hxx:188
arr< 2, f32 > arr2f
Definition array.hxx:184
constexpr auto to_arr(arr_t< Size, U > arr_val) noexcept -> arr_t< Size, T >
Definition array.hxx:139
arr< 3, i32 > arr3i
Definition array.hxx:190
arr< 2, i32 > arr2i
Definition array.hxx:186
arr< 1, f32 > arr1f
Definition array.hxx:180
arr< 4, f32 > arr4f
Definition array.hxx:192
arr< 3, u32 > arr3u
Definition array.hxx:189
constexpr auto to_vec(vec< Size, T > vec_val) noexcept -> vec< Size, T >
arr_t< Size, T > arr
Definition array.hxx:178
arr< 1, i32 > arr1i
Definition array.hxx:182
arr< 4, i32 > arr4i
Definition array.hxx:194
std::uint32_t u32
Definition types.hxx:26
constexpr arr_t(T value) noexcept
Definition array.hxx:39
T v[count_elements]
Definition array.hxx:45
static constexpr auto count_elements
Definition array.hxx:33
T x
Definition array.hxx:48
constexpr arr_t() noexcept
Definition array.hxx:35
T value_type
Definition array.hxx:32
T v[count_elements]
Definition array.hxx:73
constexpr arr_t(T value) noexcept
Definition array.hxx:63
T value_type
Definition array.hxx:56
static constexpr auto count_elements
Definition array.hxx:57
constexpr arr_t(T x, T y) noexcept
Definition array.hxx:67
T x
Definition array.hxx:76
T y
Definition array.hxx:76
constexpr arr_t() noexcept
Definition array.hxx:59
T x
Definition array.hxx:104
T value_type
Definition array.hxx:84
constexpr arr_t(T x, T y, T z) noexcept
Definition array.hxx:95
T v[count_elements]
Definition array.hxx:101
T z
Definition array.hxx:104
constexpr arr_t() noexcept
Definition array.hxx:87
static constexpr auto count_elements
Definition array.hxx:85
constexpr arr_t(T value) noexcept
Definition array.hxx:91
T y
Definition array.hxx:104
T z
Definition array.hxx:132
constexpr arr_t() noexcept
Definition array.hxx:115
constexpr arr_t(T value) noexcept
Definition array.hxx:119
T w
Definition array.hxx:132
static constexpr auto count_elements
Definition array.hxx:113
constexpr arr_t(T x, T y, T z, T w) noexcept
Definition array.hxx:123
T v[count_elements]
Definition array.hxx:129
T x
Definition array.hxx:132
T y
Definition array.hxx:132
T value_type
Definition array.hxx:112
Definition array.hxx:27
T v[count_columns][count_rows]
Definition matrix.hxx:17