diff --git a/include/Nazara/Graphics/RenderSpriteChain.inl b/include/Nazara/Graphics/RenderSpriteChain.inl index 55744d1cb..69565f880 100644 --- a/include/Nazara/Graphics/RenderSpriteChain.inl +++ b/include/Nazara/Graphics/RenderSpriteChain.inl @@ -31,7 +31,7 @@ namespace Nz { UInt64 matFlags = 1; - float distanceNear = frustum.GetPlane(FrustumPlane::Near).Distance(m_worldInstance.GetWorldMatrix().GetTranslation()); + float distanceNear = frustum.GetPlane(FrustumPlane::Near).SignedDistance(m_worldInstance.GetWorldMatrix().GetTranslation()); UInt64 distance = DistanceAsSortKey(distanceNear); // Transparent RQ index: diff --git a/include/Nazara/Graphics/RenderSubmesh.inl b/include/Nazara/Graphics/RenderSubmesh.inl index e3a481b87..7f58583c0 100644 --- a/include/Nazara/Graphics/RenderSubmesh.inl +++ b/include/Nazara/Graphics/RenderSubmesh.inl @@ -32,7 +32,7 @@ namespace Nz { UInt64 matFlags = 1; - float distanceNear = frustum.GetPlane(FrustumPlane::Near).Distance(m_worldInstance.GetWorldMatrix().GetTranslation()); + float distanceNear = frustum.GetPlane(FrustumPlane::Near).SignedDistance(m_worldInstance.GetWorldMatrix().GetTranslation()); UInt64 distance = DistanceAsSortKey(distanceNear); // Transparent RQ index: diff --git a/include/Nazara/Math/Frustum.inl b/include/Nazara/Math/Frustum.inl index 82023acaf..99e6519cf 100644 --- a/include/Nazara/Math/Frustum.inl +++ b/include/Nazara/Math/Frustum.inl @@ -151,7 +151,7 @@ namespace Nz Vector3 projectedExtents = extents * plane.normal.GetAbs(); float radius = projectedExtents.x + projectedExtents.y + projectedExtents.z; - float distance = plane.Distance(center); + float distance = plane.SignedDistance(center); if (distance < T(-radius)) return false; } @@ -182,7 +182,7 @@ namespace Nz { for (const auto& plane : m_planes) { - if (plane.Distance(sphere.GetPosition()) < -sphere.radius) + if (plane.SignedDistance(sphere.GetPosition()) < -sphere.radius) return false; } @@ -200,7 +200,7 @@ namespace Nz { for (const auto& plane : m_planes) { - if (plane.Distance(point) < T(0.0)) + if (plane.SignedDistance(point) < T(0.0)) return false; } @@ -222,7 +222,7 @@ namespace Nz std::size_t j; for (j = 0; j < pointCount; j++ ) { - if (plane.Distance(points[j]) > T(0.0)) + if (plane.SignedDistance(points[j]) > T(0.0)) break; } @@ -321,7 +321,7 @@ namespace Nz Vector3 projectedExtents = extents * plane.normal.GetAbs(); float radius = projectedExtents.x + projectedExtents.y + projectedExtents.z; - float distance = plane.Distance(center); + float distance = plane.SignedDistance(center); if (distance < T(-radius)) return IntersectionSide::Outside; @@ -358,7 +358,7 @@ namespace Nz for (const auto& plane : m_planes) { - T distance = plane.Distance(sphere.GetPosition()); + T distance = plane.SignedDistance(sphere.GetPosition()); if (distance < -sphere.radius) return IntersectionSide::Outside; else if (distance < sphere.radius) @@ -385,7 +385,7 @@ namespace Nz std::size_t j; for (j = 0; j < pointCount; j++ ) { - if (plane.Distance(points[j]) > T(0.0)) + if (plane.SignedDistance(points[j]) > T(0.0)) break; } diff --git a/include/Nazara/Math/Plane.hpp b/include/Nazara/Math/Plane.hpp index faeae0381..13ca6a311 100644 --- a/include/Nazara/Math/Plane.hpp +++ b/include/Nazara/Math/Plane.hpp @@ -30,11 +30,10 @@ namespace Nz constexpr bool ApproxEqual(const Plane& plane, T maxDifference = std::numeric_limits::epsilon()) const; - constexpr T Distance(T x, T y, T z) const; - constexpr T Distance(const Vector3& point) const; - Plane& Normalize(T* length = nullptr); + constexpr T SignedDistance(const Vector3& point) const; + std::string ToString() const; constexpr Plane& operator=(const Plane& other) = default; diff --git a/include/Nazara/Math/Plane.inl b/include/Nazara/Math/Plane.inl index 808f136d7..e3455820c 100644 --- a/include/Nazara/Math/Plane.inl +++ b/include/Nazara/Math/Plane.inl @@ -113,22 +113,17 @@ namespace Nz return NumberEquals(distance, plane.distance, maxDifference); } - /*! - * \brief Returns the distance from the plane to the point - * \return 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 - * - * \remark If T is negative, it means that the point is in the opposite direction of the normal - * - * \see Distance - */ template - constexpr T Plane::Distance(T x, T y, T z) const + Plane& Plane::Normalize(T* length) { - return Distance(Vector3(x, y, z)); + T normalLength = normal.GetLength(); + normal /= normalLength; + distance /= normalLength; + + if (length) + *length = normalLength; + + return *this; } /*! @@ -142,24 +137,11 @@ namespace Nz * \see Distance */ template - constexpr T Plane::Distance(const Vector3& point) const + constexpr T Plane::SignedDistance(const Vector3& point) const { return normal.DotProduct(point) + distance; // ax + by + cz + d = 0. } - template - Plane& Plane::Normalize(T* length) - { - T normalLength = normal.GetLength(); - normal /= normalLength; - distance /= normalLength; - - if (length) - *length = normalLength; - - return *this; - } - /*! * \brief Gives a string representation * \return A string representation of the object: "Plane(Normal: Vector3(x, y, z); Distance: w)" diff --git a/tests/UnitTests/Engine/Math/PlaneTest.cpp b/tests/UnitTests/Engine/Math/PlaneTest.cpp index bee01f807..0ea6c9497 100644 --- a/tests/UnitTests/Engine/Math/PlaneTest.cpp +++ b/tests/UnitTests/Engine/Math/PlaneTest.cpp @@ -19,30 +19,30 @@ SCENARIO("Plane", "[MATH][PLANE]") AND_THEN("They have the same distance from the same point") { Nz::Vector3f point(-2.f, 3.f, 1.f); - REQUIRE(firstPlane.Distance(point) == Catch::Approx(secondPlane.Distance(point))); - REQUIRE(firstPlane.Distance(-2.f, 3.f, 1.f) == Catch::Approx(0.1547f)); + REQUIRE(firstPlane.SignedDistance(point) == Catch::Approx(secondPlane.SignedDistance(point))); + REQUIRE(firstPlane.SignedDistance({ -2.f, 3.f, 1.f }) == Catch::Approx(0.1547f)); } AND_THEN("Distance between Plane (0, 1, 0), distance 1 and point (0, 2, 0) should be 1") { - REQUIRE(Nz::Planef(Nz::Vector3f::UnitY(), 1.f).Distance(Nz::Vector3f::UnitY() * 2.f) == Catch::Approx(1.f)); + REQUIRE(Nz::Planef(Nz::Vector3f::UnitY(), 1.f).SignedDistance(Nz::Vector3f::UnitY() * 2.f) == Catch::Approx(1.f)); } AND_THEN("Distance between Plane (0, 1, 0), distance 5 and point (0, 2, 0) should be -3") { - REQUIRE(Nz::Planef(Nz::Vector3f::UnitY(), 5.f).Distance(Nz::Vector3f::UnitY() * 2.f) == Catch::Approx(-3.f)); + REQUIRE(Nz::Planef(Nz::Vector3f::UnitY(), 5.f).SignedDistance(Nz::Vector3f::UnitY() * 2.f) == Catch::Approx(-3.f)); } AND_THEN("Distance between Plane (0, 1, 0), distance 1000 and point (0, 500, 0) and (0, 1500, 0)") { - REQUIRE(Nz::Planef(Nz::Vector3f::UnitY(), 1000.f).Distance(Nz::Vector3f::UnitY() * 500.f) == Catch::Approx(-500.f)); - REQUIRE(Nz::Planef(Nz::Vector3f::UnitY(), 1000.f).Distance(Nz::Vector3f::UnitY() * 1500.f) == Catch::Approx(500.f)); + REQUIRE(Nz::Planef(Nz::Vector3f::UnitY(), 1000.f).SignedDistance(Nz::Vector3f::UnitY() * 500.f) == Catch::Approx(-500.f)); + REQUIRE(Nz::Planef(Nz::Vector3f::UnitY(), 1000.f).SignedDistance(Nz::Vector3f::UnitY() * 1500.f) == Catch::Approx(500.f)); } AND_THEN("Distance between Plane (0, -1, 0), distance -1000 and point (0, 500, 0) and (0, 1500, 0)") { - REQUIRE(Nz::Planef(-Nz::Vector3f::UnitY(), -1000.f).Distance(Nz::Vector3f::UnitY() * 500.f) == Catch::Approx(500.f)); - REQUIRE(Nz::Planef(-Nz::Vector3f::UnitY(), -1000.f).Distance(Nz::Vector3f::UnitY() * 1500.f) == Catch::Approx(-500.f)); + REQUIRE(Nz::Planef(-Nz::Vector3f::UnitY(), -1000.f).SignedDistance(Nz::Vector3f::UnitY() * 500.f) == Catch::Approx(500.f)); + REQUIRE(Nz::Planef(-Nz::Vector3f::UnitY(), -1000.f).SignedDistance(Nz::Vector3f::UnitY() * 1500.f) == Catch::Approx(-500.f)); } }