Vec3.h 9.5 KB

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