Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Vec4.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <Jolt/Math/Float4.h>
8#include <Jolt/Math/Swizzle.h>
10
12
13class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Vec4
14{
15public:
17
18 // Underlying vector type
19#if defined(JPH_USE_SSE)
20 using Type = __m128;
21#elif defined(JPH_USE_NEON)
22 using Type = float32x4_t;
23#else
24 using Type = struct { float mData[4]; };
25#endif
26
28 Vec4() = default;
29 Vec4(const Vec4 &inRHS) = default;
30 Vec4 & operator = (const Vec4 &inRHS) = default;
31 explicit JPH_INLINE Vec4(Vec3Arg inRHS);
32 JPH_INLINE Vec4(Vec3Arg inRHS, float inW);
33 JPH_INLINE Vec4(Type inRHS) : mValue(inRHS) { }
34
36 JPH_INLINE Vec4(float inX, float inY, float inZ, float inW);
37
39 static JPH_INLINE Vec4 sZero();
40
42 static JPH_INLINE Vec4 sNaN();
43
45 static JPH_INLINE Vec4 sReplicate(float inV);
46
48 static JPH_INLINE Vec4 sLoadFloat4(const Float4 *inV);
49
51 static JPH_INLINE Vec4 sLoadFloat4Aligned(const Float4 *inV);
52
54 template <const int Scale>
55 static JPH_INLINE Vec4 sGatherFloat4(const float *inBase, UVec4Arg inOffsets);
56
58 static JPH_INLINE Vec4 sMin(Vec4Arg inV1, Vec4Arg inV2);
59
61 static JPH_INLINE Vec4 sMax(Vec4Arg inV1, Vec4Arg inV2);
62
64 static JPH_INLINE UVec4 sEquals(Vec4Arg inV1, Vec4Arg inV2);
65
67 static JPH_INLINE UVec4 sLess(Vec4Arg inV1, Vec4Arg inV2);
68
70 static JPH_INLINE UVec4 sLessOrEqual(Vec4Arg inV1, Vec4Arg inV2);
71
73 static JPH_INLINE UVec4 sGreater(Vec4Arg inV1, Vec4Arg inV2);
74
76 static JPH_INLINE UVec4 sGreaterOrEqual(Vec4Arg inV1, Vec4Arg inV2);
77
79 static JPH_INLINE Vec4 sFusedMultiplyAdd(Vec4Arg inMul1, Vec4Arg inMul2, Vec4Arg inAdd);
80
82 static JPH_INLINE Vec4 sSelect(Vec4Arg inNotSet, Vec4Arg inSet, UVec4Arg inControl);
83
85 static JPH_INLINE Vec4 sOr(Vec4Arg inV1, Vec4Arg inV2);
86
88 static JPH_INLINE Vec4 sXor(Vec4Arg inV1, Vec4Arg inV2);
89
91 static JPH_INLINE Vec4 sAnd(Vec4Arg inV1, Vec4Arg inV2);
92
95 static JPH_INLINE void sSort4(Vec4 &ioValue, UVec4 &ioIndex);
96
99 static JPH_INLINE void sSort4Reverse(Vec4 &ioValue, UVec4 &ioIndex);
100
102#if defined(JPH_USE_SSE)
103 JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); }
104 JPH_INLINE float GetY() const { return mF32[1]; }
105 JPH_INLINE float GetZ() const { return mF32[2]; }
106 JPH_INLINE float GetW() const { return mF32[3]; }
107#elif defined(JPH_USE_NEON)
108 JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); }
109 JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); }
110 JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); }
111 JPH_INLINE float GetW() const { return vgetq_lane_f32(mValue, 3); }
112#else
113 JPH_INLINE float GetX() const { return mF32[0]; }
114 JPH_INLINE float GetY() const { return mF32[1]; }
115 JPH_INLINE float GetZ() const { return mF32[2]; }
116 JPH_INLINE float GetW() const { return mF32[3]; }
117#endif
118
120 JPH_INLINE void SetX(float inX) { mF32[0] = inX; }
121 JPH_INLINE void SetY(float inY) { mF32[1] = inY; }
122 JPH_INLINE void SetZ(float inZ) { mF32[2] = inZ; }
123 JPH_INLINE void SetW(float inW) { mF32[3] = inW; }
124
126 JPH_INLINE void Set(float inX, float inY, float inZ, float inW) { *this = Vec4(inX, inY, inZ, inW); }
127
129 JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }
130 JPH_INLINE float & operator [] (uint inCoordinate) { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }
131
133 JPH_INLINE bool operator == (Vec4Arg inV2) const;
134 JPH_INLINE bool operator != (Vec4Arg inV2) const { return !(*this == inV2); }
135
137 JPH_INLINE bool IsClose(Vec4Arg inV2, float inMaxDistSq = 1.0e-12f) const;
138
140 JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const;
141
143 JPH_INLINE bool IsNaN() const;
144
146 JPH_INLINE Vec4 operator * (Vec4Arg inV2) const;
147
149 JPH_INLINE Vec4 operator * (float inV2) const;
150
152 friend JPH_INLINE Vec4 operator * (float inV1, Vec4Arg inV2);
153
155 JPH_INLINE Vec4 operator / (float inV2) const;
156
158 JPH_INLINE Vec4 & operator *= (float inV2);
159
161 JPH_INLINE Vec4 & operator *= (Vec4Arg inV2);
162
164 JPH_INLINE Vec4 & operator /= (float inV2);
165
167 JPH_INLINE Vec4 operator + (Vec4Arg inV2) const;
168
170 JPH_INLINE Vec4 & operator += (Vec4Arg inV2);
171
173 JPH_INLINE Vec4 operator - () const;
174
176 JPH_INLINE Vec4 operator - (Vec4Arg inV2) const;
177
179 JPH_INLINE Vec4 & operator -= (Vec4Arg inV2);
180
182 JPH_INLINE Vec4 operator / (Vec4Arg inV2) const;
183
185 template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ, uint32 SwizzleW>
186 JPH_INLINE Vec4 Swizzle() const;
187
189 JPH_INLINE Vec4 SplatX() const;
190
192 JPH_INLINE Vec4 SplatY() const;
193
195 JPH_INLINE Vec4 SplatZ() const;
196
198 JPH_INLINE Vec4 SplatW() const;
199
201 JPH_INLINE Vec4 Abs() const;
202
204 JPH_INLINE Vec4 Reciprocal() const;
205
207 JPH_INLINE Vec4 DotV(Vec4Arg inV2) const;
208
210 JPH_INLINE float Dot(Vec4Arg inV2) const;
211
213 JPH_INLINE float LengthSq() const;
214
216 JPH_INLINE float Length() const;
217
219 JPH_INLINE Vec4 Normalized() const;
220
222 JPH_INLINE void StoreFloat4(Float4 *outV) const;
223
225 JPH_INLINE UVec4 ToInt() const;
226
228 JPH_INLINE UVec4 ReinterpretAsInt() const;
229
231 JPH_INLINE int GetSignBits() const;
232
234 JPH_INLINE float ReduceMin() const;
235
237 JPH_INLINE float ReduceMax() const;
238
240 JPH_INLINE Vec4 Sqrt() const;
241
243 JPH_INLINE Vec4 GetSign() const;
244
246 inline void SinCos(Vec4 &outSin, Vec4 &outCos) const;
247
249 inline Vec4 Tan() const;
250
253 inline Vec4 ASin() const;
254
257 inline Vec4 ACos() const;
258
260 inline Vec4 ATan() const;
261
263 inline static Vec4 sATan2(Vec4Arg inY, Vec4Arg inX);
264
266 friend ostream & operator << (ostream &inStream, Vec4Arg inV)
267 {
268 inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2] << ", " << inV.mF32[3];
269 return inStream;
270 }
271
272 union
273 {
275 float mF32[4];
276 };
277};
278
279static_assert(std::is_trivial<Vec4>(), "Is supposed to be a trivial type!");
280
282
283#include "Vec4.inl"
unsigned int uint
Definition Core.h:446
#define JPH_NAMESPACE_END
Definition Core.h:379
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
DVec3 operator*(double inV1, DVec3Arg inV2)
Definition DVec3.inl:451
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:31
JPH_INLINE float Tan(float inX)
Tangent of x (input in radians)
Definition Trigonometry.h:28
JPH_INLINE float ACos(float inX)
Definition Trigonometry.h:42
JPH_INLINE float ASin(float inX)
Definition Trigonometry.h:35
JPH_INLINE float ATan(float inX)
Arc tangent of x (returns value in the range [-PI / 2, PI / 2])
Definition Trigonometry.h:68
Class that holds 4 float values. Convert to Vec4 to perform calculations.
Definition Float4.h:11
Definition UVec4.h:12
Definition Vec3.h:17
Definition Vec4.h:14
float mF32[4]
Definition Vec4.h:275
Vec4()=default
Constructor.
JPH_INLINE void SetW(float inW)
Definition Vec4.h:123
JPH_INLINE float GetW() const
Definition Vec4.h:116
JPH_INLINE Vec4(Type inRHS)
Definition Vec4.h:33
JPH_INLINE void SetZ(float inZ)
Definition Vec4.h:122
Vec4(const Vec4 &inRHS)=default
static JPH_INLINE Vec4 sGatherFloat4(const float *inBase, UVec4Arg inOffsets)
Gather 4 floats from memory at inBase + inOffsets[i] * Scale.
JPH_INLINE void SetY(float inY)
Definition Vec4.h:121
JPH_INLINE void SetX(float inX)
Set individual components.
Definition Vec4.h:120
JPH_INLINE float GetX() const
Get individual components.
Definition Vec4.h:113
{ float mData[4] Type
Definition Vec4.h:24
JPH_INLINE Vec4 Swizzle() const
Swizzle the elements in inV.
Type mValue
Definition Vec4.h:274
JPH_INLINE float GetZ() const
Definition Vec4.h:115
JPH_INLINE void Set(float inX, float inY, float inZ, float inW)
Set all components.
Definition Vec4.h:126
JPH_INLINE float GetY() const
Definition Vec4.h:114