Merge FloatMath and MathHelper (#1234)

Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
Visne
2020-08-20 19:21:07 +02:00
committed by GitHub
parent 21d493a6ec
commit 9d1bec9bb2
31 changed files with 295 additions and 213 deletions

View File

@@ -97,15 +97,15 @@ namespace Robust.Client.Animations
case Vector4 vector4:
return Vector4.Lerp(vector4, (Vector4) b, t);
case float f:
return FloatMath.Lerp(f, (float) b, t);
return MathHelper.Lerp(f, (float) b, t);
case double d:
return FloatMath.Lerp(d, (double) b, t);
return MathHelper.Lerp(d, (double) b, t);
case Angle angle:
return (Angle) FloatMath.Lerp(angle, (Angle) b, t);
return (Angle) MathHelper.Lerp(angle, (Angle) b, t);
case Color color:
return Color.InterpolateBetween(color, (Color) b, t);
case int i:
return (int) FloatMath.Lerp((double) i, (int) b, t);
return (int) MathHelper.Lerp((double) i, (int) b, t);
default:
// Fall back to "previous" interpolation, treating this as a discrete value.
return a;
@@ -123,11 +123,11 @@ namespace Robust.Client.Animations
case Vector4 vector4:
return Vector4.InterpolateCubic((Vector4) preA, vector4, (Vector4) b, (Vector4) postB, t);
case float f:
return FloatMath.InterpolateCubic((float) preA, f, (float) b, (float) postB, t);
return MathHelper.InterpolateCubic((float) preA, f, (float) b, (float) postB, t);
case double d:
return FloatMath.InterpolateCubic((double) preA, d, (double) b, (double) postB, t);
return MathHelper.InterpolateCubic((double) preA, d, (double) b, (double) postB, t);
case int i:
return (int) FloatMath.InterpolateCubic((int) preA, (double) i, (int) b, (int) postB, t);
return (int) MathHelper.InterpolateCubic((int) preA, (double) i, (int) b, (int) postB, t);
default:
// Fall back to "previous" interpolation, treating this as a discrete value.
return a;

View File

@@ -217,7 +217,7 @@ namespace Robust.Client.Debugging
public override Color CalcWakeColor(Color color, float wakePercent)
{
var percent = FloatMath.Clamp(wakePercent, 0, 1);
var percent = MathHelper.Clamp(wakePercent, 0, 1);
var r = 1 - (percent * (1 - color.R));
var g = 1 - (percent * (1 - color.G));

View File

@@ -81,7 +81,7 @@ namespace Robust.Client.GameObjects.EntitySystems
var currentDir = currentEye.Rotation.ToVec();
var dot = Vector2.Dot(closestDir, currentDir);
if (FloatMath.CloseTo(dot, 1, CameraSnapTolerance))
if (MathHelper.CloseTo(dot, 1, CameraSnapTolerance))
{
currentEye.Rotation = closestDir.ToAngle();
}

View File

@@ -380,13 +380,13 @@ namespace Robust.Client.Graphics.Clyde
lightShader.SetUniformTextureMaybe(UniIMainTexture, TextureUnit.Texture0);
}
if (!FloatMath.CloseTo(lastRange, component.Radius))
if (!MathHelper.CloseTo(lastRange, component.Radius))
{
lastRange = component.Radius;
lightShader.SetUniformMaybe("lightRange", lastRange);
}
if (!FloatMath.CloseTo(lastPower, component.Energy))
if (!MathHelper.CloseTo(lastPower, component.Energy))
{
lastPower = component.Energy;
lightShader.SetUniformMaybe("lightPower", lastPower);

View File

@@ -473,7 +473,7 @@ namespace Robust.Client.UserInterface.Controls
{
base.MouseWheel(args);
if (FloatMath.CloseTo(0, args.Delta.Y))
if (MathHelper.CloseTo(0, args.Delta.Y))
{
return;
}

View File

@@ -126,7 +126,7 @@ namespace Robust.Client.UserInterface.Controls
get => _cursorPosition;
set
{
_cursorPosition = FloatMath.Clamp(value, 0, _text.Length);
_cursorPosition = MathHelper.Clamp(value, 0, _text.Length);
_selectionStart = _cursorPosition;
}
}
@@ -134,7 +134,7 @@ namespace Robust.Client.UserInterface.Controls
public int SelectionStart
{
get => _selectionStart;
set => _selectionStart = FloatMath.Clamp(value, 0, _text.Length);
set => _selectionStart = MathHelper.Clamp(value, 0, _text.Length);
}
public int SelectionLower => Math.Min(_selectionStart, _cursorPosition);
@@ -236,7 +236,7 @@ namespace Robust.Client.UserInterface.Controls
_drawOffset += (int) Math.Ceiling(args.DeltaSeconds / MouseScrollDelay);
}
var index = GetIndexAtPos(FloatMath.Clamp(_lastMousePosition, contentBox.Left, contentBox.Right));
var index = GetIndexAtPos(MathHelper.Clamp(_lastMousePosition, contentBox.Left, contentBox.Right));
_cursorPosition = index;
}

View File

