Plasma Engine  2.0
Loading...
Searching...
No Matches
Vec4.h
1#pragma once
2
3#include <Foundation/Math/Math.h>
4#include <Foundation/Math/Vec3.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, w;
19
20 // *** Constructors ***
21public:
23 plVec4Template(); // [tested]
24
26 plVec4Template(Type x, Type y, Type z, Type w); // [tested]
27
30
32 explicit plVec4Template(Type v); // [tested]
33 // no copy-constructor and operator= since the default-generated ones will be faster
34
36 PL_DECLARE_IF_FLOAT_TYPE
37 [[nodiscard]] static plVec4Template<Type>
39
41 [[nodiscard]] static plVec4Template<Type> MakeZero() { return plVec4Template<Type>(0); } // [tested]
42
43#if PL_ENABLED(PL_MATH_CHECK_FOR_NAN)
44 void AssertNotNaN() const
45 {
46 PL_ASSERT_ALWAYS(!IsNaN(), "This object contains NaN values. This can happen when you forgot to initialize it before using it. Please "
47 "check that all code-paths properly initialize this object.");
48 }
49#endif
50
51 // *** Conversions ***
52public:
54 const plVec2Template<Type> GetAsVec2() const; // [tested]
55
57 const plVec3Template<Type> GetAsVec3() const; // [tested]
58
60 const Type* GetData() const { return &x; }
61
63 Type* GetData() { return &x; }
64
65 // *** Functions to set the vector to specific values ***
66public:
68 void Set(Type xyzw); // [tested]
69
71 void Set(Type x, Type y, Type z, Type w); // [tested]
72
74 void SetZero(); // [tested]
75
76 // *** Functions dealing with length ***
77public:
79 PL_DECLARE_IF_FLOAT_TYPE
80 Type GetLength() const; // [tested]
81
84 Type GetLengthSquared() const; // [tested]
85
88 PL_DECLARE_IF_FLOAT_TYPE
89 Type GetLengthAndNormalize(); // [tested]
90
92 PL_DECLARE_IF_FLOAT_TYPE
93 const plVec4Template<Type> GetNormalized() const; // [tested]
94
96 PL_DECLARE_IF_FLOAT_TYPE
97 void Normalize(); // [tested]
98
101 PL_DECLARE_IF_FLOAT_TYPE
102 plResult NormalizeIfNotZero(const plVec4Template<Type>& vFallback = plVec4Template<Type>(1, 0, 0, 0), Type fEpsilon = plMath::SmallEpsilon<Type>()); // [tested]
103
105 bool IsZero() const; // [tested]
106
108 bool IsZero(Type fEpsilon) const; // [tested]
109
111 PL_DECLARE_IF_FLOAT_TYPE
112 bool IsNormalized(Type fEpsilon = plMath::HugeEpsilon<Type>()) const; // [tested]
113
115 bool IsNaN() const; // [tested]
116
118 bool IsValid() const; // [tested]
119
120
121 // *** Operators ***
122public:
124 const plVec4Template<Type> operator-() const; // [tested]
125
127 void operator+=(const plVec4Template<Type>& vCc); // [tested]
128
130 void operator-=(const plVec4Template<Type>& vCc); // [tested]
131
133 void operator*=(Type f); // [tested]
134
136 void operator/=(Type f); // [tested]
137
139 bool IsIdentical(const plVec4Template<Type>& rhs) const; // [tested]
140
142 bool IsEqual(const plVec4Template<Type>& rhs, Type fEpsilon) const; // [tested]
143
144
145 // *** Common vector operations ***
146public:
148 Type Dot(const plVec4Template<Type>& rhs) const; // [tested]
149
151 const plVec4Template<Type> CompMin(const plVec4Template<Type>& rhs) const; // [tested]
152
154 const plVec4Template<Type> CompMax(const plVec4Template<Type>& rhs) const; // [tested]
155
157 const plVec4Template<Type> CompClamp(const plVec4Template<Type>& vLow, const plVec4Template<Type>& vHigh) const; // [tested]
158
160 const plVec4Template<Type> CompMul(const plVec4Template<Type>& rhs) const; // [tested]
161
163 const plVec4Template<Type> CompDiv(const plVec4Template<Type>& rhs) const; // [tested]
164
166 const plVec4Template<Type> Abs() const; // [tested]
167};
168
169// *** Operators ***
170
171template <typename Type>
172const plVec4Template<Type> operator+(const plVec4Template<Type>& v1, const plVec4Template<Type>& v2); // [tested]
173
174template <typename Type>
175const plVec4Template<Type> operator-(const plVec4Template<Type>& v1, const plVec4Template<Type>& v2); // [tested]
176
177
178template <typename Type>
179const plVec4Template<Type> operator*(Type f, const plVec4Template<Type>& v); // [tested]
180
181template <typename Type>
182const plVec4Template<Type> operator*(const plVec4Template<Type>& v, Type f); // [tested]
183
184
185template <typename Type>
186const plVec4Template<Type> operator/(const plVec4Template<Type>& v, Type f); // [tested]
187
188
189template <typename Type>
190bool operator==(const plVec4Template<Type>& v1, const plVec4Template<Type>& v2); // [tested]
191
192template <typename Type>
193bool operator!=(const plVec4Template<Type>& v1, const plVec4Template<Type>& v2); // [tested]
194
196template <typename Type>
197bool operator<(const plVec4Template<Type>& v1, const plVec4Template<Type>& v2); // [tested]
198
199#include <Foundation/Math/Implementation/Vec4_inl.h>
A 2-component vector class.
Definition Vec2.h:14
A 3-component vector class.
Definition Vec3.h:9
A 4-component vector class.
Definition Vec4.h:9
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 Vec4_inl.h:202
PL_DECLARE_IF_FLOAT_TYPE plResult NormalizeIfNotZero(const plVec4Template< Type > &vFallback=plVec4Template< Type >(1, 0, 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 Vec4_inl.h:182
plVec4Template()
Default-constructed vector is uninitialized (for speed)
Definition Vec4_inl.h:63
const plVec4Template< Type > CompMin(const plVec4Template< Type > &rhs) const
Returns the component-wise minimum of *this and rhs.
Definition Vec4_inl.h:327
bool IsZero() const
Returns, whether this vector is (0, 0, 0, 0).
Definition Vec4_inl.h:209
bool IsEqual(const plVec4Template< Type > &rhs, Type fEpsilon) const
Equality Check with epsilon.
Definition Vec4_inl.h:444
const plVec4Template< Type > CompDiv(const plVec4Template< Type > &rhs) const
Returns the component-wise division of *this and rhs.
Definition Vec4_inl.h:366
void SetZero()
Sets the vector to all zero.
Definition Vec4_inl.h:139
PL_DECLARE_IF_FLOAT_TYPE Type GetLengthAndNormalize()
Normalizes this vector and returns its previous length in one operation. More efficient than calling ...
Definition Vec4_inl.h:159
const plVec3Template< Type > GetAsVec3() const
Returns an plVec3Template with x,y and z from this vector.
Definition Vec4_inl.h:112
void operator-=(const plVec4Template< Type > &vCc)
Subtracts cc component-wise from this vector.
Definition Vec4_inl.h:274
bool IsNaN() const
Returns true, if any of x, y, z or w is NaN.
Definition Vec4_inl.h:225
const plVec4Template< Type > CompClamp(const plVec4Template< Type > &vLow, const plVec4Template< Type > &vHigh) const
Returns the component-wise clamped value of *this between low and high.
Definition Vec4_inl.h:345
PL_DECLARE_IF_FLOAT_TYPE void Normalize()
Normalizes this vector.
Definition Vec4_inl.h:176
const plVec4Template< Type > operator-() const
Returns the negation of this vector.
Definition Vec4_inl.h:255
void operator/=(Type f)
Divides all components of this vector by f.
Definition Vec4_inl.h:296
const plVec4Template< Type > Abs() const
brief Returns the component-wise absolute of *this.
Definition Vec4_inl.h:376
static plVec4Template< Type > MakeZero()
Returns a vector with all components set to zero.
Definition Vec4.h:41
Type * GetData()
Returns the data as an array.
Definition Vec4.h:63
const Type * GetData() const
Returns the data as an array.
Definition Vec4.h:60
PL_DECLARE_IF_FLOAT_TYPE const plVec4Template< Type > GetNormalized() const
Returns a normalized version of this vector, leaves the vector itself unchanged.
Definition Vec4_inl.h:167
Type Dot(const plVec4Template< Type > &rhs) const
Returns the dot-product of the two vectors (commutative, order does not matter).
Definition Vec4_inl.h:318
void operator*=(Type f)
Multiplies all components of this vector with f.
Definition Vec4_inl.h:285
void Set(Type xyzw)
Sets all 4 components to this value.
Definition Vec4_inl.h:121
const plVec4Template< Type > CompMul(const plVec4Template< Type > &rhs) const
Returns the component-wise multiplication of *this and rhs.
Definition Vec4_inl.h:355
void operator+=(const plVec4Template< Type > &vCc)
Adds cc component-wise to this vector.
Definition Vec4_inl.h:263
bool IsIdentical(const plVec4Template< Type > &rhs) const
Equality Check (bitwise).
Definition Vec4_inl.h:435
const plVec4Template< Type > CompMax(const plVec4Template< Type > &rhs) const
Returns the component-wise maximum of *this and rhs.
Definition Vec4_inl.h:336
static PL_DECLARE_IF_FLOAT_TYPE plVec4Template< Type > MakeNaN()
Returns a vector with all components set to Not-a-Number (NaN).
Definition Vec4.h:38
const plVec2Template< Type > GetAsVec2() const
Returns an plVec2Template with x and y from this vector.
Definition Vec4_inl.h:103
bool IsValid() const
Checks that all components are finite numbers.
Definition Vec4_inl.h:240
Type GetLengthSquared() const
Returns the squared length. Faster, since no square-root is taken. Useful, if one only wants to compa...
Definition Vec4_inl.h:151
PL_DECLARE_IF_FLOAT_TYPE Type GetLength() const
Returns the length of the vector.
Definition Vec4_inl.h:145
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