ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Vector3D.cpp
Go to the documentation of this file.
1#include "Vector3D.h"
2
3Vector3D::Vector3D() : X(0.0f), Y(0.0f), Z(0.0f) {}
4
5Vector3D::Vector3D(const Vector3D& vector) : X(vector.X), Y(vector.Y), Z(vector.Z) {}
6
7Vector3D::Vector3D(const Vector3D* vector) : X(vector->X), Y(vector->Y), Z(vector->Z) {}
8
9Vector3D::Vector3D(const float& X, const float& Y, const float& Z) : X(X), Y(Y), Z(Z) {}
10
11// Implementations of non-static member functions
13 return Vector3D{
14 fabsf(this->X),
15 fabsf(this->Y),
16 fabsf(this->Z)
17 };
18}
19
21 float magn = Magnitude();
22
24 return (*this);
25 }
27 return Multiply(3.40282e+038f);
28 }
29 else{
30 return Multiply(1.0f / magn);
31 }
32}
33
34Vector3D Vector3D::Add(const float& value) const {
35 return Vector3D{
36 this->X + value,
37 this->Y + value,
38 this->Z + value
39 };
40}
41
42Vector3D Vector3D::Subtract(const float& value) const {
43 return Vector3D {
44 this->X - value,
45 this->Y - value,
46 this->Z - value
47 };
48}
49
51 return Vector3D{
52 this->X + vector.X,
53 this->Y + vector.Y,
54 this->Z + vector.Z
55 };
56}
57
59 return Vector3D {
60 this->X - vector.X,
61 this->Y - vector.Y,
62 this->Z - vector.Z
63 };
64}
65
67 return Vector3D {
68 this->X * vector.X,
69 this->Y * vector.Y,
70 this->Z * vector.Z
71 };
72}
73
75 return Vector3D {
76 this->X / vector.X,
77 this->Y / vector.Y,
78 this->Z / vector.Z
79 };
80}
81
82Vector3D Vector3D::Multiply(const float& scalar) const {
83 return Vector3D {
84 this->X * scalar,
85 this->Y * scalar,
86 this->Z * scalar
87 };
88}
89
90Vector3D Vector3D::Divide(const float& scalar) const {
91 return Vector3D {
92 this->X / scalar,
93 this->Y / scalar,
94 this->Z / scalar
95 };
96}
97
99 return Vector3D {
100 (this->Y * vector.Z) - (this->Z * vector.Y),
101 (this->Z * vector.X) - (this->X * vector.Z),
102 (this->X * vector.Y) - (this->Y * vector.X)
103 };
104}
105
107 Vector3D vector = Vector3D(this->X, this->Y, this->Z);
108 float length = vector.Magnitude();
109
110 if (Mathematics::IsClose(length, 1.0f, Mathematics::EPSILON)) return Vector3D(this->X, this->Y, this->Z);
111 if (length == 0) return Vector3D(0, 1, 0);
112
113 return Vector3D {
114 vector.X / length,
115 vector.Y / length,
116 vector.Z / length
117 };
118}
119
120Vector3D Vector3D::Constrain(const float& minimum, const float& maximum) const {
121 return Vector3D{
122 Mathematics::Constrain(X, minimum, maximum),
123 Mathematics::Constrain(Y, minimum, maximum),
124 Mathematics::Constrain(Z, minimum, maximum)
125 };
126}
127
128Vector3D Vector3D::Constrain(const Vector3D& minimum, const Vector3D& maximum) const {
129 return Vector3D{
130 Mathematics::Constrain(X, minimum.X, maximum.X),
131 Mathematics::Constrain(Y, minimum.Y, maximum.Y),
132 Mathematics::Constrain(Z, minimum.Z, maximum.Z)
133 };
134}
135
137 Vector3D v = Vector3D(this->X, this->Y, this->Z);
138 float perm[3];
139
140 perm[(int)permutation.X] = v.X;
141 perm[(int)permutation.Y] = v.Y;
142 perm[(int)permutation.Z] = v.Z;
143
144 v.X = perm[0];
145 v.Y = perm[1];
146 v.Z = perm[2];
147
148 return v;
149}
150
151float Vector3D::Magnitude() const {
152 return Mathematics::Sqrt(X * X + Y * Y + Z * Z);
153}
154
156 return (X * vector.X) + (Y * vector.Y) + (Z * vector.Z);
157}
158
160 Vector3D offset = Vector3D(X - vector.X, Y - vector.Y, Z - vector.Z);
161
162 return offset.Magnitude();
163}
164
166 Vector3D absV = this->Absolute();
167
168 // Find the two largest absolute values
169 float max1 = absV.Max();
170 float max2 = (max1 == absV.X) ? Mathematics::Max(absV.Y, absV.Z) : (max1 == absV.Y) ? Mathematics::Max(absV.X, absV.Z) : Mathematics::Max(absV.X, absV.Y);
171
172 // Compute the average of the two largest values
173 return (max1 + max2) / 2.0f;
174}
175
176float Vector3D::Max() const{
177 return Mathematics::Max(X, Y, Z);
178}
179
180float Vector3D::Min() const{
181 return Mathematics::Min(X, Y, Z);
182}
183
185 return (this->X == vector.X) && (this->Y == vector.Y) && (this->Z == vector.Z);
186
187}
188
193
194 return "[" + x + ", " + y + ", " + z + "]";
195}
196
197// Implementations of static member functions
199 return Vector3D(input.X > max.X ? input.X : max.X,
200 input.Y > max.Y ? input.Y : max.Y,
201 input.Z > max.Z ? input.Z : max.Z);
202}
203
205 return Vector3D(input.X < min.X ? input.X : min.X,
206 input.Y < min.Y ? input.Y : min.Y,
207 input.Z < min.Z ? input.Z : min.Z);
208}
209
210Vector3D Vector3D::LERP(const Vector3D& start, const Vector3D& finish, const float& ratio) {
212
213 return finishL * ratio + startL * (1.0f - ratio);
214}
215
219
223
225 Vector3D normal = vector;
226
227 return normal.Normal();
228}
229
231 return Vector3D(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);
232}
233
235 return Vector3D(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);
236}
237
239 return Vector3D(v1.X * v2.X, v1.Y * v2.Y, v1.Z * v2.Z);
240}
241
243 return Vector3D(v1.X / v2.X, v1.Y / v2.Y, v1.Z / v2.Z);
244}
245
247 return Vector3D(vector.X * scalar, vector.Y * scalar, vector.Z * scalar);
248}
249
251 return Vector3D(vector.X * scalar, vector.Y * scalar, vector.Z * scalar);
252}
253
255 return Vector3D(vector.X / scalar, vector.Y / scalar, vector.Z / scalar);
256}
257
259 return Vector3D {
260 (v1.Y * v2.Z) - (v1.Z * v2.Y),
261 (v1.Z * v2.X) - (v1.X * v2.Z),
262 (v1.X * v2.Y) - (v1.Y * v2.X)
263 };
264}
265
267 return (v1.X * v2.X) + (v1.Y * v2.Y) + (v1.Z * v2.Z);
268}
269
271 Vector3D offset = Vector3D(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);
272
273 return offset.Magnitude();
274}
275
277 return (v1.X == v2.X) && (v1.Y == v2.Y) && (v1.Z == v2.Z);
278}
279
280// Operator overloads
282 return this->IsEqual(vector);
283}
284
286 return !(this->IsEqual(vector));
287}
288
290 this->X += vector.X;
291 this->Y += vector.Y;
292 this->Z += vector.Z;
293
294 return *this;
295}
296
298 this->X = vector.X;
299 this->Y = vector.Y;
300 this->Z = vector.Z;
301
302 return *this;
303}
304
306 return Add(vector);
307}
308
310 return Subtract(vector);
311}
312
314 return Multiply(vector);
315}
316
318 return Divide(vector);
319}
320
322 return Add(value);
323}
324
326 return Subtract(value);
327}
328
330 return Multiply(value);
331}
332
334 return Divide(value);
335}
Defines a 3D vector and various related operations.
Implements a generic Kalman Filter for 1D data.
static float Sqrt(float value)
Computes the square root of a value.
static T Min(T value1, T value2)
Returns the minimum of two values.
static bool IsClose(float v1, float v2, float epsilon)
Checks if two values are close within a specified epsilon.
static String DoubleToCleanString(float value)
Converts a floating-point value to a String, removing trailing decimals if not needed.
static T Max(T value1, T value2)
Returns the maximum of two values.
static const float MPID180
The value of , useful for converting degrees to radians.
Definition Mathematics.h:47
static const float EPSILON
A small constant used for floating-point comparisons.
Definition Mathematics.h:37
static T Constrain(T value, T minimum, T maximum)
Constrains a value between minimum and maximum.
static const float M180DPI
The value of , useful for converting radians to degrees.
Definition Mathematics.h:52
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