Math/Plane: Rename Distance method to SignedDistance

This commit is contained in:
SirLynix 2023-06-22 17:56:18 +02:00
parent 8481cc7c15
commit b2538028b4
6 changed files with 29 additions and 48 deletions

View File

@ -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:

View File

@ -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:

View File

@ -151,7 +151,7 @@ namespace Nz
Vector3<T> 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<T> 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;
}

View File

@ -30,11 +30,10 @@ namespace Nz
constexpr bool ApproxEqual(const Plane& plane, T maxDifference = std::numeric_limits<T>::epsilon()) const;
constexpr T Distance(T x, T y, T z) const;
constexpr T Distance(const Vector3<T>& point) const;
Plane& Normalize(T* length = nullptr);
constexpr T SignedDistance(const Vector3<T>& point) const;
std::string ToString() const;
constexpr Plane& operator=(const Plane& other) = default;

View File

@ -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<typename T>
constexpr T Plane<T>::Distance(T x, T y, T z) const
Plane<T>& Plane<T>::Normalize(T* length)
{
return Distance(Vector3<T>(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<typename T>
constexpr T Plane<T>::Distance(const Vector3<T>& point) const
constexpr T Plane<T>::SignedDistance(const Vector3<T>& point) const
{
return normal.DotProduct(point) + distance; // ax + by + cz + d = 0.
}
template<typename T>
Plane<T>& Plane<T>::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)"

View File

@ -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));
}
}