Path: blob/master/thirdparty/jolt_physics/Jolt/Math/Vec3.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/Core/StaticArray.h>7#include <Jolt/Math/Float3.h>8#include <Jolt/Math/Swizzle.h>9#include <Jolt/Math/MathTypes.h>1011JPH_NAMESPACE_BEGIN1213/// 3 component vector (stored as 4 vectors).14/// Note that we keep the 4th component the same as the 3rd component to avoid divisions by zero when JPH_FLOATING_POINT_EXCEPTIONS_ENABLED defined15class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Vec316{17public:18JPH_OVERRIDE_NEW_DELETE1920// Underlying vector type21#if defined(JPH_USE_SSE)22using Type = __m128;23#elif defined(JPH_USE_NEON)24using Type = float32x4_t;25#else26using Type = Vec4::Type;27#endif2829// Argument type30using ArgType = Vec3Arg;3132/// Constructor33Vec3() = default; ///< Intentionally not initialized for performance reasons34Vec3(const Vec3 &inRHS) = default;35Vec3 & operator = (const Vec3 &inRHS) = default;36explicit JPH_INLINE Vec3(Vec4Arg inRHS);37JPH_INLINE Vec3(Type inRHS) : mValue(inRHS) { CheckW(); }3839/// Load 3 floats from memory40explicit JPH_INLINE Vec3(const Float3 &inV);4142/// Create a vector from 3 components43JPH_INLINE Vec3(float inX, float inY, float inZ);4445/// Vector with all zeros46static JPH_INLINE Vec3 sZero();4748/// Vector with all ones49static JPH_INLINE Vec3 sOne();5051/// Vector with all NaN's52static JPH_INLINE Vec3 sNaN();5354/// Vectors with the principal axis55static JPH_INLINE Vec3 sAxisX() { return Vec3(1, 0, 0); }56static JPH_INLINE Vec3 sAxisY() { return Vec3(0, 1, 0); }57static JPH_INLINE Vec3 sAxisZ() { return Vec3(0, 0, 1); }5859/// Replicate inV across all components60static JPH_INLINE Vec3 sReplicate(float inV);6162/// Load 3 floats from memory (reads 32 bits extra which it doesn't use)63static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV);6465/// Return the minimum value of each of the components66static JPH_INLINE Vec3 sMin(Vec3Arg inV1, Vec3Arg inV2);6768/// Return the maximum of each of the components69static JPH_INLINE Vec3 sMax(Vec3Arg inV1, Vec3Arg inV2);7071/// Clamp a vector between min and max (component wise)72static JPH_INLINE Vec3 sClamp(Vec3Arg inV, Vec3Arg inMin, Vec3Arg inMax);7374/// Equals (component wise)75static JPH_INLINE UVec4 sEquals(Vec3Arg inV1, Vec3Arg inV2);7677/// Less than (component wise)78static JPH_INLINE UVec4 sLess(Vec3Arg inV1, Vec3Arg inV2);7980/// Less than or equal (component wise)81static JPH_INLINE UVec4 sLessOrEqual(Vec3Arg inV1, Vec3Arg inV2);8283/// Greater than (component wise)84static JPH_INLINE UVec4 sGreater(Vec3Arg inV1, Vec3Arg inV2);8586/// Greater than or equal (component wise)87static JPH_INLINE UVec4 sGreaterOrEqual(Vec3Arg inV1, Vec3Arg inV2);8889/// Calculates inMul1 * inMul2 + inAdd90static JPH_INLINE Vec3 sFusedMultiplyAdd(Vec3Arg inMul1, Vec3Arg inMul2, Vec3Arg inAdd);9192/// Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit of inControl = 193static JPH_INLINE Vec3 sSelect(Vec3Arg inNotSet, Vec3Arg inSet, UVec4Arg inControl);9495/// Logical or (component wise)96static JPH_INLINE Vec3 sOr(Vec3Arg inV1, Vec3Arg inV2);9798/// Logical xor (component wise)99static JPH_INLINE Vec3 sXor(Vec3Arg inV1, Vec3Arg inV2);100101/// Logical and (component wise)102static JPH_INLINE Vec3 sAnd(Vec3Arg inV1, Vec3Arg inV2);103104/// Get unit vector given spherical coordinates105/// inTheta \f$\in [0, \pi]\f$ is angle between vector and z-axis106/// inPhi \f$\in [0, 2 \pi]\f$ is the angle in the xy-plane starting from the x axis and rotating counter clockwise around the z-axis107static JPH_INLINE Vec3 sUnitSpherical(float inTheta, float inPhi);108109/// A set of vectors uniformly spanning the surface of a unit sphere, usable for debug purposes110JPH_EXPORT static const StaticArray<Vec3, 1026> sUnitSphere;111112/// Get random unit vector113template <class Random>114static inline Vec3 sRandom(Random &inRandom);115116/// Get individual components117#if defined(JPH_USE_SSE)118JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); }119JPH_INLINE float GetY() const { return mF32[1]; }120JPH_INLINE float GetZ() const { return mF32[2]; }121#elif defined(JPH_USE_NEON)122JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); }123JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); }124JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); }125#else126JPH_INLINE float GetX() const { return mF32[0]; }127JPH_INLINE float GetY() const { return mF32[1]; }128JPH_INLINE float GetZ() const { return mF32[2]; }129#endif130131/// Set individual components132JPH_INLINE void SetX(float inX) { mF32[0] = inX; }133JPH_INLINE void SetY(float inY) { mF32[1] = inY; }134JPH_INLINE void SetZ(float inZ) { mF32[2] = mF32[3] = inZ; } // Assure Z and W are the same135136/// Set all components137JPH_INLINE void Set(float inX, float inY, float inZ) { *this = Vec3(inX, inY, inZ); }138139/// Get float component by index140JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 3); return mF32[inCoordinate]; }141142/// Set float component by index143JPH_INLINE void SetComponent(uint inCoordinate, float inValue) { JPH_ASSERT(inCoordinate < 3); mF32[inCoordinate] = inValue; mValue = sFixW(mValue); } // Assure Z and W are the same144145/// Comparison146JPH_INLINE bool operator == (Vec3Arg inV2) const;147JPH_INLINE bool operator != (Vec3Arg inV2) const { return !(*this == inV2); }148149/// Test if two vectors are close150JPH_INLINE bool IsClose(Vec3Arg inV2, float inMaxDistSq = 1.0e-12f) const;151152/// Test if vector is near zero153JPH_INLINE bool IsNearZero(float inMaxDistSq = 1.0e-12f) const;154155/// Test if vector is normalized156JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const;157158/// Test if vector contains NaN elements159JPH_INLINE bool IsNaN() const;160161/// Multiply two float vectors (component wise)162JPH_INLINE Vec3 operator * (Vec3Arg inV2) const;163164/// Multiply vector with float165JPH_INLINE Vec3 operator * (float inV2) const;166167/// Multiply vector with float168friend JPH_INLINE Vec3 operator * (float inV1, Vec3Arg inV2);169170/// Divide vector by float171JPH_INLINE Vec3 operator / (float inV2) const;172173/// Multiply vector with float174JPH_INLINE Vec3 & operator *= (float inV2);175176/// Multiply vector with vector177JPH_INLINE Vec3 & operator *= (Vec3Arg inV2);178179/// Divide vector by float180JPH_INLINE Vec3 & operator /= (float inV2);181182/// Add two float vectors (component wise)183JPH_INLINE Vec3 operator + (Vec3Arg inV2) const;184185/// Add two float vectors (component wise)186JPH_INLINE Vec3 & operator += (Vec3Arg inV2);187188/// Negate189JPH_INLINE Vec3 operator - () const;190191/// Subtract two float vectors (component wise)192JPH_INLINE Vec3 operator - (Vec3Arg inV2) const;193194/// Subtract two float vectors (component wise)195JPH_INLINE Vec3 & operator -= (Vec3Arg inV2);196197/// Divide (component wise)198JPH_INLINE Vec3 operator / (Vec3Arg inV2) const;199200/// Swizzle the elements in inV201template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ>202JPH_INLINE Vec3 Swizzle() const;203204/// Replicate the X component to all components205JPH_INLINE Vec4 SplatX() const;206207/// Replicate the Y component to all components208JPH_INLINE Vec4 SplatY() const;209210/// Replicate the Z component to all components211JPH_INLINE Vec4 SplatZ() const;212213/// Get index of component with lowest value214JPH_INLINE int GetLowestComponentIndex() const;215216/// Get index of component with highest value217JPH_INLINE int GetHighestComponentIndex() const;218219/// Return the absolute value of each of the components220JPH_INLINE Vec3 Abs() const;221222/// Reciprocal vector (1 / value) for each of the components223JPH_INLINE Vec3 Reciprocal() const;224225/// Cross product226JPH_INLINE Vec3 Cross(Vec3Arg inV2) const;227228/// Dot product, returns the dot product in X, Y and Z components229JPH_INLINE Vec3 DotV(Vec3Arg inV2) const;230231/// Dot product, returns the dot product in X, Y, Z and W components232JPH_INLINE Vec4 DotV4(Vec3Arg inV2) const;233234/// Dot product235JPH_INLINE float Dot(Vec3Arg inV2) const;236237/// Squared length of vector238JPH_INLINE float LengthSq() const;239240/// Length of vector241JPH_INLINE float Length() const;242243/// Normalize vector244JPH_INLINE Vec3 Normalized() const;245246/// Normalize vector or return inZeroValue if the length of the vector is zero247JPH_INLINE Vec3 NormalizedOr(Vec3Arg inZeroValue) const;248249/// Store 3 floats to memory250JPH_INLINE void StoreFloat3(Float3 *outV) const;251252/// Convert each component from a float to an int253JPH_INLINE UVec4 ToInt() const;254255/// Reinterpret Vec3 as a UVec4 (doesn't change the bits)256JPH_INLINE UVec4 ReinterpretAsInt() const;257258/// Get the minimum of X, Y and Z259JPH_INLINE float ReduceMin() const;260261/// Get the maximum of X, Y and Z262JPH_INLINE float ReduceMax() const;263264/// Component wise square root265JPH_INLINE Vec3 Sqrt() const;266267/// Get normalized vector that is perpendicular to this vector268JPH_INLINE Vec3 GetNormalizedPerpendicular() const;269270/// Get vector that contains the sign of each element (returns 1.0f if positive, -1.0f if negative)271JPH_INLINE Vec3 GetSign() const;272273/// To String274friend ostream & operator << (ostream &inStream, Vec3Arg inV)275{276inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2];277return inStream;278}279280/// Internal helper function that checks that W is equal to Z, so e.g. dividing by it should not generate div by 0281JPH_INLINE void CheckW() const;282283/// Internal helper function that ensures that the Z component is replicated to the W component to prevent divisions by zero284static JPH_INLINE Type sFixW(Type inValue);285286union287{288Type mValue;289float mF32[4];290};291};292293static_assert(std::is_trivial<Vec3>(), "Is supposed to be a trivial type!");294295JPH_NAMESPACE_END296297#include "Vec3.inl"298299300