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 explicit JPH_INLINE Vec3(Vec4Arg inRHS);
36 JPH_INLINE Vec3(Type inRHS) : mValue(inRHS) { CheckW(); }
37
39 explicit JPH_INLINE Vec3(const Float3 &inV);
40
42 JPH_INLINE Vec3(float inX, float inY, float inZ);
43
45 static JPH_INLINE Vec3 sZero();
46
48 static JPH_INLINE Vec3 sNaN();
49
51 static JPH_INLINE Vec3 sAxisX() { return Vec3(1, 0, 0); }
52 static JPH_INLINE Vec3 sAxisY() { return Vec3(0, 1, 0); }
53 static JPH_INLINE Vec3 sAxisZ() { return Vec3(0, 0, 1); }
54
56 static JPH_INLINE Vec3 sReplicate(float inV);
57
59 static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV);
60
62 static JPH_INLINE Vec3 sMin(Vec3Arg inV1, Vec3Arg inV2);
63
65 static JPH_INLINE Vec3 sMax(Vec3Arg inV1, Vec3Arg inV2);
66
68 static JPH_INLINE Vec3 sClamp(Vec3Arg inV, Vec3Arg inMin, Vec3Arg inMax);
69
71 static JPH_INLINE UVec4 sEquals(Vec3Arg inV1, Vec3Arg inV2);
72
74 static JPH_INLINE UVec4 sLess(Vec3Arg inV1, Vec3Arg inV2);
75
77 static JPH_INLINE UVec4 sLessOrEqual(Vec3Arg inV1, Vec3Arg inV2);
78
80 static JPH_INLINE UVec4 sGreater(Vec3Arg inV1, Vec3Arg inV2);
81
83 static JPH_INLINE UVec4 sGreaterOrEqual(Vec3Arg inV1, Vec3Arg inV2);
84
86 static JPH_INLINE Vec3 sFusedMultiplyAdd(Vec3Arg inMul1, Vec3Arg inMul2, Vec3Arg inAdd);
87
89 static JPH_INLINE Vec3 sSelect(Vec3Arg inV1, Vec3Arg inV2, UVec4Arg inControl);
90
92 static JPH_INLINE Vec3 sOr(Vec3Arg inV1, Vec3Arg inV2);
93
95 static JPH_INLINE Vec3 sXor(Vec3Arg inV1, Vec3Arg inV2);
96
98 static JPH_INLINE Vec3 sAnd(Vec3Arg inV1, Vec3Arg inV2);
99
103 static JPH_INLINE Vec3 sUnitSpherical(float inTheta, float inPhi);
104
106 static const std::vector<Vec3> sUnitSphere;
107
109 template <class Random>
110 static inline Vec3 sRandom(Random &inRandom);
111
113#if defined(JPH_USE_SSE)
114 JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); }
115 JPH_INLINE float GetY() const { return mF32[1]; }
116 JPH_INLINE float GetZ() const { return mF32[2]; }
117#elif defined(JPH_USE_NEON)
118 JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); }
119 JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); }
120 JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); }
121#else
122 JPH_INLINE float GetX() const { return mF32[0]; }
123 JPH_INLINE float GetY() const { return mF32[1]; }
124 JPH_INLINE float GetZ() const { return mF32[2]; }
125#endif
126
128 JPH_INLINE void SetX(float inX) { mF32[0] = inX; }
129 JPH_INLINE void SetY(float inY) { mF32[1] = inY; }
130 JPH_INLINE void SetZ(float inZ) { mF32[2] = mF32[3] = inZ; } // Assure Z and W are the same
131
133 JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 3); return mF32[inCoordinate]; }
134
136 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
137
139 JPH_INLINE bool operator == (Vec3Arg inV2) const;
140 JPH_INLINE bool operator != (Vec3Arg inV2) const { return !(*this == inV2); }
141
143 JPH_INLINE bool IsClose(Vec3Arg inV2, float inMaxDistSq = 1.0e-12f) const;
144
146 JPH_INLINE bool IsNearZero(float inMaxDistSq = 1.0e-12f) const;
147
149 JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const;
150
152 JPH_INLINE bool IsNaN() const;
153
155 JPH_INLINE Vec3 operator * (Vec3Arg inV2) const;
156
158 JPH_INLINE Vec3 operator * (float inV2) const;
159
161 friend JPH_INLINE Vec3 operator * (float inV1, Vec3Arg inV2);
162
164 JPH_INLINE Vec3 operator / (float inV2) const;
165
167 JPH_INLINE Vec3 & operator *= (float inV2);
168
170 JPH_INLINE Vec3 & operator *= (Vec3Arg inV2);
171
173 JPH_INLINE Vec3 & operator /= (float inV2);
174
176 JPH_INLINE Vec3 operator + (Vec3Arg inV2) const;
177
179 JPH_INLINE Vec3 & operator += (Vec3Arg inV2);
180
182 JPH_INLINE Vec3 operator - () const;
183
185 JPH_INLINE Vec3 operator - (Vec3Arg inV2) const;
186
188 JPH_INLINE Vec3 & operator -= (Vec3Arg inV2);
189
191 JPH_INLINE Vec3 operator / (Vec3Arg inV2) const;
192
194 template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ>
195 JPH_INLINE Vec3 Swizzle() const;
196
198 JPH_INLINE Vec4 SplatX() const;
199
201 JPH_INLINE Vec4 SplatY() const;
202
204 JPH_INLINE Vec4 SplatZ() const;
205
207 JPH_INLINE int GetLowestComponentIndex() const;
208
210 JPH_INLINE int GetHighestComponentIndex() const;
211
213 JPH_INLINE Vec3 Abs() const;
214
216 JPH_INLINE Vec3 Reciprocal() const;
217
219 JPH_INLINE Vec3 Cross(Vec3Arg inV2) const;
220
222 JPH_INLINE Vec3 DotV(Vec3Arg inV2) const;
223
225 JPH_INLINE Vec4 DotV4(Vec3Arg inV2) const;
226
228 JPH_INLINE float Dot(Vec3Arg inV2) const;
229
231 JPH_INLINE float LengthSq() const;
232
234 JPH_INLINE float Length() const;
235
237 JPH_INLINE Vec3 Normalized() const;
238
240 JPH_INLINE Vec3 NormalizedOr(Vec3Arg inZeroValue) const;
241
243 JPH_INLINE void StoreFloat3(Float3 *outV) const;
244
246 JPH_INLINE UVec4 ToInt() const;
247
249 JPH_INLINE UVec4 ReinterpretAsInt() const;
250
252 JPH_INLINE float ReduceMin() const;
253
255 JPH_INLINE float ReduceMax() const;
256
258 JPH_INLINE Vec3 Sqrt() const;
259
261 JPH_INLINE Vec3 GetNormalizedPerpendicular() const;
262
264 JPH_INLINE Vec3 GetSign() const;
265
267 friend ostream & operator << (ostream &inStream, Vec3Arg inV)
268 {
269 inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2];
270 return inStream;
271 }
272
274 JPH_INLINE void CheckW() const;
275
277 static JPH_INLINE Type sFixW(Type inValue);
278
279 union
280 {
282 float mF32[4];
283 };
284};
285
286static_assert(is_trivial<Vec3>(), "Is supposed to be a trivial type!");
287
289
290#include "Vec3.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
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:51
JPH_INLINE void SetComponent(uint inCoordinate, float inValue)
Set float component by index.
Definition: Vec3.h:136
Vec3(const Vec3 &inRHS)=default
static JPH_INLINE Vec3 sAxisY()
Definition: Vec3.h:52
JPH_INLINE float GetX() const
Get individual components.
Definition: Vec3.h:122
static const std::vector< Vec3 > sUnitSphere
A set of vectors uniformly spanning the surface of a unit sphere, usable for debug purposes.
Definition: Vec3.h:106
JPH_INLINE void SetY(float inY)
Definition: Vec3.h:129
JPH_INLINE Vec3(Type inRHS)
Definition: Vec3.h:36
JPH_INLINE void SetZ(float inZ)
Definition: Vec3.h:130
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:53
JPH_INLINE void SetX(float inX)
Set individual components.
Definition: Vec3.h:128
Type mValue
Definition: Vec3.h:281
JPH_INLINE float GetY() const
Definition: Vec3.h:123
float mF32[4]
Definition: Vec3.h:282
JPH_INLINE float GetZ() const
Definition: Vec3.h:124
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