From 523d961f28b5da81aa3cd62edbe608ad8d981f8b Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 18 May 2022 22:30:50 +0200 Subject: [PATCH] Add various improvements for C# math library --- Source/Engine/Core/Math/Mathd.cs | 30 ++++--------------- Source/Engine/Core/Math/Mathf.cs | 29 +++++------------- .../Math/TypeConverters/Vector2Converter.cs | 3 +- .../Math/TypeConverters/Vector3Converter.cs | 3 +- .../Math/TypeConverters/Vector4Converter.cs | 3 +- Source/Engine/Core/Math/Vector3.cs | 6 ++-- 6 files changed, 21 insertions(+), 53 deletions(-) diff --git a/Source/Engine/Core/Math/Mathd.cs b/Source/Engine/Core/Math/Mathd.cs index 447eb4a9d..e46137b99 100644 --- a/Source/Engine/Core/Math/Mathd.cs +++ b/Source/Engine/Core/Math/Mathd.cs @@ -14,7 +14,7 @@ namespace FlaxEngine /// /// The value for which all absolute numbers smaller than are considered equal to zero. /// - public const double Epsilon = 1e-7f; + public const double Epsilon = 1e-300; /// /// A value specifying the approximation of π which is 180 degrees. @@ -721,21 +721,12 @@ namespace FlaxEngine } /// - /// Checks if a and b are almost equals, taking into account the magnitude of floating point numbers (unlike - /// method). See Remarks. - /// See remarks. + /// Checks if a and b are almost equals, taking into account the magnitude of floating point numbers (unlike method). See Remarks. See remarks. /// /// The left value to compare. /// The right value to compare. /// true if a almost equal to b, false otherwise - /// - /// The code is using the technique described by Bruce Dawson in - /// - /// Comparing - /// Floating point numbers 2012 edition - /// - /// . - /// + /// The code is using the technique described by Bruce Dawson in Comparing Floating point numbers 2012 edition. public static unsafe bool NearEqual(double a, double b) { // Check if the numbers are really close -- needed @@ -888,17 +879,14 @@ namespace FlaxEngine public static double UnwindRadians(double angle) { // TODO: make it faster? - while (angle > Pi) { angle -= TwoPi; } - while (angle < -Pi) { angle += TwoPi; } - return angle; } @@ -910,17 +898,14 @@ namespace FlaxEngine public static double UnwindDegrees(double angle) { // TODO: make it faster? - while (angle > 180.0f) { angle -= 360.0f; } - while (angle < -180.0f) { angle += 360.0f; } - return angle; } @@ -962,9 +947,7 @@ namespace FlaxEngine /// Value between 0 and 1 indicating interpolation amount. public static double SmoothStep(double amount) { - return amount <= 0d ? 0d - : amount >= 1d ? 1d - : amount * amount * (3d - 2d * amount); + return amount <= 0d ? 0d : amount >= 1d ? 1d : amount * amount * (3d - 2d * amount); } /// @@ -976,9 +959,7 @@ namespace FlaxEngine /// Value between 0 and 1 indicating interpolation amount. public static double SmootherStep(double amount) { - return amount <= 0d ? 0d - : amount >= 1d ? 1d - : amount * amount * amount * (amount * (amount * 6d - 15d) + 10d); + return amount <= 0d ? 0d : amount >= 1d ? 1d : amount * amount * amount * (amount * (amount * 6d - 15d) + 10d); } /// @@ -991,7 +972,6 @@ namespace FlaxEngine { if (modulo == 0d) return value; - return value % modulo; } diff --git a/Source/Engine/Core/Math/Mathf.cs b/Source/Engine/Core/Math/Mathf.cs index ad82635a1..10e42f831 100644 --- a/Source/Engine/Core/Math/Mathf.cs +++ b/Source/Engine/Core/Math/Mathf.cs @@ -65,7 +65,7 @@ namespace FlaxEngine /// public static float Acos(float f) { - return (float)Math.Acos(f); + return (float)Mathf.Acos(f); } /// @@ -1092,17 +1092,14 @@ namespace FlaxEngine public static float UnwindRadians(float angle) { // TODO: make it faster? - while (angle > Pi) { angle -= TwoPi; } - while (angle < -Pi) { angle += TwoPi; } - return angle; } @@ -1114,17 +1111,14 @@ namespace FlaxEngine public static float UnwindDegrees(float angle) { // TODO: make it faster? - while (angle > 180.0f) { angle -= 360.0f; } - while (angle < -180.0f) { angle += 360.0f; } - return angle; } @@ -1204,8 +1198,7 @@ namespace FlaxEngine /// Interpolates between two values using a linear function by a given amount. /// /// - /// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and - /// http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/ + /// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/ /// /// Value to interpolate from. /// Value to interpolate to. @@ -1220,8 +1213,7 @@ namespace FlaxEngine /// Interpolates between two values using a linear function by a given amount. /// /// - /// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and - /// http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/ + /// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/ /// /// Value to interpolate from. /// Value to interpolate to. @@ -1236,8 +1228,7 @@ namespace FlaxEngine /// Interpolates between two values using a linear function by a given amount. /// /// - /// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and - /// http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/ + /// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/ /// /// Value to interpolate from. /// Value to interpolate to. @@ -1252,8 +1243,7 @@ namespace FlaxEngine /// Interpolates between two values using a linear function by a given amount. /// /// - /// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and - /// http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/ + /// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/ /// /// Value to interpolate from. /// Value to interpolate to. @@ -1273,9 +1263,7 @@ namespace FlaxEngine /// Value between 0 and 1 indicating interpolation amount. public static float SmoothStep(float amount) { - return amount <= 0 ? 0 - : amount >= 1 ? 1 - : amount * amount * (3 - 2 * amount); + return amount <= 0 ? 0 : amount >= 1 ? 1 : amount * amount * (3 - 2 * amount); } /// @@ -1287,9 +1275,7 @@ namespace FlaxEngine /// Value between 0 and 1 indicating interpolation amount. public static float SmootherStep(float amount) { - return amount <= 0 ? 0 - : amount >= 1 ? 1 - : amount * amount * amount * (amount * (amount * 6 - 15) + 10); + return amount <= 0 ? 0 : amount >= 1 ? 1 : amount * amount * amount * (amount * (amount * 6 - 15) + 10); } /// @@ -1302,7 +1288,6 @@ namespace FlaxEngine { if (modulo == 0.0f) return value; - return value % modulo; } diff --git a/Source/Engine/Core/Math/TypeConverters/Vector2Converter.cs b/Source/Engine/Core/Math/TypeConverters/Vector2Converter.cs index 965307f53..df4ed0cac 100644 --- a/Source/Engine/Core/Math/TypeConverters/Vector2Converter.cs +++ b/Source/Engine/Core/Math/TypeConverters/Vector2Converter.cs @@ -36,7 +36,8 @@ namespace FlaxEngine.TypeConverters { if (destinationType == typeof(string)) { - return ((Vector2)value).X + "," + ((Vector2)value).Y; + var v = (Vector2)value; + return v.X + "," + v.Y; } return base.ConvertTo(context, culture, value, destinationType); diff --git a/Source/Engine/Core/Math/TypeConverters/Vector3Converter.cs b/Source/Engine/Core/Math/TypeConverters/Vector3Converter.cs index 1f4205cc1..08831120b 100644 --- a/Source/Engine/Core/Math/TypeConverters/Vector3Converter.cs +++ b/Source/Engine/Core/Math/TypeConverters/Vector3Converter.cs @@ -36,7 +36,8 @@ namespace FlaxEngine.TypeConverters { if (destinationType == typeof(string)) { - return ((Vector3)value).X + "," + ((Vector3)value).Y + "," + ((Vector3)value).Z; + var v = (Vector3)value; + return v.X + "," + v.Y + "," + v.Z; } return base.ConvertTo(context, culture, value, destinationType); diff --git a/Source/Engine/Core/Math/TypeConverters/Vector4Converter.cs b/Source/Engine/Core/Math/TypeConverters/Vector4Converter.cs index 977607fe5..b59e80816 100644 --- a/Source/Engine/Core/Math/TypeConverters/Vector4Converter.cs +++ b/Source/Engine/Core/Math/TypeConverters/Vector4Converter.cs @@ -36,7 +36,8 @@ namespace FlaxEngine.TypeConverters { if (destinationType == typeof(string)) { - return ((Vector4)value).X + "," + ((Vector4)value).Y + "," + ((Vector4)value).Z + "," + ((Vector4)value).W; + var v = (Vector4)value; + return v.X + "," + v.Y + "," + v.Z + "," + v.W; } return base.ConvertTo(context, culture, value, destinationType); diff --git a/Source/Engine/Core/Math/Vector3.cs b/Source/Engine/Core/Math/Vector3.cs index e9ec2b0c1..4868a2d4f 100644 --- a/Source/Engine/Core/Math/Vector3.cs +++ b/Source/Engine/Core/Math/Vector3.cs @@ -1204,9 +1204,9 @@ namespace FlaxEngine /// The angle (in degrees). public static float Angle(Vector3 from, Vector3 to) { - float dot = Mathf.Clamp(Dot(from.Normalized, to.Normalized), -1F, 1F); - if (Mathf.Abs(dot) > (1F - Mathf.Epsilon)) - return dot > 0F ? 0F : 180F; + float dot = Mathf.Clamp(Dot(from.Normalized, to.Normalized), -1.0f, 1.0f); + if (Mathf.Abs(dot) > (1.0f - Mathf.Epsilon)) + return dot > 0.0f ? 0.0f : 180.0f; return Mathf.Acos(dot) * Mathf.RadiansToDegrees; }