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 JPH_INLINE explicit DVec3(Vec3Arg inRHS);
40 JPH_INLINE explicit DVec3(Vec4Arg inRHS);
41 JPH_INLINE DVec3(TypeArg inRHS) : mValue(inRHS) { CheckW(); }
42
44 JPH_INLINE DVec3(double inX, double inY, double inZ);
45
47 explicit JPH_INLINE DVec3(const Double3 &inV);
48
50 static JPH_INLINE DVec3 sZero();
51
53 static JPH_INLINE DVec3 sAxisX() { return DVec3(1, 0, 0); }
54 static JPH_INLINE DVec3 sAxisY() { return DVec3(0, 1, 0); }
55 static JPH_INLINE DVec3 sAxisZ() { return DVec3(0, 0, 1); }
56
58 static JPH_INLINE DVec3 sReplicate(double inV);
59
61 static JPH_INLINE DVec3 sNaN();
62
64 static JPH_INLINE DVec3 sLoadDouble3Unsafe(const Double3 &inV);
65
67 JPH_INLINE void StoreDouble3(Double3 *outV) const;
68
70 JPH_INLINE explicit operator Vec3() const;
71
73 JPH_INLINE DVec3 PrepareRoundToZero() const;
74
76 JPH_INLINE DVec3 PrepareRoundToInf() const;
77
79 JPH_INLINE Vec3 ToVec3RoundDown() const;
80
82 JPH_INLINE Vec3 ToVec3RoundUp() const;
83
85 static JPH_INLINE DVec3 sMin(DVec3Arg inV1, DVec3Arg inV2);
86
88 static JPH_INLINE DVec3 sMax(DVec3Arg inV1, DVec3Arg inV2);
89
91 static JPH_INLINE DVec3 sClamp(DVec3Arg inV, DVec3Arg inMin, DVec3Arg inMax);
92
94 static JPH_INLINE DVec3 sEquals(DVec3Arg inV1, DVec3Arg inV2);
95
97 static JPH_INLINE DVec3 sLess(DVec3Arg inV1, DVec3Arg inV2);
98
100 static JPH_INLINE DVec3 sLessOrEqual(DVec3Arg inV1, DVec3Arg inV2);
101
103 static JPH_INLINE DVec3 sGreater(DVec3Arg inV1, DVec3Arg inV2);
104
106 static JPH_INLINE DVec3 sGreaterOrEqual(DVec3Arg inV1, DVec3Arg inV2);
107
109 static JPH_INLINE DVec3 sFusedMultiplyAdd(DVec3Arg inMul1, DVec3Arg inMul2, DVec3Arg inAdd);
110
112 static JPH_INLINE DVec3 sSelect(DVec3Arg inV1, DVec3Arg inV2, DVec3Arg inControl);
113
115 static JPH_INLINE DVec3 sOr(DVec3Arg inV1, DVec3Arg inV2);
116
118 static JPH_INLINE DVec3 sXor(DVec3Arg inV1, DVec3Arg inV2);
119
121 static JPH_INLINE DVec3 sAnd(DVec3Arg inV1, DVec3Arg inV2);
122
124 JPH_INLINE int GetTrues() const;
125
127 JPH_INLINE bool TestAnyTrue() const;
128
130 JPH_INLINE bool TestAllTrue() const;
131
133#if defined(JPH_USE_AVX)
134 JPH_INLINE double GetX() const { return _mm_cvtsd_f64(_mm256_castpd256_pd128(mValue)); }
135 JPH_INLINE double GetY() const { return mF64[1]; }
136 JPH_INLINE double GetZ() const { return mF64[2]; }
137#elif defined(JPH_USE_SSE)
138 JPH_INLINE double GetX() const { return _mm_cvtsd_f64(mValue.mLow); }
139 JPH_INLINE double GetY() const { return mF64[1]; }
140 JPH_INLINE double GetZ() const { return _mm_cvtsd_f64(mValue.mHigh); }
141#elif defined(JPH_USE_NEON)
142 JPH_INLINE double GetX() const { return vgetq_lane_f64(mValue.val[0], 0); }
143 JPH_INLINE double GetY() const { return vgetq_lane_f64(mValue.val[0], 1); }
144 JPH_INLINE double GetZ() const { return vgetq_lane_f64(mValue.val[1], 0); }
145#else
146 JPH_INLINE double GetX() const { return mF64[0]; }
147 JPH_INLINE double GetY() const { return mF64[1]; }
148 JPH_INLINE double GetZ() const { return mF64[2]; }
149#endif
150
152 JPH_INLINE void SetX(double inX) { mF64[0] = inX; }
153 JPH_INLINE void SetY(double inY) { mF64[1] = inY; }
154 JPH_INLINE void SetZ(double inZ) { mF64[2] = mF64[3] = inZ; } // Assure Z and W are the same
155
157 JPH_INLINE double operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 3); return mF64[inCoordinate]; }
158
160 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
161
163 JPH_INLINE bool operator == (DVec3Arg inV2) const;
164 JPH_INLINE bool operator != (DVec3Arg inV2) const { return !(*this == inV2); }
165
167 JPH_INLINE bool IsClose(DVec3Arg inV2, double inMaxDistSq = 1.0e-24) const;
168
170 JPH_INLINE bool IsNearZero(double inMaxDistSq = 1.0e-24) const;
171
173 JPH_INLINE bool IsNormalized(double inTolerance = 1.0e-12) const;
174
176 JPH_INLINE bool IsNaN() const;
177
179 JPH_INLINE DVec3 operator * (DVec3Arg inV2) const;
180
182 JPH_INLINE DVec3 operator * (double inV2) const;
183
185 friend JPH_INLINE DVec3 operator * (double inV1, DVec3Arg inV2);
186
188 JPH_INLINE DVec3 operator / (double inV2) const;
189
191 JPH_INLINE DVec3 & operator *= (double inV2);
192
194 JPH_INLINE DVec3 & operator *= (DVec3Arg inV2);
195
197 JPH_INLINE DVec3 & operator /= (double inV2);
198
200 JPH_INLINE DVec3 operator + (Vec3Arg inV2) const;
201
203 JPH_INLINE DVec3 operator + (DVec3Arg inV2) const;
204
206 JPH_INLINE DVec3 & operator += (Vec3Arg inV2);
207
209 JPH_INLINE DVec3 & operator += (DVec3Arg inV2);
210
212 JPH_INLINE DVec3 operator - () const;
213
215 JPH_INLINE DVec3 operator - (Vec3Arg inV2) const;
216
218 JPH_INLINE DVec3 operator - (DVec3Arg inV2) const;
219
221 JPH_INLINE DVec3 & operator -= (Vec3Arg inV2);
222
224 JPH_INLINE DVec3 & operator -= (DVec3Arg inV2);
225
227 JPH_INLINE DVec3 operator / (DVec3Arg inV2) const;
228
230 JPH_INLINE DVec3 Abs() const;
231
233 JPH_INLINE DVec3 Reciprocal() const;
234
236 JPH_INLINE DVec3 Cross(DVec3Arg inV2) const;
237
239 JPH_INLINE double Dot(DVec3Arg inV2) const;
240
242 JPH_INLINE double LengthSq() const;
243
245 JPH_INLINE double Length() const;
246
248 JPH_INLINE DVec3 Normalized() const;
249
251 JPH_INLINE DVec3 Sqrt() const;
252
254 JPH_INLINE DVec3 GetSign() const;
255
257 friend ostream & operator << (ostream &inStream, DVec3Arg inV)
258 {
259 inStream << inV.mF64[0] << ", " << inV.mF64[1] << ", " << inV.mF64[2];
260 return inStream;
261 }
262
264 JPH_INLINE void CheckW() const;
265
267 static JPH_INLINE Type sFixW(TypeArg inValue);
268
270 inline static const double cTrue = BitCast<double>(~uint64(0));
271 inline static const double cFalse = 0.0;
272
273 union
274 {
276 double mF64[4];
277 };
278};
279
280static_assert(is_trivial<DVec3>(), "Is supposed to be a trivial type!");
281
283
284#include "DVec3.inl"
unsigned int uint
Definition: Core.h:309
#define JPH_NAMESPACE_END
Definition: Core.h:240
uint64_t uint64
Definition: Core.h:313
#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
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:276
{ double mData[4] Type
Definition: DVec3.h:29
static JPH_INLINE DVec3 sAxisY()
Definition: DVec3.h:54
JPH_INLINE DVec3(TypeArg inRHS)
Definition: DVec3.h:41
Type mValue
Definition: DVec3.h:275
const Type & TypeArg
Definition: DVec3.h:30
JPH_INLINE void SetComponent(uint inCoordinate, double inValue)
Set double component by index.
Definition: DVec3.h:160
static JPH_INLINE DVec3 sAxisX()
Vectors with the principal axis.
Definition: DVec3.h:53
JPH_INLINE void SetY(double inY)
Definition: DVec3.h:153
JPH_INLINE double GetZ() const
Definition: DVec3.h:148
DVec3()=default
Constructor.
static JPH_INLINE DVec3 sAxisZ()
Definition: DVec3.h:55
JPH_INLINE DVec3(Vec3Arg inRHS)
JPH_INLINE void SetZ(double inZ)
Definition: DVec3.h:154
JPH_INLINE double GetY() const
Definition: DVec3.h:147
DVec3(const DVec3 &inRHS)=default
JPH_INLINE void SetX(double inX)
Set individual components.
Definition: DVec3.h:152
JPH_INLINE double GetX() const
Get individual components.
Definition: DVec3.h:146
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