@@ -152,7 +152,7 @@ namespace Robust.Client.UserInterface.Controls
{
base.MouseWheel(args);
if (FloatMath.CloseTo(0, args.Delta.Y))
if (MathHelper.CloseTo(0, args.Delta.Y))
{
return;
}

View File

@@ -76,7 +76,7 @@ namespace Robust.Client.UserInterface.Controls
private void _ensureValueClamped()
{
var newValue = ClampValue(_value);
if (!FloatMath.CloseTo(newValue, _value))
if (!MathHelper.CloseTo(newValue, _value))
{
_value = newValue;
OnValueChanged?.Invoke(this);
@@ -86,7 +86,7 @@ namespace Robust.Client.UserInterface.Controls
[Pure]
protected float ClampValue(float value)
{
return FloatMath.Clamp(value, _minValue, _maxValue-_page);
return MathHelper.Clamp(value, _minValue, _maxValue-_page);
}
}
}

View File

@@ -51,7 +51,7 @@ namespace Robust.Client.UserInterface.Controls
get
{
var offset = ValueTarget + Page;
return offset > MaxValue || FloatMath.CloseTo(offset, MaxValue);
return offset > MaxValue || MathHelper.CloseTo(offset, MaxValue);
}
}
@@ -65,14 +65,14 @@ namespace Robust.Client.UserInterface.Controls
{
base.FrameUpdate(args);
if (!VisibleInTree || FloatMath.CloseTo(Value, ValueTarget))
if (!VisibleInTree || MathHelper.CloseTo(Value, ValueTarget))
{
Value = ValueTarget;
}
else
{
_updating = true;
Value = FloatMath.Lerp(Value, ValueTarget, args.DeltaSeconds * 15);
Value = MathHelper.Lerp(Value, ValueTarget, args.DeltaSeconds * 15);
_updating = false;
}
}

View File

@@ -117,7 +117,7 @@ namespace Robust.Client.UserInterface.Controls
/// <returns></returns>
private float ClampSplitCenter(float splitCenter, float? firstMinSize = null, float? secondMinSize = null)
{
splitCenter = FloatMath.Clamp(splitCenter, SplitMin, SplitMax);
splitCenter = MathHelper.Clamp(splitCenter, SplitMin, SplitMax);
if (ResizeMode == SplitResizeMode.RespectChildrenMinSize && ChildCount == 2)
{
@@ -128,7 +128,7 @@ namespace Robust.Client.UserInterface.Controls
secondMinSize ??= (Vertical ? second.CombinedMinimumSize.Y : second.CombinedMinimumSize.X);
var size = Vertical ? Height : Width;
splitCenter = FloatMath.Clamp(splitCenter, firstMinSize.Value, size - (secondMinSize.Value + SplitWidth));
splitCenter = MathHelper.Clamp(splitCenter, firstMinSize.Value, size - (secondMinSize.Value + SplitWidth));
}
return splitCenter;
@@ -181,7 +181,7 @@ namespace Robust.Client.UserInterface.Controls
_splitCenter = firstMinSize;
}
_splitCenter += FloatMath.Clamp(0f, firstMinSize - _splitCenter, size - secondMinSize - SplitWidth - _splitCenter);
_splitCenter += MathHelper.Clamp(0f, firstMinSize - _splitCenter, size - secondMinSize - SplitWidth - _splitCenter);
break;
}
}

View File

@@ -127,7 +127,7 @@ namespace Robust.Client.UserInterface.CustomControls
}
else
{
posY = FloatMath.Lerp(posY, targetLocation, args.DeltaSeconds * 20);
posY = MathHelper.Lerp(posY, targetLocation, args.DeltaSeconds * 20);
}
LayoutContainer.SetPosition(MainControl, (posX, posY));

View File

@@ -114,9 +114,9 @@ namespace Robust.Shared.Maths
// The second two expressions cover an edge case where one number is barely non-negative while the other number is negative.
// In this case, the negative number will get FlipPositived to ~2pi and the comparison will give a false negative.
return FloatMath.CloseTo(aPositive, bPositive)
|| FloatMath.CloseTo(aPositive + MathHelper.TwoPi, bPositive)
|| FloatMath.CloseTo(aPositive, bPositive + MathHelper.TwoPi);
return MathHelper.CloseTo(aPositive, bPositive)
|| MathHelper.CloseTo(aPositive + MathHelper.TwoPi, bPositive)
|| MathHelper.CloseTo(aPositive, bPositive + MathHelper.TwoPi);
}
private static bool EqualsApprox(Angle a, Angle b, double tolerance)
@@ -130,9 +130,9 @@ namespace Robust.Shared.Maths
// The second two expressions cover an edge case where one number is barely non-negative while the other number is negative.
// In this case, the negative number will get FlipPositived to ~2pi and the comparison will give a false negative.
return FloatMath.CloseTo(aPositive, bPositive, tolerance)
|| FloatMath.CloseTo(aPositive + MathHelper.TwoPi, bPositive, tolerance)
|| FloatMath.CloseTo(aPositive, bPositive + MathHelper.TwoPi, tolerance);
return MathHelper.CloseTo(aPositive, bPositive, tolerance)
|| MathHelper.CloseTo(aPositive + MathHelper.TwoPi, bPositive, tolerance)
|| MathHelper.CloseTo(aPositive, bPositive + MathHelper.TwoPi, tolerance);
}
/// <summary>
@@ -205,12 +205,12 @@ namespace Robust.Shared.Maths
/// </summary>
public static Angle Lerp(in Angle a, in Angle b, float factor)
{
var degA = FloatMath.RadToDeg * Angle.Reduce(a);
var degB = FloatMath.RadToDeg * Angle.Reduce(b);
var delta = FloatMath.Repeat((float) (degB - degA), 360);
var degA = MathHelper.RadiansToDegrees(Reduce(a));
var degB = MathHelper.RadiansToDegrees(Reduce(b));
var delta = MathHelper.Mod((degB - degA), 360);
if (delta > 180)
delta -= 360;
return new Angle(FloatMath.DegToRad * (degA + delta * FloatMath.Clamp01(factor)));
return new Angle(MathHelper.DegreesToRadians(degA + delta * MathHelper.Clamp(factor, 0, 1)));
}
/// <summary>

