RayAABox.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_NAMESPACE_BEGIN
  5. /// Helper structure holding the reciprocal of a ray for Ray vs AABox testing
  6. class RayInvDirection
  7. {
  8. public:
  9. /// Constructors
  10. inline RayInvDirection() = default;
  11. inline explicit RayInvDirection(Vec3Arg inDirection) { Set(inDirection); }
  12. /// Set reciprocal from ray direction
  13. inline void Set(Vec3Arg inDirection)
  14. {
  15. // if (abs(inDirection) <= Epsilon) the ray is nearly parallel to the slab.
  16. mIsParallel = Vec3::sLessOrEqual(inDirection.Abs(), Vec3::sReplicate(1.0e-20f));
  17. // Calculate 1 / direction while avoiding division by zero
  18. mInvDirection = Vec3::sSelect(inDirection, Vec3::sReplicate(1.0f), mIsParallel).Reciprocal();
  19. }
  20. Vec3 mInvDirection; ///< 1 / ray direction
  21. UVec4 mIsParallel; ///< for each component if it is parallel to the coordinate axis
  22. };
  23. /// Intersect AABB with ray, returns minimal distance along ray or FLT_MAX if no hit
  24. /// Note: Can return negative value if ray starts in box
  25. JPH_INLINE float RayAABox(Vec3Arg inOrigin, const RayInvDirection &inInvDirection, Vec3Arg inBoundsMin, Vec3Arg inBoundsMax)
  26. {
  27. // Constants
  28. Vec3 flt_min = Vec3::sReplicate(-FLT_MAX);
  29. Vec3 flt_max = Vec3::sReplicate(FLT_MAX);
  30. // Test against all three axii simultaneously.
  31. Vec3 t1 = (inBoundsMin - inOrigin) * inInvDirection.mInvDirection;
  32. Vec3 t2 = (inBoundsMax - inOrigin) * inInvDirection.mInvDirection;
  33. // Compute the max of min(t1,t2) and the min of max(t1,t2) ensuring we don't
  34. // use the results from any directions parallel to the slab.
  35. Vec3 t_min = Vec3::sSelect(Vec3::sMin(t1, t2), flt_min, inInvDirection.mIsParallel);
  36. Vec3 t_max = Vec3::sSelect(Vec3::sMax(t1, t2), flt_max, inInvDirection.mIsParallel);
  37. // t_min.xyz = maximum(t_min.x, t_min.y, t_min.z);
  38. t_min = Vec3::sMax(t_min, t_min.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>());
  39. t_min = Vec3::sMax(t_min, t_min.Swizzle<SWIZZLE_Z, SWIZZLE_X, SWIZZLE_Y>());
  40. // t_max.xyz = minimum(t_max.x, t_max.y, t_max.z);
  41. t_max = Vec3::sMin(t_max, t_max.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>());
  42. t_max = Vec3::sMin(t_max, t_max.Swizzle<SWIZZLE_Z, SWIZZLE_X, SWIZZLE_Y>());
  43. // if (t_min > t_max) return FLT_MAX;
  44. UVec4 no_intersection = Vec3::sGreater(t_min, t_max);
  45. // if (t_max < 0.0f) return FLT_MAX;
  46. no_intersection = UVec4::sOr(no_intersection, Vec3::sLess(t_max, Vec3::sZero()));
  47. // if (inInvDirection.mIsParallel && !(Min <= inOrigin && inOrigin <= Max)) return FLT_MAX; else return t_min;
  48. UVec4 no_parallel_overlap = UVec4::sOr(Vec3::sLess(inOrigin, inBoundsMin), Vec3::sGreater(inOrigin, inBoundsMax));
  49. no_intersection = UVec4::sOr(no_intersection, UVec4::sAnd(inInvDirection.mIsParallel, no_parallel_overlap));
  50. no_intersection = UVec4::sOr(no_intersection, no_intersection.SplatY());
  51. no_intersection = UVec4::sOr(no_intersection, no_intersection.SplatZ());
  52. return Vec3::sSelect(t_min, flt_max, no_intersection).GetX();
  53. }
  54. /// Intersect 4 AABBs with ray, returns minimal distance along ray or FLT_MAX if no hit
  55. /// Note: Can return negative value if ray starts in box
  56. JPH_INLINE Vec4 RayAABox4(Vec3Arg inOrigin, const RayInvDirection &inInvDirection, Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ)
  57. {
  58. // Constants
  59. Vec4 flt_min = Vec4::sReplicate(-FLT_MAX);
  60. Vec4 flt_max = Vec4::sReplicate(FLT_MAX);
  61. // Origin
  62. Vec4 originx = inOrigin.SplatX();
  63. Vec4 originy = inOrigin.SplatY();
  64. Vec4 originz = inOrigin.SplatZ();
  65. // Parallel
  66. UVec4 parallelx = inInvDirection.mIsParallel.SplatX();
  67. UVec4 parallely = inInvDirection.mIsParallel.SplatY();
  68. UVec4 parallelz = inInvDirection.mIsParallel.SplatZ();
  69. // Inverse direction
  70. Vec4 invdirx = inInvDirection.mInvDirection.SplatX();
  71. Vec4 invdiry = inInvDirection.mInvDirection.SplatY();
  72. Vec4 invdirz = inInvDirection.mInvDirection.SplatZ();
  73. // Test against all three axii simultaneously.
  74. Vec4 t1x = (inBoundsMinX - originx) * invdirx;
  75. Vec4 t1y = (inBoundsMinY - originy) * invdiry;
  76. Vec4 t1z = (inBoundsMinZ - originz) * invdirz;
  77. Vec4 t2x = (inBoundsMaxX - originx) * invdirx;
  78. Vec4 t2y = (inBoundsMaxY - originy) * invdiry;
  79. Vec4 t2z = (inBoundsMaxZ - originz) * invdirz;
  80. // Compute the max of min(t1,t2) and the min of max(t1,t2) ensuring we don't
  81. // use the results from any directions parallel to the slab.
  82. Vec4 t_minx = Vec4::sSelect(Vec4::sMin(t1x, t2x), flt_min, parallelx);
  83. Vec4 t_miny = Vec4::sSelect(Vec4::sMin(t1y, t2y), flt_min, parallely);
  84. Vec4 t_minz = Vec4::sSelect(Vec4::sMin(t1z, t2z), flt_min, parallelz);
  85. Vec4 t_maxx = Vec4::sSelect(Vec4::sMax(t1x, t2x), flt_max, parallelx);
  86. Vec4 t_maxy = Vec4::sSelect(Vec4::sMax(t1y, t2y), flt_max, parallely);
  87. Vec4 t_maxz = Vec4::sSelect(Vec4::sMax(t1z, t2z), flt_max, parallelz);
  88. // t_min.xyz = maximum(t_min.x, t_min.y, t_min.z);
  89. Vec4 t_min = Vec4::sMax(Vec4::sMax(t_minx, t_miny), t_minz);
  90. // t_max.xyz = minimum(t_max.x, t_max.y, t_max.z);
  91. Vec4 t_max = Vec4::sMin(Vec4::sMin(t_maxx, t_maxy), t_maxz);
  92. // if (t_min > t_max) return FLT_MAX;
  93. UVec4 no_intersection = Vec4::sGreater(t_min, t_max);
  94. // if (t_max < 0.0f) return FLT_MAX;
  95. no_intersection = UVec4::sOr(no_intersection, Vec4::sLess(t_max, Vec4::sZero()));
  96. // if bounds are invalid return FLOAT_MAX;
  97. UVec4 bounds_invalid = UVec4::sOr(UVec4::sOr(Vec4::sGreater(inBoundsMinX, inBoundsMaxX), Vec4::sGreater(inBoundsMinY, inBoundsMaxY)), Vec4::sGreater(inBoundsMinZ, inBoundsMaxZ));
  98. no_intersection = UVec4::sOr(no_intersection, bounds_invalid);
  99. // if (inInvDirection.mIsParallel && !(Min <= inOrigin && inOrigin <= Max)) return FLT_MAX; else return t_min;
  100. UVec4 no_parallel_overlapx = UVec4::sAnd(parallelx, UVec4::sOr(Vec4::sLess(originx, inBoundsMinX), Vec4::sGreater(originx, inBoundsMaxX)));
  101. UVec4 no_parallel_overlapy = UVec4::sAnd(parallely, UVec4::sOr(Vec4::sLess(originy, inBoundsMinY), Vec4::sGreater(originy, inBoundsMaxY)));
  102. UVec4 no_parallel_overlapz = UVec4::sAnd(parallelz, UVec4::sOr(Vec4::sLess(originz, inBoundsMinZ), Vec4::sGreater(originz, inBoundsMaxZ)));
  103. no_intersection = UVec4::sOr(no_intersection, UVec4::sOr(UVec4::sOr(no_parallel_overlapx, no_parallel_overlapy), no_parallel_overlapz));
  104. return Vec4::sSelect(t_min, flt_max, no_intersection);
  105. }
  106. /// Intersect AABB with ray, returns minimal and maximal distance along ray or FLT_MAX, -FLT_MAX if no hit
  107. /// Note: Can return negative value for outMin if ray starts in box
  108. JPH_INLINE void RayAABox(Vec3Arg inOrigin, const RayInvDirection &inInvDirection, Vec3Arg inBoundsMin, Vec3Arg inBoundsMax, float &outMin, float &outMax)
  109. {
  110. // Constants
  111. Vec3 flt_min = Vec3::sReplicate(-FLT_MAX);
  112. Vec3 flt_max = Vec3::sReplicate(FLT_MAX);
  113. // Test against all three axii simultaneously.
  114. Vec3 t1 = (inBoundsMin - inOrigin) * inInvDirection.mInvDirection;
  115. Vec3 t2 = (inBoundsMax - inOrigin) * inInvDirection.mInvDirection;
  116. // Compute the max of min(t1,t2) and the min of max(t1,t2) ensuring we don't
  117. // use the results from any directions parallel to the slab.
  118. Vec3 t_min = Vec3::sSelect(Vec3::sMin(t1, t2), flt_min, inInvDirection.mIsParallel);
  119. Vec3 t_max = Vec3::sSelect(Vec3::sMax(t1, t2), flt_max, inInvDirection.mIsParallel);
  120. // t_min.xyz = maximum(t_min.x, t_min.y, t_min.z);
  121. t_min = Vec3::sMax(t_min, t_min.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>());
  122. t_min = Vec3::sMax(t_min, t_min.Swizzle<SWIZZLE_Z, SWIZZLE_X, SWIZZLE_Y>());
  123. // t_max.xyz = minimum(t_max.x, t_max.y, t_max.z);
  124. t_max = Vec3::sMin(t_max, t_max.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>());
  125. t_max = Vec3::sMin(t_max, t_max.Swizzle<SWIZZLE_Z, SWIZZLE_X, SWIZZLE_Y>());
  126. // if (t_min > t_max) return FLT_MAX;
  127. UVec4 no_intersection = Vec3::sGreater(t_min, t_max);
  128. // if (t_max < 0.0f) return FLT_MAX;
  129. no_intersection = UVec4::sOr(no_intersection, Vec3::sLess(t_max, Vec3::sZero()));
  130. // if (inInvDirection.mIsParallel && !(Min <= inOrigin && inOrigin <= Max)) return FLT_MAX; else return t_min;
  131. UVec4 no_parallel_overlap = UVec4::sOr(Vec3::sLess(inOrigin, inBoundsMin), Vec3::sGreater(inOrigin, inBoundsMax));
  132. no_intersection = UVec4::sOr(no_intersection, UVec4::sAnd(inInvDirection.mIsParallel, no_parallel_overlap));
  133. no_intersection = UVec4::sOr(no_intersection, no_intersection.SplatY());
  134. no_intersection = UVec4::sOr(no_intersection, no_intersection.SplatZ());
  135. outMin = Vec3::sSelect(t_min, flt_max, no_intersection).GetX();
  136. outMax = Vec3::sSelect(t_max, flt_min, no_intersection).GetX();
  137. }
  138. /// Intersect AABB with ray, returns true if there is a hit closer than inClosest
  139. JPH_INLINE bool RayAABoxHits(Vec3Arg inOrigin, const RayInvDirection &inInvDirection, Vec3Arg inBoundsMin, Vec3Arg inBoundsMax, float inClosest)
  140. {
  141. // Constants
  142. Vec3 flt_min = Vec3::sReplicate(-FLT_MAX);
  143. Vec3 flt_max = Vec3::sReplicate(FLT_MAX);
  144. // Test against all three axii simultaneously.
  145. Vec3 t1 = (inBoundsMin - inOrigin) * inInvDirection.mInvDirection;
  146. Vec3 t2 = (inBoundsMax - inOrigin) * inInvDirection.mInvDirection;
  147. // Compute the max of min(t1,t2) and the min of max(t1,t2) ensuring we don't
  148. // use the results from any directions parallel to the slab.
  149. Vec3 t_min = Vec3::sSelect(Vec3::sMin(t1, t2), flt_min, inInvDirection.mIsParallel);
  150. Vec3 t_max = Vec3::sSelect(Vec3::sMax(t1, t2), flt_max, inInvDirection.mIsParallel);
  151. // t_min.xyz = maximum(t_min.x, t_min.y, t_min.z);
  152. t_min = Vec3::sMax(t_min, t_min.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>());
  153. t_min = Vec3::sMax(t_min, t_min.Swizzle<SWIZZLE_Z, SWIZZLE_X, SWIZZLE_Y>());
  154. // t_max.xyz = minimum(t_max.x, t_max.y, t_max.z);
  155. t_max = Vec3::sMin(t_max, t_max.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>());
  156. t_max = Vec3::sMin(t_max, t_max.Swizzle<SWIZZLE_Z, SWIZZLE_X, SWIZZLE_Y>());
  157. // if (t_min > t_max) return false;
  158. UVec4 no_intersection = Vec3::sGreater(t_min, t_max);
  159. // if (t_max < 0.0f) return false;
  160. no_intersection = UVec4::sOr(no_intersection, Vec3::sLess(t_max, Vec3::sZero()));
  161. // if (t_min > inClosest) return false;
  162. no_intersection = UVec4::sOr(no_intersection, Vec3::sGreater(t_min, Vec3::sReplicate(inClosest)));
  163. // if (inInvDirection.mIsParallel && !(Min <= inOrigin && inOrigin <= Max)) return false; else return true;
  164. UVec4 no_parallel_overlap = UVec4::sOr(Vec3::sLess(inOrigin, inBoundsMin), Vec3::sGreater(inOrigin, inBoundsMax));
  165. no_intersection = UVec4::sOr(no_intersection, UVec4::sAnd(inInvDirection.mIsParallel, no_parallel_overlap));
  166. return !no_intersection.TestAnyXYZTrue();
  167. }
  168. /// Intersect AABB with ray without hit fraction, based on separating axis test
  169. /// @see http://www.codercorner.com/RayAABB.cpp
  170. JPH_INLINE bool RayAABoxHits(Vec3Arg inOrigin, Vec3Arg inDirection, Vec3Arg inBoundsMin, Vec3Arg inBoundsMax)
  171. {
  172. Vec3 extents = inBoundsMax - inBoundsMin;
  173. Vec3 diff = 2.0f * inOrigin - inBoundsMin - inBoundsMax;
  174. Vec3 abs_diff = diff.Abs();
  175. UVec4 no_intersection = UVec4::sAnd(Vec3::sGreater(abs_diff, extents), Vec3::sGreaterOrEqual(diff * inDirection, Vec3::sZero()));
  176. Vec3 abs_dir = inDirection.Abs();
  177. Vec3 abs_dir_yzz = abs_dir.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z>();
  178. Vec3 abs_dir_xyx = abs_dir.Swizzle<SWIZZLE_X, SWIZZLE_Y, SWIZZLE_X>();
  179. Vec3 extents_yzz = extents.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z>();
  180. Vec3 extents_xyx = extents.Swizzle<SWIZZLE_X, SWIZZLE_Y, SWIZZLE_X>();
  181. Vec3 diff_yzx = diff.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>();
  182. Vec3 dir_yzx = inDirection.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>();
  183. no_intersection = UVec4::sOr(no_intersection, Vec3::sGreater((inDirection * diff_yzx - dir_yzx * diff).Abs(), extents_xyx * abs_dir_yzz + extents_yzz * abs_dir_xyx));
  184. return !no_intersection.TestAnyXYZTrue();
  185. }
  186. JPH_NAMESPACE_END