Vec3.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/Float3.h>
  6. #include <Jolt/Math/Swizzle.h>
  7. #include <Jolt/Math/MathTypes.h>
  8. JPH_NAMESPACE_BEGIN
  9. /// 3 component vector (stored as 4 vectors).
  10. /// Note that we keep the 4th component the same as the 3rd component to avoid divisions by zero when JPH_FLOATING_POINT_EXCEPTIONS_ENABLED defined
  11. class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Vec3
  12. {
  13. public:
  14. JPH_OVERRIDE_NEW_DELETE
  15. // Underlying vector type
  16. #if defined(JPH_USE_SSE)
  17. using Type = __m128;
  18. #elif defined(JPH_USE_NEON)
  19. using Type = float32x4_t;
  20. #else
  21. using Type = Vec4::Type;
  22. #endif
  23. // Argument type
  24. using ArgType = Vec3Arg;
  25. /// Constructor
  26. Vec3() = default; ///< Intentionally not initialized for performance reasons
  27. Vec3(const Vec3 &inRHS) = default;
  28. explicit JPH_INLINE Vec3(Vec4Arg inRHS);
  29. JPH_INLINE Vec3(Type inRHS) : mValue(inRHS) { CheckW(); }
  30. /// Load 3 floats from memory
  31. explicit JPH_INLINE Vec3(const Float3 &inV);
  32. /// Create a vector from 3 components
  33. JPH_INLINE Vec3(float inX, float inY, float inZ);
  34. /// Vector with all zeros
  35. static JPH_INLINE Vec3 sZero();
  36. /// Vector with all NaN's
  37. static JPH_INLINE Vec3 sNaN();
  38. /// Vectors with the principal axis
  39. static JPH_INLINE Vec3 sAxisX() { return Vec3(1, 0, 0); }
  40. static JPH_INLINE Vec3 sAxisY() { return Vec3(0, 1, 0); }
  41. static JPH_INLINE Vec3 sAxisZ() { return Vec3(0, 0, 1); }
  42. /// Replicate inV across all components
  43. static JPH_INLINE Vec3 sReplicate(float inV);
  44. /// Load 3 floats from memory (reads 32 bits extra which it doesn't use)
  45. static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV);
  46. /// Return the minimum value of each of the components
  47. static JPH_INLINE Vec3 sMin(Vec3Arg inV1, Vec3Arg inV2);
  48. /// Return the maximum of each of the components
  49. static JPH_INLINE Vec3 sMax(Vec3Arg inV1, Vec3Arg inV2);
  50. /// Clamp a vector between min and max (component wise)
  51. static JPH_INLINE Vec3 sClamp(Vec3Arg inV, Vec3Arg inMin, Vec3Arg inMax);
  52. /// Equals (component wise)
  53. static JPH_INLINE UVec4 sEquals(Vec3Arg inV1, Vec3Arg inV2);
  54. /// Less than (component wise)
  55. static JPH_INLINE UVec4 sLess(Vec3Arg inV1, Vec3Arg inV2);
  56. /// Less than or equal (component wise)
  57. static JPH_INLINE UVec4 sLessOrEqual(Vec3Arg inV1, Vec3Arg inV2);
  58. /// Greater than (component wise)
  59. static JPH_INLINE UVec4 sGreater(Vec3Arg inV1, Vec3Arg inV2);
  60. /// Greater than or equal (component wise)
  61. static JPH_INLINE UVec4 sGreaterOrEqual(Vec3Arg inV1, Vec3Arg inV2);
  62. /// Calculates inMul1 * inMul2 + inAdd
  63. static JPH_INLINE Vec3 sFusedMultiplyAdd(Vec3Arg inMul1, Vec3Arg inMul2, Vec3Arg inAdd);
  64. /// Component wise select, returns inV1 when highest bit of inControl = 0 and inV2 when highest bit of inControl = 1
  65. static JPH_INLINE Vec3 sSelect(Vec3Arg inV1, Vec3Arg inV2, UVec4Arg inControl);
  66. /// Logical or (component wise)
  67. static JPH_INLINE Vec3 sOr(Vec3Arg inV1, Vec3Arg inV2);
  68. /// Logical xor (component wise)
  69. static JPH_INLINE Vec3 sXor(Vec3Arg inV1, Vec3Arg inV2);
  70. /// Logical and (component wise)
  71. static JPH_INLINE Vec3 sAnd(Vec3Arg inV1, Vec3Arg inV2);
  72. /// Get unit vector given spherical coordinates
  73. /// inTheta \f$\in [0, \pi]\f$ is angle between vector and z-axis
  74. /// 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-axis
  75. static JPH_INLINE Vec3 sUnitSpherical(float inTheta, float inPhi);
  76. /// A set of vectors uniformly spanning the surface of a unit sphere, usable for debug purposes
  77. static const std::vector<Vec3> sUnitSphere;
  78. /// Get random unit vector
  79. template <class Random>
  80. static inline Vec3 sRandom(Random &inRandom);
  81. /// Get individual components
  82. #if defined(JPH_USE_SSE)
  83. JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); }
  84. JPH_INLINE float GetY() const { return mF32[1]; }
  85. JPH_INLINE float GetZ() const { return mF32[2]; }
  86. #elif defined(JPH_USE_NEON)
  87. JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); }
  88. JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); }
  89. JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); }
  90. #else
  91. JPH_INLINE float GetX() const { return mF32[0]; }
  92. JPH_INLINE float GetY() const { return mF32[1]; }
  93. JPH_INLINE float GetZ() const { return mF32[2]; }
  94. #endif
  95. /// Set individual components
  96. JPH_INLINE void SetX(float inX) { mF32[0] = inX; }
  97. JPH_INLINE void SetY(float inY) { mF32[1] = inY; }
  98. JPH_INLINE void SetZ(float inZ) { mF32[2] = mF32[3] = inZ; } // Assure Z and W are the same
  99. /// Get float component by index
  100. JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 3); return mF32[inCoordinate]; }
  101. /// Set float component by index
  102. JPH_INLINE void SetComponent(uint inCoordinate, float inValue) { JPH_ASSERT(inCoordinate < 3); mF32[inCoordinate] = inValue; mValue = sFixW(mValue); } // Assure Z and W are the same
  103. /// Comparison
  104. JPH_INLINE bool operator == (Vec3Arg inV2) const;
  105. JPH_INLINE bool operator != (Vec3Arg inV2) const { return !(*this == inV2); }
  106. /// Test if two vectors are close
  107. JPH_INLINE bool IsClose(Vec3Arg inV2, float inMaxDistSq = 1.0e-12f) const;
  108. /// Test if vector is near zero
  109. JPH_INLINE bool IsNearZero(float inMaxDistSq = 1.0e-12f) const;
  110. /// Test if vector is normalized
  111. JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const;
  112. /// Test if vector contains NaN elements
  113. JPH_INLINE bool IsNaN() const;
  114. /// Multiply two float vectors (component wise)
  115. JPH_INLINE Vec3 operator * (Vec3Arg inV2) const;
  116. /// Multiply vector with float
  117. JPH_INLINE Vec3 operator * (float inV2) const;
  118. /// Multiply vector with float
  119. friend JPH_INLINE Vec3 operator * (float inV1, Vec3Arg inV2);
  120. /// Divide vector by float
  121. JPH_INLINE Vec3 operator / (float inV2) const;
  122. /// Multiply vector with float
  123. JPH_INLINE Vec3 & operator *= (float inV2);
  124. /// Multiply vector with vector
  125. JPH_INLINE Vec3 & operator *= (Vec3Arg inV2);
  126. /// Divide vector by float
  127. JPH_INLINE Vec3 & operator /= (float inV2);
  128. /// Add two float vectors (component wise)
  129. JPH_INLINE Vec3 operator + (Vec3Arg inV2) const;
  130. /// Add two float vectors (component wise)
  131. JPH_INLINE Vec3 & operator += (Vec3Arg inV2);
  132. /// Negate
  133. JPH_INLINE Vec3 operator - () const;
  134. /// Subtract two float vectors (component wise)
  135. JPH_INLINE Vec3 operator - (Vec3Arg inV2) const;
  136. /// Add two float vectors (component wise)
  137. JPH_INLINE Vec3 & operator -= (Vec3Arg inV2);
  138. /// Divide (component wise)
  139. JPH_INLINE Vec3 operator / (Vec3Arg inV2) const;
  140. /// Swizzle the elements in inV
  141. template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ>
  142. JPH_INLINE Vec3 Swizzle() const;
  143. /// Replicate the X component to all components
  144. JPH_INLINE Vec4 SplatX() const;
  145. /// Replicate the Y component to all components
  146. JPH_INLINE Vec4 SplatY() const;
  147. /// Replicate the Z component to all components
  148. JPH_INLINE Vec4 SplatZ() const;
  149. /// Get index of component with lowest value
  150. JPH_INLINE int GetLowestComponentIndex() const;
  151. /// Get index of component with highest value
  152. JPH_INLINE int GetHighestComponentIndex() const;
  153. /// Return the absolute value of each of the components
  154. JPH_INLINE Vec3 Abs() const;
  155. /// Reciprocal vector (1 / value) for each of the components
  156. JPH_INLINE Vec3 Reciprocal() const;
  157. /// Cross product
  158. JPH_INLINE Vec3 Cross(Vec3Arg inV2) const;
  159. /// Dot product, returns the dot product in X, Y and Z components
  160. JPH_INLINE Vec3 DotV(Vec3Arg inV2) const;
  161. /// Dot product, returns the dot product in X, Y, Z and W components
  162. JPH_INLINE Vec4 DotV4(Vec3Arg inV2) const;
  163. /// Dot product
  164. JPH_INLINE float Dot(Vec3Arg inV2) const;
  165. /// Squared length of vector
  166. JPH_INLINE float LengthSq() const;
  167. /// Length of vector
  168. JPH_INLINE float Length() const;
  169. /// Normalize vector
  170. JPH_INLINE Vec3 Normalized() const;
  171. /// Normalize vector or return inZeroValue if the length of the vector is zero
  172. JPH_INLINE Vec3 NormalizedOr(Vec3Arg inZeroValue) const;
  173. /// Store 3 floats to memory
  174. JPH_INLINE void StoreFloat3(Float3 *outV) const;
  175. /// Convert each component from a float to an int
  176. JPH_INLINE UVec4 ToInt() const;
  177. /// Reinterpret Vec3 as a UVec4 (doesn't change the bits)
  178. JPH_INLINE UVec4 ReinterpretAsInt() const;
  179. /// Get the minimum of X, Y and Z
  180. JPH_INLINE float ReduceMin() const;
  181. /// Get the maximum of X, Y and Z
  182. JPH_INLINE float ReduceMax() const;
  183. /// Component wise square root
  184. JPH_INLINE Vec3 Sqrt() const;
  185. /// Get normalized vector that is perpendicular to this vector
  186. JPH_INLINE Vec3 GetNormalizedPerpendicular() const;
  187. /// Get vector that contains the sign of each element (returns 1.0f if positive, -1.0f if negative)
  188. JPH_INLINE Vec3 GetSign() const;
  189. /// To String
  190. friend ostream & operator << (ostream &inStream, Vec3Arg inV)
  191. {
  192. inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2];
  193. return inStream;
  194. }
  195. /// Internal helper function that checks that W is equal to Z, so e.g. dividing by it should not generate div by 0
  196. JPH_INLINE void CheckW() const;
  197. /// Internal helper function that ensures that the Z component is replicated to the W component to prevent divisions by zero
  198. static JPH_INLINE Type sFixW(Type inValue);
  199. union
  200. {
  201. Type mValue;
  202. float mF32[4];
  203. };
  204. };
  205. static_assert(is_trivial<Vec3>(), "Is supposed to be a trivial type!");
  206. JPH_NAMESPACE_END
  207. #include "Vec3.inl"