X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=tst%2Fgeometry%2FIntersectionTest.cpp;h=521647e5115b9a3e23aeb168fa1fb81eb2dfb9ea;hb=ab0ba4313c473378b4516e3702d524dc1d1fc5d4;hp=41fb81a8764b6c5b955f8bb9c13d8ef5b4253afa;hpb=6513b55584093a86ce1e369e054263dd75c295c8;p=blank.git diff --git a/tst/geometry/IntersectionTest.cpp b/tst/geometry/IntersectionTest.cpp index 41fb81a..521647e 100644 --- a/tst/geometry/IntersectionTest.cpp +++ b/tst/geometry/IntersectionTest.cpp @@ -20,6 +20,53 @@ void IntersectionTest::tearDown() { } +void IntersectionTest::testSimpleRayBoxIntersection() { + Ray ray{ { 0, 0, 0 }, { 1, 0, 0 } }; // at origin, pointing right + ray.Update(); + AABB box{ { -1, -1, -1 }, { 1, 1, 1 } }; // 2x2x2 cube centered around origin + + const float delta = std::numeric_limits::epsilon(); + + float distance = 0; + + CPPUNIT_ASSERT_MESSAGE( + "ray at origin not intersecting box at origin", + Intersection(ray, box, distance) + ); + + // move ray outside the box, but have it still point at it + // should be 4 units to the left now + ray.orig.x = -5; + CPPUNIT_ASSERT_MESSAGE( + "ray pointing at box doesn't intersect", + Intersection(ray, box, distance) + ); + CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( + "intersection distance way off", + 4.0f, distance, delta + ); + + // move ray to the other side, so it's pointing away now + ray.orig.x = 5; + CPPUNIT_ASSERT_MESSAGE( + "ray pointing away from box still intersects", + !Intersection(ray, box, distance) + ); + + // 45 deg down from 4 units away, so should be about 4 * sqrt(2) + ray.orig = { -5.0f, 4.5f, 0.0f }; + ray.dir = { 0.70710678118654752440f, -0.70710678118654752440f, 0.0f }; + ray.Update(); + CPPUNIT_ASSERT_MESSAGE( + "ray pointing at box doesn't intersect", + Intersection(ray, box, distance) + ); + CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( + "intersection distance way off", + 5.65685424949238019520f, distance, delta + ); +} + void IntersectionTest::testRayBoxIntersection() { Ray ray{ { 0, 0, 0 }, { 1, 0, 0 } }; // at origin, pointing right AABB box{ { -1, -1, -1 }, { 1, 1, 1 } }; // 2x2x2 cube centered around origin @@ -34,10 +81,6 @@ void IntersectionTest::testRayBoxIntersection() { "ray at origin not intersecting box at origin", Intersection(ray, box, M, &distance) ); - CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( - "intersection distance way off", - 0.0f, distance, delta - ); // normal undefined, so can't test // move ray outside the box, but have it still point at it @@ -62,6 +105,22 @@ void IntersectionTest::testRayBoxIntersection() { "ray pointing away from box still intersects", !Intersection(ray, box, M) ); + + // 45 deg down from 4 units away, so should be about 4 * sqrt(2) + ray.orig = { -5.0f, 4.5f, 0.0f }; + ray.dir = { 0.70710678118654752440f, -0.70710678118654752440f, 0.0f }; + CPPUNIT_ASSERT_MESSAGE( + "ray pointing at box doesn't intersect", + Intersection(ray, box, M, &distance, &normal) + ); + CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( + "intersection distance way off", + 5.65685424949238019520f, distance, delta + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "wrong surface normal at intersection point", + glm::vec3(-1, 0, 0), normal + ); } void IntersectionTest::testBoxBoxIntersection() {