Files
RobustToolbox/Robust.Shared.Maths/Matrix3Helpers.cs
Leon Friedrich 3f19d25018 Box Simd (#6193)
* Box Simd

* Add 256 bit version of GetAABB

* Add AABB bechmarks

* No real diff between 128 & 256, so removing 256

| Method     | Mean      | Error     | StdDev    | Ratio |
|----------- |----------:|----------:|----------:|------:|
| GetAABB    | 5.8107 ns | 0.0154 ns | 0.0137 ns |  1.00 |
| GetAABB128 | 0.4927 ns | 0.0003 ns | 0.0002 ns |  0.08 |
| GetAABB256 | 0.4332 ns | 0.0006 ns | 0.0006 ns |  0.07 |

* Add Box2Rotated.Transform Benchmark

* Results

20% faster and much smaller code. Also I don't think it inlined RotateVec

* Add Matrix3x2Helper.TransformBox() benchmark

new:

| Method    | Mean     | Error     | StdDev    | Code Size |
|---------- |---------:|----------:|----------:|----------:|
| Transform | 2.463 ns | 0.0766 ns | 0.0679 ns |     216 B |

old:
| Method    | Mean     | Error     | StdDev    | Median   | Code Size |
|---------- |---------:|----------:|----------:|---------:|----------:|
| Transform | 9.469 ns | 0.2140 ns | 0.5408 ns | 9.206 ns |     621 B |

* Fix polygon constructor

* SlimPolygonBenchmark

* use new SimdHelper for other methods

* Fix bugs

* Use new methods

* Simd SlimPolygon.ComputeAABB

* Move simd transform to physics

* Cleanup

* Remove uneccesary Unsafe.SkipInit

* These tests all work on master

* Add Transform.MulSimd test

* Add SlimPolygon constructor tests

* Add ComputeAABB test

---------

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2025-11-10 18:30:08 +11:00

231 lines
7.7 KiB
C#

using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
namespace Robust.Shared.Maths;
public static class Matrix3Helpers
{
public static bool EqualsApprox(this Matrix3x2 a, Matrix3x2 b, float tolerance = 1e-6f)
{
return
Math.Abs(a.M11 - b.M11) <= tolerance &&
Math.Abs(a.M12 - b.M12) <= tolerance &&
Math.Abs(a.M21 - b.M21) <= tolerance &&
Math.Abs(a.M22 - b.M22) <= tolerance &&
Math.Abs(a.M31 - b.M31) <= tolerance &&
Math.Abs(a.M32 - b.M32) <= tolerance;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EqualsApprox(this Matrix3x2 a, Matrix3x2 b, double tolerance)
{
return a.EqualsApprox(b, (float) tolerance);
}
[Obsolete("Use TransformBox")]
// This method was previously broken, and now just returns an bounding box pretending to be a Box2Rotated
public static Box2Rotated TransformBounds(this Matrix3x2 refFromBox, Box2Rotated box)
{
return new Box2Rotated(TransformBox(refFromBox, box));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Box2 TransformBox(this Matrix3x2 refFromBox, in Box2Rotated box)
{
return (box.Transform * refFromBox).TransformBox(box.Box);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void TransformBox(
this Matrix3x2 refFromBox,
in Box2Rotated box,
out Vector128<float> x,
out Vector128<float> y)
{
(box.Transform * refFromBox).TransformBox(box.Box, out x, out y);
}
public static Box2 TransformBox(this Matrix3x2 refFromBox, in Box2 box)
{
TransformBox(refFromBox, box, out var x, out var y);
var aabb = SimdHelpers.GetAABB(x, y);
return Unsafe.As<Vector128<float>, Box2>(ref aabb);
}
/// <summary>
/// Applies a transformation matrix to all of a box's corners and returns their coordinates in two simd vectors.
/// </summary>
/// <remarks>The corners are ordered clockwise, starting from what was the bottom left corner prior to the transformation.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void TransformBox(
this Matrix3x2 refFromBox,
in Box2 box,
out Vector128<float> x,
out Vector128<float> y)
{
var boxVec = Unsafe.As<Box2, Vector128<float>>(ref Unsafe.AsRef(in box));
// Convert box into list of X and Y values for each of the 4 corners
var boxX = Vector128.Shuffle(boxVec, Vector128.Create(0, 2, 2, 0));
var boxY = Vector128.Shuffle(boxVec, Vector128.Create(1, 1, 3, 3));
// Transform coordinates
x = Vector128.Create(refFromBox.M31)
+ boxX * Vector128.Create(refFromBox.M11)
+ boxY * Vector128.Create(refFromBox.M21);
y = Vector128.Create(refFromBox.M32)
+ boxX * Vector128.Create(refFromBox.M12)
+ boxY * Vector128.Create(refFromBox.M22);
}
/// <summary>
/// Gets the rotation of the Matrix. Will have some precision loss.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Angle Rotation(this Matrix3x2 t)
{
return new Angle(Math.Atan2(t.M12, t.M11));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3x2 CreateTransform(float posX, float posY, double angle)
{
// returns a matrix that is equivalent to returning CreateRotation(angle) * CreateTranslation(posX, posY)
var sin = (float) Math.Sin(angle);
var cos = (float) Math.Cos(angle);
return new Matrix3x2
{
M11 = cos,
M21 = -sin,
M31 = posX,
M12 = sin,
M22 = cos,
M32 = posY,
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3x2 CreateTransform(float posX, float posY, double angle, float scaleX = 1, float scaleY = 1)
{
// returns a matrix that is equivalent to returning CreateScale(scale) * CreateRotation(angle) * CreateTranslation(posX, posY)
var sin = (float) Math.Sin(angle);
var cos = (float) Math.Cos(angle);
return new Matrix3x2
{
M11 = cos * scaleX,
M21 = -sin * scaleY,
M31 = posX,
M12 = sin * scaleX,
M22 = cos * scaleY,
M32 = posY,
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3x2 CreateTransform(in Vector2 position, in Angle angle)
{
// Rounding moment
return angle.Theta switch
{
-Math.PI / 2 => new Matrix3x2(0f, -1f, 1, 0, position.X, position.Y),
Math.PI / 2 => new Matrix3x2(0f, 1f, -1f, 0f, position.X, position.Y),
Math.PI => new Matrix3x2(-1f, 0f, 0f, -1f, position.X, position.Y),
_ => CreateTransform(position.X, position.Y, (float) angle.Theta)
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3x2 CreateTransform(in Vector2 position, in Angle angle, in Vector2 scale)
{
return CreateTransform(position.X, position.Y, (float)angle.Theta, scale.X, scale.Y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3x2 CreateInverseTransform(float posX, float posY, double angle, float scaleX = 1, float scaleY = 1)
{
// returns a matrix that is equivalent to returning CreateTranslation(-posX, -posY) * CreateRotation(-angle) * CreateScale(1/scaleX, 1/scaleY)
var sin = (float) Math.Sin(angle);
var cos = (float) Math.Cos(angle);
return new Matrix3x2
{
M11 = cos / scaleX,
M21 = sin / scaleX,
M31 = - (posX * cos + posY * sin) / scaleX,
M12 = -sin / scaleY,
M22 = cos / scaleY,
M32 = (posX * sin - posY * cos) / scaleY,
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3x2 CreateInverseTransform(in Vector2 position, in Angle angle)
{
return CreateInverseTransform(position.X, position.Y, (float)angle.Theta);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3x2 CreateInverseTransform(in Vector2 position, in Angle angle, in Vector2 scale)
{
return CreateInverseTransform(position.X, position.Y, (float)angle.Theta, scale.X, scale.Y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3x2 CreateTranslation(float x, float y)
{
return new Matrix3x2 {
M11 = 1,
M12 = 0,
M21 = 0,
M22 = 1,
M31 = x,
M32 = y
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3x2 CreateTranslation(Vector2 vector)
{
return CreateTranslation(vector.X, vector.Y);
}
public static Matrix3x2 CreateRotation(double angle)
{
var cos = (float) Math.Cos(angle);
var sin = (float) Math.Sin(angle);
return new Matrix3x2 {
M11 = cos,
M12 = sin,
M21 = -sin,
M22 = cos,
M31 = 0,
M32 = 0
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3x2 CreateScale(float x, float y)
{
return new Matrix3x2 {
M11 = x,
M12 = 0,
M21 = 0,
M22 = y,
M31 = 0,
M32 = 0
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3x2 CreateScale(in Vector2 scale)
{
return CreateScale(scale.X, scale.Y);
}
}