Vec4.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Math/Float4.h>
  6. #include <Jolt/Math/Swizzle.h>
  7. #include <Jolt/Math/MathTypes.h>
  8. JPH_NAMESPACE_BEGIN
  9. class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Vec4
  10. {
  11. public:
  12. JPH_OVERRIDE_NEW_DELETE
  13. // Underlying vector type
  14. #if defined(JPH_USE_SSE)
  15. using Type = __m128;
  16. #elif defined(JPH_USE_NEON)
  17. using Type = float32x4_t;
  18. #else
  19. using Type = struct { float mData[4]; };
  20. #endif
  21. /// Constructor
  22. Vec4() = default; ///< Intentionally not initialized for performance reasons
  23. Vec4(const Vec4 &inRHS) = default;
  24. Vec4 & operator = (const Vec4 &inRHS) = default;
  25. explicit JPH_INLINE Vec4(Vec3Arg inRHS); ///< WARNING: W component undefined!
  26. JPH_INLINE Vec4(Vec3Arg inRHS, float inW);
  27. JPH_INLINE Vec4(Type inRHS) : mValue(inRHS) { }
  28. /// Create a vector from 4 components
  29. JPH_INLINE Vec4(float inX, float inY, float inZ, float inW);
  30. /// Vector with all zeros
  31. static JPH_INLINE Vec4 sZero();
  32. /// Vector with all ones
  33. static JPH_INLINE Vec4 sOne();
  34. /// Vector with all NaN's
  35. static JPH_INLINE Vec4 sNaN();
  36. /// Replicate inV across all components
  37. static JPH_INLINE Vec4 sReplicate(float inV);
  38. /// Load 4 floats from memory
  39. static JPH_INLINE Vec4 sLoadFloat4(const Float4 *inV);
  40. /// Load 4 floats from memory, 16 bytes aligned
  41. static JPH_INLINE Vec4 sLoadFloat4Aligned(const Float4 *inV);
  42. /// Gather 4 floats from memory at inBase + inOffsets[i] * Scale
  43. template <const int Scale>
  44. static JPH_INLINE Vec4 sGatherFloat4(const float *inBase, UVec4Arg inOffsets);
  45. /// Return the minimum value of each of the components
  46. static JPH_INLINE Vec4 sMin(Vec4Arg inV1, Vec4Arg inV2);
  47. /// Return the maximum of each of the components
  48. static JPH_INLINE Vec4 sMax(Vec4Arg inV1, Vec4Arg inV2);
  49. /// Clamp a vector between min and max (component wise)
  50. static JPH_INLINE Vec4 sClamp(Vec4Arg inV, Vec4Arg inMin, Vec4Arg inMax);
  51. /// Equals (component wise)
  52. static JPH_INLINE UVec4 sEquals(Vec4Arg inV1, Vec4Arg inV2);
  53. /// Less than (component wise)
  54. static JPH_INLINE UVec4 sLess(Vec4Arg inV1, Vec4Arg inV2);
  55. /// Less than or equal (component wise)
  56. static JPH_INLINE UVec4 sLessOrEqual(Vec4Arg inV1, Vec4Arg inV2);
  57. /// Greater than (component wise)
  58. static JPH_INLINE UVec4 sGreater(Vec4Arg inV1, Vec4Arg inV2);
  59. /// Greater than or equal (component wise)
  60. static JPH_INLINE UVec4 sGreaterOrEqual(Vec4Arg inV1, Vec4Arg inV2);
  61. /// Calculates inMul1 * inMul2 + inAdd
  62. static JPH_INLINE Vec4 sFusedMultiplyAdd(Vec4Arg inMul1, Vec4Arg inMul2, Vec4Arg inAdd);
  63. /// Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit of inControl = 1
  64. static JPH_INLINE Vec4 sSelect(Vec4Arg inNotSet, Vec4Arg inSet, UVec4Arg inControl);
  65. /// Logical or (component wise)
  66. static JPH_INLINE Vec4 sOr(Vec4Arg inV1, Vec4Arg inV2);
  67. /// Logical xor (component wise)
  68. static JPH_INLINE Vec4 sXor(Vec4Arg inV1, Vec4Arg inV2);
  69. /// Logical and (component wise)
  70. static JPH_INLINE Vec4 sAnd(Vec4Arg inV1, Vec4Arg inV2);
  71. /// Sort the four elements of ioValue and sort ioIndex at the same time.
  72. /// Based on a sorting network: http://en.wikipedia.org/wiki/Sorting_network
  73. static JPH_INLINE void sSort4(Vec4 &ioValue, UVec4 &ioIndex);
  74. /// Reverse sort the four elements of ioValue (highest first) and sort ioIndex at the same time.
  75. /// Based on a sorting network: http://en.wikipedia.org/wiki/Sorting_network
  76. static JPH_INLINE void sSort4Reverse(Vec4 &ioValue, UVec4 &ioIndex);
  77. /// Get individual components
  78. #if defined(JPH_USE_SSE)
  79. JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); }
  80. JPH_INLINE float GetY() const { return mF32[1]; }
  81. JPH_INLINE float GetZ() const { return mF32[2]; }
  82. JPH_INLINE float GetW() const { return mF32[3]; }
  83. #elif defined(JPH_USE_NEON)
  84. JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); }
  85. JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); }
  86. JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); }
  87. JPH_INLINE float GetW() const { return vgetq_lane_f32(mValue, 3); }
  88. #else
  89. JPH_INLINE float GetX() const { return mF32[0]; }
  90. JPH_INLINE float GetY() const { return mF32[1]; }
  91. JPH_INLINE float GetZ() const { return mF32[2]; }
  92. JPH_INLINE float GetW() const { return mF32[3]; }
  93. #endif
  94. /// Set individual components
  95. JPH_INLINE void SetX(float inX) { mF32[0] = inX; }
  96. JPH_INLINE void SetY(float inY) { mF32[1] = inY; }
  97. JPH_INLINE void SetZ(float inZ) { mF32[2] = inZ; }
  98. JPH_INLINE void SetW(float inW) { mF32[3] = inW; }
  99. /// Set all components
  100. JPH_INLINE void Set(float inX, float inY, float inZ, float inW) { *this = Vec4(inX, inY, inZ, inW); }
  101. /// Get float component by index
  102. JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }
  103. JPH_INLINE float & operator [] (uint inCoordinate) { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }
  104. /// Comparison
  105. JPH_INLINE bool operator == (Vec4Arg inV2) const;
  106. JPH_INLINE bool operator != (Vec4Arg inV2) const { return !(*this == inV2); }
  107. /// Test if two vectors are close
  108. JPH_INLINE bool IsClose(Vec4Arg inV2, float inMaxDistSq = 1.0e-12f) const;
  109. /// Test if vector is near zero
  110. JPH_INLINE bool IsNearZero(float inMaxDistSq = 1.0e-12f) const;
  111. /// Test if vector is normalized
  112. JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const;
  113. /// Test if vector contains NaN elements
  114. JPH_INLINE bool IsNaN() const;
  115. /// Multiply two float vectors (component wise)
  116. JPH_INLINE Vec4 operator * (Vec4Arg inV2) const;
  117. /// Multiply vector with float
  118. JPH_INLINE Vec4 operator * (float inV2) const;
  119. /// Multiply vector with float
  120. friend JPH_INLINE Vec4 operator * (float inV1, Vec4Arg inV2);
  121. /// Divide vector by float
  122. JPH_INLINE Vec4 operator / (float inV2) const;
  123. /// Multiply vector with float
  124. JPH_INLINE Vec4 & operator *= (float inV2);
  125. /// Multiply vector with vector
  126. JPH_INLINE Vec4 & operator *= (Vec4Arg inV2);
  127. /// Divide vector by float
  128. JPH_INLINE Vec4 & operator /= (float inV2);
  129. /// Add two float vectors (component wise)
  130. JPH_INLINE Vec4 operator + (Vec4Arg inV2) const;
  131. /// Add two float vectors (component wise)
  132. JPH_INLINE Vec4 & operator += (Vec4Arg inV2);
  133. /// Negate
  134. JPH_INLINE Vec4 operator - () const;
  135. /// Subtract two float vectors (component wise)
  136. JPH_INLINE Vec4 operator - (Vec4Arg inV2) const;
  137. /// Subtract two float vectors (component wise)
  138. JPH_INLINE Vec4 & operator -= (Vec4Arg inV2);
  139. /// Divide (component wise)
  140. JPH_INLINE Vec4 operator / (Vec4Arg inV2) const;
  141. /// Swizzle the elements in inV
  142. template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ, uint32 SwizzleW>
  143. JPH_INLINE Vec4 Swizzle() const;
  144. /// Replicate the X component to all components
  145. JPH_INLINE Vec4 SplatX() const;
  146. /// Replicate the Y component to all components
  147. JPH_INLINE Vec4 SplatY() const;
  148. /// Replicate the Z component to all components
  149. JPH_INLINE Vec4 SplatZ() const;
  150. /// Replicate the W component to all components
  151. JPH_INLINE Vec4 SplatW() const;
  152. /// Replicate the X component to all components
  153. JPH_INLINE Vec3 SplatX3() const;
  154. /// Replicate the Y component to all components
  155. JPH_INLINE Vec3 SplatY3() const;
  156. /// Replicate the Z component to all components
  157. JPH_INLINE Vec3 SplatZ3() const;
  158. /// Replicate the W component to all components
  159. JPH_INLINE Vec3 SplatW3() const;
  160. /// Get index of component with lowest value
  161. JPH_INLINE int GetLowestComponentIndex() const;
  162. /// Get index of component with highest value
  163. JPH_INLINE int GetHighestComponentIndex() const;
  164. /// Return the absolute value of each of the components
  165. JPH_INLINE Vec4 Abs() const;
  166. /// Reciprocal vector (1 / value) for each of the components
  167. JPH_INLINE Vec4 Reciprocal() const;
  168. /// Dot product, returns the dot product in X, Y, Z and W components
  169. JPH_INLINE Vec4 DotV(Vec4Arg inV2) const;
  170. /// Dot product
  171. JPH_INLINE float Dot(Vec4Arg inV2) const;
  172. /// Squared length of vector
  173. JPH_INLINE float LengthSq() const;
  174. /// Length of vector
  175. JPH_INLINE float Length() const;
  176. /// Normalize vector
  177. JPH_INLINE Vec4 Normalized() const;
  178. /// Store 4 floats to memory
  179. JPH_INLINE void StoreFloat4(Float4 *outV) const;
  180. /// Convert each component from a float to an int
  181. JPH_INLINE UVec4 ToInt() const;
  182. /// Reinterpret Vec4 as a UVec4 (doesn't change the bits)
  183. JPH_INLINE UVec4 ReinterpretAsInt() const;
  184. /// Store if X is negative in bit 0, Y in bit 1, Z in bit 2 and W in bit 3
  185. JPH_INLINE int GetSignBits() const;
  186. /// Get the minimum of X, Y, Z and W
  187. JPH_INLINE float ReduceMin() const;
  188. /// Get the maximum of X, Y, Z and W
  189. JPH_INLINE float ReduceMax() const;
  190. /// Component wise square root
  191. JPH_INLINE Vec4 Sqrt() const;
  192. /// Get vector that contains the sign of each element (returns 1.0f if positive, -1.0f if negative)
  193. JPH_INLINE Vec4 GetSign() const;
  194. /// Flips the signs of the components, e.g. FlipSign<-1, 1, -1, 1>() will flip the signs of the X and Z components
  195. template <int X, int Y, int Z, int W>
  196. JPH_INLINE Vec4 FlipSign() const;
  197. /// Calculate the sine and cosine for each element of this vector (input in radians)
  198. inline void SinCos(Vec4 &outSin, Vec4 &outCos) const;
  199. /// Calculate the tangent for each element of this vector (input in radians)
  200. inline Vec4 Tan() const;
  201. /// Calculate the arc sine for each element of this vector (returns value in the range [-PI / 2, PI / 2])
  202. /// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::asin
  203. inline Vec4 ASin() const;
  204. /// Calculate the arc cosine for each element of this vector (returns value in the range [0, PI])
  205. /// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::acos
  206. inline Vec4 ACos() const;
  207. /// Calculate the arc tangent for each element of this vector (returns value in the range [-PI / 2, PI / 2])
  208. inline Vec4 ATan() const;
  209. /// 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])
  210. inline static Vec4 sATan2(Vec4Arg inY, Vec4Arg inX);
  211. /// Compress a unit vector to a 32 bit value, precision is around 0.5 * 10^-3
  212. JPH_INLINE uint32 CompressUnitVector() const;
  213. /// Decompress a unit vector from a 32 bit value
  214. JPH_INLINE static Vec4 sDecompressUnitVector(uint32 inValue);
  215. /// To String
  216. friend ostream & operator << (ostream &inStream, Vec4Arg inV)
  217. {
  218. inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2] << ", " << inV.mF32[3];
  219. return inStream;
  220. }
  221. union
  222. {
  223. Type mValue;
  224. float mF32[4];
  225. };
  226. };
  227. static_assert(std::is_trivial<Vec4>(), "Is supposed to be a trivial type!");
  228. JPH_NAMESPACE_END
  229. #include "Vec4.inl"