View File

@@ -174,7 +174,7 @@ namespace Robust.Shared.Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsEmpty()
{
return FloatMath.CloseTo(Width, 0.0f) && FloatMath.CloseTo(Height, 0.0f);
return MathHelper.CloseTo(Width, 0.0f) && MathHelper.CloseTo(Height, 0.0f);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -271,10 +271,10 @@ namespace Robust.Shared.Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Box2 a, Box2 b)
{
return FloatMath.CloseTo(a.Bottom, b.Bottom) &&
FloatMath.CloseTo(a.Right, b.Right) &&
FloatMath.CloseTo(a.Top, b.Top) &&
FloatMath.CloseTo(a.Left, b.Left);
return MathHelper.CloseTo(a.Bottom, b.Bottom) &&
MathHelper.CloseTo(a.Right, b.Right) &&
MathHelper.CloseTo(a.Top, b.Top) &&
MathHelper.CloseTo(a.Left, b.Left);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -326,8 +326,8 @@ namespace Robust.Shared.Maths
public Vector2 ClosestPoint(in Vector2 position)
{
// clamp the point to the border of the box
var cx = FloatMath.Clamp(position.X, Left, Right);
var cy = FloatMath.Clamp(position.Y, Bottom, Top);
var cx = MathHelper.Clamp(position.X, Left, Right);
var cy = MathHelper.Clamp(position.Y, Bottom, Top);
return new Vector2(cx, cy);
}

View File

@@ -68,8 +68,8 @@ namespace Robust.Shared.Maths
var d2 = dx * dx + dy * dy;
var r2 = Radius * Radius;
// Instead of d2 <= r2, use FloatMath.CloseTo to allow for some tolerance.
return (d2 < r2) || FloatMath.CloseTo(d2, r2);
// Instead of d2 <= r2, use MathHelper.CloseTo to allow for some tolerance.
return (d2 < r2) || MathHelper.CloseTo(d2, r2);
}
/// <inheritdoc />

View File

@@ -992,10 +992,10 @@ namespace Robust.Shared.Maths
public bool Equals(Color other)
{
return
FloatMath.CloseTo(R, other.R) &&
FloatMath.CloseTo(G, other.G) &&
FloatMath.CloseTo(B, other.B) &&
FloatMath.CloseTo(A, other.A);
MathHelper.CloseTo(R, other.R) &&
MathHelper.CloseTo(G, other.G) &&
MathHelper.CloseTo(B, other.B) &&
MathHelper.CloseTo(A, other.A);
}
[PublicAPI]

View File

