Fix possible division by zero in Plane.Normalize

#648
This commit is contained in:
Wojtek Figat
2021-10-11 14:32:34 +02:00
parent a013c3dd04
commit 9450111ae6
3 changed files with 22 additions and 13 deletions

View File

@@ -29,6 +29,17 @@ String Plane::ToString() const
return String::Format(TEXT("{}"), *this);
}
void Plane::Normalize()
{
const float length = Normal.Length();
if (!Math::IsZero(length))
{
const float rcp = 1.0f / length;
Normal *= rcp;
D *= rcp;
}
}
void Plane::Constlection(Matrix& result) const
{
const float x = Normal.X;

View File

@@ -214,12 +214,15 @@ namespace FlaxEngine
/// </summary>
public void Normalize()
{
float magnitude = 1.0f / (float)Math.Sqrt(Normal.X * Normal.X + Normal.Y * Normal.Y + Normal.Z * Normal.Z);
Normal.X *= magnitude;
Normal.Y *= magnitude;
Normal.Z *= magnitude;
D *= magnitude;
float length = (float)Math.Sqrt(Normal.X * Normal.X + Normal.Y * Normal.Y + Normal.Z * Normal.Z);
if (!Mathf.IsZero(length))
{
float rcp = 1.0f / length;
Normal.X *= rcp;
Normal.Y *= rcp;
Normal.Z *= rcp;
D *= rcp;
}
}
/// <summary>

View File

@@ -97,14 +97,9 @@ public:
public:
/// <summary>
/// Changes the coefficients of the normal vector of the plane to make it of unit length
/// Changes the coefficients of the normal vector of the plane to make it of unit length.
/// </summary>
void Normalize()
{
const float magnitude = Normal.InvLength();
Normal *= magnitude;
D *= magnitude;
}
void Normalize();
void Negate()
{