Path: blob/master/thirdparty/jolt_physics/Jolt/Math/Vec4.h
21654 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Math/Float4.h>7#include <Jolt/Math/Swizzle.h>8#include <Jolt/Math/MathTypes.h>910JPH_NAMESPACE_BEGIN1112class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Vec413{14public:15JPH_OVERRIDE_NEW_DELETE1617// Underlying vector type18#if defined(JPH_USE_SSE)19using Type = __m128;20#elif defined(JPH_USE_NEON)21using Type = float32x4_t;22#else23using Type = struct { float mData[4]; };24#endif2526/// Constructor27Vec4() = default; ///< Intentionally not initialized for performance reasons28Vec4(const Vec4 &inRHS) = default;29Vec4 & operator = (const Vec4 &inRHS) = default;30explicit JPH_INLINE Vec4(Vec3Arg inRHS); ///< WARNING: W component undefined!31JPH_INLINE Vec4(Vec3Arg inRHS, float inW);32JPH_INLINE Vec4(Type inRHS) : mValue(inRHS) { }3334/// Create a vector from 4 components35JPH_INLINE Vec4(float inX, float inY, float inZ, float inW);3637/// Vector with all zeros38static JPH_INLINE Vec4 sZero();3940/// Vector with all ones41static JPH_INLINE Vec4 sOne();4243/// Vector with all NaN's44static JPH_INLINE Vec4 sNaN();4546/// Replicate inV across all components47static JPH_INLINE Vec4 sReplicate(float inV);4849/// Load 4 floats from memory50static JPH_INLINE Vec4 sLoadFloat4(const Float4 *inV);5152/// Load 4 floats from memory, 16 bytes aligned53static JPH_INLINE Vec4 sLoadFloat4Aligned(const Float4 *inV);5455/// Gather 4 floats from memory at inBase + inOffsets[i] * Scale56template <const int Scale>57static JPH_INLINE Vec4 sGatherFloat4(const float *inBase, UVec4Arg inOffsets);5859/// Return the minimum value of each of the components60static JPH_INLINE Vec4 sMin(Vec4Arg inV1, Vec4Arg inV2);6162/// Return the maximum of each of the components63static JPH_INLINE Vec4 sMax(Vec4Arg inV1, Vec4Arg inV2);6465/// Clamp a vector between min and max (component wise)66static JPH_INLINE Vec4 sClamp(Vec4Arg inV, Vec4Arg inMin, Vec4Arg inMax);6768/// Equals (component wise)69static JPH_INLINE UVec4 sEquals(Vec4Arg inV1, Vec4Arg inV2);7071/// Less than (component wise)72static JPH_INLINE UVec4 sLess(Vec4Arg inV1, Vec4Arg inV2);7374/// Less than or equal (component wise)75static JPH_INLINE UVec4 sLessOrEqual(Vec4Arg inV1, Vec4Arg inV2);7677/// Greater than (component wise)78static JPH_INLINE UVec4 sGreater(Vec4Arg inV1, Vec4Arg inV2);7980/// Greater than or equal (component wise)81static JPH_INLINE UVec4 sGreaterOrEqual(Vec4Arg inV1, Vec4Arg inV2);8283/// Calculates inMul1 * inMul2 + inAdd84static JPH_INLINE Vec4 sFusedMultiplyAdd(Vec4Arg inMul1, Vec4Arg inMul2, Vec4Arg inAdd);8586/// Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit of inControl = 187static JPH_INLINE Vec4 sSelect(Vec4Arg inNotSet, Vec4Arg inSet, UVec4Arg inControl);8889/// Logical or (component wise)90static JPH_INLINE Vec4 sOr(Vec4Arg inV1, Vec4Arg inV2);9192/// Logical xor (component wise)93static JPH_INLINE Vec4 sXor(Vec4Arg inV1, Vec4Arg inV2);9495/// Logical and (component wise)96static JPH_INLINE Vec4 sAnd(Vec4Arg inV1, Vec4Arg inV2);9798/// Sort the four elements of ioValue and sort ioIndex at the same time.99/// Based on a sorting network: http://en.wikipedia.org/wiki/Sorting_network100static JPH_INLINE void sSort4(Vec4 &ioValue, UVec4 &ioIndex);101102/// Reverse sort the four elements of ioValue (highest first) and sort ioIndex at the same time.103/// Based on a sorting network: http://en.wikipedia.org/wiki/Sorting_network104static JPH_INLINE void sSort4Reverse(Vec4 &ioValue, UVec4 &ioIndex);105106/// Get individual components107#if defined(JPH_USE_SSE)108JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); }109JPH_INLINE float GetY() const { return mF32[1]; }110JPH_INLINE float GetZ() const { return mF32[2]; }111JPH_INLINE float GetW() const { return mF32[3]; }112#elif defined(JPH_USE_NEON)113JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); }114JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); }115JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); }116JPH_INLINE float GetW() const { return vgetq_lane_f32(mValue, 3); }117#else118JPH_INLINE float GetX() const { return mF32[0]; }119JPH_INLINE float GetY() const { return mF32[1]; }120JPH_INLINE float GetZ() const { return mF32[2]; }121JPH_INLINE float GetW() const { return mF32[3]; }122#endif123124/// Set individual components125JPH_INLINE void SetX(float inX) { mF32[0] = inX; }126JPH_INLINE void SetY(float inY) { mF32[1] = inY; }127JPH_INLINE void SetZ(float inZ) { mF32[2] = inZ; }128JPH_INLINE void SetW(float inW) { mF32[3] = inW; }129130/// Set all components131JPH_INLINE void Set(float inX, float inY, float inZ, float inW) { *this = Vec4(inX, inY, inZ, inW); }132133/// Get float component by index134JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }135JPH_INLINE float & operator [] (uint inCoordinate) { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }136137/// Comparison138JPH_INLINE bool operator == (Vec4Arg inV2) const;139JPH_INLINE bool operator != (Vec4Arg inV2) const { return !(*this == inV2); }140141/// Test if two vectors are close142JPH_INLINE bool IsClose(Vec4Arg inV2, float inMaxDistSq = 1.0e-12f) const;143144/// Test if vector is near zero145JPH_INLINE bool IsNearZero(float inMaxDistSq = 1.0e-12f) const;146147/// Test if vector is normalized148JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const;149150/// Test if vector contains NaN elements151JPH_INLINE bool IsNaN() const;152153/// Multiply two float vectors (component wise)154JPH_INLINE Vec4 operator * (Vec4Arg inV2) const;155156/// Multiply vector with float157JPH_INLINE Vec4 operator * (float inV2) const;158159/// Multiply vector with float160friend JPH_INLINE Vec4 operator * (float inV1, Vec4Arg inV2);161162/// Divide vector by float163JPH_INLINE Vec4 operator / (float inV2) const;164165/// Multiply vector with float166JPH_INLINE Vec4 & operator *= (float inV2);167168/// Multiply vector with vector169JPH_INLINE Vec4 & operator *= (Vec4Arg inV2);170171/// Divide vector by float172JPH_INLINE Vec4 & operator /= (float inV2);173174/// Add two float vectors (component wise)175JPH_INLINE Vec4 operator + (Vec4Arg inV2) const;176177/// Add two float vectors (component wise)178JPH_INLINE Vec4 & operator += (Vec4Arg inV2);179180/// Negate181JPH_INLINE Vec4 operator - () const;182183/// Subtract two float vectors (component wise)184JPH_INLINE Vec4 operator - (Vec4Arg inV2) const;185186/// Subtract two float vectors (component wise)187JPH_INLINE Vec4 & operator -= (Vec4Arg inV2);188189/// Divide (component wise)190JPH_INLINE Vec4 operator / (Vec4Arg inV2) const;191192/// Swizzle the elements in inV193template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ, uint32 SwizzleW>194JPH_INLINE Vec4 Swizzle() const;195196/// Replicate the X component to all components197JPH_INLINE Vec4 SplatX() const;198199/// Replicate the Y component to all components200JPH_INLINE Vec4 SplatY() const;201202/// Replicate the Z component to all components203JPH_INLINE Vec4 SplatZ() const;204205/// Replicate the W component to all components206JPH_INLINE Vec4 SplatW() const;207208/// Replicate the X component to all components209JPH_INLINE Vec3 SplatX3() const;210211/// Replicate the Y component to all components212JPH_INLINE Vec3 SplatY3() const;213214/// Replicate the Z component to all components215JPH_INLINE Vec3 SplatZ3() const;216217/// Replicate the W component to all components218JPH_INLINE Vec3 SplatW3() const;219220/// Get index of component with lowest value221JPH_INLINE int GetLowestComponentIndex() const;222223/// Get index of component with highest value224JPH_INLINE int GetHighestComponentIndex() const;225226/// Return the absolute value of each of the components227JPH_INLINE Vec4 Abs() const;228229/// Reciprocal vector (1 / value) for each of the components230JPH_INLINE Vec4 Reciprocal() const;231232/// Dot product, returns the dot product in X, Y, Z and W components233JPH_INLINE Vec4 DotV(Vec4Arg inV2) const;234235/// Dot product236JPH_INLINE float Dot(Vec4Arg inV2) const;237238/// Squared length of vector239JPH_INLINE float LengthSq() const;240241/// Length of vector242JPH_INLINE float Length() const;243244/// Normalize vector245JPH_INLINE Vec4 Normalized() const;246247/// Store 4 floats to memory248JPH_INLINE void StoreFloat4(Float4 *outV) const;249250/// Convert each component from a float to an int251JPH_INLINE UVec4 ToInt() const;252253/// Reinterpret Vec4 as a UVec4 (doesn't change the bits)254JPH_INLINE UVec4 ReinterpretAsInt() const;255256/// Store if X is negative in bit 0, Y in bit 1, Z in bit 2 and W in bit 3257JPH_INLINE int GetSignBits() const;258259/// Get the minimum of X, Y, Z and W260JPH_INLINE float ReduceMin() const;261262/// Get the maximum of X, Y, Z and W263JPH_INLINE float ReduceMax() const;264265/// Component wise square root266JPH_INLINE Vec4 Sqrt() const;267268/// Get vector that contains the sign of each element (returns 1.0f if positive, -1.0f if negative)269JPH_INLINE Vec4 GetSign() const;270271/// Flips the signs of the components, e.g. FlipSign<-1, 1, -1, 1>() will flip the signs of the X and Z components272template <int X, int Y, int Z, int W>273JPH_INLINE Vec4 FlipSign() const;274275/// Calculate the sine and cosine for each element of this vector (input in radians)276inline void SinCos(Vec4 &outSin, Vec4 &outCos) const;277278/// Calculate the tangent for each element of this vector (input in radians)279inline Vec4 Tan() const;280281/// Calculate the arc sine for each element of this vector (returns value in the range [-PI / 2, PI / 2])282/// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::asin283inline Vec4 ASin() const;284285/// Calculate the arc cosine for each element of this vector (returns value in the range [0, PI])286/// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::acos287inline Vec4 ACos() const;288289/// Calculate the arc tangent for each element of this vector (returns value in the range [-PI / 2, PI / 2])290inline Vec4 ATan() const;291292/// Calculate the arc tangent of y / x using the signs of the arguments to determine the correct quadrant (returns value in the range [-PI, PI])293inline static Vec4 sATan2(Vec4Arg inY, Vec4Arg inX);294295/// Compress a unit vector to a 32 bit value, precision is around 0.5 * 10^-3296JPH_INLINE uint32 CompressUnitVector() const;297298/// Decompress a unit vector from a 32 bit value299JPH_INLINE static Vec4 sDecompressUnitVector(uint32 inValue);300301/// To String302friend ostream & operator << (ostream &inStream, Vec4Arg inV)303{304inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2] << ", " << inV.mF32[3];305return inStream;306}307308union309{310Type mValue;311float mF32[4];312};313};314315static_assert(std::is_trivial<Vec4>(), "Is supposed to be a trivial type!");316317JPH_NAMESPACE_END318319#include "Vec4.inl"320321322