DVec3.h 7.8 KB

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