Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
DVec3.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/Double3.h>
8
10
13class [[nodiscard]] alignas(JPH_DVECTOR_ALIGNMENT) DVec3
14{
15public:
17
18 // Underlying vector type
19#if defined(JPH_USE_AVX)
20 using Type = __m256d;
21 using TypeArg = __m256d;
22#elif defined(JPH_USE_SSE)
23 using Type = struct { __m128d mLow, mHigh; };
24 using TypeArg = const Type &;
25#elif defined(JPH_USE_NEON)
26 using Type = float64x2x2_t;
27 using TypeArg = const Type &;
28#else
29 using Type = struct { double mData[4]; };
30 using TypeArg = const Type &;
31#endif
32
33 // Argument type
35
37 DVec3() = default;
38 DVec3(const DVec3 &inRHS) = default;
39 DVec3 & operator = (const DVec3 &inRHS) = default;
40 JPH_INLINE explicit DVec3(Vec3Arg inRHS);
41 JPH_INLINE explicit DVec3(Vec4Arg inRHS);
42 JPH_INLINE DVec3(TypeArg inRHS) : mValue(inRHS) { CheckW(); }
43
45 JPH_INLINE DVec3(double inX, double inY, double inZ);
46
48 explicit JPH_INLINE DVec3(const Double3 &inV);
49
51 static JPH_INLINE DVec3 sZero();
52
54 static JPH_INLINE DVec3 sAxisX() { return DVec3(1, 0, 0); }
55 static JPH_INLINE DVec3 sAxisY() { return DVec3(0, 1, 0); }
56 static JPH_INLINE DVec3 sAxisZ() { return DVec3(0, 0, 1); }
57
59 static JPH_INLINE DVec3 sReplicate(double inV);
60
62 static JPH_INLINE DVec3 sNaN();
63
65 static JPH_INLINE DVec3 sLoadDouble3Unsafe(const Double3 &inV);
66
68 JPH_INLINE void StoreDouble3(Double3 *outV) const;
69
71 JPH_INLINE explicit operator Vec3() const;
72
74 JPH_INLINE DVec3 PrepareRoundToZero() const;
75
77 JPH_INLINE DVec3 PrepareRoundToInf() const;
78
80 JPH_INLINE Vec3 ToVec3RoundDown() const;
81
83 JPH_INLINE Vec3 ToVec3RoundUp() const;
84
86 static JPH_INLINE DVec3 sMin(DVec3Arg inV1, DVec3Arg inV2);
87
89 static JPH_INLINE DVec3 sMax(DVec3Arg inV1, DVec3Arg inV2);
90
92 static JPH_INLINE DVec3 sClamp(DVec3Arg inV, DVec3Arg inMin, DVec3Arg inMax);
93
95 static JPH_INLINE DVec3 sEquals(DVec3Arg inV1, DVec3Arg inV2);
96
98 static JPH_INLINE DVec3 sLess(DVec3Arg inV1, DVec3Arg inV2);
99
101 static JPH_INLINE DVec3 sLessOrEqual(DVec3Arg inV1, DVec3Arg inV2);
102
104 static JPH_INLINE DVec3 sGreater(DVec3Arg inV1, DVec3Arg inV2);
105
107 static JPH_INLINE DVec3 sGreaterOrEqual(DVec3Arg inV1, DVec3Arg inV2);
108
110 static JPH_INLINE DVec3 sFusedMultiplyAdd(DVec3Arg inMul1, DVec3Arg inMul2, DVec3Arg inAdd);
111
113 static JPH_INLINE DVec3 sSelect(DVec3Arg inV1, DVec3Arg inV2, DVec3Arg inControl);
114
116 static JPH_INLINE DVec3 sOr(DVec3Arg inV1, DVec3Arg inV2);
117
119 static JPH_INLINE DVec3 sXor(DVec3Arg inV1, DVec3Arg inV2);
120
122 static JPH_INLINE DVec3 sAnd(DVec3Arg inV1, DVec3Arg inV2);
123
125 JPH_INLINE int GetTrues() const;
126
128 JPH_INLINE bool TestAnyTrue() const;
129
131 JPH_INLINE bool TestAllTrue() const;
132
134#if defined(JPH_USE_AVX)
135 JPH_INLINE double GetX() const { return _mm_cvtsd_f64(_mm256_castpd256_pd128(mValue)); }
136 JPH_INLINE double GetY() const { return mF64[1]; }
137 JPH_INLINE double GetZ() const { return mF64[2]; }
138#elif defined(JPH_USE_SSE)
139 JPH_INLINE double GetX() const { return _mm_cvtsd_f64(mValue.mLow); }
140 JPH_INLINE double GetY() const { return mF64[1]; }
141 JPH_INLINE double GetZ() const { return _mm_cvtsd_f64(mValue.mHigh); }
142#elif defined(JPH_USE_NEON)
143 JPH_INLINE double GetX() const { return vgetq_lane_f64(mValue.val[0], 0); }
144 JPH_INLINE double GetY() const { return vgetq_lane_f64(mValue.val[0], 1); }
145 JPH_INLINE double GetZ() const { return vgetq_lane_f64(mValue.val[1], 0); }
146#else
147 JPH_INLINE double GetX() const { return mF64[0]; }
148 JPH_INLINE double GetY() const { return mF64[1]; }
149 JPH_INLINE double GetZ() const { return mF64[2]; }
150#endif
151
153 JPH_INLINE void SetX(double inX) { mF64[0] = inX; }
154 JPH_INLINE void SetY(double inY) { mF64[1] = inY; }
155 JPH_INLINE void SetZ(double inZ) { mF64[2] = mF64[3] = inZ; } // Assure Z and W are the same
156
158 JPH_INLINE void Set(double inX, double inY, double inZ) { *this = DVec3(inX, inY, inZ); }
159
161 JPH_INLINE double operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 3); return mF64[inCoordinate]; }
162
164 JPH_INLINE void SetComponent(uint inCoordinate, double inValue) { JPH_ASSERT(inCoordinate < 3); mF64[inCoordinate] = inValue; mValue = sFixW(mValue); } // Assure Z and W are the same
165
167 JPH_INLINE bool operator == (DVec3Arg inV2) const;
168 JPH_INLINE bool operator != (DVec3Arg inV2) const { return !(*this == inV2); }
169
171 JPH_INLINE bool IsClose(DVec3Arg inV2, double inMaxDistSq = 1.0e-24) const;
172
174 JPH_INLINE bool IsNearZero(double inMaxDistSq = 1.0e-24) const;
175
177 JPH_INLINE bool IsNormalized(double inTolerance = 1.0e-12) const;
178
180 JPH_INLINE bool IsNaN() const;
181
183 JPH_INLINE DVec3 operator * (DVec3Arg inV2) const;
184
186 JPH_INLINE DVec3 operator * (double inV2) const;
187
189 friend JPH_INLINE DVec3 operator * (double inV1, DVec3Arg inV2);
190
192 JPH_INLINE DVec3 operator / (double inV2) const;
193
195 JPH_INLINE DVec3 & operator *= (double inV2);
196
198 JPH_INLINE DVec3 & operator *= (DVec3Arg inV2);
199
201 JPH_INLINE DVec3 & operator /= (double inV2);
202
204 JPH_INLINE DVec3 operator + (Vec3Arg inV2) const;
205
207 JPH_INLINE DVec3 operator + (DVec3Arg inV2) const;
208
210 JPH_INLINE DVec3 & operator += (Vec3Arg inV2);
211
213 JPH_INLINE DVec3 & operator += (DVec3Arg inV2);
214
216 JPH_INLINE DVec3 operator - () const;
217
219 JPH_INLINE DVec3 operator - (Vec3Arg inV2) const;
220
222 JPH_INLINE DVec3 operator - (DVec3Arg inV2) const;
223
225 JPH_INLINE DVec3 & operator -= (Vec3Arg inV2);
226
228 JPH_INLINE DVec3 & operator -= (DVec3Arg inV2);
229
231 JPH_INLINE DVec3 operator / (DVec3Arg inV2) const;
232
234 JPH_INLINE DVec3 Abs() const;
235
237 JPH_INLINE DVec3 Reciprocal() const;
238
240 JPH_INLINE DVec3 Cross(DVec3Arg inV2) const;
241
243 JPH_INLINE double Dot(DVec3Arg inV2) const;
244
246 JPH_INLINE double LengthSq() const;
247
249 JPH_INLINE double Length() const;
250
252 JPH_INLINE DVec3 Normalized() const;
253
255 JPH_INLINE DVec3 Sqrt() const;
256
258 JPH_INLINE DVec3 GetSign() const;
259
261 friend ostream & operator << (ostream &inStream, DVec3Arg inV)
262 {
263 inStream << inV.mF64[0] << ", " << inV.mF64[1] << ", " << inV.mF64[2];
264 return inStream;
265 }
266
268 JPH_INLINE void CheckW() const;
269
271 static JPH_INLINE Type sFixW(TypeArg inValue);
272
274 inline static const double cTrue = BitCast<double>(~uint64(0));
275 inline static const double cFalse = 0.0;
276
277 union
278 {
280 double mF64[4];
281 };
282};
283
284static_assert(is_trivial<DVec3>(), "Is supposed to be a trivial type!");
285
287
288#include "DVec3.inl"
std::uint64_t uint64
Definition: Core.h:443
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 DVec3 & DVec3Arg
Definition: MathTypes.h:24
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
Definition: DVec3.h:14
double mF64[4]
Definition: DVec3.h:280
{ double mData[4] Type
Definition: DVec3.h:29
static JPH_INLINE DVec3 sAxisY()
Definition: DVec3.h:55
JPH_INLINE DVec3(TypeArg inRHS)
Definition: DVec3.h:42
Type mValue
Definition: DVec3.h:279
const Type & TypeArg
Definition: DVec3.h:30
JPH_INLINE void SetComponent(uint inCoordinate, double inValue)
Set double component by index.
Definition: DVec3.h:164
JPH_INLINE void Set(double inX, double inY, double inZ)
Set all components.
Definition: DVec3.h:158
static JPH_INLINE DVec3 sAxisX()
Vectors with the principal axis.
Definition: DVec3.h:54
JPH_INLINE void SetY(double inY)
Definition: DVec3.h:154
JPH_INLINE double GetZ() const
Definition: DVec3.h:149
DVec3()=default
Constructor.
static JPH_INLINE DVec3 sAxisZ()
Definition: DVec3.h:56
JPH_INLINE DVec3(Vec3Arg inRHS)
JPH_INLINE void SetZ(double inZ)
Definition: DVec3.h:155
JPH_INLINE double GetY() const
Definition: DVec3.h:148
DVec3(const DVec3 &inRHS)=default
JPH_INLINE void SetX(double inX)
Set individual components.
Definition: DVec3.h:153
JPH_INLINE double GetX() const
Get individual components.
Definition: DVec3.h:147
Class that holds 3 doubles. Used as a storage class. Convert to DVec3 for calculations.
Definition: Double3.h:13
Definition: Vec3.h:16
Definition: Vec4.h:14