Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Vec3.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
8#include <Jolt/Math/Float3.h>
9#include <Jolt/Math/Swizzle.h>
10#include <Jolt/Math/MathTypes.h>
11
13
16class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Vec3
17{
18public:
20
21 // Underlying vector type
22#if defined(JPH_USE_SSE)
23 using Type = __m128;
24#elif defined(JPH_USE_NEON)
25 using Type = float32x4_t;
26#elif defined(JPH_USE_VSX)
27 using Type = __vector float;
28#else
30#endif
31
32 // Argument type
34
36 Vec3() = default;
37 Vec3(const Vec3 &inRHS) = default;
38 Vec3 & operator = (const Vec3 &inRHS) = default;
39 explicit JPH_INLINE Vec3(Vec4Arg inRHS);
40 JPH_INLINE Vec3(Type inRHS) : mValue(inRHS) { CheckW(); }
41
43 explicit JPH_INLINE Vec3(const Float3 &inV);
44
46 JPH_INLINE Vec3(float inX, float inY, float inZ);
47
49 static JPH_INLINE Vec3 sZero();
50
52 static JPH_INLINE Vec3 sOne();
53
55 static JPH_INLINE Vec3 sNaN();
56
58 static JPH_INLINE Vec3 sAxisX() { return Vec3(1, 0, 0); }
59 static JPH_INLINE Vec3 sAxisY() { return Vec3(0, 1, 0); }
60 static JPH_INLINE Vec3 sAxisZ() { return Vec3(0, 0, 1); }
61
63 static JPH_INLINE Vec3 sReplicate(float inV);
64
66 static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV);
67
69 static JPH_INLINE Vec3 sMin(Vec3Arg inV1, Vec3Arg inV2);
70
72 static JPH_INLINE Vec3 sMax(Vec3Arg inV1, Vec3Arg inV2);
73
75 static JPH_INLINE Vec3 sClamp(Vec3Arg inV, Vec3Arg inMin, Vec3Arg inMax);
76
78 static JPH_INLINE UVec4 sEquals(Vec3Arg inV1, Vec3Arg inV2);
79
81 static JPH_INLINE UVec4 sLess(Vec3Arg inV1, Vec3Arg inV2);
82
84 static JPH_INLINE UVec4 sLessOrEqual(Vec3Arg inV1, Vec3Arg inV2);
85
87 static JPH_INLINE UVec4 sGreater(Vec3Arg inV1, Vec3Arg inV2);
88
90 static JPH_INLINE UVec4 sGreaterOrEqual(Vec3Arg inV1, Vec3Arg inV2);
91
93 static JPH_INLINE Vec3 sFusedMultiplyAdd(Vec3Arg inMul1, Vec3Arg inMul2, Vec3Arg inAdd);
94
96 static JPH_INLINE Vec3 sSelect(Vec3Arg inNotSet, Vec3Arg inSet, UVec4Arg inControl);
97
99 static JPH_INLINE Vec3 sOr(Vec3Arg inV1, Vec3Arg inV2);
100
102 static JPH_INLINE Vec3 sXor(Vec3Arg inV1, Vec3Arg inV2);
103
105 static JPH_INLINE Vec3 sAnd(Vec3Arg inV1, Vec3Arg inV2);
106
110 static JPH_INLINE Vec3 sUnitSpherical(float inTheta, float inPhi);
111
114
116 template <class Random>
117 static inline Vec3 sRandom(Random &inRandom);
118
120#if defined(JPH_USE_SSE)
121 JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); }
122 JPH_INLINE float GetY() const { return mF32[1]; }
123 JPH_INLINE float GetZ() const { return mF32[2]; }
124#elif defined(JPH_USE_NEON)
125 JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); }
126 JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); }
127 JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); }
128#elif defined(JPH_USE_VSX)
129 JPH_INLINE float GetX() const { return vec_extract(mValue, 0); }
130 JPH_INLINE float GetY() const { return vec_extract(mValue, 1); }
131 JPH_INLINE float GetZ() const { return vec_extract(mValue, 2); }
132#else
133 JPH_INLINE float GetX() const { return mF32[0]; }
134 JPH_INLINE float GetY() const { return mF32[1]; }
135 JPH_INLINE float GetZ() const { return mF32[2]; }
136#endif
137
139 JPH_INLINE void SetX(float inX) { mF32[0] = inX; }
140 JPH_INLINE void SetY(float inY) { mF32[1] = inY; }
141 JPH_INLINE void SetZ(float inZ) { mF32[2] = mF32[3] = inZ; } // Assure Z and W are the same
142
144 JPH_INLINE void Set(float inX, float inY, float inZ) { *this = Vec3(inX, inY, inZ); }
145
147 JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 3); return mF32[inCoordinate]; }
148
150 JPH_INLINE void SetComponent(uint inCoordinate, float inValue) { JPH_ASSERT(inCoordinate < 3); mF32[inCoordinate] = inValue; mValue = sFixW(mValue); } // Assure Z and W are the same
151
153 JPH_INLINE bool operator == (Vec3Arg inV2) const;
154 JPH_INLINE bool operator != (Vec3Arg inV2) const { return !(*this == inV2); }
155
157 JPH_INLINE bool IsClose(Vec3Arg inV2, float inMaxDistSq = 1.0e-12f) const;
158
160 JPH_INLINE bool IsNearZero(float inMaxDistSq = 1.0e-12f) const;
161
163 JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const;
164
166 JPH_INLINE bool IsNaN() const;
167
169 JPH_INLINE Vec3 operator * (Vec3Arg inV2) const;
170
172 JPH_INLINE Vec3 operator * (float inV2) const;
173
175 friend JPH_INLINE Vec3 operator * (float inV1, Vec3Arg inV2);
176
178 JPH_INLINE Vec3 operator / (float inV2) const;
179
181 JPH_INLINE Vec3 & operator *= (float inV2);
182
184 JPH_INLINE Vec3 & operator *= (Vec3Arg inV2);
185
187 JPH_INLINE Vec3 & operator /= (float inV2);
188
190 JPH_INLINE Vec3 operator + (Vec3Arg inV2) const;
191
193 JPH_INLINE Vec3 & operator += (Vec3Arg inV2);
194
196 JPH_INLINE Vec3 operator - () const;
197
199 JPH_INLINE Vec3 operator - (Vec3Arg inV2) const;
200
202 JPH_INLINE Vec3 & operator -= (Vec3Arg inV2);
203
205 JPH_INLINE Vec3 operator / (Vec3Arg inV2) const;
206
208 template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ>
209 JPH_INLINE Vec3 Swizzle() const;
210
212 JPH_INLINE Vec4 SplatX() const;
213
215 JPH_INLINE Vec4 SplatY() const;
216
218 JPH_INLINE Vec4 SplatZ() const;
219
221 JPH_INLINE int GetLowestComponentIndex() const;
222
224 JPH_INLINE int GetHighestComponentIndex() const;
225
227 JPH_INLINE Vec3 Abs() const;
228
230 JPH_INLINE Vec3 Reciprocal() const;
231
233 JPH_INLINE static Vec3 sDifferenceOfProducts(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD);
234
236 JPH_INLINE Vec3 Cross(Vec3Arg inV2) const;
237
239 JPH_INLINE Vec3 CrossPrecise(Vec3Arg inV2) const;
240
242 JPH_INLINE Vec3 DotV(Vec3Arg inV2) const;
243
245 JPH_INLINE Vec4 DotV4(Vec3Arg inV2) const;
246
248 JPH_INLINE float Dot(Vec3Arg inV2) const;
249
251 JPH_INLINE float LengthSq() const;
252
254 JPH_INLINE float Length() const;
255
257 JPH_INLINE Vec3 Normalized() const;
258
260 JPH_INLINE Vec3 NormalizedOr(Vec3Arg inZeroValue) const;
261
263 JPH_INLINE void StoreFloat3(Float3 *outV) const;
264
266 JPH_INLINE UVec4 ToInt() const;
267
269 JPH_INLINE UVec4 ReinterpretAsInt() const;
270
272 JPH_INLINE float ReduceMin() const;
273
275 JPH_INLINE float ReduceMax() const;
276
278 JPH_INLINE float ReduceSum() const;
279
281 JPH_INLINE Vec3 Sqrt() const;
282
284 JPH_INLINE Vec3 GetNormalizedPerpendicular() const;
285
287 JPH_INLINE Vec3 GetSign() const;
288
290 template <int X, int Y, int Z>
291 JPH_INLINE Vec3 FlipSign() const;
292
294 JPH_INLINE uint32 CompressUnitVector() const;
295
297 JPH_INLINE static Vec3 sDecompressUnitVector(uint32 inValue);
298
300 friend ostream & operator << (ostream &inStream, Vec3Arg inV)
301 {
302 inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2];
303 return inStream;
304 }
305
307 JPH_INLINE void CheckW() const;
308
310 static JPH_INLINE Type sFixW(Type inValue);
311
312 union
313 {
315 float mF32[4];
316 };
317};
318
319static_assert(std::is_trivially_default_constructible<Vec3>() && std::is_trivially_copyable<Vec3>(), "Is supposed to be a trivial type!");
320
322
323#include "Vec3.inl"
#define JPH_EXPORT
Definition Core.h:310
unsigned int uint
Definition Core.h:577
#define JPH_NAMESPACE_END
Definition Core.h:494
std::uint32_t uint32
Definition Core.h:580
#define JPH_NAMESPACE_BEGIN
Definition Core.h:488
DVec3 operator*(double inV1, DVec3Arg inV2)
Definition DVec3.inl:623
#define JPH_ASSERT(...)
Definition IssueReporting.h:28
JPH_INLINE float Sqrt(float inV)
Take the square root of a float value.
Definition Math.h:76
const Vec3 Vec3Arg
Definition MathTypes.h:19
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:51
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition Float3.h:13
Simple variable length array backed by a fixed size buffer.
Definition StaticArray.h:14
Definition UVec4.h:12
Definition Vec3.h:17
static JPH_INLINE Type sFixW(Type inValue)
Internal helper function that ensures that the Z component is replicated to the W component to preven...
Vec4::Type Type
Definition Vec3.h:29
static JPH_INLINE Vec3 sAxisX()
Vectors with the principal axis.
Definition Vec3.h:58
JPH_INLINE void SetComponent(uint inCoordinate, float inValue)
Set float component by index.
Definition Vec3.h:150
Vec3(const Vec3 &inRHS)=default
static JPH_INLINE Vec3 sAxisY()
Definition Vec3.h:59
JPH_INLINE float GetX() const
Get individual components.
Definition Vec3.h:133
JPH_INLINE void SetY(float inY)
Definition Vec3.h:140
JPH_INLINE Vec3(Type inRHS)
Definition Vec3.h:40
JPH_INLINE void SetZ(float inZ)
Definition Vec3.h:141
JPH_INLINE void CheckW() const
Internal helper function that checks that W is equal to Z, so e.g. dividing by it should not generate...
static JPH_INLINE Vec3 sAxisZ()
Definition Vec3.h:60
JPH_INLINE void SetX(float inX)
Set individual components.
Definition Vec3.h:139
Type mValue
Definition Vec3.h:314
JPH_INLINE float GetY() const
Definition Vec3.h:134
float mF32[4]
Definition Vec3.h:315
JPH_INLINE void Set(float inX, float inY, float inZ)
Set all components.
Definition Vec3.h:144
JPH_INLINE float GetZ() const
Definition Vec3.h:135
Vec3()=default
Constructor.
static JPH_EXPORT const StaticArray< Vec3, 1026 > sUnitSphere
A set of vectors uniformly spanning the surface of a unit sphere, usable for debug purposes.
Definition Vec3.h:66
JPH_INLINE Vec3 Swizzle() const
Swizzle the elements in inV.
Definition Vec4.h:14
struct { float mData[4];} Type
Definition Vec4.h:26