RayAABox8.h 3.5 KB

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