From: Daniel Karbach Date: Fri, 2 Dec 2016 11:40:14 +0000 (+0100) Subject: simple AABB intersection test X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=bda41b98427c8d34f954dae0dcaf261c5ad6cb43;p=gong.git simple AABB intersection test --- diff --git a/src/geometry/geometry.cpp b/src/geometry/geometry.cpp index 1c1bfd6..002c68f 100644 --- a/src/geometry/geometry.cpp +++ b/src/geometry/geometry.cpp @@ -46,6 +46,16 @@ std::ostream &operator <<(std::ostream &out, const AABB &box) { return out << "AABB(" << box.min << ", " << box.max << ')'; } +bool Intersection(const AABB &a, const AABB &b) noexcept { + if (a.max.x < b.min.x) return false; + if (b.max.x < a.min.x) return false; + if (a.max.y < b.min.y) return false; + if (b.max.y < a.min.y) return false; + if (a.max.z < b.min.z) return false; + if (b.max.z < a.min.z) return false; + return true; +} + std::ostream &operator <<(std::ostream &out, const Ray &ray) { return out << "Ray(" << ray.orig << ", " << ray.dir << ')'; } diff --git a/src/geometry/primitive.hpp b/src/geometry/primitive.hpp index 819ef04..c922506 100644 --- a/src/geometry/primitive.hpp +++ b/src/geometry/primitive.hpp @@ -30,10 +30,17 @@ struct AABB { glm::vec3 high(glm::max(glm::abs(min), glm::abs(max))); return glm::length(high); } + + void Move(const glm::vec3 &delta) noexcept { + min += delta; + max += delta; + } }; std::ostream &operator <<(std::ostream &, const AABB &); +bool Intersection(const AABB &, const AABB &) noexcept; + // TODO: this should really use setters/getters for dir and inv_dir so // manipulating code doesn't "forget" to call Update() struct Ray { diff --git a/tst/geometry/IntersectionTest.cpp b/tst/geometry/IntersectionTest.cpp index d2332fe..36cacdf 100644 --- a/tst/geometry/IntersectionTest.cpp +++ b/tst/geometry/IntersectionTest.cpp @@ -21,6 +21,64 @@ void IntersectionTest::tearDown() { } +void IntersectionTest::testAABB() { + AABB a{ { -1, -1, -1 }, { 1, 1, 1 } }; + AABB b(a); + + CPPUNIT_ASSERT_MESSAGE( + "coincidental AABBs should intersect", + Intersection(a, b) + ); + + b.Move({ 1, 0, 0 }); + CPPUNIT_ASSERT_MESSAGE( + "AABBs should intersect", + Intersection(a, b) + ); + + b.Move({ 2, 0, 0 }); + CPPUNIT_ASSERT_MESSAGE( + "AABBs should not intersect", + !Intersection(a, b) + ); + + b.Move({ -4, 0, 0 }); + CPPUNIT_ASSERT_MESSAGE( + "AABBs should intersect", + Intersection(a, b) + ); + + b.Move({ -2, 0, 0 }); + CPPUNIT_ASSERT_MESSAGE( + "AABBs should not intersect", + !Intersection(a, b) + ); + + b.Move({ 3, 1, 0 }); + CPPUNIT_ASSERT_MESSAGE( + "AABBs should intersect", + Intersection(a, b) + ); + + b.Move({ 0, 2, 0 }); + CPPUNIT_ASSERT_MESSAGE( + "AABBs should not intersect", + !Intersection(a, b) + ); + + b.Move({ 2, 0, 0 }); + CPPUNIT_ASSERT_MESSAGE( + "AABBs should not intersect", + !Intersection(a, b) + ); + + b.Move({ 0, 0, 2 }); + CPPUNIT_ASSERT_MESSAGE( + "AABBs should not intersect", + !Intersection(a, b) + ); +} + void IntersectionTest::testSimpleRayBox() { Ray ray{ { 0, 0, 0 }, { 1, 0, 0 }, { } }; // at origin, pointing right ray.Update(); diff --git a/tst/geometry/IntersectionTest.hpp b/tst/geometry/IntersectionTest.hpp index d0c7d80..4865c88 100644 --- a/tst/geometry/IntersectionTest.hpp +++ b/tst/geometry/IntersectionTest.hpp @@ -13,6 +13,7 @@ class IntersectionTest CPPUNIT_TEST_SUITE(IntersectionTest); +CPPUNIT_TEST(testAABB); CPPUNIT_TEST(testSimpleRayBox); CPPUNIT_TEST(testRayBox); CPPUNIT_TEST(testBoxBox); @@ -25,6 +26,7 @@ public: void setUp(); void tearDown(); + void testAABB(); void testSimpleRayBox(); void testRayBox(); void testBoxBox();