Add combine AABB function

New function that creates a new AABB that encompasses the two given
AABBs.
This commit is contained in:
Stephen Seo 2024-05-06 14:28:23 +09:00
parent ef52303a6a
commit 1d44827f96
3 changed files with 71 additions and 0 deletions

View file

@ -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};
}

View file

@ -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

View file

@ -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';