From 90bafb3a069468455d8c65d030ef8750b510c39b Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 27 Aug 2023 00:30:05 +0200 Subject: [PATCH] Base: add methods to get volume and get max/min points --- src/Base/BoundBox.h | 27 +++++++++++++++++++++++++++ tests/src/Base/BoundBox.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/Base/BoundBox.h b/src/Base/BoundBox.h index e8db802395..2155d49e67 100644 --- a/src/Base/BoundBox.h +++ b/src/Base/BoundBox.h @@ -208,6 +208,10 @@ public: /** Returns the center of the box. */ inline Vector3 GetCenter () const; + /** Returns the minimum point of the box. */ + inline Vector3 GetMinimum () const; + /** Returns the maximum point of the box. */ + inline Vector3 GetMaximum () const; /** Compute the diagonal length of this bounding box. * @note It's up to the client programmer to make sure that this bounding box is valid. */ @@ -225,6 +229,8 @@ public: inline Precision LengthY () const; /** Calculates expansion in z-direction. */ inline Precision LengthZ () const; + /** Calculates the volume. If the box is invalid a negative value is returned */ + inline Precision Volume () const; /** Moves in x-direction. */ inline void MoveX (Precision value); /** Moves in y-direction. */ @@ -971,6 +977,18 @@ inline Vector3 BoundBox3::GetCenter () const (MaxZ + MinZ) / 2); } +template +inline Vector3 BoundBox3::GetMinimum () const +{ + return Vector3(MinX, MinY, MinZ); +} + +template +inline Vector3 BoundBox3::GetMaximum () const +{ + return Vector3(MaxX, MaxY, MaxZ); +} + template inline Precision BoundBox3::CalcDiagonalLength () const { @@ -1018,6 +1036,15 @@ inline Precision BoundBox3::LengthZ () const return MaxZ - MinZ; } +template +inline Precision BoundBox3::Volume () const +{ + if (!IsValid()) { + return -1.0; + } + return LengthX() * LengthY() * LengthZ(); +} + template inline void BoundBox3::MoveX (Precision value) { diff --git a/tests/src/Base/BoundBox.cpp b/tests/src/Base/BoundBox.cpp index 139de86bc1..f29f2faced 100644 --- a/tests/src/Base/BoundBox.cpp +++ b/tests/src/Base/BoundBox.cpp @@ -201,6 +201,24 @@ TEST(BoundBox, TestCenter) EXPECT_EQ(box.GetCenter(), Base::Vector3d(0.5, 0.5, 0.5)); } +TEST(BoundBox, TestMinimum) +{ + Base::BoundBox3d box; + box.Add(Base::Vector3d(0, 0, 0)); + box.Add(Base::Vector3d(1, 1, 1)); + + EXPECT_EQ(box.GetMinimum(), Base::Vector3d(0, 0, 0)); +} + +TEST(BoundBox, TestMaximum) +{ + Base::BoundBox3d box; + box.Add(Base::Vector3d(0, 0, 0)); + box.Add(Base::Vector3d(1, 1, 1)); + + EXPECT_EQ(box.GetMaximum(), Base::Vector3d(1, 1, 1)); +} + TEST(BoundBox, TestDiagonalLength) { Base::BoundBox3d box; @@ -211,6 +229,16 @@ TEST(BoundBox, TestDiagonalLength) EXPECT_LT(box.CalcDiagonalLength(), 1.8); } +TEST(BoundBox, TestVolume) +{ + Base::BoundBox3d box; + EXPECT_LT(box.Volume(), 0.0); + box.Add(Base::Vector3d(0, 0, 0)); + EXPECT_EQ(box.Volume(), 0.0); + box.Add(Base::Vector3d(1, 1, 1)); + EXPECT_GT(box.Volume(), 0.0); +} + TEST(BoundBox, TestDiagonalEnlarge) { Base::BoundBox3d box;