ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Vector3D.h
Go to the documentation of this file.
1/**
2 * @file Vector3D.h
3 * @brief Defines a 3D vector and various related operations.
4 *
5 * The `Vector3D` class provides a 3D vector representation along with utilities
6 * for common vector arithmetic, constraints, and geometric operations.
7 *
8 * @date 22/12/2024
9 * @version 1.0
10 * @author Coela Can't
11 */
12
13#pragma once
14
15#include "Mathematics.h"
16
17/**
18 * @class Vector3D
19 * @brief Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
20 *
21 * The `Vector3D` class defines basic 3D vector operations such as addition, subtraction,
22 * multiplication, division, dot product, cross product, and geometric queries. It also
23 * includes static functions to perform operations on multiple `Vector3D` objects without
24 * requiring an instance.
25 */
26class Vector3D {
27public:
28 float X; ///< The X-component of the 3D vector.
29 float Y; ///< The Y-component of the 3D vector.
30 float Z; ///< The Z-component of the 3D vector.
31
32 /**
33 * @brief Constructs a default `Vector3D` with X = 0, Y = 0, and Z = 0.
34 */
35 Vector3D();
36
37 /**
38 * @brief Copy constructor. Initializes this vector with the same values as another `Vector3D`.
39 * @param vector The `Vector3D` to copy from.
40 */
41 Vector3D(const Vector3D& vector);
42
43 /**
44 * @brief Constructs a `Vector3D` by copying the components of another `Vector3D` pointer.
45 * @param vector Pointer to the `Vector3D` to copy from.
46 */
47 Vector3D(const Vector3D* vector);
48
49 /**
50 * @brief Constructs a `Vector3D` using specified float components.
51 * @param X The X-component of the vector.
52 * @param Y The Y-component of the vector.
53 * @param Z The Z-component of the vector.
54 */
55 Vector3D(const float& X, const float& Y, const float& Z);
56
57 /**
58 * @brief Returns a vector with the absolute value of each component.
59 * @return A `Vector3D` where each component is `abs(X)`, `abs(Y)`, and `abs(Z)`.
60 */
61 Vector3D Absolute() const;
62
63 /**
64 * @brief Computes the squared magnitude of the vector (X^2 + Y^2 + Z^2).
65 * @return A `Vector3D` containing squared values of each component.
66 *
67 * Note: This naming follows the pattern in other classes but can be
68 * confusing; some might expect `Normal()` to return a normalized vector.
69 */
70 Vector3D Normal() const;
71
72 /**
73 * @brief Adds a scalar value to each component of the vector.
74 * @param value The scalar to add.
75 * @return A new `Vector3D` with components incremented by `value`.
76 */
77 Vector3D Add(const float& value) const;
78
79 /**
80 * @brief Subtracts a scalar value from each component of the vector.
81 * @param value The scalar to subtract.
82 * @return A new `Vector3D` with components decremented by `value`.
83 */
84 Vector3D Subtract(const float& value) const;
85
86 /**
87 * @brief Adds another `Vector3D` to this one component-wise.
88 * @param vector The vector to add.
89 * @return A new `Vector3D` representing the sum.
90 */
91 Vector3D Add(const Vector3D& vector) const;
92
93 /**
94 * @brief Subtracts another `Vector3D` from this one component-wise.
95 * @param vector The vector to subtract.
96 * @return A new `Vector3D` representing the difference.
97 */
98 Vector3D Subtract(const Vector3D& vector) const;
99
100 /**
101 * @brief Multiplies this vector by another `Vector3D` component-wise.
102 * @param vector The vector to multiply with.
103 * @return A new `Vector3D` representing the product.
104 */
105 Vector3D Multiply(const Vector3D& vector) const;
106
107 /**
108 * @brief Divides this vector by another `Vector3D` component-wise.
109 * @param vector The vector to divide by.
110 * @return A new `Vector3D` representing the quotient.
111 */
112 Vector3D Divide(const Vector3D& vector) const;
113
114 /**
115 * @brief Scales this vector by a float (each component multiplied by `scalar`).
116 * @param scalar The scaling factor.
117 * @return A new `Vector3D` scaled by `scalar`.
118 */
119 Vector3D Multiply(const float& scalar) const;
120
121 /**
122 * @brief Divides this vector by a float (each component divided by `scalar`).
123 * @param scalar The scalar divisor.
124 * @return A new `Vector3D` after the division.
125 */
126 Vector3D Divide(const float& scalar) const;
127
128 /**
129 * @brief Computes the cross product of this vector with another `Vector3D`.
130 * @param vector The other `Vector3D`.
131 * @return A new `Vector3D` representing the cross product.
132 */
133 Vector3D CrossProduct(const Vector3D& vector) const;
134
135 /**
136 * @brief Normalizes this vector such that its magnitude is 1 (if non-zero).
137 * @return A new `Vector3D` representing the unit sphere position.
138 */
139 Vector3D UnitSphere() const;
140
141 /**
142 * @brief Constrains each component of this vector between two scalar bounds.
143 * @param minimum The lower bound.
144 * @param maximum The upper bound.
145 * @return A new `Vector3D` with each component constrained between [min, max].
146 */
147 Vector3D Constrain(const float& minimum, const float& maximum) const;
148
149 /**
150 * @brief Constrains each component of this vector between the corresponding components
151 * of two other `Vector3D` objects.
152 * @param minimum The lower bound vector.
153 * @param maximum The upper bound vector.
154 * @return A new `Vector3D` with each component constrained.
155 */
156 Vector3D Constrain(const Vector3D& minimum, const Vector3D& maximum) const;
157
158 /**
159 * @brief Permutates the components of this vector using another `Vector3D` as an index/offset.
160 * @param permutation A vector whose components may dictate a specific reordering or transformation.
161 * @return A new `Vector3D` based on the permutation logic.
162 */
164
165 /**
166 * @brief Computes the magnitude (length) of this vector using the formula sqrt(X^2 + Y^2 + Z^2).
167 * @return The length of the vector.
168 */
169 float Magnitude() const;
170
171 /**
172 * @brief Computes the dot product of this vector with another `Vector3D`.
173 * @param vector The other `Vector3D`.
174 * @return The dot product result (X1*X2 + Y1*Y2 + Z1*Z2).
175 */
176 float DotProduct(const Vector3D& vector) const;
177
178 /**
179 * @brief Calculates the Euclidean distance between this vector and another `Vector3D`.
180 * @param vector The other `Vector3D`.
181 * @return The distance between the two vectors.
182 */
183 float CalculateEuclideanDistance(const Vector3D& vector) const;
184
185 /**
186 * @brief Computes the average of the highest two components of this vector.
187 *
188 * For example, if (X, Y, Z) = (3, 7, 5), the highest two are 7 and 5,
189 * whose average is 6.
190 *
191 * @return The average of the top two components.
192 */
193 float AverageHighestTwoComponents() const;
194
195 /**
196 * @brief Returns the maximum component value among X, Y, Z.
197 * @return The maximum component.
198 */
199 float Max() const;
200
201 /**
202 * @brief Returns the minimum component value among X, Y, Z.
203 * @return The minimum component.
204 */
205 float Min() const;
206
207 /**
208 * @brief Checks if this vector is equal to another `Vector3D` component-wise.
209 * @param vector The `Vector3D` to compare.
210 * @return `true` if equal, otherwise `false`.
211 */
212 bool IsEqual(const Vector3D& vector) const;
213
214 /**
215 * @brief Converts the vector to a string representation.
216 * @return A `String` in the format "(X, Y, Z)".
217 */
218 String ToString() const;
219
220
221 // --- Static function declarations ---
222
223 /**
224 * @brief Returns a new vector composed of the maximum components of `max` and `input`.
225 * @param max The first `Vector3D` to compare.
226 * @param input The second `Vector3D` to compare.
227 * @return A `Vector3D` taking the maximum of each component.
228 */
229 static Vector3D Max(const Vector3D& max, const Vector3D& input);
230
231 /**
232 * @brief Returns a new vector composed of the minimum components of `min` and `input`.
233 * @param min The first `Vector3D` to compare.
234 * @param input The second `Vector3D` to compare.
235 * @return A `Vector3D` taking the minimum of each component.
236 */
237 static Vector3D Min(const Vector3D& min, const Vector3D& input);
238
239 /**
240 * @brief Performs linear interpolation between two `Vector3D`s.
241 * @param start The start `Vector3D`.
242 * @param finish The end `Vector3D`.
243 * @param ratio A normalized factor (0 to 1).
244 * @return A new `Vector3D` representing the linear interpolation result.
245 */
246 static Vector3D LERP(const Vector3D& start, const Vector3D& finish, const float& ratio);
247
248 /**
249 * @brief Converts a `Vector3D` of degrees to radians (component-wise).
250 * @param degrees The vector in degrees.
251 * @return A `Vector3D` in radians.
252 */
254
255 /**
256 * @brief Converts a `Vector3D` of radians to degrees (component-wise).
257 * @param radians The vector in radians.
258 * @return A `Vector3D` in degrees.
259 */
261
262 /**
263 * @brief Returns the squared magnitude of a given vector (X^2 + Y^2 + Z^2) as a Vector3D.
264 * @param vector The input `Vector3D`.
265 * @return A `Vector3D` containing squared values of each component.
266 */
267 static Vector3D Normal(const Vector3D& vector);
268
269 /**
270 * @brief Adds two vectors (component-wise).
271 * @param v1 The first `Vector3D`.
272 * @param v2 The second `Vector3D`.
273 * @return A new `Vector3D` representing the sum.
274 */
275 static Vector3D Add(const Vector3D& v1, const Vector3D& v2);
276
277 /**
278 * @brief Subtracts one vector from another (component-wise).
279 * @param v1 The first `Vector3D`.
280 * @param v2 The second `Vector3D` to subtract from `v1`.
281 * @return A new `Vector3D` representing the difference.
282 */
283 static Vector3D Subtract(const Vector3D& v1, const Vector3D& v2);
284
285 /**
286 * @brief Multiplies two vectors component-wise.
287 * @param v1 The first `Vector3D`.
288 * @param v2 The second `Vector3D`.
289 * @return A new `Vector3D` representing the product.
290 */
291 static Vector3D Multiply(const Vector3D& v1, const Vector3D& v2);
292
293 /**
294 * @brief Divides two vectors component-wise.
295 * @param v1 The first `Vector3D`.
296 * @param v2 The second `Vector3D` (divisor).
297 * @return A new `Vector3D` representing the quotient.
298 */
299 static Vector3D Divide(const Vector3D& v1, const Vector3D& v2);
300
301 /**
302 * @brief Scales a `Vector3D` by a float, component-wise.
303 * @param vector The `Vector3D`.
304 * @param scalar The scaling factor.
305 * @return A new `Vector3D` scaled by `scalar`.
306 */
307 static Vector3D Multiply(const Vector3D& vector, const float& scalar);
308
309 /**
310 * @brief Scales a `Vector3D` by a float, component-wise (scalar on the left).
311 * @param scalar The scaling factor.
312 * @param vector The `Vector3D`.
313 * @return A new `Vector3D` scaled by `scalar`.
314 */
315 static Vector3D Multiply(const float& scalar, const Vector3D& vector);
316
317 /**
318 * @brief Divides a `Vector3D` by a float, component-wise.
319 * @param vector The `Vector3D`.
320 * @param scalar The scalar divisor.
321 * @return A new `Vector3D` after the division.
322 */
323 static Vector3D Divide(const Vector3D& vector, const float& scalar);
324
325 /**
326 * @brief Computes the cross product of two `Vector3D`s.
327 * @param v1 The first `Vector3D`.
328 * @param v2 The second `Vector3D`.
329 * @return A new `Vector3D` representing the cross product.
330 */
331 static Vector3D CrossProduct(const Vector3D& v1, const Vector3D& v2);
332
333 /**
334 * @brief Computes the dot product of two `Vector3D`s.
335 * @param v1 The first `Vector3D`.
336 * @param v2 The second `Vector3D`.
337 * @return The dot product (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z).
338 */
339 static float DotProduct(const Vector3D& v1, const Vector3D& v2);
340
341 /**
342 * @brief Calculates the Euclidean distance between two `Vector3D`s.
343 * @param v1 The first `Vector3D`.
344 * @param v2 The second `Vector3D`.
345 * @return The distance between `v1` and `v2`.
346 */
347 static float CalculateEuclideanDistance(const Vector3D& v1, const Vector3D& v2);
348
349 /**
350 * @brief Checks if two `Vector3D`s are equal component-wise.
351 * @param v1 The first `Vector3D`.
352 * @param v2 The second `Vector3D`.
353 * @return `true` if equal, otherwise `false`.
354 */
355 static bool IsEqual(const Vector3D& v1, const Vector3D& v2);
356
357
358 // --- Operator overloads ---
359
360 /**
361 * @brief Equality operator. Checks if two `Vector3D`s are equal (component-wise).
362 * @param vector The vector to compare with.
363 * @return `true` if equal, otherwise `false`.
364 */
365 bool operator ==(const Vector3D& vector) const;
366
367 /**
368 * @brief Inequality operator. Checks if two `Vector3D`s differ (component-wise).
369 * @param vector The vector to compare with.
370 * @return `true` if not equal, otherwise `false`.
371 */
372 bool operator !=(const Vector3D& vector) const;
373
374 /**
375 * @brief In-place addition operator. Adds another vector to this one component-wise.
376 * @param vector The right-hand side `Vector3D`.
377 * @return A reference to this `Vector3D` after addition.
378 */
380
381 /**
382 * @brief Assignment operator. Copies another `Vector3D` into this one.
383 * @param vector The vector to copy.
384 * @return A reference to this `Vector3D`.
385 */
387
388 /**
389 * @brief Addition operator. Adds two vectors component-wise.
390 * @param vector The right-hand side `Vector3D`.
391 * @return A new `Vector3D` representing the sum.
392 */
393 Vector3D operator +(const Vector3D& vector) const;
394
395 /**
396 * @brief Subtraction operator. Subtracts two vectors component-wise.
397 * @param vector The right-hand side `Vector3D`.
398 * @return A new `Vector3D` representing the difference.
399 */
400 Vector3D operator -(const Vector3D& vector) const;
401
402 /**
403 * @brief Multiplication operator. Multiplies two vectors component-wise.
404 * @param vector The right-hand side `Vector3D`.
405 * @return A new `Vector3D` representing the product.
406 */
407 Vector3D operator *(const Vector3D& vector) const;
408
409 /**
410 * @brief Division operator. Divides two vectors component-wise.
411 * @param vector The right-hand side `Vector3D` (divisor).
412 * @return A new `Vector3D` representing the quotient.
413 */
414 Vector3D operator /(const Vector3D& vector) const;
415
416 /**
417 * @brief Addition operator with a float scalar. Adds the scalar to each component.
418 * @param value The scalar to add.
419 * @return A new `Vector3D` incremented by `value`.
420 */
421 Vector3D operator +(const float& value) const;
422
423 /**
424 * @brief Subtraction operator with a float scalar. Subtracts the scalar from each component.
425 * @param value The scalar to subtract.
426 * @return A new `Vector3D` decremented by `value`.
427 */
428 Vector3D operator -(const float& value) const;
429
430 /**
431 * @brief Multiplication operator with a float scalar. Scales each component.
432 * @param value The scalar factor.
433 * @return A new `Vector3D` scaled by `value`.
434 */
435 Vector3D operator *(const float& value) const;
436
437 /**
438 * @brief Division operator with a float scalar. Divides each component by `value`.
439 * @param value The scalar divisor.
440 * @return A new `Vector3D` after division.
441 */
442 Vector3D operator /(const float& value) const;
443};
Provides a collection of mathematical utility functions and constants.
Implements a generic Kalman Filter for 1D data.
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26
Vector3D Normal() const
Computes the squared magnitude of the vector (X^2 + Y^2 + Z^2).
Definition Vector3D.cpp:20
float Max() const
Returns the maximum component value among X, Y, Z.
Definition Vector3D.cpp:176
float Min() const
Returns the minimum component value among X, Y, Z.
Definition Vector3D.cpp:180
Vector3D operator-(const Vector3D &vector) const
Subtraction operator. Subtracts two vectors component-wise.
Definition Vector3D.cpp:309
static Vector3D RadiansToDegrees(const Vector3D &radians)
Converts a Vector3D of radians to degrees (component-wise).
Definition Vector3D.cpp:220
bool operator==(const Vector3D &vector) const
Equality operator. Checks if two Vector3Ds are equal (component-wise).
Definition Vector3D.cpp:281
Vector3D CrossProduct(const Vector3D &vector) const
Computes the cross product of this vector with another Vector3D.
Definition Vector3D.cpp:98
Vector3D Divide(const Vector3D &vector) const
Divides this vector by another Vector3D component-wise.
Definition Vector3D.cpp:74
Vector3D operator+(const Vector3D &vector) const
Addition operator. Adds two vectors component-wise.
Definition Vector3D.cpp:305
float AverageHighestTwoComponents() const
Computes the average of the highest two components of this vector.
Definition Vector3D.cpp:165
Vector3D operator+=(const Vector3D &vector)
In-place addition operator. Adds another vector to this one component-wise.
Definition Vector3D.cpp:289
Vector3D operator*(const Vector3D &vector) const
Multiplication operator. Multiplies two vectors component-wise.
Definition Vector3D.cpp:313
Vector3D operator/(const Vector3D &vector) const
Division operator. Divides two vectors component-wise.
Definition Vector3D.cpp:317
static Vector3D LERP(const Vector3D &start, const Vector3D &finish, const float &ratio)
Performs linear interpolation between two Vector3Ds.
Definition Vector3D.cpp:210
String ToString() const
Converts the vector to a string representation.
Definition Vector3D.cpp:189
Vector3D operator=(const Vector3D &vector)
Assignment operator. Copies another Vector3D into this one.
Definition Vector3D.cpp:297
float Z
The Z-component of the 3D vector.
Definition Vector3D.h:30
static Vector3D DegreesToRadians(const Vector3D &degrees)
Converts a Vector3D of degrees to radians (component-wise).
Definition Vector3D.cpp:216
Vector3D UnitSphere() const
Normalizes this vector such that its magnitude is 1 (if non-zero).
Definition Vector3D.cpp:106
Vector3D Permutate(const Vector3D &permutation) const
Permutates the components of this vector using another Vector3D as an index/offset.
Definition Vector3D.cpp:136
Vector3D Multiply(const Vector3D &vector) const
Multiplies this vector by another Vector3D component-wise.
Definition Vector3D.cpp:66
Vector3D Constrain(const float &minimum, const float &maximum) const
Constrains each component of this vector between two scalar bounds.
Definition Vector3D.cpp:120
float X
The X-component of the 3D vector.
Definition Vector3D.h:28
float Y
The Y-component of the 3D vector.
Definition Vector3D.h:29
float DotProduct(const Vector3D &vector) const
Computes the dot product of this vector with another Vector3D.
Definition Vector3D.cpp:155
float CalculateEuclideanDistance(const Vector3D &vector) const
Calculates the Euclidean distance between this vector and another Vector3D.
Definition Vector3D.cpp:159
float Magnitude() const
Computes the magnitude (length) of this vector using the formula sqrt(X^2 + Y^2 + Z^2).
Definition Vector3D.cpp:151
bool IsEqual(const Vector3D &vector) const
Checks if this vector is equal to another Vector3D component-wise.
Definition Vector3D.cpp:184
Vector3D Add(const float &value) const
Adds a scalar value to each component of the vector.
Definition Vector3D.cpp:34
Vector3D Absolute() const
Returns a vector with the absolute value of each component.
Definition Vector3D.cpp:12
Vector3D()
Constructs a default Vector3D with X = 0, Y = 0, and Z = 0.
Definition Vector3D.cpp:3
Vector3D Subtract(const float &value) const
Subtracts a scalar value from each component of the vector.
Definition Vector3D.cpp:42
bool operator!=(const Vector3D &vector) const
Inequality operator. Checks if two Vector3Ds differ (component-wise).
Definition Vector3D.cpp:285