EHS
Vec4.h
Go to the documentation of this file.
1#pragma once
2
3#include "Types.h"
4#include "UTF.h"
5#include "Str.h"
6#include "Math.h"
7#include "Vec2.h"
8#include "Vec3.h"
9#include "Log.h"
10
11namespace ehs
12{
13 template<typename T>
14 class Vec4
15 {
16 public:
17 T x;
18 T y;
19 T z;
20 T w;
21
22 Vec4(const T scalar = 0)
23 : x(scalar), y(scalar), z(scalar), w(scalar)
24 {
25 }
26
27 Vec4(const T x, const T y, const T z, const T w)
28 : x(x), y(y), z(z), w(w)
29 {
30 }
31
32 template<typename C>
33 Vec4(const Vec2<C>& vec, const T z = 0, const T w = 0)
34 : x((T)vec.x), y((T)vec.y), z(z), w(w)
35 {
36 }
37
38 template<typename C>
39 Vec4(const Vec3<C>& vec, const T w = 1)
40 : x((T)vec.x), y((T)vec.y), z((T)vec.z), w(w)
41 {
42 }
43
44 template<typename C>
45 Vec4(const Vec4<C>& vec)
46 : x((T)vec.x), y((T)vec.y), z((T)vec.z), w((T)vec.w)
47 {
48 }
49
50 template<typename C>
52 {
53 x = (T)vec.x;
54 y = (T)vec.y;
55 z = (T)0;
56 w = (T)0;
57
58 return*this;
59 }
60
61 template<typename C>
63 {
64 x = (T)vec.x;
65 y = (T)vec.y;
66 z = (T)vec.z;
67 w = (T)0;
68
69 return*this;
70 }
71
72 template<typename C>
74 {
75 x = (T)vec.x;
76 y = (T)vec.y;
77 z = (T)vec.z;
78 w = (T)vec.w;
79
80 return*this;
81 }
82
83 bool operator==(const Vec4<T>& vec)
84 {
85 return Math::ComCmp(x, vec.x) && Math::ComCmp(y, vec.y) && Math::ComCmp(z, vec.z) && Math::ComCmp(w, vec.w);
86 }
87
88 bool operator!=(const Vec4<T>& vec)
89 {
90 return !Math::ComCmp(z, vec.z) || !Math::ComCmp(y, vec.y) || !Math::ComCmp(z, vec.z) || !Math::ComCmp(w, vec.w);
91 }
92
94 {
95 x += vec.x;
96 y += vec.y;
97 z += vec.z;
98 w += vec.w;
99
100 return *this;
101 }
102
104 {
105 Vec4<T> tmp;
106
107 tmp.x = x + vec.x;
108 tmp.y = y + vec.y;
109 tmp.z = z + vec.z;
110 tmp.w = w + vec.w;
111
112 return tmp;
113 }
114
115 Vec4<T>& operator+=(const T scalar)
116 {
117 x += scalar;
118 y += scalar;
119 z += scalar;
120 w += scalar;
121
122 return *this;
123 }
124
125 Vec4<T> operator+(const T scalar)
126 {
127 Vec4<T> tmp;
128
129 tmp.x = x + scalar;
130 tmp.y = y + scalar;
131 tmp.z = z + scalar;
132 tmp.w = w + scalar;
133
134 return tmp;
135 }
136
138 {
139 x -= vec.x;
140 y -= vec.y;
141 z -= vec.z;
142 w -= vec.w;
143
144 return *this;
145 }
146
148 {
149 Vec4<T> tmp;
150
151 tmp.x = x - vec.x;
152 tmp.y = y - vec.y;
153 tmp.z = z - vec.z;
154 tmp.w = w - vec.w;
155
156 return tmp;
157 }
158
159 Vec4<T>& operator-=(const T scalar)
160 {
161 x -= scalar;
162 y -= scalar;
163 z -= scalar;
164 w -= scalar;
165
166 return *this;
167 }
168
169 Vec4<T> operator-(const T scalar)
170 {
171 Vec4<T> tmp;
172
173 tmp.x = x - scalar;
174 tmp.y = y - scalar;
175 tmp.z = z - scalar;
176 tmp.w = w - scalar;
177
178 return tmp;
179 }
180
182 {
183 x *= vec.x;
184 y *= vec.y;
185 z *= vec.z;
186 w *= vec.w;
187
188 return *this;
189 }
190
192 {
193 Vec4<T> tmp;
194
195 tmp.x = x * vec.x;
196 tmp.y = y * vec.y;
197 tmp.z = z * vec.z;
198 tmp.w = w * vec.w;
199
200 return tmp;
201 }
202
203 Vec4<T>& operator*=(const T scalar)
204 {
205 x *= scalar;
206 y *= scalar;
207 z *= scalar;
208 w *= scalar;
209
210 return *this;
211 }
212
213 Vec4<T> operator*(const T scalar)
214 {
215 Vec4<T> tmp;
216
217 tmp.x = x * scalar;
218 tmp.y = y * scalar;
219 tmp.z = z * scalar;
220 tmp.w = w * scalar;
221
222 return tmp;
223 }
224
226 {
227 x /= vec.x;
228 y /= vec.y;
229 z /= vec.z;
230 w /= vec.w;
231
232 return *this;
233 }
234
236 {
237 Vec4<T> tmp;
238
239 tmp.x = x / vec.x;
240 tmp.y = y / vec.y;
241 tmp.z = z / vec.z;
242 tmp.w = w / vec.w;
243
244 return tmp;
245 }
246
247 Vec4<T>& operator/=(const T scalar)
248 {
249 x /= scalar;
250 y /= scalar;
251 z /= scalar;
252 w /= scalar;
253
254 return *this;
255 }
256
257 Vec4<T> operator/(const T scalar)
258 {
259 Vec4<T> tmp;
260
261 tmp.x = x / scalar;
262 tmp.y = y / scalar;
263 tmp.z = z / scalar;
264 tmp.w = w / scalar;
265
266 return tmp;
267 }
268
269 T operator[](const UInt_64 index) const
270 {
271 switch (index)
272 {
273 case 0:
274 return x;
275 case 1:
276 return y;
277 case 2:
278 return z;
279 case 3:
280 return w;
281 default:
282 EHS_LOG_INT(LogType::ERR, 0, "Index of, \"" + Str_8::FromNum(index) + "\" is out of range for a Vector 4.");
283 return x;
284 }
285 }
286
287 T& operator[](const UInt_64 index)
288 {
289 switch (index)
290 {
291 case 0:
292 return x;
293 case 1:
294 return y;
295 case 2:
296 return z;
297 case 3:
298 return w;
299 default:
300 EHS_LOG_INT(LogType::ERR, 0, "Index of, \"" + Str_8::FromNum(index) + "\" is out of range for a Vector 4.");
301 return x;
302 }
303 }
304
305 operator Vec3<T>()
306 {
307 return Vec3<T>(x, y, z);
308 }
309
310 operator Vec2<T>()
311 {
312 return Vec2<T>(x, y);
313 }
314
315 T GetDotProduct(const Vec4<T>& vec) const
316 {
317 return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
318 }
319
320 T GetMagnitude() const
321 {
322 return Math::Sqrt(x * x + y * y + z * z + w * w);
323 }
324
326 {
327 return x * x + y * y + z * z + w * w;
328 }
329 };
330
345}
#define EHS_LOG_INT(type, code, msg)
Definition: Log.h:137
static bool ComCmp(float a, float b)
Combined absolute and relative tolerance comparison for single precision floats.
Definition: Math.cpp:26
static T Sqrt(const T from)
Definition: Math.h:238
static Str< Char_8, UInt_64 > FromNum(const SInt_64 num)
Definition: Str.h:1399
Definition: Vec2.h:13
T y
Definition: Vec2.h:16
T x
Definition: Vec2.h:15
Definition: Vec3.h:14
T x
Definition: Vec3.h:16
T z
Definition: Vec3.h:18
T y
Definition: Vec3.h:17
Definition: Vec4.h:15
Vec4< T > & operator-=(const T scalar)
Definition: Vec4.h:159
T GetMagnitude2() const
Definition: Vec4.h:325
T GetMagnitude() const
Definition: Vec4.h:320
Vec4< T > operator-(const Vec4< T > &vec)
Definition: Vec4.h:147
Vec4< T > operator+(const T scalar)
Definition: Vec4.h:125
T & operator[](const UInt_64 index)
Definition: Vec4.h:287
Vec4< T > & operator-=(const Vec4< T > &vec)
Definition: Vec4.h:137
Vec4< T > & operator=(const Vec3< C > &vec)
Definition: Vec4.h:62
Vec4(const Vec4< C > &vec)
Definition: Vec4.h:45
Vec4(const Vec3< C > &vec, const T w=1)
Definition: Vec4.h:39
T z
Definition: Vec4.h:19
Vec4< T > operator/(const Vec4< T > &vec)
Definition: Vec4.h:235
T operator[](const UInt_64 index) const
Definition: Vec4.h:269
T y
Definition: Vec4.h:18
bool operator==(const Vec4< T > &vec)
Definition: Vec4.h:83
T GetDotProduct(const Vec4< T > &vec) const
Definition: Vec4.h:315
T x
Definition: Vec4.h:17
Vec4< T > & operator*=(const T scalar)
Definition: Vec4.h:203
Vec4< T > & operator=(const Vec2< C > &vec)
Definition: Vec4.h:51
Vec4< T > operator+(const Vec4< T > &vec)
Definition: Vec4.h:103
Vec4< T > operator-(const T scalar)
Definition: Vec4.h:169
Vec4< T > & operator*=(const Vec4< T > &vec)
Definition: Vec4.h:181
Vec4< T > & operator=(const Vec4< C > &vec)
Definition: Vec4.h:73
Vec4< T > & operator/=(const T scalar)
Definition: Vec4.h:247
Vec4(const T scalar=0)
Definition: Vec4.h:22
Vec4< T > & operator+=(const Vec4< T > &vec)
Definition: Vec4.h:93
bool operator!=(const Vec4< T > &vec)
Definition: Vec4.h:88
Vec4< T > operator*(const T scalar)
Definition: Vec4.h:213
Vec4< T > operator*(const Vec4< T > &vec)
Definition: Vec4.h:191
T w
Definition: Vec4.h:20
Vec4(const T x, const T y, const T z, const T w)
Definition: Vec4.h:27
Vec4< T > operator/(const T scalar)
Definition: Vec4.h:257
Vec4< T > & operator+=(const T scalar)
Definition: Vec4.h:115
Vec4(const Vec2< C > &vec, const T z=0, const T w=0)
Definition: Vec4.h:33
Vec4< T > & operator/=(const Vec4< T > &vec)
Definition: Vec4.h:225
Definition: Anchor.h:6
Vec4< UInt_16 > Vec4_u16
Definition: Vec4.h:337
Vec4< SInt_16 > Vec4_s16
Definition: Vec4.h:338
Vec4< float > Vec4_f
Definition: Vec4.h:343
Vec4< SInt_64 > Vec4_s64
Definition: Vec4.h:332
Vec4< UInt_8 > Vec4_u8
Definition: Vec4.h:340
Vec4< Int_32 > Vec4_32
Definition: Vec4.h:336
Vec4< Int_8 > Vec4_8
Definition: Vec4.h:342
Vec4< SInt_32 > Vec4_s32
Definition: Vec4.h:335
Vec4< UInt_64 > Vec4_u64
Definition: Vec4.h:331
Vec4< double > Vec4_d
Definition: Vec4.h:344
Vec4< UInt_32 > Vec4_u32
Definition: Vec4.h:334
Vec4< Int_64 > Vec4_64
Definition: Vec4.h:333
Vec4< SInt_8 > Vec4_s8
Definition: Vec4.h:341
Vec4< Int_16 > Vec4_16
Definition: Vec4.h:339