@@ -3,121 +3,43 @@ using System.Runtime.CompilerServices;
namespace Robust.Shared.Maths
{
[Obsolete("Use MathHelper instead.")]
public static class FloatMath
{
public const float RadToDeg = (float) (180.0 / Math.PI);
public const float DegToRad = (float) (Math.PI / 180.0);
/// <summary>
/// Returns the largest integer smaller to or equal to f.
/// </summary>
#if NETCOREAPP
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Floor(float f) => MathF.Floor(f);
#else
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Floor(float f) => (float)Math.Floor(f);
#endif
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Clamp<T>(T val, T min, T max)
where T : IComparable<T>
public static float Clamp01(float val)
{
if (val.CompareTo(min) < 0) return min;
if (val.CompareTo(max) > 0) return max;
return val;
return MathHelper.Clamp01(val);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Clamp<T>(T val, T min, T max) where T : IComparable<T>
{
return MathHelper.Clamp(val, min, max);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Clamp(float val, float min, float max)
{
return Math.Max(Math.Min(val, max), min);
return MathHelper.Clamp(val, min, max);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Clamp(double val, double min, double max)
{
return Math.Max(Math.Min(val, max), min);
return MathHelper.Clamp(val, min, max);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CloseTo(float a, float b, double tolerance = .00001)
{
var epsilon =
Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance,
tolerance); // .001% of the smaller value for the epsilon check as per MSDN reference suggestion
return Math.Abs(a - b) <= epsilon;
return MathHelper.CloseTo(a, b, tolerance);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CloseTo(float a, double b, double tolerance = .00001)
{
var epsilon =
Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance,
tolerance); // .001% of the smaller value for the epsilon check as per MSDN reference suggestion
return Math.Abs(a - b) <= epsilon;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CloseTo(double a, float b, double tolerance = .00001)
{
var epsilon =
Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance,
tolerance); // .001% of the smaller value for the epsilon check as per MSDN reference suggestion
return Math.Abs(a - b) <= epsilon;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CloseTo(double a, double b, double tolerance = .00001)
{
var epsilon =
Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance,
tolerance); // .001% of the smaller value for the epsilon check as per MSDN reference suggestion
return Math.Abs(a - b) <= epsilon;
}
/// <summary>
/// <c>blend</c> 0 means <c>a</c>
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Lerp(double a, double b, double blend)
{
return a + (b - a) * blend;
}
/// <summary>
/// <c>blend</c> 0 means <c>a</c>
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Lerp(float a, float b, float blend)
{
return a + (b - a) * blend;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float InterpolateCubic(float preA, float a, float b, float postB, float t)
{
return a + 0.5f * t * (b - preA + t * (2.0f * preA - 5.0f * a + 4.0f * b - postB + t * (3.0f * (a - b) + postB - preA)));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double InterpolateCubic(double preA, double a, double b, double postB, double t)
{
return a + 0.5 * t * (b - preA + t * (2.0 * preA - 5.0 * a + 4.0 * b - postB + t * (3.0 * (a - b) + postB - preA)));
}
// Clamps value between 0 and 1 and returns value
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Clamp01(float value)
{
return FloatMath.Clamp(value, 0, 1);
}
// Loops the value t, so that it is never larger than length and never smaller than 0.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Repeat(float t, float length)
{
return Clamp(t - Floor(t / length) * length, 0.0f, length);
return MathHelper.Lerp(a, b, blend);
}
}
}

View File

@@ -88,7 +88,7 @@ namespace Robust.Shared.Maths
public static long NextPowerOfTwo(long n)
{
if (n <= 0) throw new ArgumentOutOfRangeException(nameof(n), "Must be positive.");
return (long)NextPowerOfTwo((double) n);
return (long) NextPowerOfTwo((double) n);
}
/// <summary>
@@ -100,7 +100,7 @@ namespace Robust.Shared.Maths
public static int NextPowerOfTwo(int n)
{
if (n <= 0) throw new ArgumentOutOfRangeException(nameof(n), "Must be positive.");
return (int)NextPowerOfTwo((double) n);
return (int) NextPowerOfTwo((double) n);
}
/// <summary>
@@ -111,9 +111,10 @@ namespace Robust.Shared.Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float NextPowerOfTwo(float n)
{
if (float.IsNaN(n) || float.IsInfinity(n)) throw new ArgumentOutOfRangeException(nameof(n), "Must be a number.");
if (float.IsNaN(n) || float.IsInfinity(n))
throw new ArgumentOutOfRangeException(nameof(n), "Must be a number.");
if (n <= 0) throw new ArgumentOutOfRangeException(nameof(n), "Must be positive.");
return (float)NextPowerOfTwo((double) n);
return (float) NextPowerOfTwo((double) n);
}
/// <summary>
@@ -124,7 +125,8 @@ namespace Robust.Shared.Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double NextPowerOfTwo(double n)
{
if (double.IsNaN(n) || double.IsInfinity(n)) throw new ArgumentOutOfRangeException(nameof(n), "Must be a number.");
if (double.IsNaN(n) || double.IsInfinity(n))
throw new ArgumentOutOfRangeException(nameof(n), "Must be a number.");
if (n <= 0) throw new ArgumentOutOfRangeException(nameof(n), "Must be positive.");
// Don't return negative powers of two, that's nonsense.
@@ -254,12 +256,18 @@ namespace Robust.Shared.Maths
#region MinMax
/// <summary>
/// Returns the minimum of 4 values
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Min(float a, float b, float c, float d)
{
return MathF.Min(a, MathF.Min(b, MathF.Min(c, d)));
}
/// <summary>
/// Returns the maximum of 4 values
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Max(float a, float b, float c, float d)
{
@@ -269,7 +277,7 @@ namespace Robust.Shared.Maths
/// <summary>
/// Returns the median value out of a, b and c.
/// </summary>
/// <returns>THe median.</returns>
/// <returns>The median.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Median(float a, float b, float c)
{
@@ -278,6 +286,8 @@ namespace Robust.Shared.Maths
#endregion MinMax
#region Mod
/// <summary>
/// This method provides floored modulus.
/// C-like languages use truncated modulus for their '%' operator.
@@ -292,6 +302,20 @@ namespace Robust.Shared.Maths
return n - Math.Floor(n / d) * d;
}
/// <summary>
/// This method provides floored modulus.
/// C-like languages use truncated modulus for their '%' operator.
/// </summary>
/// <param name="n">The dividend.</param>
/// <param name="d">The divisor.</param>
/// <returns>The remainder.</returns>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Mod(float n, float d)
{
return n - (float) Math.Floor(n / d) * d;
}
/// <summary>
/// This method provides floored modulus.
/// C-like languages use truncated modulus for their '%' operator.
@@ -307,6 +331,142 @@ namespace Robust.Shared.Maths
return r < 0 ? r + d : r;
}
#endregion Mod
/// <summary>
/// Clamps <paramref name="val"/> between <paramref name="min"/> and <paramref name="max"/>.
/// </summary>
#region Clamp
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Clamp<T>(T val, T min, T max)
where T : IComparable<T>
{
if (val.CompareTo(min) < 0) return min;
if (val.CompareTo(max) > 0) return max;
return val;
}
/// <summary>
/// Clamps <paramref name="val"/> between <paramref name="min"/> and <paramref name="max"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Clamp(float val, float min, float max)
{
return Math.Max(Math.Min(val, max), min);
}
/// <summary>
/// Clamps <paramref name="val"/> between <paramref name="min"/> and <paramref name="max"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Clamp(double val, double min, double max)
{
return Math.Max(Math.Min(val, max), min);
}
/// <summary>
/// Clamps <paramref name="val"/> between 0 and 1.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Clamp01(float val)
{
return Clamp(val, 0, 1);
}
#endregion Clamp
#region CloseTo
/// <summary>
/// Returns whether two floating point numbers are within <paramref name="tolerance"/> of eachother
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CloseTo(float a, float b, double tolerance = .00001)
{
// .001% of the smaller value for the epsilon check as per MSDN reference suggestion
double epsilon = Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance, tolerance);
return Math.Abs(a - b) <= epsilon;
}
/// <summary>
/// Returns whether two floating point numbers are within <paramref name="tolerance"/> of eachother
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CloseTo(float a, double b, double tolerance = .00001)
{
// .001% of the smaller value for the epsilon check as per MSDN reference suggestion
double epsilon = Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance, tolerance);
return Math.Abs(a - b) <= epsilon;
}
/// <summary>
/// Returns whether two floating point numbers are within <paramref name="tolerance"/> of eachother
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CloseTo(double a, float b, double tolerance = .00001)
{
// .001% of the smaller value for the epsilon check as per MSDN reference suggestion
double epsilon = Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance, tolerance);
return Math.Abs(a - b) <= epsilon;
}
/// <summary>
/// Returns whether two floating point numbers are within <paramref name="tolerance"/> of eachother
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CloseTo(double a, double b, double tolerance = .00001)
{
// .001% of the smaller value for the epsilon check as per MSDN reference suggestion
double epsilon = Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance, tolerance);
return Math.Abs(a - b) <= epsilon;
}
#endregion CloseTo
#region Lerp
/// <summary>
/// Linearly interpolates between <paramref name="a"/> to <paramref name="b"/>, returning the value at <paramref name="blend"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Lerp(double a, double b, double blend)
{
return a + (b - a) * blend;
}
/// <summary>
/// Linearly interpolates between <paramref name="a"/> to <paramref name="b"/>, returning the value at <paramref name="blend"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Lerp(float a, float b, float blend)
{
return a + (b - a) * blend;
}
#endregion Lerp
#region InterpolateCubic
/// <summary>
/// Cubic interpolates form <paramref name="a"/> to <paramref name="b"/>, where <paramref name="preA"/> and <paramref name="postB"/> are handles and returns the position at <paramref name="t"/>
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float InterpolateCubic(float preA, float a, float b, float postB, float t)
{
return a + 0.5f * t * (b - preA + t * (2.0f * preA - 5.0f * a + 4.0f * b - postB + t * (3.0f * (a - b) + postB - preA)));
}
/// <summary>
/// Cubic interpolates form <paramref name="a"/> to <paramref name="b"/>, where <paramref name="preA"/> and <paramref name="postB"/> are handles and returns the position at <paramref name="t"/>
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double InterpolateCubic(double preA, double a, double b, double postB, double t)
{
return a + 0.5 * t * (b - preA + t * (2.0 * preA - 5.0 * a + 4.0 * b - postB + t * (3.0 * (a - b) + postB - preA)));
}
#endregion InterpolateCubic
#endregion Public Members
}
}

