From 1d44827f96b3862e2c25bc11bf9b6b80218321e1 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 6 May 2024 14:28:23 +0900 Subject: [PATCH] Add combine AABB function New function that creates a new AABB that encompasses the two given AABBs. --- src/sc_sacd.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ src/sc_sacd.h | 4 ++++ src/test.cpp | 15 ++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/src/sc_sacd.cpp b/src/sc_sacd.cpp index 070f4e8..62a7d40 100644 --- a/src/sc_sacd.cpp +++ b/src/sc_sacd.cpp @@ -627,3 +627,55 @@ SC_SACD_AABB_Box SC_SACD_Generic_Box_To_AABB(const SC_SACD_Generic_Box s) { return aabb; } + +SC_SACD_AABB_Box SC_SACD_AABB_Combine(const SC_SACD_AABB_Box a, + const SC_SACD_AABB_Box b) { + SC_SACD_Vec3 min, max; + + // Populate min values. + + float temp_a = a.x - a.width / 2.0F; + float temp_b = b.x - b.width / 2.0F; + + min.x = temp_a < temp_b ? temp_a : temp_b; + + temp_a = a.y - a.height / 2.0F; + temp_b = b.y - b.height / 2.0F; + + min.y = temp_a < temp_b ? temp_a : temp_b; + + temp_a = a.z - a.depth / 2.0F; + temp_b = b.z - b.depth / 2.0F; + + min.z = temp_a < temp_b ? temp_a : temp_b; + + // Populate max values. + + temp_a = a.x + a.width / 2.0F; + temp_b = b.x + b.width / 2.0F; + + max.x = temp_a > temp_b ? temp_a : temp_b; + + temp_a = a.y + a.height / 2.0F; + temp_b = b.y + b.height / 2.0F; + + max.y = temp_a > temp_b ? temp_a : temp_b; + + temp_a = a.z + a.depth / 2.0F; + temp_b = b.z + b.depth / 2.0F; + + max.z = temp_a > temp_b ? temp_a : temp_b; + + // Populate the result. + + temp_a = max.x - min.x; + temp_b = max.y - min.y; + float temp_c = max.z - min.z; + + return SC_SACD_AABB_Box{min.x + temp_a / 2.0F, + min.y + temp_b / 2.0F, + min.z + temp_c / 2.0F, + temp_a, + temp_b, + temp_c}; +} diff --git a/src/sc_sacd.h b/src/sc_sacd.h index b18dcef..bf5725d 100644 --- a/src/sc_sacd.h +++ b/src/sc_sacd.h @@ -137,6 +137,10 @@ SC_SACD_EXPORT SC_SACD_AABB_Box SC_SACD_Sphere_To_AABB(const SC_SACD_Sphere s); SC_SACD_EXPORT SC_SACD_AABB_Box SC_SACD_Generic_Box_To_AABB(const SC_SACD_Generic_Box s); +/// Combines AABB's such that the new AABB encompasses the two AABB's. +SC_SACD_EXPORT SC_SACD_AABB_Box SC_SACD_AABB_Combine(const SC_SACD_AABB_Box a, + const SC_SACD_AABB_Box b); + #ifdef __cplusplus } #endif diff --git a/src/test.cpp b/src/test.cpp index 83b08ea..4986e1a 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -681,6 +681,21 @@ int main() { CHECK_FLOAT(aabb.depth, box.depth); } + // Test combining AABB. + { + SC_SACD_AABB_Box a{5.0F, 5.0F, 5.0F, 2.0F, 2.0F, 2.0F}; + + SC_SACD_AABB_Box b{-3.0F, -3.0F, -3.0F, 2.0F, 2.0F, 2.0F}; + + auto combined = SC_SACD_AABB_Combine(a, b); + CHECK_FLOAT(combined.x, (7.0F - 5.0F) / 2.0F); + CHECK_FLOAT(combined.y, (7.0F - 5.0F) / 2.0F); + CHECK_FLOAT(combined.z, (7.0F - 5.0F) / 2.0F); + CHECK_FLOAT(combined.width, 10.0F); + CHECK_FLOAT(combined.height, 10.0F); + CHECK_FLOAT(combined.depth, 10.0F); + } + std::cout << "Checks checked: " << checks_checked << '\n' << "Checks passed: " << checks_passed << '\n';