Optimize vectors normalization

This commit is contained in:
Wojtek Figat
2023-04-17 11:10:14 +02:00
parent c3cc78b7c2
commit b85184eee0
13 changed files with 80 additions and 72 deletions

View File

@@ -268,7 +268,7 @@ namespace FlaxEngine
public void Normalize()
{
double length = Length;
if (!Mathd.IsZero(length))
if (length >= Mathd.Epsilon)
{
double inv = 1.0 / length;
X *= inv;

View File

@@ -345,7 +345,7 @@ namespace FlaxEngine
public void Normalize()
{
double length = Length;
if (!Mathd.IsZero(length))
if (length >= Mathd.Epsilon)
{
double inv = 1.0 / length;
X *= inv;

View File

@@ -322,7 +322,7 @@ namespace FlaxEngine
public void Normalize()
{
double length = Length;
if (!Mathd.IsZero(length))
if (length >= Mathd.Epsilon)
{
double inverse = 1.0 / length;
X *= inverse;

View File

@@ -286,7 +286,7 @@ namespace FlaxEngine
public void Normalize()
{
float length = Length;
if (!Mathf.IsZero(length))
if (length >= Mathf.Epsilon)
{
float inv = 1.0f / length;
X *= inv;

View File

@@ -339,7 +339,7 @@ namespace FlaxEngine
public void Normalize()
{
float length = Length;
if (!Mathf.IsZero(length))
if (length >= Mathf.Epsilon)
{
float inv = 1.0f / length;
X *= inv;

View File

@@ -304,7 +304,7 @@ namespace FlaxEngine
public void Normalize()
{
float length = Length;
if (!Mathf.IsZero(length))
if (length >= Mathf.Epsilon)
{
float inverse = 1.0f / length;
X *= inverse;

View File

@@ -2,8 +2,10 @@
#if USE_LARGE_WORLDS
using Real = System.Double;
using Mathr = FlaxEngine.Mathd;
#else
using Real = System.Single;
using Mathr = FlaxEngine.Mathf;
#endif
// -----------------------------------------------------------------------------
@@ -175,7 +177,7 @@ namespace FlaxEngine
public void Normalize()
{
Real length = Normal.Length;
if (!Mathf.IsZero(length))
if (length >= Mathr.Epsilon)
{
Real rcp = 1.0f / length;
Normal.X *= rcp;

View File

@@ -336,7 +336,7 @@ namespace FlaxEngine
public void Normalize()
{
float length = Length;
if (!Mathf.IsZero(length))
if (length >= Mathf.Epsilon)
{
float inverse = 1.0f / length;
X *= inverse;

View File

@@ -2,8 +2,10 @@
#if USE_LARGE_WORLDS
using Real = System.Double;
using Mathr = FlaxEngine.Mathd;
#else
using Real = System.Single;
using Mathr = FlaxEngine.Mathf;
#endif
// -----------------------------------------------------------------------------
@@ -203,22 +205,22 @@ namespace FlaxEngine
/// <summary>
/// Gets a value indicting whether this instance is normalized.
/// </summary>
public bool IsNormalized => Mathf.IsOne(X * X + Y * Y);
public bool IsNormalized => Mathr.IsOne(X * X + Y * Y);
/// <summary>
/// Gets a value indicting whether this vector is zero
/// </summary>
public bool IsZero => Mathf.IsZero(X) && Mathf.IsZero(Y);
public bool IsZero => Mathr.IsZero(X) && Mathr.IsZero(Y);
/// <summary>
/// Gets a minimum component value
/// </summary>
public Real MinValue => Mathf.Min(X, Y);
public Real MinValue => Mathr.Min(X, Y);
/// <summary>
/// Gets a maximum component value
/// </summary>
public Real MaxValue => Mathf.Max(X, Y);
public Real MaxValue => Mathr.Max(X, Y);
/// <summary>
/// Gets an arithmetic average value of all vector components.
@@ -233,7 +235,7 @@ namespace FlaxEngine
/// <summary>
/// Gets a vector with values being absolute values of that vector.
/// </summary>
public Vector2 Absolute => new Vector2(Mathf.Abs(X), Mathf.Abs(Y));
public Vector2 Absolute => new Vector2(Mathr.Abs(X), Mathr.Abs(Y));
/// <summary>
/// Gets a vector with values being opposite to values of that vector.
@@ -292,8 +294,8 @@ namespace FlaxEngine
/// </summary>
public void Normalize()
{
Real length = Length;
if (!Mathf.IsZero(length))
Real length = (Real)Math.Sqrt(X * X + Y * Y);
if (length >= Mathr.Epsilon)
{
Real inv = 1.0f / length;
X *= inv;
@@ -904,8 +906,8 @@ namespace FlaxEngine
/// <remarks>Passing <paramref name="amount" /> a value of 0 will cause <paramref name="start" /> to be returned; a value of 1 will cause <paramref name="end" /> to be returned.</remarks>
public static void Lerp(ref Vector2 start, ref Vector2 end, float amount, out Vector2 result)
{
result.X = Mathf.Lerp(start.X, end.X, amount);
result.Y = Mathf.Lerp(start.Y, end.Y, amount);
result.X = Mathr.Lerp(start.X, end.X, amount);
result.Y = Mathr.Lerp(start.Y, end.Y, amount);
}
/// <summary>
@@ -932,8 +934,8 @@ namespace FlaxEngine
/// <remarks>Passing <paramref name="amount" /> a value of 0 will cause <paramref name="start" /> to be returned; a value of 1 will cause <paramref name="end" /> to be returned.</remarks>
public static void Lerp(ref Vector2 start, ref Vector2 end, ref Vector2 amount, out Vector2 result)
{
result.X = Mathf.Lerp(start.X, end.X, amount.X);
result.Y = Mathf.Lerp(start.Y, end.Y, amount.Y);
result.X = Mathr.Lerp(start.X, end.X, amount.X);
result.Y = Mathr.Lerp(start.Y, end.Y, amount.Y);
}
/// <summary>
@@ -959,7 +961,7 @@ namespace FlaxEngine
/// <param name="result">When the method completes, contains the cubic interpolation of the two vectors.</param>
public static void SmoothStep(ref Vector2 start, ref Vector2 end, float amount, out Vector2 result)
{
amount = Mathf.SmoothStep(amount);
amount = Mathr.SmoothStep(amount);
Lerp(ref start, ref end, amount, out result);
}
@@ -1552,7 +1554,7 @@ namespace FlaxEngine
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vector2 left, Vector2 right)
{
return Mathf.NearEqual(left.X, right.X) && Mathf.NearEqual(left.Y, right.Y);
return Mathr.NearEqual(left.X, right.X) && Mathr.NearEqual(left.Y, right.Y);
}
/// <summary>
@@ -1564,7 +1566,7 @@ namespace FlaxEngine
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Vector2 left, Vector2 right)
{
return !Mathf.NearEqual(left.X, right.X) || !Mathf.NearEqual(left.Y, right.Y);
return !Mathr.NearEqual(left.X, right.X) || !Mathr.NearEqual(left.Y, right.Y);
}
/// <summary>
@@ -1670,7 +1672,7 @@ namespace FlaxEngine
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(ref Vector2 other)
{
return Mathf.NearEqual(other.X, X) && Mathf.NearEqual(other.Y, Y);
return Mathr.NearEqual(other.X, X) && Mathr.NearEqual(other.Y, Y);
}
/// <summary>
@@ -1678,7 +1680,7 @@ namespace FlaxEngine
/// </summary>
public static bool Equals(ref Vector2 a, ref Vector2 b)
{
return Mathf.NearEqual(a.X, b.X) && Mathf.NearEqual(a.Y, b.Y);
return Mathr.NearEqual(a.X, b.X) && Mathr.NearEqual(a.Y, b.Y);
}
/// <summary>
@@ -1689,7 +1691,7 @@ namespace FlaxEngine
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Vector2 other)
{
return Mathf.NearEqual(other.X, X) && Mathf.NearEqual(other.Y, Y);
return Mathr.NearEqual(other.X, X) && Mathr.NearEqual(other.Y, Y);
}
/// <summary>
@@ -1699,7 +1701,7 @@ namespace FlaxEngine
/// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
public override bool Equals(object value)
{
return value is Vector2 other && Mathf.NearEqual(other.X, X) && Mathf.NearEqual(other.Y, Y);
return value is Vector2 other && Mathr.NearEqual(other.X, X) && Mathr.NearEqual(other.Y, Y);
}
}
}

View File

@@ -240,9 +240,9 @@ public:
void Normalize()
{
const T length = Math::Sqrt(X * X + Y * Y);
if (!Math::IsZero(length))
if (length >= ZeroTolerance)
{
const T invLength = 1.0f / length;
const T invLength = (T)1.0f / length;
X *= invLength;
Y *= invLength;
}
@@ -547,9 +547,9 @@ public:
{
Vector2Base r = v;
const T length = Math::Sqrt(r.X * r.X + r.Y * r.Y);
if (Math::Abs(length) >= ZeroTolerance)
if (length >= ZeroTolerance)
{
const T inv = 1.0f / length;
const T inv = (T)1.0f / length;
r.X *= inv;
r.Y *= inv;
}

View File

@@ -2,8 +2,10 @@
#if USE_LARGE_WORLDS
using Real = System.Double;
using Mathr = FlaxEngine.Mathd;
#else
using Real = System.Single;
using Mathr = FlaxEngine.Mathf;
#endif
// -----------------------------------------------------------------------------
@@ -252,7 +254,7 @@ namespace FlaxEngine
/// <summary>
/// Gets a value indicting whether this instance is normalized.
/// </summary>
public bool IsNormalized => Mathf.IsOne(X * X + Y * Y + Z * Z);
public bool IsNormalized => Mathr.IsOne(X * X + Y * Y + Z * Z);
/// <summary>
/// Gets the normalized vector. Returned vector has length equal 1.
@@ -270,22 +272,22 @@ namespace FlaxEngine
/// <summary>
/// Gets a value indicting whether this vector is zero
/// </summary>
public bool IsZero => Mathf.IsZero(X) && Mathf.IsZero(Y) && Mathf.IsZero(Z);
public bool IsZero => Mathr.IsZero(X) && Mathr.IsZero(Y) && Mathr.IsZero(Z);
/// <summary>
/// Gets a value indicting whether this vector is one
/// </summary>
public bool IsOne => Mathf.IsOne(X) && Mathf.IsOne(Y) && Mathf.IsOne(Z);
public bool IsOne => Mathr.IsOne(X) && Mathr.IsOne(Y) && Mathr.IsOne(Z);
/// <summary>
/// Gets a minimum component value
/// </summary>
public Real MinValue => Mathf.Min(X, Mathf.Min(Y, Z));
public Real MinValue => Mathr.Min(X, Mathr.Min(Y, Z));
/// <summary>
/// Gets a maximum component value
/// </summary>
public Real MaxValue => Mathf.Max(X, Mathf.Max(Y, Z));
public Real MaxValue => Mathr.Max(X, Mathr.Max(Y, Z));
/// <summary>
/// Gets an arithmetic average value of all vector components.
@@ -300,7 +302,7 @@ namespace FlaxEngine
/// <summary>
/// Gets a vector with values being absolute values of that vector.
/// </summary>
public Vector3 Absolute => new Vector3(Mathf.Abs(X), Mathf.Abs(Y), Mathf.Abs(Z));
public Vector3 Absolute => new Vector3(Mathr.Abs(X), Mathr.Abs(Y), Mathr.Abs(Z));
/// <summary>
/// Gets a vector with values being opposite to values of that vector.
@@ -363,8 +365,8 @@ namespace FlaxEngine
/// </summary>
public void Normalize()
{
Real length = Length;
if (!Mathf.IsZero(length))
Real length = (Real)Math.Sqrt(X * X + Y * Y + Z * Z);
if (length >= Mathr.Epsilon)
{
Real inv = 1.0f / length;
X *= inv;
@@ -1020,9 +1022,9 @@ namespace FlaxEngine
/// <remarks>Passing <paramref name="amount" /> a value of 0 will cause <paramref name="start" /> to be returned; a value of 1 will cause <paramref name="end" /> to be returned.</remarks>
public static void Lerp(ref Vector3 start, ref Vector3 end, float amount, out Vector3 result)
{
result.X = Mathf.Lerp(start.X, end.X, amount);
result.Y = Mathf.Lerp(start.Y, end.Y, amount);
result.Z = Mathf.Lerp(start.Z, end.Z, amount);
result.X = Mathr.Lerp(start.X, end.X, amount);
result.Y = Mathr.Lerp(start.Y, end.Y, amount);
result.Z = Mathr.Lerp(start.Z, end.Z, amount);
}
/// <summary>
@@ -1048,7 +1050,7 @@ namespace FlaxEngine
/// <param name="result">When the method completes, contains the cubic interpolation of the two vectors.</param>
public static void SmoothStep(ref Vector3 start, ref Vector3 end, float amount, out Vector3 result)
{
amount = Mathf.SmoothStep(amount);
amount = Mathr.SmoothStep(amount);
Lerp(ref start, ref end, amount, out result);
}
@@ -1210,7 +1212,7 @@ namespace FlaxEngine
public static Vector3 Project(Vector3 vector, Vector3 onNormal)
{
Real sqrMag = Dot(onNormal, onNormal);
if (sqrMag < Mathf.Epsilon)
if (sqrMag < Mathr.Epsilon)
return Zero;
return onNormal * Dot(vector, onNormal) / sqrMag;
}
@@ -1234,10 +1236,10 @@ namespace FlaxEngine
/// <returns>The angle (in degrees).</returns>
public static Real Angle(Vector3 from, Vector3 to)
{
Real dot = Mathf.Clamp(Dot(from.Normalized, to.Normalized), -1.0f, 1.0f);
if (Mathf.Abs(dot) > (1.0f - Mathf.Epsilon))
Real dot = Mathr.Clamp(Dot(from.Normalized, to.Normalized), -1.0f, 1.0f);
if (Mathr.Abs(dot) > (1.0f - Mathr.Epsilon))
return dot > 0.0f ? 0.0f : 180.0f;
return (Real)Math.Acos(dot) * Mathf.RadiansToDegrees;
return (Real)Math.Acos(dot) * Mathr.RadiansToDegrees;
}
/// <summary>
@@ -1825,7 +1827,7 @@ namespace FlaxEngine
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vector3 left, Vector3 right)
{
return Mathf.NearEqual(left.X, right.X) && Mathf.NearEqual(left.Y, right.Y) && Mathf.NearEqual(left.Z, right.Z);
return Mathr.NearEqual(left.X, right.X) && Mathr.NearEqual(left.Y, right.Y) && Mathr.NearEqual(left.Z, right.Z);
}
/// <summary>
@@ -1837,7 +1839,7 @@ namespace FlaxEngine
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Vector3 left, Vector3 right)
{
return !Mathf.NearEqual(left.X, right.X) || !Mathf.NearEqual(left.Y, right.Y) || !Mathf.NearEqual(left.Z, right.Z);
return !Mathr.NearEqual(left.X, right.X) || !Mathr.NearEqual(left.Y, right.Y) || !Mathr.NearEqual(left.Z, right.Z);
}
/// <summary>
@@ -1946,7 +1948,7 @@ namespace FlaxEngine
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(ref Vector3 other)
{
return Mathf.NearEqual(other.X, X) && Mathf.NearEqual(other.Y, Y) && Mathf.NearEqual(other.Z, Z);
return Mathr.NearEqual(other.X, X) && Mathr.NearEqual(other.Y, Y) && Mathr.NearEqual(other.Z, Z);
}
/// <summary>
@@ -1957,7 +1959,7 @@ namespace FlaxEngine
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Vector3 other)
{
return Mathf.NearEqual(other.X, X) && Mathf.NearEqual(other.Y, Y) && Mathf.NearEqual(other.Z, Z);
return Mathr.NearEqual(other.X, X) && Mathr.NearEqual(other.Y, Y) && Mathr.NearEqual(other.Z, Z);
}
/// <summary>
@@ -1967,7 +1969,7 @@ namespace FlaxEngine
/// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
public override bool Equals(object value)
{
return value is Vector3 other && Mathf.NearEqual(other.X, X) && Mathf.NearEqual(other.Y, Y) && Mathf.NearEqual(other.Z, Z);
return value is Vector3 other && Mathr.NearEqual(other.X, X) && Mathr.NearEqual(other.Y, Y) && Mathr.NearEqual(other.Z, Z);
}
}
}

View File

@@ -266,9 +266,9 @@ public:
void Normalize()
{
const T length = Math::Sqrt(X * X + Y * Y + Z * Z);
if (Math::Abs(length) >= ZeroTolerance)
if (length >= ZeroTolerance)
{
const T inv = 1.0f / length;
const T inv = (T)1.0f / length;
X *= inv;
Y *= inv;
Z *= inv;
@@ -645,9 +645,9 @@ public:
{
Vector3Base r = v;
const T length = Math::Sqrt(r.X * r.X + r.Y * r.Y + r.Z * r.Z);
if (Math::Abs(length) >= ZeroTolerance)
if (length >= ZeroTolerance)
{
const T inv = 1.0f / length;
const T inv = (T)1.0f / length;
r.X *= inv;
r.Y *= inv;
r.Z *= inv;

View File

@@ -2,8 +2,10 @@
#if USE_LARGE_WORLDS
using Real = System.Double;
using Mathr = FlaxEngine.Mathd;
#else
using Real = System.Single;
using Mathr = FlaxEngine.Mathf;
#endif
// -----------------------------------------------------------------------------
@@ -255,27 +257,27 @@ namespace FlaxEngine
/// <summary>
/// Gets a value indicting whether this instance is normalized.
/// </summary>
public bool IsNormalized => Mathf.IsOne(X * X + Y * Y + Z * Z + W * W);
public bool IsNormalized => Mathr.IsOne(X * X + Y * Y + Z * Z + W * W);
/// <summary>
/// Gets a value indicting whether this vector is zero
/// </summary>
public bool IsZero => Mathf.IsZero(X) && Mathf.IsZero(Y) && Mathf.IsZero(Z) && Mathf.IsZero(W);
public bool IsZero => Mathr.IsZero(X) && Mathr.IsZero(Y) && Mathr.IsZero(Z) && Mathr.IsZero(W);
/// <summary>
/// Gets a value indicting whether this vector is one
/// </summary>
public bool IsOne => Mathf.IsOne(X) && Mathf.IsOne(Y) && Mathf.IsOne(Z) && Mathf.IsOne(W);
public bool IsOne => Mathr.IsOne(X) && Mathr.IsOne(Y) && Mathr.IsOne(Z) && Mathr.IsOne(W);
/// <summary>
/// Gets a minimum component value
/// </summary>
public Real MinValue => Mathf.Min(X, Mathf.Min(Y, Mathf.Min(Z, W)));
public Real MinValue => Mathr.Min(X, Mathr.Min(Y, Mathr.Min(Z, W)));
/// <summary>
/// Gets a maximum component value
/// </summary>
public Real MaxValue => Mathf.Max(X, Mathf.Max(Y, Mathf.Max(Z, W)));
public Real MaxValue => Mathr.Max(X, Mathr.Max(Y, Mathr.Max(Z, W)));
/// <summary>
/// Gets an arithmetic average value of all vector components.
@@ -290,7 +292,7 @@ namespace FlaxEngine
/// <summary>
/// Gets a vector with values being absolute values of that vector.
/// </summary>
public Vector4 Absolute => new Vector4(Mathf.Abs(X), Mathf.Abs(Y), Mathf.Abs(Z), Mathf.Abs(W));
public Vector4 Absolute => new Vector4(Mathr.Abs(X), Mathr.Abs(Y), Mathr.Abs(Z), Mathr.Abs(W));
/// <summary>
/// Gets a vector with values being opposite to values of that vector.
@@ -357,8 +359,8 @@ namespace FlaxEngine
/// </summary>
public void Normalize()
{
Real length = Length;
if (!Mathf.IsZero(length))
Real length = (Real)Math.Sqrt(X * X + Y * Y + Z * Z + W * W);
if (length >= Mathr.Epsilon)
{
Real inverse = 1.0f / length;
X *= inverse;
@@ -855,10 +857,10 @@ namespace FlaxEngine
/// <remarks>Passing <paramref name="amount" /> a value of 0 will cause <paramref name="start" /> to be returned; a value of 1 will cause <paramref name="end" /> to be returned.</remarks>
public static void Lerp(ref Vector4 start, ref Vector4 end, Real amount, out Vector4 result)
{
result.X = Mathf.Lerp(start.X, end.X, amount);
result.Y = Mathf.Lerp(start.Y, end.Y, amount);
result.Z = Mathf.Lerp(start.Z, end.Z, amount);
result.W = Mathf.Lerp(start.W, end.W, amount);
result.X = Mathr.Lerp(start.X, end.X, amount);
result.Y = Mathr.Lerp(start.Y, end.Y, amount);
result.Z = Mathr.Lerp(start.Z, end.Z, amount);
result.W = Mathr.Lerp(start.W, end.W, amount);
}
/// <summary>
@@ -884,7 +886,7 @@ namespace FlaxEngine
/// <param name="result">When the method completes, contains the cubic interpolation of the two vectors.</param>
public static void SmoothStep(ref Vector4 start, ref Vector4 end, Real amount, out Vector4 result)
{
amount = Mathf.SmoothStep(amount);
amount = Mathr.SmoothStep(amount);
Lerp(ref start, ref end, amount, out result);
}
@@ -1359,7 +1361,7 @@ namespace FlaxEngine
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vector4 left, Vector4 right)
{
return Mathf.NearEqual(left.X, right.X) && Mathf.NearEqual(left.Y, right.Y) && Mathf.NearEqual(left.Z, right.Z) && Mathf.NearEqual(left.W, right.W);
return Mathr.NearEqual(left.X, right.X) && Mathr.NearEqual(left.Y, right.Y) && Mathr.NearEqual(left.Z, right.Z) && Mathr.NearEqual(left.W, right.W);
}
/// <summary>
@@ -1480,7 +1482,7 @@ namespace FlaxEngine
/// <returns><c>true</c> if the specified <see cref="Vector4" /> is equal to this instance; otherwise, <c>false</c>.</returns>
public bool Equals(ref Vector4 other)
{
return Mathf.NearEqual(other.X, X) && Mathf.NearEqual(other.Y, Y) && Mathf.NearEqual(other.Z, Z) && Mathf.NearEqual(other.W, W);
return Mathr.NearEqual(other.X, X) && Mathr.NearEqual(other.Y, Y) && Mathr.NearEqual(other.Z, Z) && Mathr.NearEqual(other.W, W);
}
/// <summary>
@@ -1491,7 +1493,7 @@ namespace FlaxEngine
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Vector4 other)
{
return Mathf.NearEqual(other.X, X) && Mathf.NearEqual(other.Y, Y) && Mathf.NearEqual(other.Z, Z) && Mathf.NearEqual(other.W, W);
return Mathr.NearEqual(other.X, X) && Mathr.NearEqual(other.Y, Y) && Mathr.NearEqual(other.Z, Z) && Mathr.NearEqual(other.W, W);
}
/// <summary>
@@ -1501,7 +1503,7 @@ namespace FlaxEngine
/// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
public override bool Equals(object value)
{
return value is Vector4 other && Mathf.NearEqual(other.X, X) && Mathf.NearEqual(other.Y, Y) && Mathf.NearEqual(other.Z, Z) && Mathf.NearEqual(other.W, W);
return value is Vector4 other && Mathr.NearEqual(other.X, X) && Mathr.NearEqual(other.Y, Y) && Mathr.NearEqual(other.Z, Z) && Mathr.NearEqual(other.W, W);
}
}
}