Vec3.h 9.7 KB

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