Debian Codegraph @

thirdparty/jolt_physics/Jolt/Math/Vec4.h

raw symbols SCIP
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 /// Clamp a vector between min and max (component wise)
67 static JPH_INLINE Vec4 sClamp(Vec4Arg inV, Vec4Arg inMin, Vec4Arg inMax);
68
69 /// Equals (component wise)
70 static JPH_INLINE UVec4 sEquals(Vec4Arg inV1, Vec4Arg inV2);
71
72 /// Less than (component wise)
73 static JPH_INLINE UVec4 sLess(Vec4Arg inV1, Vec4Arg inV2);
74
75 /// Less than or equal (component wise)
76 static JPH_INLINE UVec4 sLessOrEqual(Vec4Arg inV1, Vec4Arg inV2);
77
78 /// Greater than (component wise)
79 static JPH_INLINE UVec4 sGreater(Vec4Arg inV1, Vec4Arg inV2);
80
81 /// Greater than or equal (component wise)
82 static JPH_INLINE UVec4 sGreaterOrEqual(Vec4Arg inV1, Vec4Arg inV2);
83
84 /// Calculates inMul1 * inMul2 + inAdd
85 static JPH_INLINE Vec4 sFusedMultiplyAdd(Vec4Arg inMul1, Vec4Arg inMul2, Vec4Arg inAdd);
86
87 /// Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit of inControl = 1
88 static JPH_INLINE Vec4 sSelect(Vec4Arg inNotSet, Vec4Arg inSet, UVec4Arg inControl);
89
90 /// Logical or (component wise)
91 static JPH_INLINE Vec4 sOr(Vec4Arg inV1, Vec4Arg inV2);
92
93 /// Logical xor (component wise)
94 static JPH_INLINE Vec4 sXor(Vec4Arg inV1, Vec4Arg inV2);
95
96 /// Logical and (component wise)
97 static JPH_INLINE Vec4 sAnd(Vec4Arg inV1, Vec4Arg inV2);
98
99 /// Sort the four elements of ioValue and sort ioIndex at the same time.
100 /// Based on a sorting network: http://en.wikipedia.org/wiki/Sorting_network
101 static JPH_INLINE void sSort4(Vec4 &ioValue, UVec4 &ioIndex);
102
103 /// Reverse sort the four elements of ioValue (highest first) and sort ioIndex at the same time.
104 /// Based on a sorting network: http://en.wikipedia.org/wiki/Sorting_network
105 static JPH_INLINE void sSort4Reverse(Vec4 &ioValue, UVec4 &ioIndex);
106
107 /// Get individual components
108 #if defined(JPH_USE_SSE)
109 JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); }
110 JPH_INLINE float GetY() const { return mF32[1]; }
111 JPH_INLINE float GetZ() const { return mF32[2]; }
112 JPH_INLINE float GetW() const { return mF32[3]; }
113 #elif defined(JPH_USE_NEON)
114 JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); }
115 JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); }
116 JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); }
117 JPH_INLINE float GetW() const { return vgetq_lane_f32(mValue, 3); }
118 #else
119 JPH_INLINE float GetX() const { return mF32[0]; }
120 JPH_INLINE float GetY() const { return mF32[1]; }
121 JPH_INLINE float GetZ() const { return mF32[2]; }
122 JPH_INLINE float GetW() const { return mF32[3]; }
123 #endif
124
125 /// Set individual components
126 JPH_INLINE void SetX(float inX) { mF32[0] = inX; }
127 JPH_INLINE void SetY(float inY) { mF32[1] = inY; }
128 JPH_INLINE void SetZ(float inZ) { mF32[2] = inZ; }
129 JPH_INLINE void SetW(float inW) { mF32[3] = inW; }
130
131 /// Set all components
132 JPH_INLINE void Set(float inX, float inY, float inZ, float inW) { *this = Vec4(inX, inY, inZ, inW); }
133
134 /// Get float component by index
135 JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }
136 JPH_INLINE float & operator [] (uint inCoordinate) { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }
137
138 /// Comparison
139 JPH_INLINE bool operator == (Vec4Arg inV2) const;
140 JPH_INLINE bool operator != (Vec4Arg inV2) const { return !(*this == inV2); }
141
142 /// Test if two vectors are close
143 JPH_INLINE bool IsClose(Vec4Arg inV2, float inMaxDistSq = 1.0e-12f) const;
144
145 /// Test if vector is near zero
146 JPH_INLINE bool IsNearZero(float inMaxDistSq = 1.0e-12f) const;
147
148 /// Test if vector is normalized
149 JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const;
150
151 /// Test if vector contains NaN elements
152 JPH_INLINE bool IsNaN() const;
153
154 /// Multiply two float vectors (component wise)
155 JPH_INLINE Vec4 operator * (Vec4Arg inV2) const;
156
157 /// Multiply vector with float
158 JPH_INLINE Vec4 operator * (float inV2) const;
159
160 /// Multiply vector with float
161 friend JPH_INLINE Vec4 operator * (float inV1, Vec4Arg inV2);
162
163 /// Divide vector by float
164 JPH_INLINE Vec4 operator / (float inV2) const;
165
166 /// Multiply vector with float
167 JPH_INLINE Vec4 & operator *= (float inV2);
168
169 /// Multiply vector with vector
170 JPH_INLINE Vec4 & operator *= (Vec4Arg inV2);
171
172 /// Divide vector by float
173 JPH_INLINE Vec4 & operator /= (float inV2);
174
175 /// Add two float vectors (component wise)
176 JPH_INLINE Vec4 operator + (Vec4Arg inV2) const;
177
178 /// Add two float vectors (component wise)
179 JPH_INLINE Vec4 & operator += (Vec4Arg inV2);
180
181 /// Negate
182 JPH_INLINE Vec4 operator - () const;
183
184 /// Subtract two float vectors (component wise)
185 JPH_INLINE Vec4 operator - (Vec4Arg inV2) const;
186
187 /// Subtract two float vectors (component wise)
188 JPH_INLINE Vec4 & operator -= (Vec4Arg inV2);
189
190 /// Divide (component wise)
191 JPH_INLINE Vec4 operator / (Vec4Arg inV2) const;
192
193 /// Swizzle the elements in inV
194 template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ, uint32 SwizzleW>
195 JPH_INLINE Vec4 Swizzle() const;
196
197 /// Replicate the X component to all components
198 JPH_INLINE Vec4 SplatX() const;
199
200 /// Replicate the Y component to all components
201 JPH_INLINE Vec4 SplatY() const;
202
203 /// Replicate the Z component to all components
204 JPH_INLINE Vec4 SplatZ() const;
205
206 /// Replicate the W component to all components
207 JPH_INLINE Vec4 SplatW() const;
208
209 /// Replicate the X component to all components
210 JPH_INLINE Vec3 SplatX3() const;
211
212 /// Replicate the Y component to all components
213 JPH_INLINE Vec3 SplatY3() const;
214
215 /// Replicate the Z component to all components
216 JPH_INLINE Vec3 SplatZ3() const;
217
218 /// Replicate the W component to all components
219 JPH_INLINE Vec3 SplatW3() const;
220
221 /// Get index of component with lowest value
222 JPH_INLINE int GetLowestComponentIndex() const;
223
224 /// Get index of component with highest value
225 JPH_INLINE int GetHighestComponentIndex() const;
226
227 /// Return the absolute value of each of the components
228 JPH_INLINE Vec4 Abs() const;
229
230 /// Reciprocal vector (1 / value) for each of the components
231 JPH_INLINE Vec4 Reciprocal() const;
232
233 /// Dot product, returns the dot product in X, Y, Z and W components
234 JPH_INLINE Vec4 DotV(Vec4Arg inV2) const;
235
236 /// Dot product
237 JPH_INLINE float Dot(Vec4Arg inV2) const;
238
239 /// Squared length of vector
240 JPH_INLINE float LengthSq() const;
241
242 /// Length of vector
243 JPH_INLINE float Length() const;
244
245 /// Normalize vector
246 JPH_INLINE Vec4 Normalized() const;
247
248 /// Store 4 floats to memory
249 JPH_INLINE void StoreFloat4(Float4 *outV) const;
250
251 /// Convert each component from a float to an int
252 JPH_INLINE UVec4 ToInt() const;
253
254 /// Reinterpret Vec4 as a UVec4 (doesn't change the bits)
255 JPH_INLINE UVec4 ReinterpretAsInt() const;
256
257 /// Store if X is negative in bit 0, Y in bit 1, Z in bit 2 and W in bit 3
258 JPH_INLINE int GetSignBits() const;
259
260 /// Get the minimum of X, Y, Z and W
261 JPH_INLINE float ReduceMin() const;
262
263 /// Get the maximum of X, Y, Z and W
264 JPH_INLINE float ReduceMax() const;
265
266 /// Component wise square root
267 JPH_INLINE Vec4 Sqrt() const;
268
269 /// Get vector that contains the sign of each element (returns 1.0f if positive, -1.0f if negative)
270 JPH_INLINE Vec4 GetSign() const;
271
272 /// Flips the signs of the components, e.g. FlipSign<-1, 1, -1, 1>() will flip the signs of the X and Z components
273 template <int X, int Y, int Z, int W>
274 JPH_INLINE Vec4 FlipSign() const;
275
276 /// Calculate the sine and cosine for each element of this vector (input in radians)
277 inline void SinCos(Vec4 &outSin, Vec4 &outCos) const;
278
279 /// Calculate the tangent for each element of this vector (input in radians)
280 inline Vec4 Tan() const;
281
282 /// Calculate the arc sine for each element of this vector (returns value in the range [-PI / 2, PI / 2])
283 /// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::asin
284 inline Vec4 ASin() const;
285
286 /// Calculate the arc cosine for each element of this vector (returns value in the range [0, PI])
287 /// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::acos
288 inline Vec4 ACos() const;
289
290 /// Calculate the arc tangent for each element of this vector (returns value in the range [-PI / 2, PI / 2])
291 inline Vec4 ATan() const;
292
293 /// 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])
294 inline static Vec4 sATan2(Vec4Arg inY, Vec4Arg inX);
295
296 /// Compress a unit vector to a 32 bit value, precision is around 0.5 * 10^-3
297 JPH_INLINE uint32 CompressUnitVector() const;
298
299 /// Decompress a unit vector from a 32 bit value
300 JPH_INLINE static Vec4 sDecompressUnitVector(uint32 inValue);
301
302 /// To String
303 friend ostream & operator << (ostream &inStream, Vec4Arg inV)
304 {
305 inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2] << ", " << inV.mF32[3];
306 return inStream;
307 }
308
309 union
310 {
311 Type mValue;
312 float mF32[4];
313 };
314 };
315
316 static_assert(std::is_trivial<Vec4>(), "Is supposed to be a trivial type!");
317
318 JPH_NAMESPACE_END
319
320 #include "Vec4.inl"