mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
Matrix3 and Angle: add readonly and ref->in (#2919)
* Matrix3 and Angle readonly and ref->in * fix precision
This commit is contained in:
@@ -1357,7 +1357,7 @@ namespace Robust.Client.GameObjects
|
||||
// However, at some point later the eye-matrix is applied separately, so we subtract -eye rotation for now:
|
||||
var entityMatrix = Matrix3.CreateTransform(worldPosition, NoRotation ? -eyeRotation : worldRotation);
|
||||
|
||||
Matrix3.Multiply(ref LocalMatrix, ref entityMatrix, out var transform);
|
||||
Matrix3.Multiply(in LocalMatrix, in entityMatrix, out var transform);
|
||||
|
||||
var angle = worldRotation + eyeRotation; // angle on-screen. Used to decide the direction of 4/8 directional RSIs
|
||||
foreach (var layer in Layers)
|
||||
@@ -2030,7 +2030,7 @@ namespace Robust.Client.GameObjects
|
||||
layerDrawMatrix = LocalMatrix;
|
||||
else
|
||||
{
|
||||
Matrix3.Multiply(ref _rsiDirectionMatrices[(int)dir], ref LocalMatrix, out layerDrawMatrix);
|
||||
Matrix3.Multiply(in _rsiDirectionMatrices[(int)dir], in LocalMatrix, out layerDrawMatrix);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2060,7 +2060,7 @@ namespace Robust.Client.GameObjects
|
||||
|
||||
// Set the drawing transform for this layer
|
||||
GetLayerDrawMatrix(dir, out var layerMatrix);
|
||||
Matrix3.Multiply(ref layerMatrix, ref spriteMatrix, out var transformMatrix);
|
||||
Matrix3.Multiply(in layerMatrix, in spriteMatrix, out var transformMatrix);
|
||||
drawingHandle.SetTransform(in transformMatrix);
|
||||
|
||||
// The direction used to draw the sprite can differ from the one that the angle would naively suggest,
|
||||
|
||||
@@ -882,7 +882,7 @@ namespace Robust.Client.Graphics.Clyde
|
||||
// Second modification is that output must be fov-centred (difference-space)
|
||||
uZero -= fovCentre;
|
||||
|
||||
var clipToDiff = new Matrix3(ref uX, ref uY, ref uZero);
|
||||
var clipToDiff = new Matrix3(in uX, in uY, in uZero);
|
||||
|
||||
fovShader.SetUniformMaybe("clipToDiff", clipToDiff);
|
||||
_drawQuad(Vector2.Zero, Vector2.One, Matrix3.Identity, fovShader);
|
||||
|
||||
@@ -54,14 +54,14 @@ namespace Robust.Shared.Maths
|
||||
/// where an angle of zero is usually considered "south" (0, -1).
|
||||
/// </remarks>
|
||||
/// <returns>Unit Direction Vector</returns>
|
||||
public Vector2 ToVec()
|
||||
public readonly Vector2 ToVec()
|
||||
{
|
||||
var x = Math.Cos(Theta);
|
||||
var y = Math.Sin(Theta);
|
||||
return new Vector2((float) x, (float) y);
|
||||
}
|
||||
|
||||
public Vector2 ToWorldVec()
|
||||
public readonly Vector2 ToWorldVec()
|
||||
{
|
||||
return (this - MathHelper.PiOver2).ToVec();
|
||||
}
|
||||
@@ -69,7 +69,7 @@ namespace Robust.Shared.Maths
|
||||
private const double Segment = 2 * Math.PI / 8.0; // Cut the circle into 8 pieces
|
||||
private const double Offset = Segment / 2.0; // offset the pieces by 1/2 their size
|
||||
|
||||
public Direction GetDir()
|
||||
public readonly Direction GetDir()
|
||||
{
|
||||
var ang = Theta % (2 * Math.PI);
|
||||
|
||||
@@ -82,7 +82,7 @@ namespace Robust.Shared.Maths
|
||||
private const double CardinalSegment = 2 * Math.PI / 4.0; // Cut the circle into 4 pieces
|
||||
private const double CardinalOffset = CardinalSegment / 2.0; // offset the pieces by 1/2 their size
|
||||
|
||||
public Direction GetCardinalDir()
|
||||
public readonly Direction GetCardinalDir()
|
||||
{
|
||||
var ang = Theta % (2 * Math.PI);
|
||||
|
||||
@@ -98,7 +98,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="vec">Vector to rotate.</param>
|
||||
/// <returns>New rotated vector.</returns>
|
||||
[Pure]
|
||||
public Vector2 RotateVec(in Vector2 vec)
|
||||
public readonly Vector2 RotateVec(in Vector2 vec)
|
||||
{
|
||||
// No calculation necessery when theta is zero
|
||||
if (Theta == 0) return vec;
|
||||
@@ -157,7 +157,7 @@ namespace Robust.Shared.Maths
|
||||
/// <summary>
|
||||
/// Removes revolutions from a positive or negative angle to make it as small as possible.
|
||||
/// </summary>
|
||||
public Angle Reduced()
|
||||
public readonly Angle Reduced()
|
||||
{
|
||||
return new(Reduce(Theta));
|
||||
}
|
||||
@@ -173,20 +173,20 @@ namespace Robust.Shared.Maths
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Equals(Angle other)
|
||||
public readonly bool Equals(Angle other)
|
||||
{
|
||||
return this.Theta.Equals(other.Theta);
|
||||
return Theta.Equals(other.Theta);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj)
|
||||
public readonly override bool Equals(object? obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj)) return false;
|
||||
return obj is Angle angle && Equals(angle);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int GetHashCode()
|
||||
public readonly override int GetHashCode()
|
||||
{
|
||||
|
||||
return Theta.GetHashCode();
|
||||
@@ -203,12 +203,12 @@ namespace Robust.Shared.Maths
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
public Angle Opposite()
|
||||
public readonly Angle Opposite()
|
||||
{
|
||||
return new Angle(FlipPositive(Theta-Math.PI));
|
||||
}
|
||||
|
||||
public Angle FlipPositive()
|
||||
public readonly Angle FlipPositive()
|
||||
{
|
||||
return new(FlipPositive(Theta));
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ namespace Robust.Shared.Maths
|
||||
public float this[int row, int column]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
readonly get
|
||||
{
|
||||
switch (row)
|
||||
{
|
||||
@@ -173,7 +173,7 @@ namespace Robust.Shared.Maths
|
||||
public float this[int index]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
readonly get
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
@@ -230,7 +230,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="matrix">The matrix to convert.</param>
|
||||
/// <returns>An array of floats for the matrix.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator float[](Matrix3 matrix)
|
||||
public static explicit operator float[](in Matrix3 matrix)
|
||||
{
|
||||
return new[]
|
||||
{
|
||||
@@ -253,7 +253,7 @@ namespace Robust.Shared.Maths
|
||||
/// <summary>Constructs left matrix with the same components as the given matrix.</summary>
|
||||
/// <param name="matrix">The matrix whose components to copy.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Matrix3(ref Matrix3 matrix)
|
||||
public Matrix3(in Matrix3 matrix)
|
||||
{
|
||||
R0C0 = matrix.R0C0;
|
||||
R0C1 = matrix.R0C1;
|
||||
@@ -324,7 +324,7 @@ namespace Robust.Shared.Maths
|
||||
/// </summary>
|
||||
/// <param name="matrix">A Matrix4 to take the upper-left 3x3 from.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Matrix3(Matrix4 matrix)
|
||||
public Matrix3(in Matrix4 matrix)
|
||||
{
|
||||
R0C0 = matrix.Row0.X;
|
||||
R0C1 = matrix.Row0.Y;
|
||||
@@ -346,7 +346,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="y">A Vector2 for the second, conventionally Y basis, vector.</param>
|
||||
/// <param name="origin">A Vector2 for the third, conventionally origin vector.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Matrix3(ref Vector2 x, ref Vector2 y, ref Vector2 origin)
|
||||
public Matrix3(in Vector2 x, in Vector2 y, in Vector2 origin)
|
||||
{
|
||||
R0C0 = x.X;
|
||||
R0C1 = y.X;
|
||||
@@ -492,7 +492,7 @@ namespace Robust.Shared.Maths
|
||||
/// <summary>Indicates whether the current matrix is equal to another matrix.</summary>
|
||||
/// <param name="matrix">The Matrix3 structure to compare with.</param>
|
||||
/// <returns>true if the current matrix is equal to the matrix parameter; otherwise, false.</returns>
|
||||
public bool Equals(Matrix3 other)
|
||||
public readonly bool Equals(Matrix3 other)
|
||||
{
|
||||
return
|
||||
R0C0 == other.R0C0 &&
|
||||
@@ -509,7 +509,7 @@ namespace Robust.Shared.Maths
|
||||
/// <summary>Indicates whether the current matrix is equal to another matrix.</summary>
|
||||
/// <param name="matrix">The Matrix3 structure to compare to.</param>
|
||||
/// <returns>true if the current matrix is equal to the matrix parameter; otherwise, false.</returns>
|
||||
public bool Equals(ref Matrix3 matrix)
|
||||
public readonly bool Equals(in Matrix3 matrix)
|
||||
{
|
||||
return
|
||||
R0C0 == matrix.R0C0 &&
|
||||
@@ -527,7 +527,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="left">The left-hand operand.</param>
|
||||
/// <param name="right">The right-hand operand.</param>
|
||||
/// <returns>true if the current matrix is equal to the matrix parameter; otherwise, false.</returns>
|
||||
public static bool Equals(ref Matrix3 left, ref Matrix3 right)
|
||||
public static bool Equals(in Matrix3 left, in Matrix3 right)
|
||||
{
|
||||
return
|
||||
left.R0C0 == right.R0C0 &&
|
||||
@@ -544,20 +544,20 @@ namespace Robust.Shared.Maths
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool EqualsApprox(Matrix3 other)
|
||||
{
|
||||
return EqualsApprox(ref other, 1.0E-6f);
|
||||
return EqualsApprox(in other, 1.0E-6f);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool EqualsApprox(Matrix3 other, double tolerance)
|
||||
{
|
||||
return EqualsApprox(ref other, (float) tolerance);
|
||||
return EqualsApprox(in other, (float) tolerance);
|
||||
}
|
||||
|
||||
/// <summary>Indicates whether the current matrix is approximately equal to another matrix.</summary>
|
||||
/// <param name="matrix">The Matrix3 structure to compare with.</param>
|
||||
/// <param name="tolerance">The limit below which the matrices are considered equal.</param>
|
||||
/// <returns>true if the current matrix is approximately equal to the matrix parameter; otherwise, false.</returns>
|
||||
public bool EqualsApprox(ref Matrix3 matrix, float tolerance)
|
||||
public readonly bool EqualsApprox(in Matrix3 matrix, float tolerance)
|
||||
{
|
||||
return
|
||||
Math.Abs(R0C0 - matrix.R0C0) <= tolerance &&
|
||||
@@ -576,7 +576,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="right">The right-hand operand.</param>
|
||||
/// <param name="tolerance">The limit below which the matrices are considered equal.</param>
|
||||
/// <returns>true if the current matrix is approximately equal to the matrix parameter; otherwise, false.</returns>
|
||||
public static bool EqualsApprox(ref Matrix3 left, ref Matrix3 right, float tolerance)
|
||||
public static bool EqualsApprox(in Matrix3 left, in Matrix3 right, float tolerance)
|
||||
{
|
||||
return
|
||||
Math.Abs(left.R0C0 - right.R0C0) <= tolerance &&
|
||||
@@ -597,7 +597,7 @@ namespace Robust.Shared.Maths
|
||||
/// <summary>Add left matrix to this matrix.</summary>
|
||||
/// <param name="matrix">The matrix to add.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Add(ref Matrix3 matrix)
|
||||
public void Add(in Matrix3 matrix)
|
||||
{
|
||||
R0C0 = R0C0 + matrix.R0C0;
|
||||
R0C1 = R0C1 + matrix.R0C1;
|
||||
@@ -614,7 +614,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="matrix">The matrix to add.</param>
|
||||
/// <param name="result">The resulting matrix of the addition.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Add(ref Matrix3 matrix, out Matrix3 result)
|
||||
public readonly void Add(in Matrix3 matrix, out Matrix3 result)
|
||||
{
|
||||
result.R0C0 = R0C0 + matrix.R0C0;
|
||||
result.R0C1 = R0C1 + matrix.R0C1;
|
||||
@@ -632,7 +632,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="right">The matrix on the right side of the equation</param>
|
||||
/// <param name="result">The resulting matrix of the addition.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Add(ref Matrix3 left, ref Matrix3 right, out Matrix3 result)
|
||||
public static void Add(in Matrix3 left, in Matrix3 right, out Matrix3 result)
|
||||
{
|
||||
result.R0C0 = left.R0C0 + right.R0C0;
|
||||
result.R0C1 = left.R0C1 + right.R0C1;
|
||||
@@ -648,7 +648,7 @@ namespace Robust.Shared.Maths
|
||||
/// <summary>Subtract matrix from this matrix.</summary>
|
||||
/// <param name="matrix">The matrix to subtract.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Subtract(ref Matrix3 matrix)
|
||||
public void Subtract(in Matrix3 matrix)
|
||||
{
|
||||
R0C0 = R0C0 - matrix.R0C0;
|
||||
R0C1 = R0C1 - matrix.R0C1;
|
||||
@@ -665,7 +665,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="matrix">The matrix to subtract.</param>
|
||||
/// <param name="result">The resulting matrix of the subtraction.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Subtract(ref Matrix3 matrix, out Matrix3 result)
|
||||
public readonly void Subtract(in Matrix3 matrix, out Matrix3 result)
|
||||
{
|
||||
result.R0C0 = R0C0 - matrix.R0C0;
|
||||
result.R0C1 = R0C1 - matrix.R0C1;
|
||||
@@ -683,7 +683,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="right">The matrix on the right side of the equation</param>
|
||||
/// <param name="result">The resulting matrix of the subtraction.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Subtract(ref Matrix3 left, ref Matrix3 right, out Matrix3 result)
|
||||
public static void Subtract(in Matrix3 left, in Matrix3 right, out Matrix3 result)
|
||||
{
|
||||
result.R0C0 = left.R0C0 - right.R0C0;
|
||||
result.R0C1 = left.R0C1 - right.R0C1;
|
||||
@@ -726,7 +726,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="matrix">The matrix to multiply.</param>
|
||||
/// <param name="result">The resulting matrix of the multiplication.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Multiply(ref Matrix3 matrix, out Matrix3 result)
|
||||
public readonly void Multiply(in Matrix3 matrix, out Matrix3 result)
|
||||
{
|
||||
result.R0C0 = matrix.R0C0 * R0C0 + matrix.R0C1 * R1C0 + matrix.R0C2 * R2C0;
|
||||
result.R0C1 = matrix.R0C0 * R0C1 + matrix.R0C1 * R1C1 + matrix.R0C2 * R2C1;
|
||||
@@ -744,7 +744,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="right">The matrix on the right side of the equation</param>
|
||||
/// <param name="result">The resulting matrix of the multiplication.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Multiply(ref Matrix3 left, ref Matrix3 right, out Matrix3 result)
|
||||
public static void Multiply(in Matrix3 left, in Matrix3 right, out Matrix3 result)
|
||||
{
|
||||
result.R0C0 = right.R0C0 * left.R0C0 + right.R0C1 * left.R1C0 + right.R0C2 * left.R2C0;
|
||||
result.R0C1 = right.R0C0 * left.R0C1 + right.R0C1 * left.R1C1 + right.R0C2 * left.R2C1;
|
||||
@@ -777,7 +777,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="scalar">The scalar to multiply.</param>
|
||||
/// <param name="result">The resulting matrix of the multiplication.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Multiply(float scalar, out Matrix3 result)
|
||||
public readonly void Multiply(float scalar, out Matrix3 result)
|
||||
{
|
||||
result.R0C0 = scalar * R0C0;
|
||||
result.R0C1 = scalar * R0C1;
|
||||
@@ -795,7 +795,7 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="scalar">The scalar on the right side of the equation</param>
|
||||
/// <param name="result">The resulting matrix of the multiplication.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Multiply(ref Matrix3 matrix, float scalar, out Matrix3 result)
|
||||
public static void Multiply(in Matrix3 matrix, float scalar, out Matrix3 result)
|
||||
{
|
||||
result.R0C0 = scalar * matrix.R0C0;
|
||||
result.R0C1 = scalar * matrix.R0C1;
|
||||
@@ -812,7 +812,7 @@ namespace Robust.Shared.Maths
|
||||
|
||||
#region Functions
|
||||
|
||||
public float Determinant
|
||||
public readonly double Determinant
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => R0C0 * R1C1 * R2C2 - R0C0 * R1C2 * R2C1 - R0C1 * R1C0 * R2C2 + R0C2 * R1C0 * R2C1 + R0C1 * R1C2 * R2C0 - R0C2 * R1C1 * R2C0;
|
||||
@@ -827,7 +827,7 @@ namespace Robust.Shared.Maths
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Transpose(out Matrix3 result)
|
||||
public readonly void Transpose(out Matrix3 result)
|
||||
{
|
||||
result.R0C0 = R0C0;
|
||||
result.R0C1 = R1C0;
|
||||
@@ -841,7 +841,7 @@ namespace Robust.Shared.Maths
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Transpose(ref Matrix3 matrix, out Matrix3 result)
|
||||
public static void Transpose(in Matrix3 matrix, out Matrix3 result)
|
||||
{
|
||||
result.R0C0 = matrix.R0C0;
|
||||
result.R0C1 = matrix.R1C0;
|
||||
@@ -861,29 +861,28 @@ namespace Robust.Shared.Maths
|
||||
/// <returns>The inverse of the given matrix if it has one, or the input if it is singular</returns>
|
||||
/// <exception cref="InvalidOperationException">Thrown if the Matrix4 is singular.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Matrix3 Invert(Matrix3 mat)
|
||||
public static Matrix3 Invert(in Matrix3 mat)
|
||||
{
|
||||
var result = new Matrix3();
|
||||
mat.Invert(ref result);
|
||||
Invert(mat, out var result);
|
||||
return result;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Invert(ref Matrix3 minv)
|
||||
public readonly Matrix3 Invert()
|
||||
{
|
||||
Invert(this, out var result);
|
||||
return result;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Invert(in Matrix3 m, out Matrix3 minv)
|
||||
{
|
||||
//Credit: https://stackoverflow.com/a/18504573
|
||||
|
||||
var d = Determinant;
|
||||
if (MathHelper.CloseToPercent(d, 0))
|
||||
var det = m.Determinant;
|
||||
if (MathHelper.CloseToPercent(det, 0))
|
||||
throw new InvalidOperationException("Matrix is singular and cannot be inverted.");
|
||||
|
||||
var m = this;
|
||||
|
||||
// computes the inverse of a matrix m
|
||||
double det = m.R0C0 * (m.R1C1 * m.R2C2 - m.R2C1 * m.R1C2) -
|
||||
m.R0C1 * (m.R1C0 * m.R2C2 - m.R1C2 * m.R2C0) +
|
||||
m.R0C2 * (m.R1C0 * m.R2C1 - m.R1C1 * m.R2C0);
|
||||
|
||||
var invdet = 1 / det;
|
||||
|
||||
minv.R0C0 = (float) ((m.R1C1 * m.R2C2 - m.R2C1 * m.R1C2) * invdet);
|
||||
@@ -902,7 +901,7 @@ namespace Robust.Shared.Maths
|
||||
#region Transformation Functions
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Transform(ref Vector3 vector)
|
||||
public readonly void Transform(ref Vector3 vector)
|
||||
{
|
||||
Transform(this, ref vector);
|
||||
}
|
||||
@@ -1020,11 +1019,9 @@ namespace Robust.Shared.Maths
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Transform(ref Vector3 vector, out Vector3 result)
|
||||
public void Transform(in Vector3 vector, out Vector3 result)
|
||||
{
|
||||
result.X = R0C0 * vector.X + R0C1 * vector.Y + R0C2 * vector.Z;
|
||||
result.Y = R1C0 * vector.X + R1C1 * vector.Y + R1C2 * vector.Z;
|
||||
result.Z = R2C0 * vector.X + R2C1 * vector.Y + R2C2 * vector.Z;
|
||||
Transform(this, vector, out result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1036,10 +1033,9 @@ namespace Robust.Shared.Maths
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Transform(in Matrix3 matrix, in Vector3 vector, out Vector3 result)
|
||||
{
|
||||
var x = matrix.R0C0 * vector.X + matrix.R0C1 * vector.Y + matrix.R0C2 * vector.Z;
|
||||
var y = matrix.R1C0 * vector.X + matrix.R1C1 * vector.Y + matrix.R1C2 * vector.Z;
|
||||
var z = matrix.R2C0 * vector.X + matrix.R2C1 * vector.Y + matrix.R2C2 * vector.Z;
|
||||
result = new Vector3(x, y, z);
|
||||
result.X = matrix.R0C0 * vector.X + matrix.R0C1 * vector.Y + matrix.R0C2 * vector.Z;
|
||||
result.Y = matrix.R1C0 * vector.X + matrix.R1C1 * vector.Y + matrix.R1C2 * vector.Z;
|
||||
result.Z = matrix.R2C0 * vector.X + matrix.R2C1 * vector.Y + matrix.R2C2 * vector.Z;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1052,9 +1048,8 @@ namespace Robust.Shared.Maths
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Transform(in Matrix3 matrix, in Vector2 vector, out Vector2 result)
|
||||
{
|
||||
var x = matrix.R0C0 * vector.X + matrix.R0C1 * vector.Y + matrix.R0C2;
|
||||
var y = matrix.R1C0 * vector.X + matrix.R1C1 * vector.Y + matrix.R1C2;
|
||||
result = new Vector2(x, y);
|
||||
result.X = matrix.R0C0 * vector.X + matrix.R0C1 * vector.Y + matrix.R0C2;
|
||||
result.Y = matrix.R1C0 * vector.X + matrix.R1C1 * vector.Y + matrix.R1C2;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1066,10 +1061,9 @@ namespace Robust.Shared.Maths
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Transform(in Vector3 vector, in Matrix3 matrix, out Vector3 result)
|
||||
{
|
||||
var x = (vector.X * matrix.R0C0) + (vector.Y * matrix.R1C0) + (vector.Z * matrix.R2C0);
|
||||
var y = (vector.X * matrix.R0C1) + (vector.Y * matrix.R1C1) + (vector.Z * matrix.R2C1);
|
||||
var z = (vector.X * matrix.R0C2) + (vector.Y * matrix.R1C2) + (vector.Z * matrix.R2C2);
|
||||
result = new Vector3(x, y, z);
|
||||
result.X = (vector.X * matrix.R0C0) + (vector.Y * matrix.R1C0) + (vector.Z * matrix.R2C0);
|
||||
result.Y = (vector.X * matrix.R0C1) + (vector.Y * matrix.R1C1) + (vector.Z * matrix.R2C1);
|
||||
result.Z = (vector.X * matrix.R0C2) + (vector.Y * matrix.R1C2) + (vector.Z * matrix.R2C2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1082,9 +1076,8 @@ namespace Robust.Shared.Maths
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Transform(in Vector2 vector, in Matrix3 matrix, out Vector2 result)
|
||||
{
|
||||
var x = (vector.X * matrix.R0C0) + (vector.Y * matrix.R1C0) + (matrix.R2C0);
|
||||
var y = (vector.X * matrix.R0C1) + (vector.Y * matrix.R1C1) + (matrix.R2C1);
|
||||
result = new Vector2(x, y);
|
||||
result.X = (vector.X * matrix.R0C0) + (vector.Y * matrix.R1C0) + (matrix.R2C0);
|
||||
result.Y = (vector.X * matrix.R0C1) + (vector.Y * matrix.R1C1) + (matrix.R2C1);
|
||||
}
|
||||
|
||||
|
||||
@@ -1156,7 +1149,7 @@ namespace Robust.Shared.Maths
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2 operator *(in Matrix3 matrix, in Vector2 vector)
|
||||
{
|
||||
Transform(in matrix, in vector, out var result);
|
||||
Transform(in matrix, vector, out var result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1179,9 +1172,9 @@ namespace Robust.Shared.Maths
|
||||
/// <param name="right">The matrix on the right side of the equation</param>
|
||||
/// <returns>The resulting matrix of the multiplication.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Matrix3 operator *(Matrix3 left, Matrix3 right)
|
||||
public static Matrix3 operator *(in Matrix3 left, in Matrix3 right)
|
||||
{
|
||||
Multiply(ref left, ref right, out var result);
|
||||
Multiply(in left, in right, out var result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1209,7 +1202,7 @@ namespace Robust.Shared.Maths
|
||||
|
||||
/// <summary>Returns the hash code for this instance.</summary>
|
||||
/// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
|
||||
public override int GetHashCode()
|
||||
public readonly override int GetHashCode()
|
||||
{
|
||||
return
|
||||
R0C0.GetHashCode() ^ R0C1.GetHashCode() ^ R0C2.GetHashCode() ^
|
||||
@@ -1223,7 +1216,7 @@ namespace Robust.Shared.Maths
|
||||
|
||||
/// <summary>Returns the fully qualified type name of this instance.</summary>
|
||||
/// <returns>A System.String containing left fully qualified type name.</returns>
|
||||
public override string ToString()
|
||||
public readonly override string ToString()
|
||||
{
|
||||
return $"|{R0C0}, {R0C1}, {R0C2}|\n"
|
||||
+ $"|{R1C0}, {R1C1}, {R1C2}|\n"
|
||||
|
||||
@@ -234,7 +234,7 @@ namespace Robust.Shared.GameObjects
|
||||
var parentMatrix = parentXform._localMatrix;
|
||||
parent = parentXform.ParentUid;
|
||||
|
||||
Matrix3.Multiply(ref myMatrix, ref parentMatrix, out var result);
|
||||
Matrix3.Multiply(in myMatrix, in parentMatrix, out var result);
|
||||
myMatrix = result;
|
||||
}
|
||||
|
||||
@@ -259,7 +259,7 @@ namespace Robust.Shared.GameObjects
|
||||
var parentMatrix = parentXform._invLocalMatrix;
|
||||
parent = parentXform.ParentUid;
|
||||
|
||||
Matrix3.Multiply(ref parentMatrix, ref myMatrix, out var result);
|
||||
Matrix3.Multiply(in parentMatrix, in myMatrix, out var result);
|
||||
myMatrix = result;
|
||||
}
|
||||
|
||||
@@ -760,7 +760,7 @@ namespace Robust.Shared.GameObjects
|
||||
var xform = xforms.GetComponent(parent);
|
||||
worldRot += xform.LocalRotation;
|
||||
var parentMatrix = xform._localMatrix;
|
||||
Matrix3.Multiply(ref worldMatrix, ref parentMatrix, out var result);
|
||||
Matrix3.Multiply(in worldMatrix, in parentMatrix, out var result);
|
||||
worldMatrix = result;
|
||||
parent = xform.ParentUid;
|
||||
}
|
||||
@@ -823,11 +823,11 @@ namespace Robust.Shared.GameObjects
|
||||
worldRot += xform.LocalRotation;
|
||||
|
||||
var parentMatrix = xform._localMatrix;
|
||||
Matrix3.Multiply(ref worldMatrix, ref parentMatrix, out var result);
|
||||
Matrix3.Multiply(in worldMatrix, in parentMatrix, out var result);
|
||||
worldMatrix = result;
|
||||
|
||||
var parentInvMatrix = xform._invLocalMatrix;
|
||||
Matrix3.Multiply(ref parentInvMatrix, ref invMatrix, out var invResult);
|
||||
Matrix3.Multiply(in parentInvMatrix, in invMatrix, out var invResult);
|
||||
invMatrix = invResult;
|
||||
|
||||
parent = xform.ParentUid;
|
||||
|
||||
@@ -405,8 +405,8 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
|
||||
var worldMat = node4Trans.WorldMatrix;
|
||||
var invWorldMat = node4Trans.InvWorldMatrix;
|
||||
|
||||
Matrix3.Multiply(ref worldMat, ref invWorldMat, out var leftVerifyMatrix);
|
||||
Matrix3.Multiply(ref invWorldMat, ref worldMat, out var rightVerifyMatrix);
|
||||
Matrix3.Multiply(in worldMat, in invWorldMat, out var leftVerifyMatrix);
|
||||
Matrix3.Multiply(in invWorldMat, in worldMat, out var rightVerifyMatrix);
|
||||
|
||||
//Assert
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using Robust.Shared.Maths;
|
||||
|
||||
@@ -111,8 +111,8 @@ namespace Robust.UnitTesting.Shared.Maths
|
||||
var invMatrix = Matrix3.Invert(normalMatrix);
|
||||
|
||||
// multiply it back together
|
||||
Matrix3.Multiply(ref normalMatrix, ref invMatrix, out var leftVerifyMatrix);
|
||||
Matrix3.Multiply(ref invMatrix, ref normalMatrix, out var rightVerifyMatrix);
|
||||
Matrix3.Multiply(in normalMatrix, in invMatrix, out var leftVerifyMatrix);
|
||||
Matrix3.Multiply(in invMatrix, in normalMatrix, out var rightVerifyMatrix);
|
||||
|
||||
// these should be the same (A × A-1 = A-1 × A = I)
|
||||
Assert.That(leftVerifyMatrix, new ApproxEqualityConstraint(rightVerifyMatrix, epsilon));
|
||||
@@ -129,8 +129,8 @@ namespace Robust.UnitTesting.Shared.Maths
|
||||
var mat2 = Matrix3.CreateTranslation(new Vector2(-2, -2));
|
||||
var mat3 = Matrix3.CreateTranslation(new Vector2(3, 3));
|
||||
|
||||
mat1.Multiply(ref mat2, out var res2);
|
||||
res2.Multiply(ref mat3, out var res3);
|
||||
mat1.Multiply(in mat2, out var res2);
|
||||
res2.Multiply(in mat3, out var res3);
|
||||
|
||||
// Act
|
||||
Vector3 test = new Vector3(0, 0, 1);
|
||||
@@ -152,7 +152,7 @@ namespace Robust.UnitTesting.Shared.Maths
|
||||
|
||||
// NOTE: Matrix Product is NOT commutative. OpenTK (and this) uses pre-multiplication, OpenGL and all the tutorials
|
||||
// you will read about it use post-multiplication. So in OpenTK MVP = M*V*P; in OpenGL it is MVP = P*V*M.
|
||||
Matrix3.Multiply(ref rotateMatrix, ref translateMatrix, out var transformMatrix);
|
||||
Matrix3.Multiply(in rotateMatrix, in translateMatrix, out var transformMatrix);
|
||||
|
||||
// Act
|
||||
Matrix3.Transform(in transformMatrix, in startPoint, out var localPoint);
|
||||
|
||||
Reference in New Issue
Block a user