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