RayAABox8.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/Vec8.h>
  6. #include <Jolt/Geometry/RayAABox.h>
  7. JPH_NAMESPACE_BEGIN
  8. /// Intersect 8 AABBs with ray, returns minimal distance along ray or FLT_MAX if no hit
  9. /// Note: Can return negative value if ray starts in box
  10. JPH_INLINE Vec8 RayAABox8(Vec3Arg inOrigin, const RayInvDirection &inInvDirection, Vec8Arg inBoundsMinX, Vec8Arg inBoundsMinY, Vec8Arg inBoundsMinZ, Vec8Arg inBoundsMaxX, Vec8Arg inBoundsMaxY, Vec8Arg inBoundsMaxZ)
  11. {
  12. // Constants
  13. Vec8 flt_min = Vec8::sReplicate(-FLT_MAX);
  14. Vec8 flt_max = Vec8::sReplicate(FLT_MAX);
  15. // Origin
  16. Vec8 originx = Vec8::sSplatX(Vec4(inOrigin));
  17. Vec8 originy = Vec8::sSplatY(Vec4(inOrigin));
  18. Vec8 originz = Vec8::sSplatZ(Vec4(inOrigin));
  19. // Parallel
  20. UVec8 parallelx = UVec8::sSplatX(inInvDirection.mIsParallel);
  21. UVec8 parallely = UVec8::sSplatY(inInvDirection.mIsParallel);
  22. UVec8 parallelz = UVec8::sSplatZ(inInvDirection.mIsParallel);
  23. // Inverse direction
  24. Vec8 invdirx = Vec8::sSplatX(Vec4(inInvDirection.mInvDirection));
  25. Vec8 invdiry = Vec8::sSplatY(Vec4(inInvDirection.mInvDirection));
  26. Vec8 invdirz = Vec8::sSplatZ(Vec4(inInvDirection.mInvDirection));
  27. // Test against all three axii simultaneously.
  28. Vec8 t1x = (inBoundsMinX - originx) * invdirx;
  29. Vec8 t1y = (inBoundsMinY - originy) * invdiry;
  30. Vec8 t1z = (inBoundsMinZ - originz) * invdirz;
  31. Vec8 t2x = (inBoundsMaxX - originx) * invdirx;
  32. Vec8 t2y = (inBoundsMaxY - originy) * invdiry;
  33. Vec8 t2z = (inBoundsMaxZ - originz) * invdirz;
  34. // Compute the max of min(t1,t2) and the min of max(t1,t2) ensuring we don't
  35. // use the results from any directions parallel to the slab.
  36. Vec8 t_minx = Vec8::sSelect(Vec8::sMin(t1x, t2x), flt_min, parallelx);
  37. Vec8 t_miny = Vec8::sSelect(Vec8::sMin(t1y, t2y), flt_min, parallely);
  38. Vec8 t_minz = Vec8::sSelect(Vec8::sMin(t1z, t2z), flt_min, parallelz);
  39. Vec8 t_maxx = Vec8::sSelect(Vec8::sMax(t1x, t2x), flt_max, parallelx);
  40. Vec8 t_maxy = Vec8::sSelect(Vec8::sMax(t1y, t2y), flt_max, parallely);
  41. Vec8 t_maxz = Vec8::sSelect(Vec8::sMax(t1z, t2z), flt_max, parallelz);
  42. // t_min.xyz = maximum(t_min.x, t_min.y, t_min.z);
  43. Vec8 t_min = Vec8::sMax(Vec8::sMax(t_minx, t_miny), t_minz);
  44. // t_max.xyz = minimum(t_max.x, t_max.y, t_max.z);
  45. Vec8 t_max = Vec8::sMin(Vec8::sMin(t_maxx, t_maxy), t_maxz);
  46. // if (t_min > t_max) return FLT_MAX;
  47. UVec8 no_intersection = Vec8::sGreater(t_min, t_max);
  48. // if (t_max < 0.0f) return FLT_MAX;
  49. no_intersection = UVec8::sOr(no_intersection, Vec8::sLess(t_max, Vec8::sZero()));
  50. // if bounds are invalid return FLOAT_MAX;
  51. UVec8 bounds_invalid = UVec8::sOr(UVec8::sOr(Vec8::sGreater(inBoundsMinX, inBoundsMaxX), Vec8::sGreater(inBoundsMinY, inBoundsMaxY)), Vec8::sGreater(inBoundsMinZ, inBoundsMaxZ));
  52. no_intersection = UVec8::sOr(no_intersection, bounds_invalid);
  53. // if (inInvDirection.mIsParallel && !(Min <= inOrigin && inOrigin <= Max)) return FLT_MAX; else return t_min;
  54. UVec8 no_parallel_overlapx = UVec8::sAnd(parallelx, UVec8::sOr(Vec8::sLess(originx, inBoundsMinX), Vec8::sGreater(originx, inBoundsMaxX)));
  55. UVec8 no_parallel_overlapy = UVec8::sAnd(parallely, UVec8::sOr(Vec8::sLess(originy, inBoundsMinY), Vec8::sGreater(originy, inBoundsMaxY)));
  56. UVec8 no_parallel_overlapz = UVec8::sAnd(parallelz, UVec8::sOr(Vec8::sLess(originz, inBoundsMinZ), Vec8::sGreater(originz, inBoundsMaxZ)));
  57. no_intersection = UVec8::sOr(no_intersection, UVec8::sOr(UVec8::sOr(no_parallel_overlapx, no_parallel_overlapy), no_parallel_overlapz));
  58. return Vec8::sSelect(t_min, flt_max, no_intersection);
  59. }
  60. JPH_NAMESPACE_END