sculpto
vec4.h
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file vec4.h
3 * \brief Euclidean 4D vector implementation module.
4 *
5 * \author Sabitov Kirill
6 * \date 19 June 2022
7 *********************************************************************/
8
9#pragma once
10
11#include "math_common.h"
12
13/*!*
14 * 4D vector to 4 numbers convertion function.
15 *
16 * \param v - vector to convert.
17 * \return 4 numbers.
18 */
19#define SCL_VEC_XYZW(v) SCL_VEC_XYZ(v), (v).GetW()
20
21namespace scl::math
22{
23 // Euclidean 4D vector class.
24 template <typename T>
25 class vec4
26 {
27 public:
29 T X { 0 }, Y { 0 }, Z { 0 }, W { 0 };
30
31 public:
34 T SetX(T X) { this->X = X; }
36 T SetY(T Y) { this->Y = Y; }
38 T SetZ(T Z) { this->Z = Z; }
40 T SetW(T W) { this->W = W; }
42 T GetX() const { return X; }
44 T GetY() const { return Y; }
46 T GetZ() const { return Z; }
48 T GetW() const { return W; }
49
50 public:
52 vec4() = default;
53
54 /*!*
55 * Vector constructor by one scalar.
56 * All corinates would be set to its value.
57 *
58 * \param A - scalar value of all cordinates.
59 */
60 explicit vec4(T A) : X(A), Y(A), Z(A), W(A) {}
61
62 /*!*
63 * Vector constructor by three coordinates.
64 *
65 * \param X, Y - coordiantes of creating vector.
66 */
67 vec4(T X, T Y, T Z, T W) : X(X), Y(Y), Z(Z), W(W) {}
68
69 /*!*
70 * Vector constructor by 3D vector and additional component.
71 *
72 * \param V - vector to get X, Y and Z coordinates
73 * \param W - addition vector component
74 */
75 explicit vec4(const vec3<T> &V, T W) : X(V.GetX()), Y(V.GetY()), Z(V.GetZ()), W(W) {}
76
77 /*!*
78 * Vector constructor by 3D vector and additional component.
79 *
80 * \param X - addition vector component
81 * \param V - vector to get X, Y and Z coordinates
82 */
83 explicit vec4(T X, const vec3<T> &V) : X(X), Y(V.GetX()), Z(V.GetY()), W(V.GetZ()) {}
84
85 /*!*
86 * Vector copy constructor.
87 *
88 * \param Other - vector to copy from.
89 */
90 vec4(const vec4 &Other)
91 {
92 if (this != &Other)
93 {
94 X = Other.X;
95 Y = Other.Y;
96 Z = Other.Z;
97 W = Other.W;
98 }
99 }
100
101 /*!*
102 * Vector assigments operator overloading.
103 *
104 * \param Other - vector to copy from.
105 * \return self reference.
106 */
107 vec4 &operator=(const vec4 &Other)
108 {
109 if (this != &Other)
110 {
111 X = Other.X;
112 Y = Other.Y;
113 Z = Other.Z;
114 W = Other.W;
115 }
116 return *this;
117 }
118
119 public:
120 /*!*
121 * Vector with all components set to 0 creation function.
122 *
123 * \param None.
124 * \return zero vector.
125 */
126 static vec4 Zero() { return vec4(0); }
127
128 /*!*
129 * Vector with all components set to random value in range [Min;Max] creation function.
130 *
131 * \param min - lower bound of random number.
132 * \param Max - upper bound of random number.
133 * \return random number in range [Min; Max].
134 */
135 static vec4 Rnd(T Min = 0, T Max = 1) { return vec4(::scl::math::Rnd(Min, Max)); }
136
137 /*!*
138 * Vector with compund of minimums of specified vectors components.
139 *
140 * \param A, B - vectors to take minimums of components.
141 * \return minimum vector.
142 */
143 static vec4 Min(const vec4 &A, const vec4 &B) { return vec4(math::Min(A.X, B.X), math::Min(A.Y, B.Y), math::Min(A.Z, B.Z), math::Min(A.W, B.W)); }
144
145 /*!*
146 * Vector with compund of maximums of specified vectors components.
147 *
148 * \param A, B - vectors to take maximums of components.
149 * \return minimum vector.
150 */
151 static vec4 Max(const vec4 &A, const vec4 &B) { return vec4(math::Max(A.X, B.X), math::Max(A.Y, B.Y), math::Max(A.Z, B.Z), math::Max(A.W, B.W)); }
152
153 public:
154 /*!*
155 * Vector compare function.
156 *
157 * \param Other - vector to compare with.
158 * \return is vectors equal flag.
159 */
160 bool operator==(const vec4 &Other)
161 {
162 return X == Other.X && Y == Other.Y && Z == Other.Z && W == Other.W;
163 }
164
165 /*!*
166 * Getting negative vector operation.
167 *
168 * \param None.
169 * \return negative vector.
170 */
171 const vec4 operator-() const
172 {
173 return vec4(-X, -Y, -Z, -W);
174 }
175
176 /*!*
177 * Vectors addition operator overloading.
178 *
179 * \param Other - vector to add.
180 * \return vector with added coordinates.
181 */
182 const vec4 operator+(const vec4 &Other) const
183 {
184 return vec4(X + Other.X, Y + Other.Y, Z + Other.Z, W + Other.W);
185 }
186
187 /*!*
188 * Vectors addition with assigments operator overlaoding.
189 *
190 * \param Other - vector to add.
191 * \return self reference
192 */
193 const vec4 &operator+=(const vec4 &Other)
194 {
195 X += Other.X;
196 Y += Other.Y;
197 Z += Other.Z;
198 W += Other.W;
199 return *this;
200 }
201
202 /*!*
203 * Vectors addition operator overloading.
204 *
205 * \param Scalar - scalar value to add to all vetcors components
206 * \return vector with added coordinates.
207 */
208 const vec4 operator+(float Scalar) const
209 {
210 return vec4(X + Scalar, Y + Scalar, Z + Scalar, W + Scalar);
211 }
212
213 /*!*
214 * Vectors addition with assigments operator overlaoding.
215 *
216 * \param Other - vector to add.
217 * \return self reference
218 */
219 const vec4 &operator+=(float Scalar)
220 {
221 X += Scalar;
222 Y += Scalar;
223 Z += Scalar;
224 W += Scalar;
225 return *this;
226 }
227
228 /*!*
229 * Vectors subtraction operator overloading.
230 *
231 * \param Other - vector to subtract.
232 * \return vector with subtract coordinates.
233 */
234 const vec4 operator-(const vec4 &Other) const
235 {
236 return vec4(X - Other.X, Y - Other.Y, Z - Other.Z, W - Other.W);
237 }
238
239 /*!*
240 * Vectors subtraction with assigments operator overlaoding.
241 *
242 * \param Other - vector to subtract.
243 * \return self reference.
244 */
245 const vec4 &operator-=(const vec4 &Other)
246 {
247 X -= Other.X;
248 Y -= Other.Y;
249 Z -= Other.Z;
250 W -= Other.W;
251 return *this;
252 }
253
254 /*!*
255 * Vectors subtraction operator overloading.
256 *
257 * \param Scalar - scalar value to add to all vetcors components
258 * \return vector with added coordinates.
259 */
260 const vec4 operator-(float Scalar) const
261 {
262 return vec4(X - Scalar,
263 Y - Scalar,
264 Z - Scalar,
265 W - Scalar);
266 }
267
268 /*!*
269 * Vectors subtraction with assigments operator overlaoding.
270 *
271 * \param Other - vector to add.
272 * \return self reference
273 */
274 const vec4 &operator-=(float Scalar)
275 {
276 X -= Scalar;
277 Y -= Scalar;
278 Z -= Scalar;
279 W -= Scalar;
280 return *this;
281 }
282
283 /*!*
284 * Vectors multiplying operator overloading.
285 *
286 * \param Other - vector to multiply.
287 * \return vector with multiplied coordinates.
288 */
289 const vec4 operator*(const vec4 &Other) const
290 {
291 return vec4(X * Other.X, Y * Other.Y, Z * Other.Z, W * Other.W);
292 }
293
294 /*!*
295 * Vectors multiplying with assigments operator overlaoding.
296 *
297 * \param Other - vector to multiply.
298 * \return self reference.
299 */
300 const vec4 &operator*=(const vec4 &Other)
301 {
302 X *= Other.X;
303 Y *= Other.Y;
304 Z *= Other.Z;
305 W *= Other.W;
306 return *this;
307 }
308
309 /*!*
310 * Vectors multiplying operator overloading.
311 *
312 * \param Scalar - scalar value to add to all vetcors components
313 * \return vector with added coordinates.
314 */
315 const vec4 operator*(float Scalar) const
316 {
317 return vec4(X * Scalar,
318 Y * Scalar,
319 Z * Scalar,
320 W * Scalar);
321 }
322
323 /*!*
324 * Vectors multiplying with assigments operator overlaoding.
325 *
326 * \param Other - vector to add.
327 * \return self reference
328 */
329 const vec4 &operator*=(float Scalar)
330 {
331 X *= Scalar;
332 Y *= Scalar;
333 Z *= Scalar;
334 W *= Scalar;
335 return *this;
336 }
337
338 /*!*
339 * Vectors dividing operator overloading.
340 *
341 * \param Other - vector to devide.
342 * \return vector with devided coordinates.
343 */
344 const vec4 operator/(const vec4 &Other) const
345 {
346 return vec4(X / Other.X, Y / Other.Y, Z / Other.Z, W / Other.W);
347 }
348
349 /*!*
350 * Vectors dividing with assigments operator overlaoding.
351 *
352 * \param Other - vector to devide.
353 * \return self reference.
354 */
355 const vec4 &operator/=(const vec4 &Other)
356 {
357 X /= Other.X;
358 Y /= Other.Y;
359 Z /= Other.Z;
360 W /= Other.W;
361 return *this;
362 }
363
364 /*!*
365 * Vectors dividing operator overloading.
366 *
367 * \param Scalar - scalar value to add to all vetcors components
368 * \return vector with added coordinates.
369 */
370 const vec4 operator/(float Scalar) const
371 {
372 return vec4(X / Scalar,
373 Y / Scalar,
374 Z / Scalar,
375 W / Scalar);
376 }
377
378 /*!*
379 * Vectors dividing with assigments operator overlaoding.
380 *
381 * \param Other - vector to add.
382 * \return self reference
383 */
384 const vec4 &operator/=(float Scalar)
385 {
386 X /= Scalar;
387 Y /= Scalar;
388 Z /= Scalar;
389 W /= Scalar;
390 return *this;
391 }
392
393 /*!*
394 * Getting pointer to first component of vector operator.
395 * Need to pass vector to shader.
396 *
397 * \return pointer to first component of vector.
398 */
399 operator T *()
400 {
401 return &X;
402 }
403
404 /*!*
405 * Getting coordinate operator overloading.
406 *
407 * \param Index - coordinate index.
408 * \return vector cartesian coordinate.
409 */
410 T &operator [](int Index)
411 {
412 switch (Index)
413 {
414 case 0: return X;
415 case 1: return Y;
416 case 2: return Z;
417
418 default:
419 case 3: return Z;
420 }
421 }
422
423 /*!*
424 * Getting coordinate operator overloading.
425 *
426 * \param Index - coordinate index.
427 * \return vector cartesian coordinate.
428 */
429 T operator [](int Index) const
430 {
431 switch (Index)
432 {
433 case 0: return X;
434 case 1: return Y;
435 case 2: return Z;
436
437 default:
438 case 3: return W;
439 }
440 }
441 };
442}
T GetZ() const
Definition: vec4.h:46
const vec4 operator*(const vec4 &Other) const
Definition: vec4.h:289
vec4(T A)
Definition: vec4.h:60
T GetY() const
Definition: vec4.h:44
const vec4 & operator/=(const vec4 &Other)
Definition: vec4.h:355
const vec4 & operator+=(const vec4 &Other)
Definition: vec4.h:193
const vec4 operator/(float Scalar) const
Definition: vec4.h:370
const vec4 & operator/=(float Scalar)
Definition: vec4.h:384
const vec4 & operator-=(const vec4 &Other)
Definition: vec4.h:245
vec4(const vec3< T > &V, T W)
Definition: vec4.h:75
const vec4 & operator*=(const vec4 &Other)
Definition: vec4.h:300
bool operator==(const vec4 &Other)
Definition: vec4.h:160
static vec4 Max(const vec4 &A, const vec4 &B)
Definition: vec4.h:151
static vec4 Zero()
Definition: vec4.h:126
const vec4 & operator-=(float Scalar)
Definition: vec4.h:274
vec4(const vec4 &Other)
Definition: vec4.h:90
T SetZ(T Z)
Definition: vec4.h:38
vec4()=default
T SetX(T X)
Definition: vec4.h:34
const vec4 operator-() const
Definition: vec4.h:171
static vec4 Min(const vec4 &A, const vec4 &B)
Definition: vec4.h:143
vec4(T X, T Y, T Z, T W)
Definition: vec4.h:67
vec4(T X, const vec3< T > &V)
Definition: vec4.h:83
const vec4 operator+(const vec4 &Other) const
Definition: vec4.h:182
T SetW(T W)
Definition: vec4.h:40
const vec4 operator*(float Scalar) const
Definition: vec4.h:315
T GetW() const
Definition: vec4.h:48
const vec4 operator+(float Scalar) const
Definition: vec4.h:208
const vec4 operator-(const vec4 &Other) const
Definition: vec4.h:234
const vec4 & operator+=(float Scalar)
Definition: vec4.h:219
static vec4 Rnd(T Min=0, T Max=1)
Definition: vec4.h:135
const vec4 operator/(const vec4 &Other) const
Definition: vec4.h:344
T SetY(T Y)
Definition: vec4.h:36
vec4 & operator=(const vec4 &Other)
Definition: vec4.h:107
const vec4 operator-(float Scalar) const
Definition: vec4.h:260
const vec4 & operator*=(float Scalar)
Definition: vec4.h:329
T GetX() const
Definition: vec4.h:42
T & operator[](int Index)
Definition: vec4.h:410
T Max(T Num1, T Num2)
Definition: math_common.h:61
T Min(T Num1, T Num2)
Definition: math_common.h:49
T Rnd(T Min=0, T Max=1)
Definition: math_common.h:116