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 explicit JPH_INLINE Vec4(Vec3Arg inRHS);
31 JPH_INLINE Vec4(Vec3Arg inRHS, float inW);
32 JPH_INLINE Vec4(Type inRHS) : mValue(inRHS) { }
33
35 JPH_INLINE Vec4(float inX, float inY, float inZ, float inW);
36
38 static JPH_INLINE Vec4 sZero();
39
41 static JPH_INLINE Vec4 sNaN();
42
44 static JPH_INLINE Vec4 sReplicate(float inV);
45
47 static JPH_INLINE Vec4 sLoadFloat4(const Float4 *inV);
48
50 static JPH_INLINE Vec4 sLoadFloat4Aligned(const Float4 *inV);
51
53 template <const int Scale>
54 static JPH_INLINE Vec4 sGatherFloat4(const float *inBase, UVec4Arg inOffsets);
55
57 static JPH_INLINE Vec4 sMin(Vec4Arg inV1, Vec4Arg inV2);
58
60 static JPH_INLINE Vec4 sMax(Vec4Arg inV1, Vec4Arg inV2);
61
63 static JPH_INLINE UVec4 sEquals(Vec4Arg inV1, Vec4Arg inV2);
64
66 static JPH_INLINE UVec4 sLess(Vec4Arg inV1, Vec4Arg inV2);
67
69 static JPH_INLINE UVec4 sLessOrEqual(Vec4Arg inV1, Vec4Arg inV2);
70
72 static JPH_INLINE UVec4 sGreater(Vec4Arg inV1, Vec4Arg inV2);
73
75 static JPH_INLINE UVec4 sGreaterOrEqual(Vec4Arg inV1, Vec4Arg inV2);
76
78 static JPH_INLINE Vec4 sFusedMultiplyAdd(Vec4Arg inMul1, Vec4Arg inMul2, Vec4Arg inAdd);
79
81 static JPH_INLINE Vec4 sSelect(Vec4Arg inV1, Vec4Arg inV2, UVec4Arg inControl);
82
84 static JPH_INLINE Vec4 sOr(Vec4Arg inV1, Vec4Arg inV2);
85
87 static JPH_INLINE Vec4 sXor(Vec4Arg inV1, Vec4Arg inV2);
88
90 static JPH_INLINE Vec4 sAnd(Vec4Arg inV1, Vec4Arg inV2);
91
94 static JPH_INLINE void sSort4(Vec4 &ioValue, UVec4 &ioIndex);
95
98 static JPH_INLINE void sSort4Reverse(Vec4 &ioValue, UVec4 &ioIndex);
99
101#if defined(JPH_USE_SSE)
102 JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); }
103 JPH_INLINE float GetY() const { return mF32[1]; }
104 JPH_INLINE float GetZ() const { return mF32[2]; }
105 JPH_INLINE float GetW() const { return mF32[3]; }
106#elif defined(JPH_USE_NEON)
107 JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); }
108 JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); }
109 JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); }
110 JPH_INLINE float GetW() const { return vgetq_lane_f32(mValue, 3); }
111#else
112 JPH_INLINE float GetX() const { return mF32[0]; }
113 JPH_INLINE float GetY() const { return mF32[1]; }
114 JPH_INLINE float GetZ() const { return mF32[2]; }
115 JPH_INLINE float GetW() const { return mF32[3]; }
116#endif
117
119 JPH_INLINE void SetX(float inX) { mF32[0] = inX; }
120 JPH_INLINE void SetY(float inY) { mF32[1] = inY; }
121 JPH_INLINE void SetZ(float inZ) { mF32[2] = inZ; }
122 JPH_INLINE void SetW(float inW) { mF32[3] = inW; }
123
125 JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }
126 JPH_INLINE float & operator [] (uint inCoordinate) { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }
127
129 JPH_INLINE bool operator == (Vec4Arg inV2) const;
130 JPH_INLINE bool operator != (Vec4Arg inV2) const { return !(*this == inV2); }
131
133 JPH_INLINE bool IsClose(Vec4Arg inV2, float inMaxDistSq = 1.0e-12f) const;
134
136 JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const;
137
139 JPH_INLINE bool IsNaN() const;
140
142 JPH_INLINE Vec4 operator * (Vec4Arg inV2) const;
143
145 JPH_INLINE Vec4 operator * (float inV2) const;
146
148 friend JPH_INLINE Vec4 operator * (float inV1, Vec4Arg inV2);
149
151 JPH_INLINE Vec4 operator / (float inV2) const;
152
154 JPH_INLINE Vec4 & operator *= (float inV2);
155
157 JPH_INLINE Vec4 & operator *= (Vec4Arg inV2);
158
160 JPH_INLINE Vec4 & operator /= (float inV2);
161
163 JPH_INLINE Vec4 operator + (Vec4Arg inV2) const;
164
166 JPH_INLINE Vec4 & operator += (Vec4Arg inV2);
167
169 JPH_INLINE Vec4 operator - () const;
170
172 JPH_INLINE Vec4 operator - (Vec4Arg inV2) const;
173
175 JPH_INLINE Vec4 & operator -= (Vec4Arg inV2);
176
178 JPH_INLINE Vec4 operator / (Vec4Arg inV2) const;
179
181 template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ, uint32 SwizzleW>
182 JPH_INLINE Vec4 Swizzle() const;
183
185 JPH_INLINE Vec4 SplatX() const;
186
188 JPH_INLINE Vec4 SplatY() const;
189
191 JPH_INLINE Vec4 SplatZ() const;
192
194 JPH_INLINE Vec4 SplatW() const;
195
197 JPH_INLINE Vec4 Abs() const;
198
200 JPH_INLINE Vec4 Reciprocal() const;
201
203 JPH_INLINE Vec4 DotV(Vec4Arg inV2) const;
204
206 JPH_INLINE float Dot(Vec4Arg inV2) const;
207
209 JPH_INLINE float LengthSq() const;
210
212 JPH_INLINE float Length() const;
213
215 JPH_INLINE Vec4 Normalized() const;
216
218 JPH_INLINE void StoreFloat4(Float4 *outV) const;
219
221 JPH_INLINE UVec4 ToInt() const;
222
224 JPH_INLINE UVec4 ReinterpretAsInt() const;
225
227 JPH_INLINE int GetSignBits() const;
228
230 JPH_INLINE float ReduceMin() const;
231
233 JPH_INLINE float ReduceMax() const;
234
236 JPH_INLINE Vec4 Sqrt() const;
237
239 JPH_INLINE Vec4 GetSign() const;
240
242 inline void SinCos(Vec4 &outSin, Vec4 &outCos) const;
243
245 inline Vec4 Tan() const;
246
249 inline Vec4 ASin() const;
250
253 inline Vec4 ACos() const;
254
256 inline Vec4 ATan() const;
257
259 inline static Vec4 sATan2(Vec4Arg inY, Vec4Arg inX);
260
262 friend ostream & operator << (ostream &inStream, Vec4Arg inV)
263 {
264 inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2] << ", " << inV.mF32[3];
265 return inStream;
266 }
267
268 union
269 {
271 float mF32[4];
272 };
273};
274
275static_assert(is_trivial<Vec4>(), "Is supposed to be a trivial type!");
276
278
279#include "Vec4.inl"
unsigned int uint
Definition: Core.h:309
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
DVec3 operator*(double inV1, DVec3Arg inV2)
Definition: DVec3.inl:447
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
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:48
Class that holds 4 float values. Convert to Vec4 to perform calculations.
Definition: Float4.h:11
Definition: UVec4.h:12
Definition: Vec3.h:16
Definition: Vec4.h:14
float mF32[4]
Definition: Vec4.h:271
Vec4()=default
Constructor.
JPH_INLINE void SetW(float inW)
Definition: Vec4.h:122
JPH_INLINE float GetW() const
Definition: Vec4.h:115
JPH_INLINE Vec4(Type inRHS)
Definition: Vec4.h:32
JPH_INLINE void SetZ(float inZ)
Definition: Vec4.h:121
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:120
JPH_INLINE void SetX(float inX)
Set individual components.
Definition: Vec4.h:119
JPH_INLINE float GetX() const
Get individual components.
Definition: Vec4.h:112
{ float mData[4] Type
Definition: Vec4.h:24
JPH_INLINE Vec4 Swizzle() const
Swizzle the elements in inV.
Type mValue
Definition: Vec4.h:270
JPH_INLINE float GetZ() const
Definition: Vec4.h:114
JPH_INLINE float GetY() const
Definition: Vec4.h:113