| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/StaticArray.h> |
| 8 |
#include <Jolt/Math/Float3.h> |
| 9 |
#include <Jolt/Math/Swizzle.h> |
| 10 |
#include <Jolt/Math/MathTypes.h> |
| 11 |
|
| 12 |
JPH_NAMESPACE_BEGIN |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Vec3 |
| 17 |
{ |
| 18 |
public: |
| 19 |
JPH_OVERRIDE_NEW_DELETE |
| 20 |
|
| 21 |
|
| 22 |
#if defined(JPH_USE_SSE) |
| 23 |
using Type = __m128; |
| 24 |
#elif defined(JPH_USE_NEON) |
| 25 |
using Type = float32x4_t; |
| 26 |
#else |
| 27 |
using Type = Vec4::Type; |
| 28 |
#endif |
| 29 |
|
| 30 |
|
| 31 |
using ArgType = Vec3Arg; |
| 32 |
|
| 33 |
|
| 34 |
Vec3() = default; |
| 35 |
Vec3(const Vec3 &inRHS) = default; |
| 36 |
Vec3 & operator = (const Vec3 &inRHS) = default; |
| 37 |
explicit JPH_INLINE Vec3(Vec4Arg inRHS); |
| 38 |
JPH_INLINE Vec3(Type inRHS) : mValue(inRHS) { CheckW(); } |
| 39 |
|
| 40 |
|
| 41 |
explicit JPH_INLINE Vec3(const Float3 &inV); |
| 42 |
|
| 43 |
|
| 44 |
JPH_INLINE Vec3(float inX, float inY, float inZ); |
| 45 |
|
| 46 |
|
| 47 |
static JPH_INLINE Vec3 sZero(); |
| 48 |
|
| 49 |
|
| 50 |
static JPH_INLINE Vec3 sOne(); |
| 51 |
|
| 52 |
|
| 53 |
static JPH_INLINE Vec3 sNaN(); |
| 54 |
|
| 55 |
|
| 56 |
static JPH_INLINE Vec3 sAxisX() { return Vec3(1, 0, 0); } |
| 57 |
static JPH_INLINE Vec3 sAxisY() { return Vec3(0, 1, 0); } |
| 58 |
static JPH_INLINE Vec3 sAxisZ() { return Vec3(0, 0, 1); } |
| 59 |
|
| 60 |
|
| 61 |
static JPH_INLINE Vec3 sReplicate(float inV); |
| 62 |
|
| 63 |
|
| 64 |
static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV); |
| 65 |
|
| 66 |
|
| 67 |
static JPH_INLINE Vec3 sMin(Vec3Arg inV1, Vec3Arg inV2); |
| 68 |
|
| 69 |
|
| 70 |
static JPH_INLINE Vec3 sMax(Vec3Arg inV1, Vec3Arg inV2); |
| 71 |
|
| 72 |
|
| 73 |
static JPH_INLINE Vec3 sClamp(Vec3Arg inV, Vec3Arg inMin, Vec3Arg inMax); |
| 74 |
|
| 75 |
|
| 76 |
static JPH_INLINE UVec4 sEquals(Vec3Arg inV1, Vec3Arg inV2); |
| 77 |
|
| 78 |
|
| 79 |
static JPH_INLINE UVec4 sLess(Vec3Arg inV1, Vec3Arg inV2); |
| 80 |
|
| 81 |
|
| 82 |
static JPH_INLINE UVec4 sLessOrEqual(Vec3Arg inV1, Vec3Arg inV2); |
| 83 |
|
| 84 |
|
| 85 |
static JPH_INLINE UVec4 sGreater(Vec3Arg inV1, Vec3Arg inV2); |
| 86 |
|
| 87 |
|
| 88 |
static JPH_INLINE UVec4 sGreaterOrEqual(Vec3Arg inV1, Vec3Arg inV2); |
| 89 |
|
| 90 |
|
| 91 |
static JPH_INLINE Vec3 sFusedMultiplyAdd(Vec3Arg inMul1, Vec3Arg inMul2, Vec3Arg inAdd); |
| 92 |
|
| 93 |
|
| 94 |
static JPH_INLINE Vec3 sSelect(Vec3Arg inNotSet, Vec3Arg inSet, UVec4Arg inControl); |
| 95 |
|
| 96 |
|
| 97 |
static JPH_INLINE Vec3 sOr(Vec3Arg inV1, Vec3Arg inV2); |
| 98 |
|
| 99 |
|
| 100 |
static JPH_INLINE Vec3 sXor(Vec3Arg inV1, Vec3Arg inV2); |
| 101 |
|
| 102 |
|
| 103 |
static JPH_INLINE Vec3 sAnd(Vec3Arg inV1, Vec3Arg inV2); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
static JPH_INLINE Vec3 sUnitSpherical(float inTheta, float inPhi); |
| 109 |
|
| 110 |
|
| 111 |
JPH_EXPORT static const StaticArray<Vec3, 1026> sUnitSphere; |
| 112 |
|
| 113 |
|
| 114 |
template <class Random> |
| 115 |
static inline Vec3 sRandom(Random &inRandom); |
| 116 |
|
| 117 |
|
| 118 |
#if defined(JPH_USE_SSE) |
| 119 |
JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); } |
| 120 |
JPH_INLINE float GetY() const { return mF32[1]; } |
| 121 |
JPH_INLINE float GetZ() const { return mF32[2]; } |
| 122 |
#elif defined(JPH_USE_NEON) |
| 123 |
JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); } |
| 124 |
JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); } |
| 125 |
JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); } |
| 126 |
#else |
| 127 |
JPH_INLINE float GetX() const { return mF32[0]; } |
| 128 |
JPH_INLINE float GetY() const { return mF32[1]; } |
| 129 |
JPH_INLINE float GetZ() const { return mF32[2]; } |
| 130 |
#endif |
| 131 |
|
| 132 |
|
| 133 |
JPH_INLINE void SetX(float inX) { mF32[0] = inX; } |
| 134 |
JPH_INLINE void SetY(float inY) { mF32[1] = inY; } |
| 135 |
JPH_INLINE void SetZ(float inZ) { mF32[2] = mF32[3] = inZ; } |
| 136 |
|
| 137 |
|
| 138 |
JPH_INLINE void Set(float inX, float inY, float inZ) { *this = Vec3(inX, inY, inZ); } |
| 139 |
|
| 140 |
|
| 141 |
JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 3); return mF32[inCoordinate]; } |
| 142 |
|
| 143 |
|
| 144 |
JPH_INLINE void SetComponent(uint inCoordinate, float inValue) { JPH_ASSERT(inCoordinate < 3); mF32[inCoordinate] = inValue; mValue = sFixW(mValue); } |
| 145 |
|
| 146 |
|
| 147 |
JPH_INLINE bool operator == (Vec3Arg inV2) const; |
| 148 |
JPH_INLINE bool operator != (Vec3Arg inV2) const { return !(*this == inV2); } |
| 149 |
|
| 150 |
|
| 151 |
JPH_INLINE bool IsClose(Vec3Arg inV2, float inMaxDistSq = 1.0e-12f) const; |
| 152 |
|
| 153 |
|
| 154 |
JPH_INLINE bool IsNearZero(float inMaxDistSq = 1.0e-12f) const; |
| 155 |
|
| 156 |
|
| 157 |
JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const; |
| 158 |
|
| 159 |
|
| 160 |
JPH_INLINE bool IsNaN() const; |
| 161 |
|
| 162 |
|
| 163 |
JPH_INLINE Vec3 operator * (Vec3Arg inV2) const; |
| 164 |
|
| 165 |
|
| 166 |
JPH_INLINE Vec3 operator * (float inV2) const; |
| 167 |
|
| 168 |
|
| 169 |
friend JPH_INLINE Vec3 operator * (float inV1, Vec3Arg inV2); |
| 170 |
|
| 171 |
|
| 172 |
JPH_INLINE Vec3 operator / (float inV2) const; |
| 173 |
|
| 174 |
|
| 175 |
JPH_INLINE Vec3 & operator *= (float inV2); |
| 176 |
|
| 177 |
|
| 178 |
JPH_INLINE Vec3 & operator *= (Vec3Arg inV2); |
| 179 |
|
| 180 |
|
| 181 |
JPH_INLINE Vec3 & operator /= (float inV2); |
| 182 |
|
| 183 |
|
| 184 |
JPH_INLINE Vec3 operator + (Vec3Arg inV2) const; |
| 185 |
|
| 186 |
|
| 187 |
JPH_INLINE Vec3 & operator += (Vec3Arg inV2); |
| 188 |
|
| 189 |
|
| 190 |
JPH_INLINE Vec3 operator - () const; |
| 191 |
|
| 192 |
|
| 193 |
JPH_INLINE Vec3 operator - (Vec3Arg inV2) const; |
| 194 |
|
| 195 |
|
| 196 |
JPH_INLINE Vec3 & operator -= (Vec3Arg inV2); |
| 197 |
|
| 198 |
|
| 199 |
JPH_INLINE Vec3 operator / (Vec3Arg inV2) const; |
| 200 |
|
| 201 |
|
| 202 |
template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ> |
| 203 |
JPH_INLINE Vec3 Swizzle() const; |
| 204 |
|
| 205 |
|
| 206 |
JPH_INLINE Vec4 SplatX() const; |
| 207 |
|
| 208 |
|
| 209 |
JPH_INLINE Vec4 SplatY() const; |
| 210 |
|
| 211 |
|
| 212 |
JPH_INLINE Vec4 SplatZ() const; |
| 213 |
|
| 214 |
|
| 215 |
JPH_INLINE int GetLowestComponentIndex() const; |
| 216 |
|
| 217 |
|
| 218 |
JPH_INLINE int GetHighestComponentIndex() const; |
| 219 |
|
| 220 |
|
| 221 |
JPH_INLINE Vec3 Abs() const; |
| 222 |
|
| 223 |
|
| 224 |
JPH_INLINE Vec3 Reciprocal() const; |
| 225 |
|
| 226 |
|
| 227 |
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const; |
| 228 |
|
| 229 |
|
| 230 |
JPH_INLINE Vec3 DotV(Vec3Arg inV2) const; |
| 231 |
|
| 232 |
|
| 233 |
JPH_INLINE Vec4 DotV4(Vec3Arg inV2) const; |
| 234 |
|
| 235 |
|
| 236 |
JPH_INLINE float Dot(Vec3Arg inV2) const; |
| 237 |
|
| 238 |
|
| 239 |
JPH_INLINE float LengthSq() const; |
| 240 |
|
| 241 |
|
| 242 |
JPH_INLINE float Length() const; |
| 243 |
|
| 244 |
|
| 245 |
JPH_INLINE Vec3 Normalized() const; |
| 246 |
|
| 247 |
|
| 248 |
JPH_INLINE Vec3 NormalizedOr(Vec3Arg inZeroValue) const; |
| 249 |
|
| 250 |
|
| 251 |
JPH_INLINE void StoreFloat3(Float3 *outV) const; |
| 252 |
|
| 253 |
|
| 254 |
JPH_INLINE UVec4 ToInt() const; |
| 255 |
|
| 256 |
|
| 257 |
JPH_INLINE UVec4 ReinterpretAsInt() const; |
| 258 |
|
| 259 |
|
| 260 |
JPH_INLINE float ReduceMin() const; |
| 261 |
|
| 262 |
|
| 263 |
JPH_INLINE float ReduceMax() const; |
| 264 |
|
| 265 |
|
| 266 |
JPH_INLINE Vec3 Sqrt() const; |
| 267 |
|
| 268 |
|
| 269 |
JPH_INLINE Vec3 GetNormalizedPerpendicular() const; |
| 270 |
|
| 271 |
|
| 272 |
JPH_INLINE Vec3 GetSign() const; |
| 273 |
|
| 274 |
|
| 275 |
template <int X, int Y, int Z> |
| 276 |
JPH_INLINE Vec3 FlipSign() const; |
| 277 |
|
| 278 |
|
| 279 |
JPH_INLINE uint32 CompressUnitVector() const; |
| 280 |
|
| 281 |
|
| 282 |
JPH_INLINE static Vec3 sDecompressUnitVector(uint32 inValue); |
| 283 |
|
| 284 |
|
| 285 |
friend ostream & operator << (ostream &inStream, Vec3Arg inV) |
| 286 |
{ |
| 287 |
inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2]; |
| 288 |
return inStream; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
JPH_INLINE void CheckW() const; |
| 293 |
|
| 294 |
|
| 295 |
static JPH_INLINE Type sFixW(Type inValue); |
| 296 |
|
| 297 |
union |
| 298 |
{ |
| 299 |
Type mValue; |
| 300 |
float mF32[4]; |
| 301 |
}; |
| 302 |
}; |
| 303 |
|
| 304 |
static_assert(std::is_trivial<Vec3>(), "Is supposed to be a trivial type!"); |
| 305 |
|
| 306 |
JPH_NAMESPACE_END |
| 307 |
|
| 308 |
#include "Vec3.inl" |