summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorassiduous <assiduous@diligentgraphics.com>2020-06-28 02:51:50 +0000
committerassiduous <assiduous@diligentgraphics.com>2020-06-28 02:51:50 +0000
commit23583ced0fbb484c97deb2b7abb3a5d594eaee56 (patch)
treebcab12bc3e473807841fc349b56a2aa84da96697
parentMathLib: updated ViewFrustum struct (diff)
downloadDiligentCore-23583ced0fbb484c97deb2b7abb3a5d594eaee56.tar.gz
DiligentCore-23583ced0fbb484c97deb2b7abb3a5d594eaee56.zip
MathLib: added bound box transform function
-rw-r--r--Common/interface/AdvancedMath.hpp32
-rw-r--r--Tests/DiligentCoreTest/src/Common/MathLibTest.cpp36
2 files changed, 68 insertions, 0 deletions
diff --git a/Common/interface/AdvancedMath.hpp b/Common/interface/AdvancedMath.hpp
index 21a633cc..b48da1bc 100644
--- a/Common/interface/AdvancedMath.hpp
+++ b/Common/interface/AdvancedMath.hpp
@@ -175,6 +175,38 @@ struct BoundBox
{
float3 Min;
float3 Max;
+
+ // Computes new bounding box by applying transform matrix m to the box
+ BoundBox Transform(const float4x4& m) const
+ {
+ BoundBox NewBB;
+ NewBB.Min = float3::MakeVector(m[3]);
+ NewBB.Max = NewBB.Min;
+ float3 v0, v1;
+
+ float3 right = float3::MakeVector(m[0]);
+
+ v0 = right * Min.x;
+ v1 = right * Max.x;
+ NewBB.Min += std::min(v0, v1);
+ NewBB.Max += std::max(v0, v1);
+
+ float3 up = float3::MakeVector(m[1]);
+
+ v0 = up * Min.y;
+ v1 = up * Max.y;
+ NewBB.Min += std::min(v0, v1);
+ NewBB.Max += std::max(v0, v1);
+
+ float3 back = float3::MakeVector(m[2]);
+
+ v0 = back * Min.z;
+ v1 = back * Max.z;
+ NewBB.Min += std::min(v0, v1);
+ NewBB.Max += std::max(v0, v1);
+
+ return NewBB;
+ }
};
enum class BoxVisibility
diff --git a/Tests/DiligentCoreTest/src/Common/MathLibTest.cpp b/Tests/DiligentCoreTest/src/Common/MathLibTest.cpp
index 35456966..dcf1cfbb 100644
--- a/Tests/DiligentCoreTest/src/Common/MathLibTest.cpp
+++ b/Tests/DiligentCoreTest/src/Common/MathLibTest.cpp
@@ -1910,4 +1910,40 @@ TEST(Common_AdvancedMath, RasterizeTriangle)
}
}
+TEST(Common_AdvancedMath, TransformBoundBox)
+{
+ BoundBox BB;
+ BB.Min = float3{1, 2, 3};
+ BB.Max = float3{2, 4, 6};
+
+ {
+ float3 Offset{2.5f, 3.125f, -7.25f};
+ auto TransformedBB = BB.Transform(float4x4::Translation(Offset));
+ EXPECT_EQ(TransformedBB.Min, BB.Min + Offset);
+ EXPECT_EQ(TransformedBB.Max, BB.Max + Offset);
+ }
+
+ {
+ float3 Scale{0.5f, 2.125f, 1.25f};
+ auto TransformedBB = BB.Transform(float4x4::Scale(Scale));
+ EXPECT_EQ(TransformedBB.Min, BB.Min * Scale);
+ EXPECT_EQ(TransformedBB.Max, BB.Max * Scale);
+ }
+
+ {
+ // clang-format off
+ float4x4 Rotation
+ {
+ 0, 1, 0, 0,
+ 0, 0, 1, 0,
+ -1, 0, 0, 0,
+ 0, 0, 0, 1
+ };
+ // clang-format on
+ auto TransformedBB = BB.Transform(Rotation);
+ EXPECT_EQ(TransformedBB.Min, float3(-6, 1, 2));
+ EXPECT_EQ(TransformedBB.Max, float3(-3, 2, 4));
+ }
+}
+
} // namespace