View File

@@ -794,7 +794,7 @@ namespace Robust.Shared.Maths
//Credit: https://stackoverflow.com/a/18504573
var d = Determinant;
if (FloatMath.CloseTo(d, 0))
if (MathHelper.CloseTo(d, 0))
throw new InvalidOperationException("Matrix is singular and cannot be inverted.");
var m = this;

View File

@@ -68,7 +68,7 @@ namespace Robust.Shared.Maths
public bool IsEmpty()
{
return FloatMath.CloseTo(Width, 0.0f) && FloatMath.CloseTo(Height, 0.0f);
return MathHelper.CloseTo(Width, 0.0f) && MathHelper.CloseTo(Height, 0.0f);
}
public bool Encloses(UIBox2 inner)
@@ -149,10 +149,10 @@ namespace Robust.Shared.Maths
/// </summary>
public static bool operator ==(UIBox2 a, UIBox2 b)
{
return FloatMath.CloseTo(a.Bottom, b.Bottom) &&
FloatMath.CloseTo(a.Right, b.Right) &&
FloatMath.CloseTo(a.Top, b.Top) &&
FloatMath.CloseTo(a.Left, b.Left);
return MathHelper.CloseTo(a.Bottom, b.Bottom) &&
MathHelper.CloseTo(a.Right, b.Right) &&
MathHelper.CloseTo(a.Top, b.Top) &&
MathHelper.CloseTo(a.Left, b.Left);
}
public static bool operator !=(UIBox2 a, UIBox2 b)

