Path: blob/master/thirdparty/jolt_physics/Jolt/Math/Vec4.h
9913 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/// Equals (component wise)66static JPH_INLINE UVec4 sEquals(Vec4Arg inV1, Vec4Arg inV2);6768/// Less than (component wise)69static JPH_INLINE UVec4 sLess(Vec4Arg inV1, Vec4Arg inV2);7071/// Less than or equal (component wise)72static JPH_INLINE UVec4 sLessOrEqual(Vec4Arg inV1, Vec4Arg inV2);7374/// Greater than (component wise)75static JPH_INLINE UVec4 sGreater(Vec4Arg inV1, Vec4Arg inV2);7677/// Greater than or equal (component wise)78static JPH_INLINE UVec4 sGreaterOrEqual(Vec4Arg inV1, Vec4Arg inV2);7980/// Calculates inMul1 * inMul2 + inAdd81static JPH_INLINE Vec4 sFusedMultiplyAdd(Vec4Arg inMul1, Vec4Arg inMul2, Vec4Arg inAdd);8283/// Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit of inControl = 184static JPH_INLINE Vec4 sSelect(Vec4Arg inNotSet, Vec4Arg inSet, UVec4Arg inControl);8586/// Logical or (component wise)87static JPH_INLINE Vec4 sOr(Vec4Arg inV1, Vec4Arg inV2);8889/// Logical xor (component wise)90static JPH_INLINE Vec4 sXor(Vec4Arg inV1, Vec4Arg inV2);9192/// Logical and (component wise)93static JPH_INLINE Vec4 sAnd(Vec4Arg inV1, Vec4Arg inV2);9495/// Sort the four elements of ioValue and sort ioIndex at the same time.96/// Based on a sorting network: http://en.wikipedia.org/wiki/Sorting_network97static JPH_INLINE void sSort4(Vec4 &ioValue, UVec4 &ioIndex);9899/// Reverse sort the four elements of ioValue (highest first) and sort ioIndex at the same time.100/// Based on a sorting network: http://en.wikipedia.org/wiki/Sorting_network101static JPH_INLINE void sSort4Reverse(Vec4 &ioValue, UVec4 &ioIndex);102103/// Get individual components104#if defined(JPH_USE_SSE)105JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); }106JPH_INLINE float GetY() const { return mF32[1]; }107JPH_INLINE float GetZ() const { return mF32[2]; }108JPH_INLINE float GetW() const { return mF32[3]; }109#elif defined(JPH_USE_NEON)110JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); }111JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); }112JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); }113JPH_INLINE float GetW() const { return vgetq_lane_f32(mValue, 3); }114#else115JPH_INLINE float GetX() const { return mF32[0]; }116JPH_INLINE float GetY() const { return mF32[1]; }117JPH_INLINE float GetZ() const { return mF32[2]; }118JPH_INLINE float GetW() const { return mF32[3]; }119#endif120121/// Set individual components122JPH_INLINE void SetX(float inX) { mF32[0] = inX; }123JPH_INLINE void SetY(float inY) { mF32[1] = inY; }124JPH_INLINE void SetZ(float inZ) { mF32[2] = inZ; }125JPH_INLINE void SetW(float inW) { mF32[3] = inW; }126127/// Set all components128JPH_INLINE void Set(float inX, float inY, float inZ, float inW) { *this = Vec4(inX, inY, inZ, inW); }129130/// Get float component by index131JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }132JPH_INLINE float & operator [] (uint inCoordinate) { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }133134/// Comparison135JPH_INLINE bool operator == (Vec4Arg inV2) const;136JPH_INLINE bool operator != (Vec4Arg inV2) const { return !(*this == inV2); }137138/// Test if two vectors are close139JPH_INLINE bool IsClose(Vec4Arg inV2, float inMaxDistSq = 1.0e-12f) const;140141/// Test if vector is normalized142JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const;143144/// Test if vector contains NaN elements145JPH_INLINE bool IsNaN() const;146147/// Multiply two float vectors (component wise)148JPH_INLINE Vec4 operator * (Vec4Arg inV2) const;149150/// Multiply vector with float151JPH_INLINE Vec4 operator * (float inV2) const;152153/// Multiply vector with float154friend JPH_INLINE Vec4 operator * (float inV1, Vec4Arg inV2);155156/// Divide vector by float157JPH_INLINE Vec4 operator / (float inV2) const;158159/// Multiply vector with float160JPH_INLINE Vec4 & operator *= (float inV2);161162/// Multiply vector with vector163JPH_INLINE Vec4 & operator *= (Vec4Arg inV2);164165/// Divide vector by float166JPH_INLINE Vec4 & operator /= (float inV2);167168/// Add two float vectors (component wise)169JPH_INLINE Vec4 operator + (Vec4Arg inV2) const;170171/// Add two float vectors (component wise)172JPH_INLINE Vec4 & operator += (Vec4Arg inV2);173174/// Negate175JPH_INLINE Vec4 operator - () const;176177/// Subtract two float vectors (component wise)178JPH_INLINE Vec4 operator - (Vec4Arg inV2) const;179180/// Subtract two float vectors (component wise)181JPH_INLINE Vec4 & operator -= (Vec4Arg inV2);182183/// Divide (component wise)184JPH_INLINE Vec4 operator / (Vec4Arg inV2) const;185186/// Swizzle the elements in inV187template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ, uint32 SwizzleW>188JPH_INLINE Vec4 Swizzle() const;189190/// Replicate the X component to all components191JPH_INLINE Vec4 SplatX() const;192193/// Replicate the Y component to all components194JPH_INLINE Vec4 SplatY() const;195196/// Replicate the Z component to all components197JPH_INLINE Vec4 SplatZ() const;198199/// Replicate the W component to all components200JPH_INLINE Vec4 SplatW() const;201202/// Return the absolute value of each of the components203JPH_INLINE Vec4 Abs() const;204205/// Reciprocal vector (1 / value) for each of the components206JPH_INLINE Vec4 Reciprocal() const;207208/// Dot product, returns the dot product in X, Y and Z components209JPH_INLINE Vec4 DotV(Vec4Arg inV2) const;210211/// Dot product212JPH_INLINE float Dot(Vec4Arg inV2) const;213214/// Squared length of vector215JPH_INLINE float LengthSq() const;216217/// Length of vector218JPH_INLINE float Length() const;219220/// Normalize vector221JPH_INLINE Vec4 Normalized() const;222223/// Store 4 floats to memory224JPH_INLINE void StoreFloat4(Float4 *outV) const;225226/// Convert each component from a float to an int227JPH_INLINE UVec4 ToInt() const;228229/// Reinterpret Vec4 as a UVec4 (doesn't change the bits)230JPH_INLINE UVec4 ReinterpretAsInt() const;231232/// Store if X is negative in bit 0, Y in bit 1, Z in bit 2 and W in bit 3233JPH_INLINE int GetSignBits() const;234235/// Get the minimum of X, Y, Z and W236JPH_INLINE float ReduceMin() const;237238/// Get the maximum of X, Y, Z and W239JPH_INLINE float ReduceMax() const;240241/// Component wise square root242JPH_INLINE Vec4 Sqrt() const;243244/// Get vector that contains the sign of each element (returns 1.0f if positive, -1.0f if negative)245JPH_INLINE Vec4 GetSign() const;246247/// Calculate the sine and cosine for each element of this vector (input in radians)248inline void SinCos(Vec4 &outSin, Vec4 &outCos) const;249250/// Calculate the tangent for each element of this vector (input in radians)251inline Vec4 Tan() const;252253/// Calculate the arc sine for each element of this vector (returns value in the range [-PI / 2, PI / 2])254/// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::asin255inline Vec4 ASin() const;256257/// Calculate the arc cosine for each element of this vector (returns value in the range [0, PI])258/// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::acos259inline Vec4 ACos() const;260261/// Calculate the arc tangent for each element of this vector (returns value in the range [-PI / 2, PI / 2])262inline Vec4 ATan() const;263264/// 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])265inline static Vec4 sATan2(Vec4Arg inY, Vec4Arg inX);266267/// To String268friend ostream & operator << (ostream &inStream, Vec4Arg inV)269{270inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2] << ", " << inV.mF32[3];271return inStream;272}273274union275{276Type mValue;277float mF32[4];278};279};280281static_assert(std::is_trivial<Vec4>(), "Is supposed to be a trivial type!");282283JPH_NAMESPACE_END284285#include "Vec4.inl"286287288