2
0

RayAABoxTests.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include "UnitTestFramework.h"
  4. #include <Jolt/Geometry/AABox.h>
  5. #include <Jolt/Geometry/RayAABox.h>
  6. TEST_SUITE("RayAABoxTests")
  7. {
  8. TEST_CASE("TestRayAABox")
  9. {
  10. AABox box(Vec3::sReplicate(-1.0f), Vec3::sReplicate(1.0f));
  11. for (int axis = 0; axis < 3; ++axis)
  12. {
  13. {
  14. // Ray starting in the center of the box, pointing high
  15. Vec3 origin = Vec3::sZero();
  16. Vec3 direction = Vec3::sZero();
  17. direction.SetComponent(axis, 1.0f);
  18. float fraction = RayAABox(origin, RayInvDirection(direction), box.mMin, box.mMax);
  19. CHECK_APPROX_EQUAL(-1.0f, fraction, 1.0e-6f);
  20. }
  21. {
  22. // Ray starting in the center of the box, pointing low
  23. Vec3 origin = Vec3::sZero();
  24. Vec3 direction = Vec3::sZero();
  25. direction.SetComponent(axis, -1.0f);
  26. float fraction = RayAABox(origin, RayInvDirection(direction), box.mMin, box.mMax);
  27. CHECK_APPROX_EQUAL(-1.0f, fraction, 1.0e-6f);
  28. }
  29. {
  30. // Ray starting high, pointing to low
  31. Vec3 origin = Vec3::sZero();
  32. origin.SetComponent(axis, 1.1f);
  33. Vec3 direction = Vec3::sZero();
  34. direction.SetComponent(axis, -1.0f);
  35. float fraction = RayAABox(origin, RayInvDirection(direction), box.mMin, box.mMax);
  36. CHECK_APPROX_EQUAL(0.1f, fraction, 1.0e-6f);
  37. }
  38. {
  39. // Ray starting high, pointing to high
  40. Vec3 origin = Vec3::sZero();
  41. origin.SetComponent(axis, 1.1f);
  42. Vec3 direction = Vec3::sZero();
  43. direction.SetComponent(axis, 1.0f);
  44. float fraction = RayAABox(origin, RayInvDirection(direction), box.mMin, box.mMax);
  45. CHECK(fraction == FLT_MAX);
  46. }
  47. {
  48. // Ray starting low, pointing to high
  49. Vec3 origin = Vec3::sZero();
  50. origin.SetComponent(axis, -1.1f);
  51. Vec3 direction = Vec3::sZero();
  52. direction.SetComponent(axis, 1.0f);
  53. float fraction = RayAABox(origin, RayInvDirection(direction), box.mMin, box.mMax);
  54. CHECK_APPROX_EQUAL(0.1f, fraction, 1.0e-6f);
  55. }
  56. {
  57. // Ray starting low, pointing to low
  58. Vec3 origin = Vec3::sZero();
  59. origin.SetComponent(axis, -1.1f);
  60. Vec3 direction = Vec3::sZero();
  61. direction.SetComponent(axis, -1.0f);
  62. float fraction = RayAABox(origin, RayInvDirection(direction), box.mMin, box.mMax);
  63. CHECK(fraction == FLT_MAX);
  64. }
  65. }
  66. {
  67. // Test ray that hits top plane under an angle
  68. Vec3 expected_hit = Vec3(0, 1, 0);
  69. float expected_fraction = 0.123f;
  70. Vec3 direction = Vec3(4, -4, 0);
  71. Vec3 origin = expected_hit - expected_fraction * direction;
  72. float fraction = RayAABox(origin, RayInvDirection(direction), box.mMin, box.mMax);
  73. CHECK_APPROX_EQUAL(expected_fraction, fraction, 1.0e-6f);
  74. }
  75. }
  76. }