From 741dc6b7e8e60b7747e8dca9b4e58bed868af499 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Tue, 16 May 2023 15:43:05 +0200 Subject: [PATCH] Math/Box: Add ScaleAroundCenter --- include/Nazara/Math/Box.hpp | 3 ++ include/Nazara/Math/Box.inl | 40 +++++++++++++++++++++++++ tests/UnitTests/Engine/Math/BoxTest.cpp | 37 +++++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/include/Nazara/Math/Box.hpp b/include/Nazara/Math/Box.hpp index 19f4c9990..1f06d421d 100644 --- a/include/Nazara/Math/Box.hpp +++ b/include/Nazara/Math/Box.hpp @@ -64,6 +64,9 @@ namespace Nz Box& Scale(T scalar); Box& Scale(const Vector3& vec); + Box& ScaleAroundCenter(T scalar); + Box& ScaleAroundCenter(const Vector3& vec); + std::string ToString() const; Box& Transform(const Matrix4& matrix, bool applyTranslation = true); diff --git a/include/Nazara/Math/Box.inl b/include/Nazara/Math/Box.inl index 8e89d8b6c..15f1e6811 100644 --- a/include/Nazara/Math/Box.inl +++ b/include/Nazara/Math/Box.inl @@ -545,6 +545,46 @@ namespace Nz return *this; } + + /*! + * \brief Multiplies the lengths of this box with the scalar (the box center doesn't move) + * \return A reference to this box where lengths are the product of these lengths and the scalar + * + * \param scalar The scalar to multiply width, height and depth with + */ + template + Box& Box::ScaleAroundCenter(T scalar) + { + x -= (width * scalar - width) / T(2.0); + y -= (height * scalar - height) / T(2.0); + z -= (depth * scalar - depth) / T(2.0); + + width *= scalar; + height *= scalar; + depth *= scalar; + + return *this; + } + + /*! + * \brief Multiplies the lengths of this box with the vector but changes the origin (the box center doesn't move) + * \return A reference to this box where width, height and depth are the product of the old width, height and depth with the vec + * + * \param vec The vector where component one multiply width, two height and three depth + */ + template + Box& Box::ScaleAroundCenter(const Vector3& vec) + { + x -= (width * vec.x - width) / T(2.0); + y -= (height * vec.y - height) / T(2.0); + z -= (depth * vec.z - depth) / T(2.0); + + width *= vec.x; + height *= vec.y; + depth *= vec.z; + + return *this; + } /*! * \brief Gives a string representation diff --git a/tests/UnitTests/Engine/Math/BoxTest.cpp b/tests/UnitTests/Engine/Math/BoxTest.cpp index c11b6ea7b..ede8a6cdf 100644 --- a/tests/UnitTests/Engine/Math/BoxTest.cpp +++ b/tests/UnitTests/Engine/Math/BoxTest.cpp @@ -89,6 +89,43 @@ SCENARIO("Box", "[MATH][BOX]") REQUIRE(Nz::Boxf::Lerp(nullBox, centerAndUnit, 0.5f) == result); } } + + WHEN("We scale the boxes") + { + WHEN("We scale uniformly") + { + firstCenterAndUnit.Scale(2.f); + CHECK(firstCenterAndUnit.GetCenter() == Nz::Vector3f::Unit()); + CHECK(firstCenterAndUnit.GetLengths() == 2.f * Nz::Vector3f::Unit()); + } + + WHEN("We scale non-uniformly") + { + firstCenterAndUnit.Scale({ 2.f, 1.f, 0.01f }); + CHECK(firstCenterAndUnit.GetCenter() == Nz::Vector3f(1.f, 0.5f, 0.005f)); + CHECK(firstCenterAndUnit.GetLengths() == Nz::Vector3f(2.f, 1.f, 0.01f)); + } + + WHEN("We scale uniformly around center") + { + Nz::Vector3f center = firstCenterAndUnit.GetCenter(); + + firstCenterAndUnit.ScaleAroundCenter(2.f); + + CHECK(firstCenterAndUnit.GetCenter() == center); + CHECK(firstCenterAndUnit.GetLengths() == 2.f * Nz::Vector3f::Unit()); + } + + WHEN("We scale non-uniformly around center") + { + Nz::Vector3f center = firstCenterAndUnit.GetCenter(); + + firstCenterAndUnit.ScaleAroundCenter({ 2.f, 1.f, 0.01f }); + + CHECK(firstCenterAndUnit.GetCenter() == center); + CHECK(firstCenterAndUnit.GetLengths() == Nz::Vector3f(2.f, 1.f, 0.01f)); + } + } } GIVEN("Two wrong box (negative width, height and depth")