Plasma Engine  2.0
Loading...
Searching...
No Matches
Vec3.h
1#pragma once
2
3#include <Foundation/Math/Math.h>
4#include <Foundation/Math/Vec2.h>
5
7template <typename Type>
9{
10public:
11 // Means that vectors can be copied using memcpy instead of copy construction.
12 PL_DECLARE_POD_TYPE();
13
14 using ComponentType = Type;
15
16 // *** Data ***
17public:
18 Type x, y, z;
19
20 // *** Constructors ***
21public:
23 plVec3Template<Type>(); // [tested]
24
26 plVec3Template<Type>(Type x, Type y, Type z); // [tested]
27
29 explicit plVec3Template<Type>(Type v); // [tested]
30
31 // no copy-constructor and operator= since the default-generated ones will be faster
32
34 PL_DECLARE_IF_FLOAT_TYPE
36
38 [[nodiscard]] static plVec3Template<Type> MakeZero() { return plVec3Template<Type>(0); } // [tested]
39
41 [[nodiscard]] static plVec3Template<Type> MakeAxisX() { return plVec3Template<Type>(1, 0, 0); } // [tested]
42
44 [[nodiscard]] static plVec3Template<Type> MakeAxisY() { return plVec3Template<Type>(0, 1, 0); } // [tested]
45
47 [[nodiscard]] static plVec3Template<Type> MakeAxisZ() { return plVec3Template<Type>(0, 0, 1); } // [tested]
48
50 [[nodiscard]] static plVec3Template<Type> Make(Type x, Type y, Type z) { return plVec3Template<Type>(x, y, z); } // [tested]
51
52#if PL_ENABLED(PL_MATH_CHECK_FOR_NAN)
53 void AssertNotNaN() const
54 {
55 PL_ASSERT_ALWAYS(!IsNaN(), "This object contains NaN values. This can happen when you forgot to initialize it before using it. Please "
56 "check that all code-paths properly initialize this object.");
57 }
58#endif
59
60 // *** Conversions ***
61public:
63 const plVec2Template<Type> GetAsVec2() const; // [tested]
64
66 const plVec4Template<Type> GetAsVec4(Type w) const; // [tested]
67
69 const plVec4Template<Type> GetAsPositionVec4() const; // [tested]
70
72 const plVec4Template<Type> GetAsDirectionVec4() const; // [tested]
73
75 const Type* GetData() const { return &x; }
76
78 Type* GetData() { return &x; }
79
80 // *** Functions to set the vector to specific values ***
81public:
83 void Set(Type xyz); // [tested]
84
86 void Set(Type x, Type y, Type z); // [tested]
87
89 void SetZero(); // [tested]
90
91 // *** Functions dealing with length ***
92public:
94 PL_DECLARE_IF_FLOAT_TYPE
95 Type GetLength() const; // [tested]
96
99 PL_DECLARE_IF_FLOAT_TYPE
100 plResult SetLength(Type fNewLength, Type fEpsilon = plMath::DefaultEpsilon<Type>()); // [tested]
101
104 Type GetLengthSquared() const; // [tested]
105
108 PL_DECLARE_IF_FLOAT_TYPE
109 Type GetLengthAndNormalize(); // [tested]
110
112 PL_DECLARE_IF_FLOAT_TYPE
113 const plVec3Template<Type> GetNormalized() const; // [tested]
114
116 PL_DECLARE_IF_FLOAT_TYPE
117 void Normalize(); // [tested]
118
121 PL_DECLARE_IF_FLOAT_TYPE
122 plResult NormalizeIfNotZero(const plVec3Template<Type>& vFallback = plVec3Template<Type>(1, 0, 0), Type fEpsilon = plMath::SmallEpsilon<Type>()); // [tested]
123
125 bool IsZero() const; // [tested]
126
128 bool IsZero(Type fEpsilon) const; // [tested]
129
131 PL_DECLARE_IF_FLOAT_TYPE
132 bool IsNormalized(Type fEpsilon = plMath::HugeEpsilon<Type>()) const; // [tested]
133
135 bool IsNaN() const; // [tested]
136
138 bool IsValid() const; // [tested]
139
140
141 // *** Operators ***
142public:
144 const plVec3Template<Type> operator-() const; // [tested]
145
147 void operator+=(const plVec3Template<Type>& rhs); // [tested]
148
150 void operator-=(const plVec3Template<Type>& rhs); // [tested]
151
153 void operator*=(const plVec3Template<Type>& rhs);
154
156 void operator/=(const plVec3Template<Type>& rhs);
157
159 void operator*=(Type f); // [tested]
160
162 void operator/=(Type f); // [tested]
163
165 bool IsIdentical(const plVec3Template<Type>& rhs) const; // [tested]
166
168 bool IsEqual(const plVec3Template<Type>& rhs, Type fEpsilon) const; // [tested]
169
170
171 // *** Common vector operations ***
172public:
175 plAngle GetAngleBetween(const plVec3Template<Type>& rhs) const; // [tested]
176
178 Type Dot(const plVec3Template<Type>& rhs) const; // [tested]
179
180
181
183 const plVec3Template<Type> CrossRH(const plVec3Template<Type>& rhs) const; // [tested]
184
186 const plVec3Template<Type> CompMin(const plVec3Template<Type>& rhs) const; // [tested]
187
189 const plVec3Template<Type> CompMax(const plVec3Template<Type>& rhs) const; // [tested]
190
192 const plVec3Template<Type> CompClamp(const plVec3Template<Type>& vLow, const plVec3Template<Type>& vHigh) const; // [tested]
193
195 const plVec3Template<Type> CompMul(const plVec3Template<Type>& rhs) const; // [tested]
196
198 const plVec3Template<Type> CompDiv(const plVec3Template<Type>& rhs) const; // [tested]
199
201 const plVec3Template<Type> Abs() const; // [tested]
202
203
204 // *** Other common operations ***
205public:
207 PL_DECLARE_IF_FLOAT_TYPE
209
214 PL_DECLARE_IF_FLOAT_TYPE
215 void MakeOrthogonalTo(const plVec3Template<Type>& vNormal); // [tested]
216
218 PL_DECLARE_IF_FLOAT_TYPE
219 const plVec3Template<Type> GetOrthogonalVector() const; // [tested]
220
222 PL_DECLARE_IF_FLOAT_TYPE
223 const plVec3Template<Type> GetReflectedVector(const plVec3Template<Type>& vNormal) const; // [tested]
224
226 PL_DECLARE_IF_FLOAT_TYPE
227 const plVec3Template<Type> GetRefractedVector(const plVec3Template<Type>& vNormal, Type fRefIndex1, Type fRefIndex2) const;
228
230 PL_DECLARE_IF_FLOAT_TYPE
231 [[nodiscard]] static plVec3Template<Type>
232 MakeRandomPointInSphere(plRandom& inout_rng); // [tested]
233
235 PL_DECLARE_IF_FLOAT_TYPE
236 [[nodiscard]] static plVec3Template<Type>
237 MakeRandomDirection(plRandom& inout_rng); // [tested]
238
241 PL_DECLARE_IF_FLOAT_TYPE
242 [[nodiscard]] static plVec3Template<Type>
243 MakeRandomDeviationX(plRandom& inout_rng, const plAngle& maxDeviation); // [tested]
244
247 PL_DECLARE_IF_FLOAT_TYPE
248 [[nodiscard]] static plVec3Template<Type>
249 MakeRandomDeviationY(plRandom& inout_rng, const plAngle& maxDeviation); // [tested]
250
253 PL_DECLARE_IF_FLOAT_TYPE
254 [[nodiscard]] static plVec3Template<Type>
255 MakeRandomDeviationZ(plRandom& inout_rng, const plAngle& maxDeviation); // [tested]
256
260 PL_DECLARE_IF_FLOAT_TYPE
261 [[nodiscard]] static plVec3Template<Type>
262 MakeRandomDeviation(plRandom& inout_rng, const plAngle& maxDeviation, const plVec3Template<Type>& vNormal); // [tested]
263};
264
265// *** Operators ***
266
267template <typename Type>
268const plVec3Template<Type> operator+(const plVec3Template<Type>& v1, const plVec3Template<Type>& v2); // [tested]
269
270template <typename Type>
271const plVec3Template<Type> operator-(const plVec3Template<Type>& v1, const plVec3Template<Type>& v2); // [tested]
272
273
274template <typename Type>
275const plVec3Template<Type> operator*(Type f, const plVec3Template<Type>& v); // [tested]
276
277template <typename Type>
278const plVec3Template<Type> operator*(const plVec3Template<Type>& v, Type f); // [tested]
279
280
281template <typename Type>
282const plVec3Template<Type> operator/(const plVec3Template<Type>& v, Type f); // [tested]
283
284
285template <typename Type>
286bool operator==(const plVec3Template<Type>& v1, const plVec3Template<Type>& v2); // [tested]
287
288template <typename Type>
289bool operator!=(const plVec3Template<Type>& v1, const plVec3Template<Type>& v2); // [tested]
290
292template <typename Type>
293bool operator<(const plVec3Template<Type>& v1, const plVec3Template<Type>& v2); // [tested]
294
295#include <Foundation/Math/Implementation/Vec3_inl.h>
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
A random number generator. Currently uses the WELL512 algorithm.
Definition Random.h:9
A 2-component vector class.
Definition Vec2.h:14
A 3-component vector class.
Definition Vec3.h:9
static plVec3Template< Type > MakeZero()
Returns a vector with all components set to zero.
Definition Vec3.h:38
void Set(Type xyz)
Sets all 3 components to this value.
Definition Vec3_inl.h:32
PL_DECLARE_IF_FLOAT_TYPE Type GetLength() const
Returns the length of the vector.
Definition Vec3_inl.h:54
const plVec3Template< Type > CompClamp(const plVec3Template< Type > &vLow, const plVec3Template< Type > &vHigh) const
Returns the component-wise clamped value of *this between low and high.
Definition Vec3_inl.h:335
bool IsNaN() const
Returns true, if any of x, y or z is NaN.
Definition Vec3_inl.h:144
const plVec3Template< Type > CompDiv(const plVec3Template< Type > &rhs) const
Returns the component-wise division of *this and rhs.
Definition Vec3_inl.h:354
const plVec3Template< Type > CompMin(const plVec3Template< Type > &rhs) const
Returns the component-wise minimum of *this and rhs.
Definition Vec3_inl.h:317
const plVec3Template< Type > CrossRH(const plVec3Template< Type > &rhs) const
Returns the Cross-product of the two vectors (NOT commutative, order DOES matter)
Definition Vec3_inl.h:299
bool IsIdentical(const plVec3Template< Type > &rhs) const
Equality Check (bitwise)
Definition Vec3_inl.h:422
void operator+=(const plVec3Template< Type > &rhs)
Adds rhs component-wise to this vector.
Definition Vec3_inl.h:178
const plVec3Template< Type > Abs() const
brief Returns the component-wise absolute of *this.
Definition Vec3_inl.h:363
bool IsValid() const
Checks that all components are finite numbers.
Definition Vec3_inl.h:157
PL_DECLARE_IF_FLOAT_TYPE bool IsNormalized(Type fEpsilon=plMath::HugeEpsilon< Type >()) const
Returns, whether the squared length of this vector is between 0.999f and 1.001f.
Definition Vec3_inl.h:121
void operator*=(const plVec3Template< Type > &rhs)
Multiplies rhs component-wise to this vector.
Definition Vec3_inl.h:198
Type GetLengthSquared() const
Returns the squared length. Faster, since no square-root is taken. Useful, if one only wants to compa...
Definition Vec3_inl.h:70
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeNaN()
Returns a vector with all components set to Not-a-Number (NaN).
Definition Vec3.h:35
const plVec4Template< Type > GetAsVec4(Type w) const
Returns an plVec4Template with x,y,z from this vector and w set to the parameter.
Definition Vec4_inl.h:35
PL_DECLARE_IF_FLOAT_TYPE const plVec3Template< Type > GetReflectedVector(const plVec3Template< Type > &vNormal) const
Returns this vector reflected at vNormal.
Definition Vec3_inl.h:282
bool IsEqual(const plVec3Template< Type > &rhs, Type fEpsilon) const
Equality Check with epsilon.
Definition Vec3_inl.h:431
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDeviationZ(plRandom &inout_rng, const plAngle &maxDeviation)
Creates a random vector around the z axis with a maximum deviation angle of maxDeviation....
Definition AllClassesRandom_inl.h:58
const plVec3Template< Type > CompMax(const plVec3Template< Type > &rhs) const
Returns the component-wise maximum of *this and rhs.
Definition Vec3_inl.h:326
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDeviationX(plRandom &inout_rng, const plAngle &maxDeviation)
Creates a random vector around the x axis with a maximum deviation angle of maxDeviation....
Definition AllClassesRandom_inl.h:34
PL_DECLARE_IF_FLOAT_TYPE Type GetLengthAndNormalize()
Normalizes this vector and returns its previous length in one operation. More efficient than calling ...
Definition Vec3_inl.h:78
PL_DECLARE_IF_FLOAT_TYPE void Normalize()
Normalizes this vector.
Definition Vec3_inl.h:95
static plVec3Template< Type > Make(Type x, Type y, Type z)
Returns a vector initialized to x,y,z.
Definition Vec3.h:50
void SetZero()
Sets the vector to all zero.
Definition Vec3_inl.h:48
PL_DECLARE_IF_FLOAT_TYPE const plVec3Template< Type > GetNormalized() const
Returns a normalized version of this vector, leaves the vector itself unchanged.
Definition Vec3_inl.h:86
const Type * GetData() const
Returns the data as an array.
Definition Vec3.h:75
const plVec4Template< Type > GetAsPositionVec4() const
Returns an plVec4Template with x,y,z from this vector and w set 1.
Definition Vec4_inl.h:43
PL_DECLARE_IF_FLOAT_TYPE plResult NormalizeIfNotZero(const plVec3Template< Type > &vFallback=plVec3Template< Type >(1, 0, 0), Type fEpsilon=plMath::SmallEpsilon< Type >())
Tries to normalize this vector. If the vector is too close to zero, PL_FAILURE is returned and the ve...
Definition Vec3_inl.h:101
const plVec4Template< Type > GetAsDirectionVec4() const
Returns an plVec4Template with x,y,z from this vector and w set 0.
Definition Vec4_inl.h:52
void operator/=(const plVec3Template< Type > &rhs)
Divides this vector component-wise by rhs.
Definition Vec3_inl.h:210
Type Dot(const plVec3Template< Type > &rhs) const
Returns the Dot-product of the two vectors (commutative, order does not matter)
Definition Vec3_inl.h:290
void operator-=(const plVec3Template< Type > &rhs)
Subtracts rhs component-wise from this vector.
Definition Vec3_inl.h:188
const plVec3Template< Type > CompMul(const plVec3Template< Type > &rhs) const
Returns the component-wise multiplication of *this and rhs.
Definition Vec3_inl.h:345
bool IsZero() const
Returns, whether this vector is (0, 0, 0).
Definition Vec3_inl.h:128
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomPointInSphere(plRandom &inout_rng)
Returns a random point inside a unit sphere (radius 1).
Definition AllClassesRandom_inl.h:8
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDirection(plRandom &inout_rng)
Creates a random direction vector. The vector is normalized.
Definition AllClassesRandom_inl.h:26
plVec3Template()
default-constructed vector is uninitialized (for speed)
Definition Vec3_inl.h:4
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDeviationY(plRandom &inout_rng, const plAngle &maxDeviation)
Creates a random vector around the y axis with a maximum deviation angle of maxDeviation....
Definition AllClassesRandom_inl.h:50
PL_DECLARE_IF_FLOAT_TYPE const plVec3Template< Type > GetOrthogonalVector() const
Returns some arbitrary vector orthogonal to this one. The vector is NOT normalized.
Definition Vec3_inl.h:270
PL_DECLARE_IF_FLOAT_TYPE const plVec3Template< Type > GetRefractedVector(const plVec3Template< Type > &vNormal, Type fRefIndex1, Type fRefIndex2) const
Returns this vector, refracted at vNormal, using the refraction index of the current medium and the m...
Definition Vec3_inl.h:470
static plVec3Template< Type > MakeAxisX()
Returns a vector initialized to the X unit vector (1, 0, 0).
Definition Vec3.h:41
const plVec2Template< Type > GetAsVec2() const
Returns an plVec2Template with x and y from this vector.
Definition Vec4_inl.h:26
PL_DECLARE_IF_FLOAT_TYPE void MakeOrthogonalTo(const plVec3Template< Type > &vNormal)
Modifies this direction vector to be orthogonal to the given (normalized) direction vector....
Definition Vec3_inl.h:260
const plVec3Template< Type > operator-() const
Returns the negation of this vector.
Definition Vec3_inl.h:170
PL_DECLARE_IF_FLOAT_TYPE plResult CalculateNormal(const plVec3Template< Type > &v1, const plVec3Template< Type > &v2, const plVec3Template< Type > &v3)
Calculates the normal of the triangle defined by the three vertices. Vertices are assumed to be order...
Definition Vec3_inl.h:253
static plVec3Template< Type > MakeAxisY()
Returns a vector initialized to the Y unit vector (0, 1, 0).
Definition Vec3.h:44
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDeviation(plRandom &inout_rng, const plAngle &maxDeviation, const plVec3Template< Type > &vNormal)
Creates a random vector around the given normal with a maximum deviation.
Definition AllClassesRandom_inl.h:66
static plVec3Template< Type > MakeAxisZ()
Returns a vector initialized to the Z unit vector (0, 0, 1).
Definition Vec3.h:47
plAngle GetAngleBetween(const plVec3Template< Type > &rhs) const
Returns the positive angle between *this and rhs. Both this and rhs must be normalized.
Definition Vec3_inl.h:308
Type * GetData()
Returns the data as an array.
Definition Vec3.h:78
PL_DECLARE_IF_FLOAT_TYPE plResult SetLength(Type fNewLength, Type fEpsilon=plMath::DefaultEpsilon< Type >())
Tries to rescale the vector to the given length. If the vector is too close to zero,...
Definition Vec3_inl.h:60
A 4-component vector class.
Definition Vec4.h:9
constexpr TYPE NaN()
Returns the value for NaN as the template type. Returns zero, if the type does not support NaN.
Definition Constants_inl.h:58
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54