mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
Replace Robust Vector2 with System.Numerics (#4092)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
@@ -37,7 +38,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="dir"></param>
|
||||
public Angle(Vector2 dir)
|
||||
{
|
||||
dir = dir.Normalized;
|
||||
dir = dir.Normalized();
|
||||
Theta = Math.Atan2(dir.Y, dir.X);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using JetBrains.Annotations;
|
||||
@@ -134,8 +135,8 @@ namespace Robust.Shared.Maths
|
||||
[Pure]
|
||||
public static Box2 FromTwoPoints(Vector2 a, Vector2 b)
|
||||
{
|
||||
var min = Vector2.ComponentMin(a, b);
|
||||
var max = Vector2.ComponentMax(a, b);
|
||||
var min = Vector2.Min(a, b);
|
||||
var max = Vector2.Max(a, b);
|
||||
|
||||
return new Box2(min, max);
|
||||
}
|
||||
@@ -427,7 +428,7 @@ namespace Robust.Shared.Maths
|
||||
&& MathHelper.CloseToPercent(Right, other.Right)
|
||||
&& MathHelper.CloseToPercent(Top, other.Top);
|
||||
}
|
||||
|
||||
|
||||
public bool EqualsApprox(Box2 other, double tolerance)
|
||||
{
|
||||
return MathHelper.CloseToPercent(Left, other.Left, tolerance)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Robust.Shared.Maths
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Robust.Shared.Maths
|
||||
{
|
||||
@@ -249,13 +250,13 @@ namespace Robust.Shared.Maths
|
||||
|
||||
private static readonly Vector2[] DirectionVectors = {
|
||||
new (0, -1),
|
||||
new Vector2(1, -1).Normalized,
|
||||
new Vector2(1, -1).Normalized(),
|
||||
new (1, 0),
|
||||
new Vector2(1, 1).Normalized,
|
||||
new Vector2(1, 1).Normalized(),
|
||||
new (0, 1),
|
||||
new Vector2(-1, 1).Normalized,
|
||||
new Vector2(-1, 1).Normalized(),
|
||||
new (-1, 0),
|
||||
new Vector2(-1, -1).Normalized
|
||||
new Vector2(-1, -1).Normalized()
|
||||
};
|
||||
|
||||
private static readonly Vector2i[] IntDirectionVectors = {
|
||||
|
||||
@@ -647,7 +647,7 @@ namespace Robust.Shared.Maths
|
||||
/// <returns></returns>
|
||||
public static bool TryGetIntersecting(Vector2 start, Vector2 end, float radius, [NotNullWhen(true)] out Vector2? point)
|
||||
{
|
||||
var maxFraction = (end - start).Length;
|
||||
var maxFraction = (end - start).Length();
|
||||
float b = Vector2.Dot(start, start) - radius * radius;
|
||||
|
||||
// Solve quadratic equation.
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
using System.Numerics;
|
||||
|
||||
namespace Robust.Shared.Maths
|
||||
{
|
||||
public struct Matrix22
|
||||
|
||||
@@ -25,6 +25,7 @@ SOFTWARE.
|
||||
#endregion --- License ---
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
using System.Numerics;
|
||||
|
||||
namespace Robust.Shared.Maths
|
||||
{
|
||||
public struct Matrix33
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Numerics;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
@@ -57,7 +58,7 @@ namespace Robust.Shared.Maths
|
||||
|
||||
public readonly Vector2 Inflate(in Vector2 size)
|
||||
{
|
||||
return (size.X + SumHorizontal, size.Y + SumVertical);
|
||||
return new(size.X + SumHorizontal, size.Y + SumVertical);
|
||||
}
|
||||
|
||||
public readonly UIBox2 Deflate(in UIBox2 box)
|
||||
@@ -74,9 +75,9 @@ namespace Robust.Shared.Maths
|
||||
|
||||
public readonly Vector2 Deflate(in Vector2 size)
|
||||
{
|
||||
return Vector2.ComponentMax(
|
||||
return Vector2.Max(
|
||||
Vector2.Zero,
|
||||
(size.X - SumHorizontal, size.Y - SumVertical));
|
||||
new(size.X - SumHorizontal, size.Y - SumVertical));
|
||||
}
|
||||
|
||||
[SuppressMessage("ReSharper", "CompareOfFloatsByEqualityOperator")]
|
||||
@@ -88,13 +89,13 @@ namespace Robust.Shared.Maths
|
||||
Bottom == other.Bottom;
|
||||
}
|
||||
|
||||
public override readonly bool Equals(object? obj)
|
||||
public readonly override bool Equals(object? obj)
|
||||
{
|
||||
return obj is Thickness other && Equals(other);
|
||||
}
|
||||
|
||||
[SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")]
|
||||
public override readonly int GetHashCode()
|
||||
public readonly override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(Left, Top, Right, Bottom);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
@@ -1,488 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Robust.Shared.Maths
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a float vector with two components (x, y).
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
[Serializable]
|
||||
public struct Vector2 : IEquatable<Vector2>, IApproxEquatable<Vector2>, ISpanFormattable
|
||||
{
|
||||
/// <summary>
|
||||
/// The X component of the vector.
|
||||
/// </summary>
|
||||
public float X;
|
||||
|
||||
/// <summary>
|
||||
/// The Y component of the vector.
|
||||
/// </summary>
|
||||
public float Y;
|
||||
|
||||
/// <summary>
|
||||
/// A zero length vector.
|
||||
/// </summary>
|
||||
public static readonly Vector2 Zero = new(0, 0);
|
||||
|
||||
/// <summary>
|
||||
/// A vector with all components set to 1.
|
||||
/// </summary>
|
||||
public static readonly Vector2 One = new(1, 1);
|
||||
|
||||
/// <summary>
|
||||
/// A unit vector pointing in the +X direction.
|
||||
/// </summary>
|
||||
public static readonly Vector2 UnitX = new(1, 0);
|
||||
|
||||
/// <summary>
|
||||
/// A unit vector pointing in the +Y direction.
|
||||
/// </summary>
|
||||
public static readonly Vector2 UnitY = new(0, 1);
|
||||
|
||||
public static readonly Vector2 Infinity = new(float.PositiveInfinity, float.PositiveInfinity);
|
||||
|
||||
/// <summary>
|
||||
/// A vector with NaN X and Y.
|
||||
/// </summary>
|
||||
public static readonly Vector2 NaN = new(float.NaN, float.NaN);
|
||||
|
||||
/// <summary>
|
||||
/// Construct a vector from its coordinates.
|
||||
/// </summary>
|
||||
/// <param name="x">X coordinate</param>
|
||||
/// <param name="y">Y coordinate</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Vector2(float x, float y)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the length (magnitude) of the vector.
|
||||
/// </summary>
|
||||
public readonly float Length
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => MathF.Sqrt(LengthSquared);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the squared length of the vector.
|
||||
/// </summary>
|
||||
public readonly float LengthSquared
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => X * X + Y * Y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes this vector if its length > 0, otherwise sets it to 0.
|
||||
/// </summary>
|
||||
public float Normalize()
|
||||
{
|
||||
var length = Length;
|
||||
|
||||
if (length < float.Epsilon)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
var invLength = 1f / length;
|
||||
X *= invLength;
|
||||
Y *= invLength;
|
||||
return length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new, normalized, vector.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public readonly Vector2 Normalized
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
var length = Length;
|
||||
return new Vector2(X / length, Y / length);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new, rotated 90 degrees clockwise (in world Y-up orientation), vector.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public readonly Vector2 Rotated90DegreesClockwiseWorld
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
return new Vector2(Y, -X);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new, rotated 90 degrees anticlockwise (in world Y-up orientation), vector.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public readonly Vector2 Rotated90DegreesAnticlockwiseWorld
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
return new Vector2(-Y, X);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly Vector2 Rounded()
|
||||
{
|
||||
return new(MathF.Round(X), MathF.Round(Y));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly Vector2i Floored()
|
||||
{
|
||||
return new((int) MathF.Floor(X), (int) MathF.Floor(Y));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly Vector2i Ceiled()
|
||||
{
|
||||
return new((int) MathF.Ceiling(X), (int) MathF.Ceiling(Y));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subtracts a vector from another, returning a new vector.
|
||||
/// </summary>
|
||||
/// <param name="a">Vector to subtract from.</param>
|
||||
/// <param name="b">Vector to subtract with.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 operator -(Vector2 a, Vector2 b)
|
||||
{
|
||||
return new(a.X - b.X, a.Y - b.Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subtracts a scalar with each component of a vector, returning a new vecotr..
|
||||
/// </summary>
|
||||
/// <param name="a">Vector to subtract from.</param>
|
||||
/// <param name="b">Scalar to subtract with.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 operator -(Vector2 a, float b)
|
||||
{
|
||||
return new(a.X - b, a.Y - b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Negates a vector.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 operator -(Vector2 vec)
|
||||
{
|
||||
return new(-vec.X, -vec.Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds two vectors together, returning a new vector with the components of each added together.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 operator +(Vector2 a, Vector2 b)
|
||||
{
|
||||
return new(a.X + b.X, a.Y + b.Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a scalar to each component of a vector, returning a new vector.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 operator +(Vector2 a, float b)
|
||||
{
|
||||
return new(a.X + b, a.Y + b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Multiply a vector by a scale by multiplying the individual components.
|
||||
/// </summary>
|
||||
/// <param name="vec">The vector to multiply.</param>
|
||||
/// <param name="scale">The scale to multiply with.</param>
|
||||
/// <returns>A new vector.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 operator *(Vector2 vec, float scale)
|
||||
{
|
||||
return new(vec.X * scale, vec.Y * scale);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Multiplies a vector's components corresponding to a vector scale.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 operator *(Vector2 vec, Vector2 scale)
|
||||
{
|
||||
return new(vec.X * scale.X, vec.Y * scale.Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Divide a vector by a scale by dividing the individual components.
|
||||
/// </summary>
|
||||
/// <param name="vec">The vector to divide.</param>
|
||||
/// <param name="scale">The scale to divide by.</param>
|
||||
/// <returns>A new vector.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 operator /(Vector2 vec, float scale)
|
||||
{
|
||||
return new(vec.X / scale, vec.Y / scale);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Divides a vector's components corresponding to a vector scale.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 operator /(Vector2 vec, Vector2 scale)
|
||||
{
|
||||
return new(vec.X / scale.X, vec.Y / scale.Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return a vector made up of the smallest components of the provided vectors.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 ComponentMin(Vector2 a, Vector2 b)
|
||||
{
|
||||
return new(
|
||||
MathF.Min(a.X, b.X),
|
||||
MathF.Min(a.Y, b.Y)
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return a vector made up of the largest components of the provided vectors.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 ComponentMax(Vector2 a, Vector2 b)
|
||||
{
|
||||
return new(
|
||||
MathF.Max(a.X, b.X),
|
||||
MathF.Max(a.Y, b.Y)
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the vector with the smallest magnitude. If both have equal magnitude, <paramref name="b" /> is selected.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 MagnitudeMin(Vector2 a, Vector2 b)
|
||||
{
|
||||
return a.LengthSquared < b.LengthSquared ? a : b;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the vector with the largest magnitude. If both have equal magnitude, <paramref name="a" /> is selected.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 MagnitudeMax(Vector2 a, Vector2 b)
|
||||
{
|
||||
return a.LengthSquared >= b.LengthSquared ? a : b;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clamps the components of a vector to minimum and maximum vectors.
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to clamp.</param>
|
||||
/// <param name="min">The lower bound vector.</param>
|
||||
/// <param name="max">The upper bound vector.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 Clamp(Vector2 vector, Vector2 min, Vector2 max)
|
||||
{
|
||||
return new(
|
||||
MathHelper.Clamp(vector.X, min.X, max.X),
|
||||
MathHelper.Clamp(vector.Y, min.Y, max.Y)
|
||||
);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 Abs(in Vector2 a)
|
||||
{
|
||||
return new(Math.Abs(a.X), Math.Abs(a.Y));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the dot product of two vectors.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static float Dot(Vector2 a, Vector2 b)
|
||||
{
|
||||
return a.X * b.X + a.Y * b.Y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Perform the cross product on two vectors. In 2D this produces a scalar.
|
||||
/// </summary>
|
||||
public static float Cross(in Vector2 a, in Vector2 b)
|
||||
{
|
||||
return a.X * b.Y - a.Y * b.X;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Perform the cross product on a vector and a scalar. In 2D this produces
|
||||
/// a vector.
|
||||
/// </summary>
|
||||
public static Vector2 Cross(in Vector2 a, float s)
|
||||
{
|
||||
return new(s * a.Y, -s * a.X);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Perform the cross product on a scalar and a vector. In 2D this produces
|
||||
/// a vector.
|
||||
/// </summary>
|
||||
public static Vector2 Cross(float s, in Vector2 a)
|
||||
{
|
||||
return new(-s * a.Y, s * a.X);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates two vectors so make a mix based on a factor.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// a when factor=0, b when factor=1, a linear interpolation between the two otherwise.
|
||||
/// </returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 Lerp(Vector2 a, Vector2 b, float factor)
|
||||
{
|
||||
return new(
|
||||
//factor * (b.X - a.X) + a.X,
|
||||
MathHelper.Lerp(a.X, b.X, factor),
|
||||
//factor * (b.Y - a.Y) + a.Y
|
||||
MathHelper.Lerp(a.Y, b.Y, factor)
|
||||
);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 LerpClamped(in Vector2 a, in Vector2 b, float factor)
|
||||
{
|
||||
if (factor <= 0)
|
||||
return a;
|
||||
|
||||
if (factor >= 1)
|
||||
return b;
|
||||
|
||||
return Lerp(a, b, factor);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 InterpolateCubic(Vector2 preA, Vector2 a, Vector2 b, Vector2 postB, float t)
|
||||
{
|
||||
return a + (b - preA + (preA * 2.0f - a * 5.0f + b * 4.0f - postB + ((a - b) * 3.0f + postB - preA) * t) * t) * t * 0.5f;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void Deconstruct(out float x, out float y)
|
||||
{
|
||||
x = X;
|
||||
y = Y;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator Vector2((float x, float y) tuple)
|
||||
{
|
||||
var (x, y) = tuple;
|
||||
return new Vector2(x, y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a string that represents the current Vector2.
|
||||
/// </summary>
|
||||
public override readonly string ToString()
|
||||
{
|
||||
return $"({X}, {Y})";
|
||||
}
|
||||
|
||||
public readonly string ToString(string? format, IFormatProvider? formatProvider)
|
||||
{
|
||||
return ToString();
|
||||
}
|
||||
|
||||
public readonly bool TryFormat(
|
||||
Span<char> destination,
|
||||
out int charsWritten,
|
||||
ReadOnlySpan<char> format,
|
||||
IFormatProvider? provider)
|
||||
{
|
||||
return FormatHelpers.TryFormatInto(
|
||||
destination,
|
||||
out charsWritten,
|
||||
$"({X}, {Y})");
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator ==(Vector2 a, Vector2 b)
|
||||
{
|
||||
return a.Equals(b);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator !=(Vector2 a, Vector2 b)
|
||||
{
|
||||
return !a.Equals(b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compare a vector to another vector and check if they are equal.
|
||||
/// </summary>
|
||||
/// <param name="other">Other vector to check.</param>
|
||||
/// <returns>True if the two vectors are equal.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly bool Equals(Vector2 other)
|
||||
{
|
||||
// ReSharper disable CompareOfFloatsByEqualityOperator
|
||||
return X == other.X && Y == other.Y;
|
||||
// ReSharper restore CompareOfFloatsByEqualityOperator
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compare a vector to an object and check if they are equal.
|
||||
/// </summary>
|
||||
/// <param name="obj">Other object to check.</param>
|
||||
/// <returns>True if Object and vector are equal.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override readonly bool Equals(object? obj)
|
||||
{
|
||||
return obj is Vector2 vec && Equals(vec);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the hash code for this instance.
|
||||
/// </summary>
|
||||
/// <returns>A unique hash code for this instance.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override readonly int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
return (X.GetHashCode() * 397) ^ Y.GetHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly bool EqualsApprox(Vector2 other)
|
||||
{
|
||||
return MathHelper.CloseTo(X, other.X) && MathHelper.CloseTo(Y, other.Y);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly bool EqualsApprox(Vector2 other, double tolerance)
|
||||
{
|
||||
return MathHelper.CloseTo(X, other.X, tolerance) && MathHelper.CloseTo(Y, other.Y, tolerance);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly bool EqualsApproxPercent(Vector2 other, double tolerance = 0.0001)
|
||||
{
|
||||
return MathHelper.CloseToPercent(X, other.X, tolerance) && MathHelper.CloseToPercent(Y, other.Y, tolerance);
|
||||
}
|
||||
}
|
||||
}
|
||||
118
Robust.Shared.Maths/Vector2Helpers.cs
Normal file
118
Robust.Shared.Maths/Vector2Helpers.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Robust.Shared.Maths;
|
||||
|
||||
public static class Vector2Helpers
|
||||
{
|
||||
public static readonly Vector2 Infinity = new(float.PositiveInfinity, float.PositiveInfinity);
|
||||
public static readonly Vector2 NaN = new(float.NaN, float.NaN);
|
||||
|
||||
/// <summary>
|
||||
/// Half of a unit vector.
|
||||
/// </summary>
|
||||
public static readonly Vector2 Half = new(0.5f, 0.5f);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 InterpolateCubic(Vector2 preA, Vector2 a, Vector2 b, Vector2 postB, float t)
|
||||
{
|
||||
return a + (b - preA + (preA * 2.0f - a * 5.0f + b * 4.0f - postB + ((a - b) * 3.0f + postB - preA) * t) * t) * t * 0.5f;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool EqualsApprox(this Vector2 vec, Vector2 otherVec)
|
||||
{
|
||||
return MathHelper.CloseTo(vec.X, otherVec.X) && MathHelper.CloseTo(vec.Y, otherVec.Y);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool EqualsApprox(this Vector2 vec, Vector2 otherVec, double tolerance)
|
||||
{
|
||||
return MathHelper.CloseTo(vec.X, otherVec.X, tolerance) && MathHelper.CloseTo(vec.Y, otherVec.Y, tolerance);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 Normalized(this Vector2 vec)
|
||||
{
|
||||
var length = vec.Length();
|
||||
return new Vector2(vec.X / length, vec.Y / length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes this vector if its length > 0, otherwise sets it to 0.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static float Normalize(ref this Vector2 vec)
|
||||
{
|
||||
var length = vec.Length();
|
||||
|
||||
if (length < float.Epsilon)
|
||||
{
|
||||
vec = Vector2.Zero;
|
||||
return 0f;
|
||||
}
|
||||
|
||||
var invLength = 1f / length;
|
||||
vec.X *= invLength;
|
||||
vec.Y *= invLength;
|
||||
return length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Perform the cross product on a scalar and a vector. In 2D this produces
|
||||
/// a vector.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 Cross(float s, in Vector2 a)
|
||||
{
|
||||
return new(-s * a.Y, s * a.X);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Perform the cross product on a scalar and a vector. In 2D this produces
|
||||
/// a vector.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 Cross(in Vector2 a, in float s)
|
||||
{
|
||||
return new(s * a.Y, -s * a.X);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static float Cross(Vector2 a, Vector2 b)
|
||||
{
|
||||
return a.X * b.Y - a.Y * b.X;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2i Floored(this Vector2 vec)
|
||||
{
|
||||
return new Vector2i((int) MathF.Floor(vec.X), (int) MathF.Floor(vec.Y));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2i Ceiled(this Vector2 vec)
|
||||
{
|
||||
return new Vector2i((int) MathF.Ceiling(vec.X), (int) MathF.Ceiling(vec.Y));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 Rounded(this Vector2 vec)
|
||||
{
|
||||
return new Vector2(MathF.Round(vec.X), MathF.Round(vec.Y));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Deconstruct(this Vector2 vec, out float x, out float y)
|
||||
{
|
||||
x = vec.X;
|
||||
y = vec.Y;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool EqualsApproxPercent(this Vector2 vec, Vector2 other, double tolerance = 0.0001)
|
||||
{
|
||||
return MathHelper.CloseToPercent(vec.X, other.X, tolerance) && MathHelper.CloseToPercent(vec.Y, other.Y, tolerance);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Robust.Shared.Maths
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
@@ -29,6 +29,7 @@ using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Xml.Serialization;
|
||||
using Robust.Shared.Utility;
|
||||
using Vector2 = System.Numerics.Vector2;
|
||||
|
||||
namespace Robust.Shared.Maths
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user