DVec3.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Math/Swizzle.h>
  5. #ifdef JPH_USE_AVX2 // DVec3 currently uses AVX2 intrinsics but the class is currently unused so we can leave it out (it will be used in the future to support objects at a large distance from the origin)
  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]] DVec3
  10. {
  11. public:
  12. JPH_OVERRIDE_NEW_DELETE
  13. #ifdef JPH_FLOATING_POINT_EXCEPTIONS_ENABLED
  14. /// Internal helper function that checks that W is equal to Z, so e.g. dividing by it should not generate div by 0
  15. JPH_INLINE void CheckW() const { JPH_ASSERT(reinterpret_cast<const uint64 *>(mD32)[2] == reinterpret_cast<const uint64 *>(mD32)[3]); } // Avoid asserts when both components are NaN
  16. /// Internal helper function that ensures that the Z component is replicated to the W component to prevent divisions by zero
  17. static JPH_INLINE __m256d sFixW(__m256d inValue) { return _mm256_shuffle_pd(inValue, inValue, 2); }
  18. #else
  19. /// Stub function
  20. JPH_INLINE void CheckW() const { }
  21. /// Stub function
  22. static JPH_INLINE __m256d sFixW(__m256d inValue) { return inValue; }
  23. #endif // JPH_FLOATING_POINT_EXCEPTIONS_ENABLED
  24. /// Constructor
  25. DVec3() = default; ///< Intentionally not initialized for performance reasons
  26. DVec3(const DVec3 &inRHS) = default;
  27. JPH_INLINE explicit DVec3(Vec3Arg inRHS);
  28. JPH_INLINE DVec3(__m256d inRHS) : mValue(inRHS) { CheckW(); }
  29. /// Create a vector from 3 components
  30. JPH_INLINE DVec3(double inX, double inY, double inZ);
  31. /// Vector with all zeros
  32. static JPH_INLINE DVec3 sZero();
  33. /// Vectors with the principal axis
  34. static JPH_INLINE DVec3 sAxisX() { return DVec3(1, 0, 0); }
  35. static JPH_INLINE DVec3 sAxisY() { return DVec3(0, 1, 0); }
  36. static JPH_INLINE DVec3 sAxisZ() { return DVec3(0, 0, 1); }
  37. /// Replicate inV across all components
  38. static JPH_INLINE DVec3 sReplicate(double inV);
  39. /// Load 3 doubles from memory (reads 64 bits extra which it doesn't use)
  40. static JPH_INLINE DVec3 sLoadDouble3Unsafe(const double *inV);
  41. /// Convert to float vector 3
  42. JPH_INLINE Vec3 ToVec3() const;
  43. /// Return the minimum value of each of the components
  44. static JPH_INLINE DVec3 sMin(DVec3Arg inV1, DVec3Arg inV2);
  45. /// Return the maximum of each of the components
  46. static JPH_INLINE DVec3 sMax(DVec3Arg inV1, DVec3Arg inV2);
  47. /// Clamp a vector between min and max (component wise)
  48. static JPH_INLINE DVec3 sClamp(DVec3Arg inV, DVec3Arg inMin, DVec3Arg inMax);
  49. /// Equals (component wise)
  50. static JPH_INLINE DVec3 sEquals(DVec3Arg inV1, DVec3Arg inV2);
  51. /// Less than (component wise)
  52. static JPH_INLINE DVec3 sLess(DVec3Arg inV1, DVec3Arg inV2);
  53. /// Less than or equal (component wise)
  54. static JPH_INLINE DVec3 sLessOrEqual(DVec3Arg inV1, DVec3Arg inV2);
  55. /// Greater than (component wise)
  56. static JPH_INLINE DVec3 sGreater(DVec3Arg inV1, DVec3Arg inV2);
  57. /// Greater than or equal (component wise)
  58. static JPH_INLINE DVec3 sGreaterOrEqual(DVec3Arg inV1, DVec3Arg inV2);
  59. /// Calculates inMul1 * inMul2 + inAdd
  60. static JPH_INLINE DVec3 sFusedMultiplyAdd(DVec3Arg inMul1, DVec3Arg inMul2, DVec3Arg 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 DVec3 sSelect(DVec3Arg inV1, DVec3Arg inV2, DVec3Arg inControl);
  63. /// Logical or (component wise)
  64. static JPH_INLINE DVec3 sOr(DVec3Arg inV1, DVec3Arg inV2);
  65. /// Logical xor (component wise)
  66. static JPH_INLINE DVec3 sXor(DVec3Arg inV1, DVec3Arg inV2);
  67. /// Logical and (component wise)
  68. static JPH_INLINE DVec3 sAnd(DVec3Arg inV1, DVec3Arg inV2);
  69. /// 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)
  70. JPH_INLINE int GetTrues() const;
  71. /// Test if any of the components are true (true is when highest bit of component is set)
  72. JPH_INLINE bool TestAnyTrue() const;
  73. /// Test if all components are true (true is when highest bit of component is set)
  74. JPH_INLINE bool TestAllTrue() const;
  75. /// Get individual components
  76. JPH_INLINE double GetX() const { return _mm_cvtsd_f64(_mm256_castpd256_pd128(mValue)); }
  77. JPH_INLINE double GetY() const { return mD32[1]; }
  78. JPH_INLINE double GetZ() const { return mD32[2]; }
  79. /// Set individual components
  80. JPH_INLINE void SetX(double inX) { mD32[0] = inX; }
  81. JPH_INLINE void SetY(double inY) { mD32[1] = inY; }
  82. JPH_INLINE void SetZ(double inZ) { mD32[2] = mD32[3] = inZ; } // Assure Z and W are the same
  83. /// Get double component by index
  84. JPH_INLINE double operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 3); return mD32[inCoordinate]; }
  85. /// Set double component by index
  86. JPH_INLINE void SetComponent(uint inCoordinate, double inValue) { JPH_ASSERT(inCoordinate < 3); mD32[inCoordinate] = inValue; mValue = sFixW(mValue); } // Assure Z and W are the same
  87. /// Comparison
  88. JPH_INLINE bool operator == (DVec3Arg inV2) const;
  89. JPH_INLINE bool operator != (DVec3Arg inV2) const { return !(*this == inV2); }
  90. /// Test if two vectors are close
  91. JPH_INLINE bool IsClose(DVec3Arg inV2, double inMaxDistSq = 1.0e-24) const;
  92. /// Test if vector is near zero
  93. JPH_INLINE bool IsNearZero(double inMaxDistSq = 1.0e-24) const;
  94. /// Test if vector is normalized
  95. JPH_INLINE bool IsNormalized(double inTolerance = 1.0e-12) const;
  96. /// Multiply two double vectors (component wise)
  97. JPH_INLINE DVec3 operator * (DVec3Arg inV2) const;
  98. /// Multiply vector with double
  99. JPH_INLINE DVec3 operator * (double inV2) const;
  100. /// Multiply vector with double
  101. friend JPH_INLINE DVec3 operator * (double inV1, DVec3Arg inV2);
  102. /// Divide vector by double
  103. JPH_INLINE DVec3 operator / (double inV2) const;
  104. /// Multiply vector with double
  105. JPH_INLINE DVec3 & operator *= (double inV2);
  106. /// Multiply vector with vector
  107. JPH_INLINE DVec3 & operator *= (DVec3Arg inV2);
  108. /// Divide vector by double
  109. JPH_INLINE DVec3 & operator /= (double inV2);
  110. /// Add two double vectors (component wise)
  111. JPH_INLINE DVec3 operator + (DVec3Arg inV2) const;
  112. /// Add two double vectors (component wise)
  113. JPH_INLINE DVec3 & operator += (DVec3Arg inV2);
  114. /// Negate
  115. JPH_INLINE DVec3 operator - () const;
  116. /// Subtract two double vectors (component wise)
  117. JPH_INLINE DVec3 operator - (DVec3Arg inV2) const;
  118. /// Add two double vectors (component wise)
  119. JPH_INLINE DVec3 & operator -= (DVec3Arg inV2);
  120. /// Divide (component wise)
  121. JPH_INLINE DVec3 operator / (DVec3Arg inV2) const;
  122. /// Return the absolute value of each of the components
  123. JPH_INLINE DVec3 Abs() const;
  124. /// Reciprocal vector (1 / value) for each of the components
  125. JPH_INLINE DVec3 Reciprocal() const;
  126. /// Cross product
  127. JPH_INLINE DVec3 Cross(DVec3Arg inV2) const;
  128. /// Dot product
  129. JPH_INLINE double Dot(DVec3Arg inV2) const;
  130. /// Squared length of vector
  131. JPH_INLINE double LengthSq() const;
  132. /// Length of vector
  133. JPH_INLINE double Length() const;
  134. /// Normalize vector
  135. JPH_INLINE DVec3 Normalized() const;
  136. /// Component wise square root
  137. JPH_INLINE DVec3 Sqrt() const;
  138. /// Get vector that contains the sign of each element (returns 1 if positive, -1 if negative)
  139. JPH_INLINE DVec3 GetSign() const;
  140. /// To String
  141. friend ostream & operator << (ostream &inStream, DVec3Arg inV)
  142. {
  143. inStream << inV.mD32[0] << ", " << inV.mD32[1] << ", " << inV.mD32[2];
  144. return inStream;
  145. }
  146. private:
  147. union
  148. {
  149. __m256d mValue;
  150. double mD32[4];
  151. };
  152. };
  153. static_assert(is_trivial<DVec3>(), "Is supposed to be a trivial type!");
  154. JPH_NAMESPACE_END
  155. #include "DVec3.inl"
  156. #endif // JPH_USE_AVX2