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
7#include <Jolt/Math/Float3.h>
8#include <Jolt/Math/Swizzle.h>
10
12
15class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Vec3
16{
17public:
19
20 // Underlying vector type
21#if defined(JPH_USE_SSE)
22 using Type = __m128;
23#elif defined(JPH_USE_NEON)
24 using Type = float32x4_t;
25#else
27#endif
28
29 // Argument type
31
33 Vec3() = default;
34 Vec3(const Vec3 &inRHS) = default;
35 Vec3 & operator = (const Vec3 &inRHS) = default;
36 explicit JPH_INLINE Vec3(Vec4Arg inRHS);
37 JPH_INLINE Vec3(Type inRHS) : mValue(inRHS) { CheckW(); }
38
40 explicit JPH_INLINE Vec3(const Float3 &inV);
41
43 JPH_INLINE Vec3(float inX, float inY, float inZ);
44
46 static JPH_INLINE Vec3 sZero();
47
49 static JPH_INLINE Vec3 sNaN();
50
52 static JPH_INLINE Vec3 sAxisX() { return Vec3(1, 0, 0); }
53 static JPH_INLINE Vec3 sAxisY() { return Vec3(0, 1, 0); }
54 static JPH_INLINE Vec3 sAxisZ() { return Vec3(0, 0, 1); }
55
57 static JPH_INLINE Vec3 sReplicate(float inV);
58
60 static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV);
61
63 static JPH_INLINE Vec3 sMin(Vec3Arg inV1, Vec3Arg inV2);
64
66 static JPH_INLINE Vec3 sMax(Vec3Arg inV1, Vec3Arg inV2);
67
69 static JPH_INLINE Vec3 sClamp(Vec3Arg inV, Vec3Arg inMin, Vec3Arg inMax);
70
72 static JPH_INLINE UVec4 sEquals(Vec3Arg inV1, Vec3Arg inV2);
73
75 static JPH_INLINE UVec4 sLess(Vec3Arg inV1, Vec3Arg inV2);
76
78 static JPH_INLINE UVec4 sLessOrEqual(Vec3Arg inV1, Vec3Arg inV2);
79
81 static JPH_INLINE UVec4 sGreater(Vec3Arg inV1, Vec3Arg inV2);
82
84 static JPH_INLINE UVec4 sGreaterOrEqual(Vec3Arg inV1, Vec3Arg inV2);
85
87 static JPH_INLINE Vec3 sFusedMultiplyAdd(Vec3Arg inMul1, Vec3Arg inMul2, Vec3Arg inAdd);
88
90 static JPH_INLINE Vec3 sSelect(Vec3Arg inV1, Vec3Arg inV2, UVec4Arg inControl);
91
93 static JPH_INLINE Vec3 sOr(Vec3Arg inV1, Vec3Arg inV2);
94
96 static JPH_INLINE Vec3 sXor(Vec3Arg inV1, Vec3Arg inV2);
97
99 static JPH_INLINE Vec3 sAnd(Vec3Arg inV1, Vec3Arg inV2);
100
104 static JPH_INLINE Vec3 sUnitSpherical(float inTheta, float inPhi);
105
107 JPH_EXPORT static const std::vector<Vec3> sUnitSphere;
108
110 template <class Random>
111 static inline Vec3 sRandom(Random &inRandom);
112
114#if defined(JPH_USE_SSE)
115 JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); }
116 JPH_INLINE float GetY() const { return mF32[1]; }
117 JPH_INLINE float GetZ() const { return mF32[2]; }
118#elif defined(JPH_USE_NEON)
119 JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); }
120 JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); }
121 JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); }
122#else
123 JPH_INLINE float GetX() const { return mF32[0]; }
124 JPH_INLINE float GetY() const { return mF32[1]; }
125 JPH_INLINE float GetZ() const { return mF32[2]; }
126#endif
127
129 JPH_INLINE void SetX(float inX) { mF32[0] = inX; }
130 JPH_INLINE void SetY(float inY) { mF32[1] = inY; }
131 JPH_INLINE void SetZ(float inZ) { mF32[2] = mF32[3] = inZ; } // Assure Z and W are the same
132
134 JPH_INLINE void Set(float inX, float inY, float inZ) { *this = Vec3(inX, inY, inZ); }
135
137 JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 3); return mF32[inCoordinate]; }
138
140 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
141
143 JPH_INLINE bool operator == (Vec3Arg inV2) const;
144 JPH_INLINE bool operator != (Vec3Arg inV2) const { return !(*this == inV2); }
145
147 JPH_INLINE bool IsClose(Vec3Arg inV2, float inMaxDistSq = 1.0e-12f) const;
148
150 JPH_INLINE bool IsNearZero(float inMaxDistSq = 1.0e-12f) const;
151
153 JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const;
154
156 JPH_INLINE bool IsNaN() const;
157
159 JPH_INLINE Vec3 operator * (Vec3Arg inV2) const;
160
162 JPH_INLINE Vec3 operator * (float inV2) const;
163
165 friend JPH_INLINE Vec3 operator * (float inV1, Vec3Arg inV2);
166
168 JPH_INLINE Vec3 operator / (float inV2) const;
169
171 JPH_INLINE Vec3 & operator *= (float inV2);
172
174 JPH_INLINE Vec3 & operator *= (Vec3Arg inV2);
175
177 JPH_INLINE Vec3 & operator /= (float inV2);
178
180 JPH_INLINE Vec3 operator + (Vec3Arg inV2) const;
181
183 JPH_INLINE Vec3 & operator += (Vec3Arg inV2);
184
186 JPH_INLINE Vec3 operator - () const;
187
189 JPH_INLINE Vec3 operator - (Vec3Arg inV2) const;
190
192 JPH_INLINE Vec3 & operator -= (Vec3Arg inV2);
193
195 JPH_INLINE Vec3 operator / (Vec3Arg inV2) const;
196
198 template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ>
199 JPH_INLINE Vec3 Swizzle() const;
200
202 JPH_INLINE Vec4 SplatX() const;
203
205 JPH_INLINE Vec4 SplatY() const;
206
208 JPH_INLINE Vec4 SplatZ() const;
209
211 JPH_INLINE int GetLowestComponentIndex() const;
212
214 JPH_INLINE int GetHighestComponentIndex() const;
215
217 JPH_INLINE Vec3 Abs() const;
218
220 JPH_INLINE Vec3 Reciprocal() const;
221
223 JPH_INLINE Vec3 Cross(Vec3Arg inV2) const;
224
226 JPH_INLINE Vec3 DotV(Vec3Arg inV2) const;
227
229 JPH_INLINE Vec4 DotV4(Vec3Arg inV2) const;
230
232 JPH_INLINE float Dot(Vec3Arg inV2) const;
233
235 JPH_INLINE float LengthSq() const;
236
238 JPH_INLINE float Length() const;
239
241 JPH_INLINE Vec3 Normalized() const;
242
244 JPH_INLINE Vec3 NormalizedOr(Vec3Arg inZeroValue) const;
245
247 JPH_INLINE void StoreFloat3(Float3 *outV) const;
248
250 JPH_INLINE UVec4 ToInt() const;
251
253 JPH_INLINE UVec4 ReinterpretAsInt() const;
254
256 JPH_INLINE float ReduceMin() const;
257
259 JPH_INLINE float ReduceMax() const;
260
262 JPH_INLINE Vec3 Sqrt() const;
263
265 JPH_INLINE Vec3 GetNormalizedPerpendicular() const;
266
268 JPH_INLINE Vec3 GetSign() const;
269
271 friend ostream & operator << (ostream &inStream, Vec3Arg inV)
272 {
273 inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2];
274 return inStream;
275 }
276
278 JPH_INLINE void CheckW() const;
279
281 static JPH_INLINE Type sFixW(Type inValue);
282
283 union
284 {
286 float mF32[4];
287 };
288};
289
290static_assert(is_trivial<Vec3>(), "Is supposed to be a trivial type!");
291
293
294#include "Vec3.inl"
#define JPH_EXPORT
Definition: Core.h:227
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
DVec3 operator*(double inV1, DVec3Arg inV2)
Definition: DVec3.inl:447
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
const Vec3 Vec3Arg
Definition: MathTypes.h:20
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition: Float3.h:13
Definition: UVec4.h:12
Definition: Vec3.h:16
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:26
static JPH_INLINE Vec3 sAxisX()
Vectors with the principal axis.
Definition: Vec3.h:52
JPH_INLINE void SetComponent(uint inCoordinate, float inValue)
Set float component by index.
Definition: Vec3.h:140
Vec3(const Vec3 &inRHS)=default
static JPH_INLINE Vec3 sAxisY()
Definition: Vec3.h:53
JPH_INLINE float GetX() const
Get individual components.
Definition: Vec3.h:123
static JPH_EXPORT const std::vector< Vec3 > sUnitSphere
A set of vectors uniformly spanning the surface of a unit sphere, usable for debug purposes.
Definition: Vec3.h:107
JPH_INLINE void SetY(float inY)
Definition: Vec3.h:130
JPH_INLINE Vec3(Type inRHS)
Definition: Vec3.h:37
JPH_INLINE void SetZ(float inZ)
Definition: Vec3.h:131
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:54
JPH_INLINE void SetX(float inX)
Set individual components.
Definition: Vec3.h:129
Type mValue
Definition: Vec3.h:285
JPH_INLINE float GetY() const
Definition: Vec3.h:124
float mF32[4]
Definition: Vec3.h:286
JPH_INLINE void Set(float inX, float inY, float inZ)
Set all components.
Definition: Vec3.h:134
JPH_INLINE float GetZ() const
Definition: Vec3.h:125
Vec3()=default
Constructor.
JPH_INLINE Vec3 Swizzle() const
Swizzle the elements in inV.
Definition: Vec4.h:14
{ float mData[4] Type
Definition: Vec4.h:24