View File

@@ -234,8 +234,8 @@ namespace Robust.Shared.Maths
public static Vector2 Clamp(Vector2 vector, Vector2 min, Vector2 max)
{
return new Vector2(
FloatMath.Clamp(vector.X, min.X, max.X),
FloatMath.Clamp(vector.Y, min.Y, max.Y)
MathHelper.Clamp(vector.X, min.X, max.X),
MathHelper.Clamp(vector.Y, min.Y, max.Y)
);
}
@@ -259,9 +259,9 @@ namespace Robust.Shared.Maths
{
return new Vector2(
//factor * (b.X - a.X) + a.X,
FloatMath.Lerp(a.X, b.X, factor),
MathHelper.Lerp(a.X, b.X, factor),
//factor * (b.Y - a.Y) + a.Y
FloatMath.Lerp(a.Y, b.Y, factor)
MathHelper.Lerp(a.Y, b.Y, factor)
);
}
@@ -357,13 +357,13 @@ namespace Robust.Shared.Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool EqualsApprox(Vector2 other)
{
return FloatMath.CloseTo(X, other.X) && FloatMath.CloseTo(Y, other.Y);
return MathHelper.CloseTo(X, other.X) && MathHelper.CloseTo(Y, other.Y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool EqualsApprox(Vector2 other, double tolerance)
{
return FloatMath.CloseTo(X, other.X, tolerance) && FloatMath.CloseTo(Y, other.Y, tolerance);
return MathHelper.CloseTo(X, other.X, tolerance) && MathHelper.CloseTo(Y, other.Y, tolerance);
}
}
}

View File

