From f5f6c859d7f581a0fe0e8bc1bd34793dcd50c5a5 Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 15 Dec 2016 18:32:58 +0100 Subject: [PATCH] Math/Sphere: Remove SquaredDistance method --- .../Graphics/ForwardRenderTechnique.inl | 18 +++---- include/Nazara/Math/Algorithm.hpp | 2 +- include/Nazara/Math/Algorithm.inl | 5 +- include/Nazara/Math/Sphere.hpp | 3 -- include/Nazara/Math/Sphere.inl | 51 +++---------------- tests/Engine/Math/Sphere.cpp | 16 +++--- 6 files changed, 27 insertions(+), 68 deletions(-) diff --git a/include/Nazara/Graphics/ForwardRenderTechnique.inl b/include/Nazara/Graphics/ForwardRenderTechnique.inl index 5f7ba181f..208aa9f81 100644 --- a/include/Nazara/Graphics/ForwardRenderTechnique.inl +++ b/include/Nazara/Graphics/ForwardRenderTechnique.inl @@ -126,7 +126,7 @@ namespace Nz { if (uniforms.locations.type != -1) shader->SendInteger(uniforms.locations.type + uniformOffset, -1); //< Disable the light in the shader - + if (uniforms.locations.directionalSpotLightShadowMap != -1) shader->SendInteger(uniforms.locations.directionalSpotLightShadowMap + index, dummyTexture); @@ -163,7 +163,7 @@ namespace Nz inline float ForwardRenderTechnique::ComputePointLightScore(const Spheref& object, const AbstractRenderQueue::PointLight& light) { ///TODO: Compute a score depending on the light luminosity - return object.SquaredDistance(light.position); + return object.GetPosition().SquaredDistance(light.position); } /*! @@ -177,7 +177,7 @@ namespace Nz inline float ForwardRenderTechnique::ComputeSpotLightScore(const Spheref& object, const AbstractRenderQueue::SpotLight& light) { ///TODO: Compute a score depending on the light luminosity and spot direction - return object.SquaredDistance(light.position); + return object.GetPosition().SquaredDistance(light.position); } /*! @@ -199,29 +199,29 @@ namespace Nz /*! * \brief Checks whether the point light is suitable for the computations - * \return true if light is enoughly close + * \return true if light is close enough * - * \param object Sphere symbolising the object + * \param object Sphere symbolizing the object * \param light Light to compute */ inline bool ForwardRenderTechnique::IsPointLightSuitable(const Spheref& object, const AbstractRenderQueue::PointLight& light) { // If the object is too far away from this point light, there is not way it could light it - return object.SquaredDistance(light.position) <= light.radius * light.radius; + return object.Contains(light.position); } /*! * \brief Checks whether the spot light is suitable for the computations - * \return true if light is enoughly close + * \return true if light is close enough * - * \param object Sphere symbolising the object + * \param object Sphere symbolizing the object * \param light Light to compute */ inline bool ForwardRenderTechnique::IsSpotLightSuitable(const Spheref& object, const AbstractRenderQueue::SpotLight& light) { ///TODO: Exclude spot lights based on their direction and outer angle? - return object.SquaredDistance(light.position) <= light.radius * light.radius; + return object.Contains(light.position); } } diff --git a/include/Nazara/Math/Algorithm.hpp b/include/Nazara/Math/Algorithm.hpp index f138a8fc9..6decc4b28 100644 --- a/include/Nazara/Math/Algorithm.hpp +++ b/include/Nazara/Math/Algorithm.hpp @@ -53,7 +53,7 @@ namespace Nz unsigned int GetNumberLength(long double number, UInt8 precision = NAZARA_CORE_DECIMAL_DIGITS); template /*constexpr*/ unsigned int IntegralLog2(T number); template /*constexpr*/ unsigned int IntegralLog2Pot(T pot); - /*constexpr*/ unsigned int IntegralPow(unsigned int base, unsigned int exponent); + template /*constexpr*/ T IntegralPow(T base, unsigned int exponent); template constexpr T Lerp(const T& from, const T& to, const T2& interpolation); template constexpr T MultiplyAdd(T x, T y, T z); template /*constexpr*/ T NormalizeAngle(T angle); diff --git a/include/Nazara/Math/Algorithm.inl b/include/Nazara/Math/Algorithm.inl index 02ede739a..d7309a148 100644 --- a/include/Nazara/Math/Algorithm.inl +++ b/include/Nazara/Math/Algorithm.inl @@ -436,9 +436,10 @@ namespace Nz */ //TODO: Mark as constexpr when supported by all major compilers - /*constexpr*/ inline unsigned int IntegralPow(unsigned int base, unsigned int exponent) + template + /*constexpr*/ T IntegralPow(T base, unsigned int exponent) { - unsigned int r = 1; + T r = 1; for (unsigned int i = 0; i < exponent; ++i) r *= base; diff --git a/include/Nazara/Math/Sphere.hpp b/include/Nazara/Math/Sphere.hpp index 8f7ac9e14..feaa8af56 100644 --- a/include/Nazara/Math/Sphere.hpp +++ b/include/Nazara/Math/Sphere.hpp @@ -58,9 +58,6 @@ namespace Nz Sphere& Set(const T sphere[4]); template Sphere& Set(const Sphere& sphere); - T SquaredDistance(T X, T Y, T Z) const; - T SquaredDistance(const Vector3& point) const; - String ToString() const; T& operator[](unsigned int i); diff --git a/include/Nazara/Math/Sphere.inl b/include/Nazara/Math/Sphere.inl index 5252dbdbb..02847b243 100644 --- a/include/Nazara/Math/Sphere.inl +++ b/include/Nazara/Math/Sphere.inl @@ -94,7 +94,7 @@ namespace Nz template bool Sphere::Contains(T X, T Y, T Z) const { - return SquaredDistance(X, Y, Z) <= radius * radius; + return Contains(Vector3(X, Y, Z)); } /*! @@ -109,11 +109,8 @@ namespace Nz template bool Sphere::Contains(const Box& box) const { - if (box.GetMinimum().SquaredDistance(GetPosition()) <= radius * radius) - { - if (box.GetMaximum().SquaredDistance(GetPosition()) <= radius * radius) - return true; - } + if (Contains(box.GetMinimum()) && Contains(box.GetMaximum())) + return true; return false; } @@ -128,7 +125,7 @@ namespace Nz template bool Sphere::Contains(const Vector3& point) const { - return Contains(point.x, point.y, point.z); + return GetPosition().SquaredDistance(point) <= radius * radius; } /*! @@ -138,8 +135,6 @@ namespace Nz * \param X X position of the point * \param Y Y position of the point * \param Z Z position of the point - * - * \see SquaredDistance */ template @@ -153,8 +148,6 @@ namespace Nz * \return Distance to the point * * \param point Position of the point - * - * \see SquaredDistance */ template @@ -177,9 +170,7 @@ namespace Nz template Sphere& Sphere::ExtendTo(T X, T Y, T Z) { - T distance = SquaredDistance(X, Y, Z); - if (distance > radius*radius) - radius = std::sqrt(distance); + radius = std::max(radius, Distance(X, Y, Z)); return *this; } @@ -304,7 +295,7 @@ namespace Nz template bool Sphere::Intersect(const Sphere& sphere) const { - return SquaredDistance(sphere.x, sphere.y, sphere.z) <= sphere.radius * sphere.radius; + return GetPosition().SquaredDistance(sphere.x, sphere.y, sphere.z) <= IntegralPow(radius + sphere.radius, 2); } /*! @@ -458,36 +449,6 @@ namespace Nz return *this; } - /*! - * \brief Returns the squared distance from the sphere to the point (can be negative if the point is inside the sphere) - * \return Squared distance to the point - * - * \param X X position of the point - * \param Y Y position of the point - * \param Z Z position of the point - * - * \see Distance - */ - template - T Sphere::SquaredDistance(T X, T Y, T Z) const - { - return SquaredDistance({X, Y, Z}); - } - - /*! - * \brief Returns the squared distance from the sphere to the point (can be negative if the point is inside the sphere) - * \return Squared distance to the point - * - * \param point Position of the point - * - * \see Distance - */ - template - T Sphere::SquaredDistance(const Vector3& point) const - { - return Vector3f::SquaredDistance(GetPosition(), point) - radius * radius; - } - /*! * \brief Gives a string representation * \return A string representation of the object: "Sphere(x, y, z; radius)" diff --git a/tests/Engine/Math/Sphere.cpp b/tests/Engine/Math/Sphere.cpp index ef13ffb38..869987d25 100644 --- a/tests/Engine/Math/Sphere.cpp +++ b/tests/Engine/Math/Sphere.cpp @@ -41,12 +41,10 @@ SCENARIO("Sphere", "[MATH][SPHERE]") { THEN("These results are expected because we don't take into account the border") { - REQUIRE(firstCenterAndUnit.Distance(Nz::Vector3f::UnitX() * 2.f) == Approx(1.f)); - REQUIRE(firstCenterAndUnit.SquaredDistance(Nz::Vector3f::UnitX() * 2.f) == Approx(1.f)); + CHECK(firstCenterAndUnit.Distance(Nz::Vector3f::UnitX() * 2.f) == Approx(1.f)); Nz::Spheref tmp(Nz::Vector3f::UnitX(), 1.f); - REQUIRE(tmp.Distance(Nz::Vector3f::UnitX() * 4.f) == Approx(2.f)); - REQUIRE(tmp.SquaredDistance(Nz::Vector3f::UnitX() * 4.f) == Approx(4.f)); + CHECK(tmp.Distance(Nz::Vector3f::UnitX() * 4.f) == Approx(2.f)); } } @@ -56,7 +54,7 @@ SCENARIO("Sphere", "[MATH][SPHERE]") THEN("This is equal to sphere center and radius 0.75") { - REQUIRE(centerUnitBox.GetSquaredBoundingSphere() == Nz::Spheref(Nz::Vector3f::Zero(), 0.75f)); + CHECK(centerUnitBox.GetSquaredBoundingSphere() == Nz::Spheref(Nz::Vector3f::Zero(), 0.75f)); } } @@ -66,12 +64,12 @@ SCENARIO("Sphere", "[MATH][SPHERE]") THEN("Positive vertex should be the same with centered and unit sphere") { - REQUIRE(positiveVector == firstCenterAndUnit.GetPositiveVertex(positiveVector)); + CHECK(positiveVector == firstCenterAndUnit.GetPositiveVertex(positiveVector)); } AND_THEN("Negative vertex should be the opposite") { - REQUIRE(-positiveVector == firstCenterAndUnit.GetNegativeVertex(positiveVector)); + CHECK(-positiveVector == firstCenterAndUnit.GetNegativeVertex(positiveVector)); } } @@ -81,10 +79,12 @@ SCENARIO("Sphere", "[MATH][SPHERE]") firstCenterAndUnit.ExtendTo(point); + REQUIRE(firstCenterAndUnit.radius == Approx(2.f)); + THEN("Sphere must contain it and distance should be good") { CHECK(firstCenterAndUnit.Contains(point)); - REQUIRE(firstCenterAndUnit.Distance(point) == 1.f); + CHECK(firstCenterAndUnit.Distance(point) == Approx(1.f)); } }