Merge branch 'NoriteSC-MathLib'

This commit is contained in:
2023-07-26 19:33:13 +02:00
3 changed files with 149 additions and 43 deletions
+39 -20
View File
@@ -876,38 +876,54 @@ namespace FlaxEngine
/// <summary>
/// Given a heading which may be outside the +/- PI range, 'unwind' it back into that range.
/// </summary>
/// <remarks>Optimized version of <see cref="UnwindRadiansAccurate"/> that is it faster and has fixed cost but with large angle values (100 for example) starts to lose accuracy floating point problem.</remarks>
/// <param name="angle">Angle in radians to unwind.</param>
/// <returns>Valid angle in radians.</returns>
public static double UnwindRadians(double angle)
{
// TODO: make it faster?
var a = angle - Math.Floor(angle / TwoPi) * TwoPi; // Loop function between 0 and TwoPi
return a > Pi ? a - TwoPi : a; // Change range so it become Pi and -Pi
}
/// <summary>
/// The same as <see cref="UnwindRadians"/> but is more computation intensive with large <see href="angle"/> and has better accuracy with large <see href="angle"/>.
/// <br>cost of this function is <see href="angle"/> % <see cref="Pi"/></br>
/// </summary>
/// <param name="angle">Angle in radians to unwind.</param>
/// <returns>Valid angle in radians.</returns>
public static double UnwindRadiansAccurate(double angle)
{
while (angle > Pi)
{
angle -= TwoPi;
}
while (angle < -Pi)
{
angle += TwoPi;
}
return angle;
}
/// <summary>
/// Utility to ensure angle is between +/- 180 degrees by unwinding
/// Utility to ensure angle is between +/- 180 degrees by unwinding.
/// </summary>
/// <remarks>Optimized version of <see cref="UnwindDegreesAccurate"/> that is it faster and has fixed cost but with large angle values (100 for example) starts to lose accuracy floating point problem.</remarks>
/// <param name="angle">Angle in degrees to unwind.</param>
/// <returns>Valid angle in degrees.</returns>
public static double UnwindDegrees(double angle)
{
// TODO: make it faster?
while (angle > 180.0f)
{
angle -= 360.0f;
}
while (angle < -180.0f)
{
angle += 360.0f;
}
var a = angle - Math.Floor(angle / 360.0) * 360.0; // Loop function between 0 and 360
return a > 180 ? a - 360.0 : a; // Change range so it become 180 and -180
}
/// <summary>
/// The same as <see cref="UnwindDegrees"/> but is more computation intensive with large <see href="angle"/> and has better accuracy with large <see href="angle"/>.
/// <br>cost of this function is <see href="angle"/> % 180.0f</br>
/// </summary>
/// <param name="angle">Angle in radians to unwind.</param>
/// <returns>Valid angle in radians.</returns>
public static double UnwindDegreesAccurate(double angle)
{
while (angle > 180.0)
angle -= 360.0;
while (angle < -180.0)
angle += 360.0;
return angle;
}
@@ -927,8 +943,9 @@ namespace FlaxEngine
/// Interpolates between two values using a linear function by a given amount.
/// </summary>
/// <remarks>
/// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and
/// http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
/// See:
/// <br><seealso href="http://www.encyclopediaofmath.org/index.php/Linear_interpolation"/></br>
/// <br><seealso href="http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/"/></br>
/// </remarks>
/// <param name="from">Value to interpolate from.</param>
/// <param name="to">Value to interpolate to.</param>
@@ -944,7 +961,8 @@ namespace FlaxEngine
/// Performs smooth (cubic Hermite) interpolation between 0 and 1.
/// </summary>
/// <remarks>
/// See https://en.wikipedia.org/wiki/Smoothstep
/// See:
/// <br><seealso href="https://en.wikipedia.org/wiki/Smoothstep"/></br>
/// </remarks>
/// <param name="amount">Value between 0 and 1 indicating interpolation amount.</param>
public static double SmoothStep(double amount)
@@ -956,7 +974,8 @@ namespace FlaxEngine
/// Performs a smooth(er) interpolation between 0 and 1 with 1st and 2nd order derivatives of zero at endpoints.
/// </summary>
/// <remarks>
/// See https://en.wikipedia.org/wiki/Smoothstep
/// See:
/// <br><seealso href="https://en.wikipedia.org/wiki/Smoothstep"/></br>
/// </remarks>
/// <param name="amount">Value between 0 and 1 indicating interpolation amount.</param>
public static double SmootherStep(double amount)
@@ -1013,7 +1032,7 @@ namespace FlaxEngine
/// <summary>
/// Gauss function.
/// http://en.wikipedia.org/wiki/Gaussian_function#Two-dimensional_Gaussian_function
/// <br><seealso href="http://en.wikipedia.org/wiki/Gaussian_function#Two-dimensional_Gaussian_function"/></br>
/// </summary>
/// <param name="amplitude">Curve amplitude.</param>
/// <param name="x">Position X.</param>
+63 -23
View File
@@ -1176,38 +1176,54 @@ namespace FlaxEngine
/// <summary>
/// Given a heading which may be outside the +/- PI range, 'unwind' it back into that range.
/// </summary>
/// <remarks>Optimized version of <see cref="UnwindRadiansAccurate"/> that is it faster and has fixed cost but with large angle values (100 for example) starts to lose accuracy floating point problem.</remarks>
/// <param name="angle">Angle in radians to unwind.</param>
/// <returns>Valid angle in radians.</returns>
public static float UnwindRadians(float angle)
{
// TODO: make it faster?
var a = angle - (float)Math.Floor(angle / TwoPi) * TwoPi; // Loop function between 0 and TwoPi
return a > Pi ? a - TwoPi : a; // Change range so it become Pi and -Pi
}
/// <summary>
/// The same as <see cref="UnwindRadians"/> but is more computation intensive with large <see href="angle"/> and has better accuracy with large <see href="angle"/>.
/// <br>cost of this function is <see href="angle"/> % <see cref="Pi"/></br>
/// </summary>
/// <param name="angle">Angle in radians to unwind.</param>
/// <returns>Valid angle in radians.</returns>
public static float UnwindRadiansAccurate(float angle)
{
while (angle > Pi)
{
angle -= TwoPi;
}
while (angle < -Pi)
{
angle += TwoPi;
}
return angle;
}
/// <summary>
/// Utility to ensure angle is between +/- 180 degrees by unwinding
/// Utility to ensure angle is between +/- 180 degrees by unwinding.
/// </summary>
/// <remarks>Optimized version of <see cref="UnwindDegreesAccurate"/> that is it faster and has fixed cost but with large angle values (100 for example) starts to lose accuracy floating point problem.</remarks>
/// <param name="angle">Angle in degrees to unwind.</param>
/// <returns>Valid angle in degrees.</returns>
public static float UnwindDegrees(float angle)
{
// TODO: make it faster?
var a = angle - (float)Math.Floor(angle / 360.0f) * 360.0f; // Loop function between 0 and 360
return a > 180 ? a - 360.0f : a; // Change range so it become 180 and -180
}
/// <summary>
/// The same as <see cref="UnwindDegrees"/> but is more computation intensive with large <see href="angle"/> and has better accuracy with large <see href="angle"/>.
/// <br>cost of this function is <see href="angle"/> % 180.0f</br>
/// </summary>
/// <param name="angle">Angle in radians to unwind.</param>
/// <returns>Valid angle in radians.</returns>
public static float UnwindDegreesAccurate(float angle)
{
while (angle > 180.0f)
{
angle -= 360.0f;
}
while (angle < -180.0f)
{
angle += 360.0f;
}
return angle;
}
@@ -1299,8 +1315,12 @@ namespace FlaxEngine
/// <summary>
/// Interpolates between two values using a linear function by a given amount.
/// </summary>
/// <remarks>See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/</remarks>
/// <param name="from">Value to interpolate from.</param>
/// <remarks>
/// See:
/// <br><seealso href="http://www.encyclopediaofmath.org/index.php/Linear_interpolation"/></br>
/// <br><seealso href="http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/"/></br>
/// </remarks>
/// /// <param name="from">Value to interpolate from.</param>
/// <param name="to">Value to interpolate to.</param>
/// <param name="amount">Interpolation amount.</param>
/// <returns>The result of linear interpolation of values based on the amount.</returns>
@@ -1312,8 +1332,12 @@ namespace FlaxEngine
/// <summary>
/// Interpolates between two values using a linear function by a given amount.
/// </summary>
/// <remarks>See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/</remarks>
/// <param name="from">Value to interpolate from.</param>
/// <remarks>
/// See:
/// <br><seealso href="http://www.encyclopediaofmath.org/index.php/Linear_interpolation"/></br>
/// <br><seealso href="http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/"/></br>
/// </remarks>
/// /// <param name="from">Value to interpolate from.</param>
/// <param name="to">Value to interpolate to.</param>
/// <param name="amount">Interpolation amount.</param>
/// <returns>The result of linear interpolation of values based on the amount.</returns>
@@ -1325,8 +1349,12 @@ namespace FlaxEngine
/// <summary>
/// Interpolates between two values using a linear function by a given amount.
/// </summary>
/// <remarks>See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/</remarks>
/// <param name="from">Value to interpolate from.</param>
/// <remarks>
/// See:
/// <br><seealso href="http://www.encyclopediaofmath.org/index.php/Linear_interpolation"/></br>
/// <br><seealso href="http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/"/></br>
/// </remarks>
/// /// <param name="from">Value to interpolate from.</param>
/// <param name="to">Value to interpolate to.</param>
/// <param name="amount">Interpolation amount.</param>
/// <returns>The result of linear interpolation of values based on the amount.</returns>
@@ -1338,7 +1366,10 @@ namespace FlaxEngine
/// <summary>
/// Performs smooth (cubic Hermite) interpolation between 0 and 1.
/// </summary>
/// <remarks>See https://en.wikipedia.org/wiki/Smoothstep</remarks>
/// <remarks>
/// See:
/// <br><seealso href="https://en.wikipedia.org/wiki/Smoothstep"/></br>
/// </remarks>
/// <param name="amount">Value between 0 and 1 indicating interpolation amount.</param>
public static float SmoothStep(float amount)
{
@@ -1348,7 +1379,10 @@ namespace FlaxEngine
/// <summary>
/// Performs smooth (cubic Hermite) interpolation between 0 and 1.
/// </summary>
/// <remarks>See https://en.wikipedia.org/wiki/Smoothstep</remarks>
/// <remarks>
/// See:
/// <br><seealso href="https://en.wikipedia.org/wiki/Smoothstep"/></br>
/// </remarks>
/// <param name="amount">Value between 0 and 1 indicating interpolation amount.</param>
public static double SmoothStep(double amount)
{
@@ -1358,7 +1392,10 @@ namespace FlaxEngine
/// <summary>
/// Performs a smooth(er) interpolation between 0 and 1 with 1st and 2nd order derivatives of zero at endpoints.
/// </summary>
/// <remarks>See https://en.wikipedia.org/wiki/Smoothstep</remarks>
/// <remarks>
/// See:
/// <br><seealso href="https://en.wikipedia.org/wiki/Smoothstep"/></br>
/// </remarks>
/// <param name="amount">Value between 0 and 1 indicating interpolation amount.</param>
public static float SmootherStep(float amount)
{
@@ -1368,7 +1405,10 @@ namespace FlaxEngine
/// <summary>
/// Performs a smooth(er) interpolation between 0 and 1 with 1st and 2nd order derivatives of zero at endpoints.
/// </summary>
/// <remarks>See https://en.wikipedia.org/wiki/Smoothstep</remarks>
/// <remarks>
/// See:
/// <br><seealso href="https://en.wikipedia.org/wiki/Smoothstep"/></br>
/// </remarks>
/// <param name="amount">Value between 0 and 1 indicating interpolation amount.</param>
public static double SmootherStep(double amount)
{
@@ -1446,7 +1486,7 @@ namespace FlaxEngine
/// <summary>
/// Gauss function.
/// http://en.wikipedia.org/wiki/Gaussian_function#Two-dimensional_Gaussian_function
/// <br><seealso href="http://en.wikipedia.org/wiki/Gaussian_function#Two-dimensional_Gaussian_function"/></br>
/// </summary>
/// <param name="amplitude">Curve amplitude.</param>
/// <param name="x">Position X.</param>
@@ -1463,7 +1503,7 @@ namespace FlaxEngine
/// <summary>
/// Gauss function.
/// http://en.wikipedia.org/wiki/Gaussian_function#Two-dimensional_Gaussian_function
/// <br><seealso href="http://en.wikipedia.org/wiki/Gaussian_function#Two-dimensional_Gaussian_function"/></br>
/// </summary>
/// <param name="amplitude">Curve amplitude.</param>
/// <param name="x">Position X.</param>
+47
View File
@@ -0,0 +1,47 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#if FLAX_TESTS
using NUnit.Framework;
namespace FlaxEngine.Tests
{
/// <summary>
/// Tests for <see cref="Mathf"/> and <see cref="Mathd"/>.
/// </summary>
[TestFixture]
public class TestMath
{
/// <summary>
/// Test unwinding angles.
/// </summary>
[Test]
public void TestUnwind()
{
Assert.AreEqual(0.0f, Mathf.UnwindDegreesAccurate(0.0f));
Assert.AreEqual(45.0f, Mathf.UnwindDegreesAccurate(45.0f));
Assert.AreEqual(90.0f, Mathf.UnwindDegreesAccurate(90.0f));
Assert.AreEqual(180.0f, Mathf.UnwindDegreesAccurate(180.0f));
Assert.AreEqual(0.0f, Mathf.UnwindDegreesAccurate(360.0f));
Assert.AreEqual(0.0f, Mathf.UnwindDegrees(0.0f));
Assert.AreEqual(45.0f, Mathf.UnwindDegrees(45.0f));
Assert.AreEqual(90.0f, Mathf.UnwindDegrees(90.0f));
Assert.AreEqual(180.0f, Mathf.UnwindDegrees(180.0f));
Assert.AreEqual(0.0f, Mathf.UnwindDegrees(360.0f));
var fError = 0.001f;
var dError = 0.00001;
for (float f = -400.0f; f <= 400.0f; f += 0.1f)
{
var f1 = Mathf.UnwindDegreesAccurate(f);
var f2 = Mathf.UnwindDegrees(f);
if (Mathf.Abs(f1 - f2) >= fError)
throw new Exception($"Failed on angle={f}, {f1} != {f2}");
var d = (double)f;
var d1 = Mathd.UnwindDegreesAccurate(d);
var d2 = Mathd.UnwindDegrees(d);
if (Mathd.Abs(d1 - d2) >= dError)
throw new Exception($"Failed on angle={d}, {d1} != {d2}");
}
}
}
}
#endif