Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Math/Vec4.h
9913 views
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>
9
#include <Jolt/Math/MathTypes.h>
10
11
JPH_NAMESPACE_BEGIN
12
13
class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Vec4
14
{
15
public:
16
JPH_OVERRIDE_NEW_DELETE
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
27
/// Constructor
28
Vec4() = default; ///< Intentionally not initialized for performance reasons
29
Vec4(const Vec4 &inRHS) = default;
30
Vec4 & operator = (const Vec4 &inRHS) = default;
31
explicit JPH_INLINE Vec4(Vec3Arg inRHS); ///< WARNING: W component undefined!
32
JPH_INLINE Vec4(Vec3Arg inRHS, float inW);
33
JPH_INLINE Vec4(Type inRHS) : mValue(inRHS) { }
34
35
/// Create a vector from 4 components
36
JPH_INLINE Vec4(float inX, float inY, float inZ, float inW);
37
38
/// Vector with all zeros
39
static JPH_INLINE Vec4 sZero();
40
41
/// Vector with all ones
42
static JPH_INLINE Vec4 sOne();
43
44
/// Vector with all NaN's
45
static JPH_INLINE Vec4 sNaN();
46
47
/// Replicate inV across all components
48
static JPH_INLINE Vec4 sReplicate(float inV);
49
50
/// Load 4 floats from memory
51
static JPH_INLINE Vec4 sLoadFloat4(const Float4 *inV);
52
53
/// Load 4 floats from memory, 16 bytes aligned
54
static JPH_INLINE Vec4 sLoadFloat4Aligned(const Float4 *inV);
55
56
/// Gather 4 floats from memory at inBase + inOffsets[i] * Scale
57
template <const int Scale>
58
static JPH_INLINE Vec4 sGatherFloat4(const float *inBase, UVec4Arg inOffsets);
59
60
/// Return the minimum value of each of the components
61
static JPH_INLINE Vec4 sMin(Vec4Arg inV1, Vec4Arg inV2);
62
63
/// Return the maximum of each of the components
64
static JPH_INLINE Vec4 sMax(Vec4Arg inV1, Vec4Arg inV2);
65
66
/// Equals (component wise)
67
static JPH_INLINE UVec4 sEquals(Vec4Arg inV1, Vec4Arg inV2);
68
69
/// Less than (component wise)
70
static JPH_INLINE UVec4 sLess(Vec4Arg inV1, Vec4Arg inV2);
71
72
/// Less than or equal (component wise)
73
static JPH_INLINE UVec4 sLessOrEqual(Vec4Arg inV1, Vec4Arg inV2);
74
75
/// Greater than (component wise)
76
static JPH_INLINE UVec4 sGreater(Vec4Arg inV1, Vec4Arg inV2);
77
78
/// Greater than or equal (component wise)
79
static JPH_INLINE UVec4 sGreaterOrEqual(Vec4Arg inV1, Vec4Arg inV2);
80
81
/// Calculates inMul1 * inMul2 + inAdd
82
static JPH_INLINE Vec4 sFusedMultiplyAdd(Vec4Arg inMul1, Vec4Arg inMul2, Vec4Arg inAdd);
83
84
/// Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit of inControl = 1
85
static JPH_INLINE Vec4 sSelect(Vec4Arg inNotSet, Vec4Arg inSet, UVec4Arg inControl);
86
87
/// Logical or (component wise)
88
static JPH_INLINE Vec4 sOr(Vec4Arg inV1, Vec4Arg inV2);
89
90
/// Logical xor (component wise)
91
static JPH_INLINE Vec4 sXor(Vec4Arg inV1, Vec4Arg inV2);
92
93
/// Logical and (component wise)
94
static JPH_INLINE Vec4 sAnd(Vec4Arg inV1, Vec4Arg inV2);
95
96
/// Sort the four elements of ioValue and sort ioIndex at the same time.
97
/// Based on a sorting network: http://en.wikipedia.org/wiki/Sorting_network
98
static JPH_INLINE void sSort4(Vec4 &ioValue, UVec4 &ioIndex);
99
100
/// Reverse sort the four elements of ioValue (highest first) and sort ioIndex at the same time.
101
/// Based on a sorting network: http://en.wikipedia.org/wiki/Sorting_network
102
static JPH_INLINE void sSort4Reverse(Vec4 &ioValue, UVec4 &ioIndex);
103
104
/// Get individual components
105
#if defined(JPH_USE_SSE)
106
JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); }
107
JPH_INLINE float GetY() const { return mF32[1]; }
108
JPH_INLINE float GetZ() const { return mF32[2]; }
109
JPH_INLINE float GetW() const { return mF32[3]; }
110
#elif defined(JPH_USE_NEON)
111
JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); }
112
JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); }
113
JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); }
114
JPH_INLINE float GetW() const { return vgetq_lane_f32(mValue, 3); }
115
#else
116
JPH_INLINE float GetX() const { return mF32[0]; }
117
JPH_INLINE float GetY() const { return mF32[1]; }
118
JPH_INLINE float GetZ() const { return mF32[2]; }
119
JPH_INLINE float GetW() const { return mF32[3]; }
120
#endif
121
122
/// Set individual components
123
JPH_INLINE void SetX(float inX) { mF32[0] = inX; }
124
JPH_INLINE void SetY(float inY) { mF32[1] = inY; }
125
JPH_INLINE void SetZ(float inZ) { mF32[2] = inZ; }
126
JPH_INLINE void SetW(float inW) { mF32[3] = inW; }
127
128
/// Set all components
129
JPH_INLINE void Set(float inX, float inY, float inZ, float inW) { *this = Vec4(inX, inY, inZ, inW); }
130
131
/// Get float component by index
132
JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }
133
JPH_INLINE float & operator [] (uint inCoordinate) { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }
134
135
/// Comparison
136
JPH_INLINE bool operator == (Vec4Arg inV2) const;
137
JPH_INLINE bool operator != (Vec4Arg inV2) const { return !(*this == inV2); }
138
139
/// Test if two vectors are close
140
JPH_INLINE bool IsClose(Vec4Arg inV2, float inMaxDistSq = 1.0e-12f) const;
141
142
/// Test if vector is normalized
143
JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const;
144
145
/// Test if vector contains NaN elements
146
JPH_INLINE bool IsNaN() const;
147
148
/// Multiply two float vectors (component wise)
149
JPH_INLINE Vec4 operator * (Vec4Arg inV2) const;
150
151
/// Multiply vector with float
152
JPH_INLINE Vec4 operator * (float inV2) const;
153
154
/// Multiply vector with float
155
friend JPH_INLINE Vec4 operator * (float inV1, Vec4Arg inV2);
156
157
/// Divide vector by float
158
JPH_INLINE Vec4 operator / (float inV2) const;
159
160
/// Multiply vector with float
161
JPH_INLINE Vec4 & operator *= (float inV2);
162
163
/// Multiply vector with vector
164
JPH_INLINE Vec4 & operator *= (Vec4Arg inV2);
165
166
/// Divide vector by float
167
JPH_INLINE Vec4 & operator /= (float inV2);
168
169
/// Add two float vectors (component wise)
170
JPH_INLINE Vec4 operator + (Vec4Arg inV2) const;
171
172
/// Add two float vectors (component wise)
173
JPH_INLINE Vec4 & operator += (Vec4Arg inV2);
174
175
/// Negate
176
JPH_INLINE Vec4 operator - () const;
177
178
/// Subtract two float vectors (component wise)
179
JPH_INLINE Vec4 operator - (Vec4Arg inV2) const;
180
181
/// Subtract two float vectors (component wise)
182
JPH_INLINE Vec4 & operator -= (Vec4Arg inV2);
183
184
/// Divide (component wise)
185
JPH_INLINE Vec4 operator / (Vec4Arg inV2) const;
186
187
/// Swizzle the elements in inV
188
template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ, uint32 SwizzleW>
189
JPH_INLINE Vec4 Swizzle() const;
190
191
/// Replicate the X component to all components
192
JPH_INLINE Vec4 SplatX() const;
193
194
/// Replicate the Y component to all components
195
JPH_INLINE Vec4 SplatY() const;
196
197
/// Replicate the Z component to all components
198
JPH_INLINE Vec4 SplatZ() const;
199
200
/// Replicate the W component to all components
201
JPH_INLINE Vec4 SplatW() const;
202
203
/// Return the absolute value of each of the components
204
JPH_INLINE Vec4 Abs() const;
205
206
/// Reciprocal vector (1 / value) for each of the components
207
JPH_INLINE Vec4 Reciprocal() const;
208
209
/// Dot product, returns the dot product in X, Y and Z components
210
JPH_INLINE Vec4 DotV(Vec4Arg inV2) const;
211
212
/// Dot product
213
JPH_INLINE float Dot(Vec4Arg inV2) const;
214
215
/// Squared length of vector
216
JPH_INLINE float LengthSq() const;
217
218
/// Length of vector
219
JPH_INLINE float Length() const;
220
221
/// Normalize vector
222
JPH_INLINE Vec4 Normalized() const;
223
224
/// Store 4 floats to memory
225
JPH_INLINE void StoreFloat4(Float4 *outV) const;
226
227
/// Convert each component from a float to an int
228
JPH_INLINE UVec4 ToInt() const;
229
230
/// Reinterpret Vec4 as a UVec4 (doesn't change the bits)
231
JPH_INLINE UVec4 ReinterpretAsInt() const;
232
233
/// Store if X is negative in bit 0, Y in bit 1, Z in bit 2 and W in bit 3
234
JPH_INLINE int GetSignBits() const;
235
236
/// Get the minimum of X, Y, Z and W
237
JPH_INLINE float ReduceMin() const;
238
239
/// Get the maximum of X, Y, Z and W
240
JPH_INLINE float ReduceMax() const;
241
242
/// Component wise square root
243
JPH_INLINE Vec4 Sqrt() const;
244
245
/// Get vector that contains the sign of each element (returns 1.0f if positive, -1.0f if negative)
246
JPH_INLINE Vec4 GetSign() const;
247
248
/// Calculate the sine and cosine for each element of this vector (input in radians)
249
inline void SinCos(Vec4 &outSin, Vec4 &outCos) const;
250
251
/// Calculate the tangent for each element of this vector (input in radians)
252
inline Vec4 Tan() const;
253
254
/// Calculate the arc sine for each element of this vector (returns value in the range [-PI / 2, PI / 2])
255
/// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::asin
256
inline Vec4 ASin() const;
257
258
/// Calculate the arc cosine for each element of this vector (returns value in the range [0, PI])
259
/// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::acos
260
inline Vec4 ACos() const;
261
262
/// Calculate the arc tangent for each element of this vector (returns value in the range [-PI / 2, PI / 2])
263
inline Vec4 ATan() const;
264
265
/// Calculate the arc tangent of y / x using the signs of the arguments to determine the correct quadrant (returns value in the range [-PI, PI])
266
inline static Vec4 sATan2(Vec4Arg inY, Vec4Arg inX);
267
268
/// To String
269
friend ostream & operator << (ostream &inStream, Vec4Arg inV)
270
{
271
inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2] << ", " << inV.mF32[3];
272
return inStream;
273
}
274
275
union
276
{
277
Type mValue;
278
float mF32[4];
279
};
280
};
281
282
static_assert(std::is_trivial<Vec4>(), "Is supposed to be a trivial type!");
283
284
JPH_NAMESPACE_END
285
286
#include "Vec4.inl"
287
288