From 448e81e4a0d69d7530b54ac9286b9a50f68766c3 Mon Sep 17 00:00:00 2001 From: assiduous Date: Fri, 1 Dec 2023 18:54:19 -0800 Subject: [PATCH] MathLib: updated BoundBox --- Common/interface/AdvancedMath.hpp | 30 +++++++++++++++++++ .../src/Common/MathLibTest.cpp | 9 ++++++ 2 files changed, 39 insertions(+) diff --git a/Common/interface/AdvancedMath.hpp b/Common/interface/AdvancedMath.hpp index 212f78da1..85aaf7266 100644 --- a/Common/interface/AdvancedMath.hpp +++ b/Common/interface/AdvancedMath.hpp @@ -230,6 +230,36 @@ struct BoundBox return NewBB; } + + BoundBox Combine(const BoundBox& Box) const + { + return { + (std::min)(Min, Box.Min), + (std::max)(Max, Box.Max), + }; + } + + constexpr bool IsValid() const + { + return (Max.x >= Min.x && + Max.y >= Min.y && + Max.z >= Min.z); + } + + explicit constexpr operator bool() const + { + return IsValid(); + } + + constexpr bool operator==(const BoundBox& rhs) const + { + return Min == rhs.Min && Max == rhs.Max; + } + + constexpr bool operator!=(const BoundBox& rhs) const + { + return !(*this == rhs); + } }; struct OrientedBoundingBox diff --git a/Tests/DiligentCoreTest/src/Common/MathLibTest.cpp b/Tests/DiligentCoreTest/src/Common/MathLibTest.cpp index 6d5c24cfc..c135b8731 100644 --- a/Tests/DiligentCoreTest/src/Common/MathLibTest.cpp +++ b/Tests/DiligentCoreTest/src/Common/MathLibTest.cpp @@ -2647,6 +2647,15 @@ TEST(Common_AdvancedMath, TransformBoundBox) } } +TEST(Common_AdvancedMath, CombineBoundBoxes) +{ + const BoundBox BB1{float3{1, 2, 3}, float3{4, 5, 6}}; + const BoundBox BB2{float3{-2, -3, -4}, float3{50, 60, 70}}; + const BoundBox RefBB{float3{-2, -3, -4}, float3{50, 60, 70}}; + EXPECT_EQ(BB1.Combine(BB2), RefBB); + EXPECT_EQ(BB2.Combine(BB1), RefBB); +} + TEST(Common_AdvancedMath, CheckBox2DBox2DOverlap) { // clang-format off