RayAABoxTests.cpp 2.7 KB

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