DVec3.h 9.8 KB

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