Plasma Engine  2.0
Loading...
Searching...
No Matches
Vec2.h
1#pragma once
2
3#include <Foundation/Math/Math.h>
4
5#if PL_ENABLED(PL_MATH_CHECK_FOR_NAN)
6# define PL_VEC2_CHECK_FOR_NAN(obj) (obj)->AssertNotNaN();
7#else
8# define PL_VEC2_CHECK_FOR_NAN(obj)
9#endif
10
12template <typename Type>
14{
15public:
16 // Means that vectors can be copied using memcpy instead of copy construction.
17 PL_DECLARE_POD_TYPE();
18
19 using ComponentType = Type;
20
21
22 // *** Data ***
23public:
24 Type x;
25 Type y;
26
27 // *** Constructors ***
28public:
30 plVec2Template(); // [tested]
31
33 plVec2Template(Type x, Type y); // [tested]
34
36 explicit plVec2Template(Type v); // [tested]
37
38 // no copy-constructor and operator= since the default-generated ones will be faster
39
41 PL_DECLARE_IF_FLOAT_TYPE
42 [[nodiscard]] static const plVec2Template<Type>
47
49 [[nodiscard]] static constexpr plVec2Template<Type> MakeZero() { return plVec2Template(0); } // [tested]
50
51#if PL_ENABLED(PL_MATH_CHECK_FOR_NAN)
52 void AssertNotNaN() const
53 {
54 PL_ASSERT_ALWAYS(!IsNaN(), "This object contains NaN values. This can happen when you forgot to initialize it before using it. Please "
55 "check that all code-paths properly initialize this object.");
56 }
57#endif
58
59 // *** Conversions ***
60public:
62 const plVec3Template<Type> GetAsVec3(Type z) const; // [tested]
63
65 const plVec4Template<Type> GetAsVec4(Type z, Type w) const; // [tested]
66
68 const Type* GetData() const { return &x; }
69
71 Type* GetData() { return &x; }
72
73 // *** Functions to set the vector to specific values ***
74public:
76 void Set(Type xy); // [tested]
77
79 void Set(Type x, Type y); // [tested]
80
82 void SetZero(); // [tested]
83
84 // *** Functions dealing with length ***
85public:
87 PL_DECLARE_IF_FLOAT_TYPE
88 Type GetLength() const; // [tested]
89
92 PL_DECLARE_IF_FLOAT_TYPE
93 plResult SetLength(Type fNewLength, Type fEpsilon = plMath::DefaultEpsilon<Type>()); // [tested]
94
97 Type GetLengthSquared() const; // [tested]
98
101 PL_DECLARE_IF_FLOAT_TYPE
102 Type GetLengthAndNormalize(); // [tested]
103
105 PL_DECLARE_IF_FLOAT_TYPE
106 const plVec2Template<Type> GetNormalized() const; // [tested]
107
109 PL_DECLARE_IF_FLOAT_TYPE
110 void Normalize(); // [tested]
111
114 PL_DECLARE_IF_FLOAT_TYPE
115 plResult NormalizeIfNotZero(const plVec2Template<Type>& vFallback = plVec2Template<Type>(1, 0), Type fEpsilon = plMath::DefaultEpsilon<Type>()); // [tested]
116
118 bool IsZero() const; // [tested]
119
121 bool IsZero(Type fEpsilon) const; // [tested]
122
124 PL_DECLARE_IF_FLOAT_TYPE
125 bool IsNormalized(Type fEpsilon = plMath::HugeEpsilon<Type>()) const; // [tested]
126
128 bool IsNaN() const; // [tested]
129
131 bool IsValid() const; // [tested]
132
133
134 // *** Operators ***
135public:
137 const plVec2Template<Type> operator-() const; // [tested]
138
140 void operator+=(const plVec2Template<Type>& vCc); // [tested]
141
143 void operator-=(const plVec2Template<Type>& vCc); // [tested]
144
146 void operator*=(Type f); // [tested]
147
149 void operator/=(Type f); // [tested]
150
152 bool IsIdentical(const plVec2Template<Type>& rhs) const; // [tested]
153
155 bool IsEqual(const plVec2Template<Type>& rhs, Type fEpsilon) const; // [tested]
156
157
158 // *** Common vector operations ***
159public:
161 plAngle GetAngleBetween(const plVec2Template<Type>& rhs) const; // [tested]
162
164 Type Dot(const plVec2Template<Type>& rhs) const; // [tested]
165
167 const plVec2Template<Type> CompMin(const plVec2Template<Type>& rhs) const; // [tested]
168
170 const plVec2Template<Type> CompMax(const plVec2Template<Type>& rhs) const; // [tested]
171
173 const plVec2Template<Type> CompClamp(const plVec2Template<Type>& vLow, const plVec2Template<Type>& vHigh) const; // [tested]
174
176 const plVec2Template<Type> CompMul(const plVec2Template<Type>& rhs) const; // [tested]
177
179 const plVec2Template<Type> CompDiv(const plVec2Template<Type>& rhs) const; // [tested]
180
182 const plVec2Template<Type> Abs() const; // [tested]
183
184
185 // *** Other common operations ***
186public:
191 PL_DECLARE_IF_FLOAT_TYPE
192 void MakeOrthogonalTo(const plVec2Template<Type>& vNormal); // [tested]
193
195 const plVec2Template<Type> GetOrthogonalVector() const; // [tested]
196
198 PL_DECLARE_IF_FLOAT_TYPE
199 const plVec2Template<Type> GetReflectedVector(const plVec2Template<Type>& vNormal) const; // [tested]
200};
201
202// *** Operators ***
203
205template <typename Type>
206const plVec2Template<Type> operator+(const plVec2Template<Type>& v1, const plVec2Template<Type>& v2); // [tested]
207
209template <typename Type>
210const plVec2Template<Type> operator-(const plVec2Template<Type>& v1, const plVec2Template<Type>& v2); // [tested]
211
213template <typename Type>
214const plVec2Template<Type> operator*(Type f, const plVec2Template<Type>& v); // [tested]
215
217template <typename Type>
218const plVec2Template<Type> operator*(const plVec2Template<Type>& v, Type f); // [tested]
219
221template <typename Type>
222const plVec2Template<Type> operator/(const plVec2Template<Type>& v, Type f); // [tested]
223
225template <typename Type>
226bool operator==(const plVec2Template<Type>& v1, const plVec2Template<Type>& v2); // [tested]
227
229template <typename Type>
230bool operator!=(const plVec2Template<Type>& v1, const plVec2Template<Type>& v2); // [tested]
231
233template <typename Type>
234bool operator<(const plVec2Template<Type>& v1, const plVec2Template<Type>& v2);
235
236#include <Foundation/Math/Implementation/Vec2_inl.h>
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
A 2-component vector class.
Definition Vec2.h:14
plAngle GetAngleBetween(const plVec2Template< Type > &rhs) const
Returns the positive angle between *this and rhs.
Definition Vec2_inl.h:245
void operator-=(const plVec2Template< Type > &vCc)
Subtracts cc component-wise from this vector.
Definition Vec2_inl.h:174
PL_DECLARE_IF_FLOAT_TYPE Type GetLength() const
Returns the length of the vector.
Definition Vec2_inl.h:49
void operator+=(const plVec2Template< Type > &vCc)
Adds cc component-wise to this vector.
Definition Vec2_inl.h:165
const plVec3Template< Type > GetAsVec3(Type z) const
Returns an plVec3Template with x,y from this vector and z set by the parameter.
Definition Vec4_inl.h:10
void Set(Type xy)
Sets all components to this value.
Definition Vec2_inl.h:29
const plVec2Template< Type > GetOrthogonalVector() const
Returns some arbitrary vector orthogonal to this one. The vector is NOT normalized.
Definition Vec2_inl.h:219
PL_DECLARE_IF_FLOAT_TYPE void MakeOrthogonalTo(const plVec2Template< Type > &vNormal)
Modifies this direction vector to be orthogonal to the given (normalized) direction vector....
Definition Vec2_inl.h:210
const plVec4Template< Type > GetAsVec4(Type z, Type w) const
Returns an plVec4Template with x,y from this vector and z and w set by the parameters.
Definition Vec4_inl.h:18
const plVec2Template< Type > CompClamp(const plVec2Template< Type > &vLow, const plVec2Template< Type > &vHigh) const
Returns the component-wise clamped value of *this between low and high.
Definition Vec2_inl.h:272
void SetZero()
Sets the vector to all zero.
Definition Vec2_inl.h:43
const plVec2Template< Type > CompMul(const plVec2Template< Type > &rhs) const
Returns the component-wise multiplication of *this and rhs.
Definition Vec2_inl.h:282
static constexpr plVec2Template< Type > MakeZero()
Static function that returns a zero-vector.
Definition Vec2.h:49
bool IsValid() const
Checks that all components are finite numbers.
Definition Vec2_inl.h:146
Type * GetData()
Returns the data as an array.
Definition Vec2.h:71
PL_DECLARE_IF_FLOAT_TYPE const plVec2Template< Type > GetNormalized() const
Returns a normalized version of this vector, leaves the vector itself unchanged.
Definition Vec2_inl.h:79
void operator/=(Type f)
Divides all components of this vector by f.
Definition Vec2_inl.h:192
PL_DECLARE_IF_FLOAT_TYPE void Normalize()
Normalizes this vector.
Definition Vec2_inl.h:88
PL_DECLARE_IF_FLOAT_TYPE plResult NormalizeIfNotZero(const plVec2Template< Type > &vFallback=plVec2Template< Type >(1, 0), Type fEpsilon=plMath::DefaultEpsilon< Type >())
Tries to normalize this vector. If the vector is too close to zero, PL_FAILURE is returned and the ve...
Definition Vec2_inl.h:94
PL_DECLARE_IF_FLOAT_TYPE const plVec2Template< Type > GetReflectedVector(const plVec2Template< Type > &vNormal) const
Returns this vector reflected at vNormal.
Definition Vec2_inl.h:228
bool IsIdentical(const plVec2Template< Type > &rhs) const
Equality Check (bitwise)
Definition Vec2_inl.h:359
bool IsZero() const
Returns, whether this vector is (0, 0).
Definition Vec2_inl.h:121
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 Vec2_inl.h:114
const plVec2Template< Type > CompDiv(const plVec2Template< Type > &rhs) const
Returns the component-wise division of *this and rhs.
Definition Vec2_inl.h:291
Type Dot(const plVec2Template< Type > &rhs) const
Returns the Dot-product of the two vectors (commutative, order does not matter)
Definition Vec2_inl.h:236
bool IsNaN() const
Returns true, if any of x or y is NaN.
Definition Vec2_inl.h:135
const plVec2Template< Type > CompMax(const plVec2Template< Type > &rhs) const
Returns the component-wise maximum of *this and rhs.
Definition Vec2_inl.h:263
plVec2Template()
default-constructed vector is uninitialized (for speed)
Definition Vec2_inl.h:4
static PL_DECLARE_IF_FLOAT_TYPE const plVec2Template< Type > MakeNaN()
Returns a vector with all components set to Not-a-Number (NaN).
Definition Vec2.h:43
PL_DECLARE_IF_FLOAT_TYPE Type GetLengthAndNormalize()
Normalizes this vector and returns its previous length in one operation. More efficient than calling ...
Definition Vec2_inl.h:71
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 Vec2_inl.h:55
Type GetLengthSquared() const
Returns the squared length. Faster, since no square-root is taken. Useful, if one only wants to compa...
Definition Vec2_inl.h:65
const plVec2Template< Type > Abs() const
brief Returns the component-wise absolute of *this.
Definition Vec2_inl.h:300
void operator*=(Type f)
Multiplies all components of this vector with f.
Definition Vec2_inl.h:183
const Type * GetData() const
Returns the data as an array.
Definition Vec2.h:68
bool IsEqual(const plVec2Template< Type > &rhs, Type fEpsilon) const
Equality Check with epsilon.
Definition Vec2_inl.h:368
const plVec2Template< Type > CompMin(const plVec2Template< Type > &rhs) const
Returns the component-wise minimum of *this and rhs.
Definition Vec2_inl.h:254
const plVec2Template< Type > operator-() const
Returns the negation of this vector.
Definition Vec2_inl.h:157
A 3-component vector class.
Definition Vec3.h:9
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