2
0

DVec3.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/Double3.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// 3 component vector of doubles (stored as 4 vectors).
  8. /// 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
  9. class [[nodiscard]] alignas(JPH_DVECTOR_ALIGNMENT) DVec3
  10. {
  11. public:
  12. JPH_OVERRIDE_NEW_DELETE
  13. // Underlying vector type
  14. #if defined(JPH_USE_AVX)
  15. using Type = __m256d;
  16. using TypeArg = __m256d;
  17. #elif defined(JPH_USE_SSE)
  18. using Type = struct { __m128d mLow, mHigh; };
  19. using TypeArg = const Type &;
  20. #elif defined(JPH_USE_NEON)
  21. using Type = float64x2x2_t;
  22. using TypeArg = const Type &;
  23. #else
  24. using Type = struct { double mData[4]; };
  25. using TypeArg = const Type &;
  26. #endif
  27. // Argument type
  28. using ArgType = DVec3Arg;
  29. /// Constructor
  30. DVec3() = default; ///< Intentionally not initialized for performance reasons
  31. DVec3(const DVec3 &inRHS) = default;
  32. DVec3 & operator = (const DVec3 &inRHS) = default;
  33. JPH_INLINE explicit DVec3(Vec3Arg inRHS);
  34. JPH_INLINE explicit DVec3(Vec4Arg inRHS);
  35. JPH_INLINE DVec3(TypeArg inRHS) : mValue(inRHS) { CheckW(); }
  36. /// Create a vector from 3 components
  37. JPH_INLINE DVec3(double inX, double inY, double inZ);
  38. /// Load 3 doubles from memory
  39. explicit JPH_INLINE DVec3(const Double3 &inV);
  40. /// Vector with all zeros
  41. static JPH_INLINE DVec3 sZero();
  42. /// Vectors with the principal axis
  43. static JPH_INLINE DVec3 sAxisX() { return DVec3(1, 0, 0); }
  44. static JPH_INLINE DVec3 sAxisY() { return DVec3(0, 1, 0); }
  45. static JPH_INLINE DVec3 sAxisZ() { return DVec3(0, 0, 1); }
  46. /// Replicate inV across all components
  47. static JPH_INLINE DVec3 sReplicate(double inV);
  48. /// Vector with all NaN's
  49. static JPH_INLINE DVec3 sNaN();
  50. /// Load 3 doubles from memory (reads 64 bits extra which it doesn't use)
  51. static JPH_INLINE DVec3 sLoadDouble3Unsafe(const Double3 &inV);
  52. /// Store 3 doubles to memory
  53. JPH_INLINE void StoreDouble3(Double3 *outV) const;
  54. /// Convert to float vector 3 rounding to nearest
  55. JPH_INLINE explicit operator Vec3() const;
  56. /// Prepare to convert to float vector 3 rounding towards zero (returns DVec3 that can be converted to a Vec3 to get the rounding)
  57. JPH_INLINE DVec3 PrepareRoundToZero() const;
  58. /// Prepare to convert to float vector 3 rounding towards positive/negative inf (returns DVec3 that can be converted to a Vec3 to get the rounding)
  59. JPH_INLINE DVec3 PrepareRoundToInf() const;
  60. /// Convert to float vector 3 rounding down
  61. JPH_INLINE Vec3 ToVec3RoundDown() const;
  62. /// Convert to float vector 3 rounding up
  63. JPH_INLINE Vec3 ToVec3RoundUp() const;
  64. /// Return the minimum value of each of the components
  65. static JPH_INLINE DVec3 sMin(DVec3Arg inV1, DVec3Arg inV2);
  66. /// Return the maximum of each of the components
  67. static JPH_INLINE DVec3 sMax(DVec3Arg inV1, DVec3Arg inV2);
  68. /// Clamp a vector between min and max (component wise)
  69. static JPH_INLINE DVec3 sClamp(DVec3Arg inV, DVec3Arg inMin, DVec3Arg inMax);
  70. /// Equals (component wise)
  71. static JPH_INLINE DVec3 sEquals(DVec3Arg inV1, DVec3Arg inV2);
  72. /// Less than (component wise)
  73. static JPH_INLINE DVec3 sLess(DVec3Arg inV1, DVec3Arg inV2);
  74. /// Less than or equal (component wise)
  75. static JPH_INLINE DVec3 sLessOrEqual(DVec3Arg inV1, DVec3Arg inV2);
  76. /// Greater than (component wise)
  77. static JPH_INLINE DVec3 sGreater(DVec3Arg inV1, DVec3Arg inV2);
  78. /// Greater than or equal (component wise)
  79. static JPH_INLINE DVec3 sGreaterOrEqual(DVec3Arg inV1, DVec3Arg inV2);
  80. /// Calculates inMul1 * inMul2 + inAdd
  81. static JPH_INLINE DVec3 sFusedMultiplyAdd(DVec3Arg inMul1, DVec3Arg inMul2, DVec3Arg inAdd);
  82. /// Component wise select, returns inV1 when highest bit of inControl = 0 and inV2 when highest bit of inControl = 1
  83. static JPH_INLINE DVec3 sSelect(DVec3Arg inV1, DVec3Arg inV2, DVec3Arg inControl);
  84. /// Logical or (component wise)
  85. static JPH_INLINE DVec3 sOr(DVec3Arg inV1, DVec3Arg inV2);
  86. /// Logical xor (component wise)
  87. static JPH_INLINE DVec3 sXor(DVec3Arg inV1, DVec3Arg inV2);
  88. /// Logical and (component wise)
  89. static JPH_INLINE DVec3 sAnd(DVec3Arg inV1, DVec3Arg inV2);
  90. /// Store if X is true in bit 0, Y in bit 1, Z in bit 2 and W in bit 3 (true is when highest bit of component is set)
  91. JPH_INLINE int GetTrues() const;
  92. /// Test if any of the components are true (true is when highest bit of component is set)
  93. JPH_INLINE bool TestAnyTrue() const;
  94. /// Test if all components are true (true is when highest bit of component is set)
  95. JPH_INLINE bool TestAllTrue() const;
  96. /// Get individual components
  97. #if defined(JPH_USE_AVX)
  98. JPH_INLINE double GetX() const { return _mm_cvtsd_f64(_mm256_castpd256_pd128(mValue)); }
  99. JPH_INLINE double GetY() const { return mF64[1]; }
  100. JPH_INLINE double GetZ() const { return mF64[2]; }
  101. #elif defined(JPH_USE_SSE)
  102. JPH_INLINE double GetX() const { return _mm_cvtsd_f64(mValue.mLow); }
  103. JPH_INLINE double GetY() const { return mF64[1]; }
  104. JPH_INLINE double GetZ() const { return _mm_cvtsd_f64(mValue.mHigh); }
  105. #elif defined(JPH_USE_NEON)
  106. JPH_INLINE double GetX() const { return vgetq_lane_f64(mValue.val[0], 0); }
  107. JPH_INLINE double GetY() const { return vgetq_lane_f64(mValue.val[0], 1); }
  108. JPH_INLINE double GetZ() const { return vgetq_lane_f64(mValue.val[1], 0); }
  109. #else
  110. JPH_INLINE double GetX() const { return mF64[0]; }
  111. JPH_INLINE double GetY() const { return mF64[1]; }
  112. JPH_INLINE double GetZ() const { return mF64[2]; }
  113. #endif
  114. /// Set individual components
  115. JPH_INLINE void SetX(double inX) { mF64[0] = inX; }
  116. JPH_INLINE void SetY(double inY) { mF64[1] = inY; }
  117. JPH_INLINE void SetZ(double inZ) { mF64[2] = mF64[3] = inZ; } // Assure Z and W are the same
  118. /// Get double component by index
  119. JPH_INLINE double operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 3); return mF64[inCoordinate]; }
  120. /// Set double component by index
  121. JPH_INLINE void SetComponent(uint inCoordinate, double inValue) { JPH_ASSERT(inCoordinate < 3); mF64[inCoordinate] = inValue; mValue = sFixW(mValue); } // Assure Z and W are the same
  122. /// Comparison
  123. JPH_INLINE bool operator == (DVec3Arg inV2) const;
  124. JPH_INLINE bool operator != (DVec3Arg inV2) const { return !(*this == inV2); }
  125. /// Test if two vectors are close
  126. JPH_INLINE bool IsClose(DVec3Arg inV2, double inMaxDistSq = 1.0e-24) const;
  127. /// Test if vector is near zero
  128. JPH_INLINE bool IsNearZero(double inMaxDistSq = 1.0e-24) const;
  129. /// Test if vector is normalized
  130. JPH_INLINE bool IsNormalized(double inTolerance = 1.0e-12) const;
  131. /// Test if vector contains NaN elements
  132. JPH_INLINE bool IsNaN() const;
  133. /// Multiply two double vectors (component wise)
  134. JPH_INLINE DVec3 operator * (DVec3Arg inV2) const;
  135. /// Multiply vector with double
  136. JPH_INLINE DVec3 operator * (double inV2) const;
  137. /// Multiply vector with double
  138. friend JPH_INLINE DVec3 operator * (double inV1, DVec3Arg inV2);
  139. /// Divide vector by double
  140. JPH_INLINE DVec3 operator / (double inV2) const;
  141. /// Multiply vector with double
  142. JPH_INLINE DVec3 & operator *= (double inV2);
  143. /// Multiply vector with vector
  144. JPH_INLINE DVec3 & operator *= (DVec3Arg inV2);
  145. /// Divide vector by double
  146. JPH_INLINE DVec3 & operator /= (double inV2);
  147. /// Add two vectors (component wise)
  148. JPH_INLINE DVec3 operator + (Vec3Arg inV2) const;
  149. /// Add two double vectors (component wise)
  150. JPH_INLINE DVec3 operator + (DVec3Arg inV2) const;
  151. /// Add two vectors (component wise)
  152. JPH_INLINE DVec3 & operator += (Vec3Arg inV2);
  153. /// Add two double vectors (component wise)
  154. JPH_INLINE DVec3 & operator += (DVec3Arg inV2);
  155. /// Negate
  156. JPH_INLINE DVec3 operator - () const;
  157. /// Subtract two vectors (component wise)
  158. JPH_INLINE DVec3 operator - (Vec3Arg inV2) const;
  159. /// Subtract two double vectors (component wise)
  160. JPH_INLINE DVec3 operator - (DVec3Arg inV2) const;
  161. /// Add two vectors (component wise)
  162. JPH_INLINE DVec3 & operator -= (Vec3Arg inV2);
  163. /// Add two double vectors (component wise)
  164. JPH_INLINE DVec3 & operator -= (DVec3Arg inV2);
  165. /// Divide (component wise)
  166. JPH_INLINE DVec3 operator / (DVec3Arg inV2) const;
  167. /// Return the absolute value of each of the components
  168. JPH_INLINE DVec3 Abs() const;
  169. /// Reciprocal vector (1 / value) for each of the components
  170. JPH_INLINE DVec3 Reciprocal() const;
  171. /// Cross product
  172. JPH_INLINE DVec3 Cross(DVec3Arg inV2) const;
  173. /// Dot product
  174. JPH_INLINE double Dot(DVec3Arg inV2) const;
  175. /// Squared length of vector
  176. JPH_INLINE double LengthSq() const;
  177. /// Length of vector
  178. JPH_INLINE double Length() const;
  179. /// Normalize vector
  180. JPH_INLINE DVec3 Normalized() const;
  181. /// Component wise square root
  182. JPH_INLINE DVec3 Sqrt() const;
  183. /// Get vector that contains the sign of each element (returns 1 if positive, -1 if negative)
  184. JPH_INLINE DVec3 GetSign() const;
  185. /// To String
  186. friend ostream & operator << (ostream &inStream, DVec3Arg inV)
  187. {
  188. inStream << inV.mF64[0] << ", " << inV.mF64[1] << ", " << inV.mF64[2];
  189. return inStream;
  190. }
  191. /// Internal helper function that checks that W is equal to Z, so e.g. dividing by it should not generate div by 0
  192. JPH_INLINE void CheckW() const;
  193. /// Internal helper function that ensures that the Z component is replicated to the W component to prevent divisions by zero
  194. static JPH_INLINE Type sFixW(TypeArg inValue);
  195. /// Representations of true and false for boolean operations
  196. inline static const double cTrue = BitCast<double>(~uint64(0));
  197. inline static const double cFalse = 0.0;
  198. union
  199. {
  200. Type mValue;
  201. double mF64[4];
  202. };
  203. };
  204. static_assert(is_trivial<DVec3>(), "Is supposed to be a trivial type!");
  205. JPH_NAMESPACE_END
  206. #include "DVec3.inl"