@@ -500,9 +500,9 @@ namespace Robust.Shared.Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Clamp(Vector3 vec, Vector3 min, Vector3 max)
{
vec.X = FloatMath.Clamp(vec.X, min.X, max.X);
vec.Y = FloatMath.Clamp(vec.Y, min.Y, max.Y);
vec.Z = FloatMath.Clamp(vec.Z, min.Z, max.Z);
vec.X = MathHelper.Clamp(vec.X, min.X, max.X);
vec.Y = MathHelper.Clamp(vec.Y, min.Y, max.Y);
vec.Z = MathHelper.Clamp(vec.Z, min.Z, max.Z);
return vec;
}
@@ -516,9 +516,9 @@ namespace Robust.Shared.Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Clamp(ref Vector3 vec, ref Vector3 min, ref Vector3 max, out Vector3 result)
{
result.X = FloatMath.Clamp(vec.X, min.X, max.X);
result.Y = FloatMath.Clamp(vec.Y, min.Y, max.Y);
result.Z = FloatMath.Clamp(vec.Z, min.Z, max.Z);
result.X = MathHelper.Clamp(vec.X, min.X, max.X);
result.Y = MathHelper.Clamp(vec.Y, min.Y, max.Y);
result.Z = MathHelper.Clamp(vec.Z, min.Z, max.Z);
}
#endregion
@@ -628,9 +628,9 @@ namespace Robust.Shared.Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Lerp(Vector3 a, Vector3 b, float blend)
{
a.X = FloatMath.Lerp(a.X, b.X, blend);
a.Y = FloatMath.Lerp(a.Y, b.Y, blend);
a.Z = FloatMath.Lerp(a.Z, b.Z, blend);
a.X = MathHelper.Lerp(a.X, b.X, blend);
a.Y = MathHelper.Lerp(a.Y, b.Y, blend);
a.Z = MathHelper.Lerp(a.Z, b.Z, blend);
return a;
}
@@ -644,9 +644,9 @@ namespace Robust.Shared.Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Lerp(ref Vector3 a, ref Vector3 b, float blend, out Vector3 result)
{
result.X = FloatMath.Lerp(a.X, b.X, blend);
result.Y = FloatMath.Lerp(a.Y, b.Y, blend);
result.Z = FloatMath.Lerp(a.Z, b.Z, blend);
result.X = MathHelper.Lerp(a.X, b.X, blend);
result.Y = MathHelper.Lerp(a.Y, b.Y, blend);
result.Z = MathHelper.Lerp(a.Z, b.Z, blend);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@@ -507,10 +507,10 @@ namespace Robust.Shared.Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector4 Clamp(Vector4 vec, Vector4 min, Vector4 max)
{
vec.X = FloatMath.Clamp( vec.X, min.X, max.X );
vec.Y = FloatMath.Clamp( vec.Y, min.Y, max.Y );
vec.Z = FloatMath.Clamp( vec.Z, min.Z, max.Z );
vec.W = FloatMath.Clamp( vec.W, min.W, max.W );
vec.X = MathHelper.Clamp( vec.X, min.X, max.X );
vec.Y = MathHelper.Clamp( vec.Y, min.Y, max.Y );
vec.Z = MathHelper.Clamp( vec.Z, min.Z, max.Z );
vec.W = MathHelper.Clamp( vec.W, min.W, max.W );
return vec;
}
@@ -524,10 +524,10 @@ namespace Robust.Shared.Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Clamp(ref Vector4 vec, ref Vector4 min, ref Vector4 max, out Vector4 result)
{
result.X = FloatMath.Clamp( vec.X, min.X, max.X );
result.Y = FloatMath.Clamp( vec.Y, min.Y, max.Y );
result.Z = FloatMath.Clamp( vec.Z, min.Z, max.Z );
result.W = FloatMath.Clamp( vec.W, min.W, max.W );
result.X = MathHelper.Clamp( vec.X, min.X, max.X );
result.Y = MathHelper.Clamp( vec.Y, min.Y, max.Y );
result.Z = MathHelper.Clamp( vec.Z, min.Z, max.Z );
result.W = MathHelper.Clamp( vec.W, min.W, max.W );
}
#endregion Clamp
@@ -623,10 +623,10 @@ namespace Robust.Shared.Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Lerp(ref Vector4 a, ref Vector4 b, float blend, out Vector4 result)
{
result.X = FloatMath.Lerp(a.X, b.X, blend);
result.Y = FloatMath.Lerp(a.Y, b.Y, blend);
result.Z = FloatMath.Lerp(a.Z, b.Z, blend);
result.W = FloatMath.Lerp(a.W, b.W, blend);
result.X = MathHelper.Lerp(a.X, b.X, blend);
result.Y = MathHelper.Lerp(a.Y, b.Y, blend);
result.Z = MathHelper.Lerp(a.Z, b.Z, blend);
result.W = MathHelper.Lerp(a.W, b.W, blend);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@@ -153,8 +153,8 @@ namespace Robust.Shared.GameObjects
foreach (var entity in _entityTreesPerMap[mapId].Query(position, approximate))
{
var transform = entity.Transform;
if (FloatMath.CloseTo(transform.GridPosition.X, position.X) &&
FloatMath.CloseTo(transform.GridPosition.Y, position.Y))
if (MathHelper.CloseTo(transform.GridPosition.X, position.X) &&
MathHelper.CloseTo(transform.GridPosition.Y, position.Y))
{
yield return entity;
}
@@ -416,8 +416,8 @@ namespace Robust.Shared.GameObjects
{
var transform = entity.Transform;
var entPos = transform.WorldPosition;
if (FloatMath.CloseTo(entPos.X, position.X)
&& FloatMath.CloseTo(entPos.Y, position.Y))
if (MathHelper.CloseTo(entPos.X, position.X)
&& MathHelper.CloseTo(entPos.Y, position.Y))
{
yield return entity;
}

View File

@@ -106,7 +106,7 @@ namespace Robust.Shared.GameObjects.Systems
var multiplier = deltaTime / (1f / 60);
var divisions = FloatMath.Clamp(
var divisions = MathHelper.Clamp(
MathF.Round(solveIterationsAt60 * multiplier, MidpointRounding.AwayFromZero),
1,
20
@@ -310,7 +310,7 @@ namespace Robust.Shared.GameObjects.Systems
private bool FixClipping(List<Manifold> collisions, float divisions)
{
const float allowance = 1 / 128f;
var percent = FloatMath.Clamp(1f / divisions, 0.01f, 1f);
var percent = MathHelper.Clamp(1f / divisions, 0.01f, 1f);
var done = true;
foreach (var collision in collisions)
{

View File

@@ -168,13 +168,13 @@ namespace Robust.Shared.Map
/// <inheritdoc />
public bool OnSnapCenter(Vector2 position)
{
return (FloatMath.CloseTo(position.X % SnapSize, 0) && FloatMath.CloseTo(position.Y % SnapSize, 0));
return (MathHelper.CloseTo(position.X % SnapSize, 0) && MathHelper.CloseTo(position.Y % SnapSize, 0));
}
/// <inheritdoc />
public bool OnSnapBorder(Vector2 position)
{
return (FloatMath.CloseTo(position.X % SnapSize, SnapSize / 2) && FloatMath.CloseTo(position.Y % SnapSize, SnapSize / 2));
return (MathHelper.CloseTo(position.X % SnapSize, SnapSize / 2) && MathHelper.CloseTo(position.Y % SnapSize, SnapSize / 2));
}
#region TileAccess

View File

@@ -437,8 +437,8 @@ namespace Robust.Shared.Physics
public Vector2 ClosestPoint(in Vector2 position)
{
// clamp the point to the border of the box
var cx = FloatMath.Clamp(position.X, Left, Right);
var cy = FloatMath.Clamp(position.Y, Bottom, Top);
var cx = MathHelper.Clamp(position.X, Left, Right);
var cy = MathHelper.Clamp(position.Y, Bottom, Top);
return new Vector2(cx, cy);
}
@@ -611,8 +611,8 @@ namespace Robust.Shared.Physics
var yMax = HalfExtents.Y;
// clamp the point to the border of the box
var cx = FloatMath.Clamp(localPoint.X, xMin, xMax);
var cy = FloatMath.Clamp(localPoint.Y, yMin, yMax);
var cx = MathHelper.Clamp(localPoint.X, xMin, xMax);
var cy = MathHelper.Clamp(localPoint.Y, yMin, yMax);
return TransformPoint(new Vector2(cx, cy));
}

View File

@@ -33,7 +33,7 @@ namespace Robust.Shared.Maths
_position = position;
_direction = direction;
DebugTools.Assert(FloatMath.CloseTo(_direction.LengthSquared, 1));
DebugTools.Assert(MathHelper.CloseTo(_direction.LengthSquared, 1));
}

View File

@@ -273,7 +273,7 @@ namespace Robust.Shared.Timing
private TimeSpan CalcTickPeriod()
{
// ranges from -1 to 1, with 0 being 'default'
var ratio = FloatMath.Clamp(_timing.TickTimingAdjustment, -0.99f, 0.99f);
var ratio = MathHelper.Clamp(_timing.TickTimingAdjustment, -0.99f, 0.99f);
var diff = TimeSpan.FromTicks((long) (_timing.TickPeriod.Ticks * ratio));
return _timing.TickPeriod - diff;
}

View File

@@ -22,20 +22,20 @@ namespace Robust.UnitTesting
{
if (Tolerance != null)
{
return new ConstraintResult(this, actual, FloatMath.CloseTo(f1, f2, Tolerance.Value));
return new ConstraintResult(this, actual, MathHelper.CloseTo(f1, f2, Tolerance.Value));
}
return new ConstraintResult(this, actual, FloatMath.CloseTo(f1, f2));
return new ConstraintResult(this, actual, MathHelper.CloseTo(f1, f2));
}
if (Expected is double d1 && actual is float d2)
{
if (Tolerance != null)
{
return new ConstraintResult(this, actual, FloatMath.CloseTo(d1, d2, Tolerance.Value));
return new ConstraintResult(this, actual, MathHelper.CloseTo(d1, d2, Tolerance.Value));
}
return new ConstraintResult(this, actual, FloatMath.CloseTo(d1, d2));
return new ConstraintResult(this, actual, MathHelper.CloseTo(d1, d2));
}
return new ConstraintResult(this, actual, false);

View File

@@ -171,8 +171,8 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
var result = childTrans.WorldPosition;
Assert.Multiple(() =>
{
Assert.That(FloatMath.CloseTo(result.X, 0));
Assert.That(FloatMath.CloseTo(result.Y, 2));
Assert.That(MathHelper.CloseTo(result.X, 0));
Assert.That(MathHelper.CloseTo(result.Y, 2));
});
}
@@ -198,8 +198,8 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
var result = childTrans.WorldPosition;
Assert.Multiple(() =>
{
Assert.That(FloatMath.CloseTo(result.X, 1));
Assert.That(FloatMath.CloseTo(result.Y, 2));
Assert.That(MathHelper.CloseTo(result.X, 1));
Assert.That(MathHelper.CloseTo(result.Y, 2));
});
}
@@ -280,8 +280,8 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
// Assert
Assert.Multiple(() =>
{
Assert.That(FloatMath.CloseTo(oldWpos.X, newWpos.Y), newWpos.ToString);
Assert.That(FloatMath.CloseTo(oldWpos.Y, newWpos.Y), newWpos.ToString);
Assert.That(MathHelper.CloseTo(oldWpos.X, newWpos.Y), newWpos.ToString);
Assert.That(MathHelper.CloseTo(oldWpos.Y, newWpos.Y), newWpos.ToString);
});
}
@@ -327,8 +327,8 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
Assert.Multiple(() =>
{
Assert.That(FloatMath.CloseTo(oldWpos.X, newWpos.Y));
Assert.That(FloatMath.CloseTo(oldWpos.Y, newWpos.Y));
Assert.That(MathHelper.CloseTo(oldWpos.X, newWpos.Y));
Assert.That(MathHelper.CloseTo(oldWpos.Y, newWpos.Y));
});
}

View File

@@ -48,8 +48,8 @@ namespace Robust.UnitTesting.Shared.Maths
var control = testCase.Item1;
var result = test.Xy;
Assert.That(FloatMath.CloseTo(control.X, result.X), Is.True, result.ToString);
Assert.That(FloatMath.CloseTo(control.Y, result.Y), Is.True, result.ToString);
Assert.That(MathHelper.CloseTo(control.X, result.X), Is.True, result.ToString);
Assert.That(MathHelper.CloseTo(control.Y, result.Y), Is.True, result.ToString);
}
[Test]
@@ -114,8 +114,8 @@ namespace Robust.UnitTesting.Shared.Maths
var result = test.Xy;
// Assert
Assert.That(FloatMath.CloseTo(result.X, 2), result.ToString);
Assert.That(FloatMath.CloseTo(result.Y, 2), result.ToString);
Assert.That(MathHelper.CloseTo(result.X, 2), result.ToString);
Assert.That(MathHelper.CloseTo(result.Y, 2), result.ToString);
}
[Test]
@@ -137,8 +137,8 @@ namespace Robust.UnitTesting.Shared.Maths
Matrix3.Transform(in invMatrix, in localPoint, out var result);
// Assert
Assert.That(FloatMath.CloseTo(startPoint.X, result.X), Is.True, result.ToString);
Assert.That(FloatMath.CloseTo(startPoint.Y, result.Y), Is.True, result.ToString);
Assert.That(MathHelper.CloseTo(startPoint.X, result.X), Is.True, result.ToString);
Assert.That(MathHelper.CloseTo(startPoint.Y, result.Y), Is.True, result.ToString);
}
}
}