mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
Add GetRotation for matrices and better precision (#4691)
This commit is contained in:
@@ -30,6 +30,7 @@ using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Robust.Shared.Maths
|
||||
@@ -385,7 +386,7 @@ namespace Robust.Shared.Maths
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Matrix3 CreateRotation(Angle angle)
|
||||
public static Matrix3 CreateRotation(double angle)
|
||||
{
|
||||
var cos = (float) Math.Cos(angle);
|
||||
var sin = (float) Math.Sin(angle);
|
||||
@@ -426,12 +427,12 @@ namespace Robust.Shared.Maths
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Matrix3 CreateTransform(float posX, float posY, float angle, float scaleX = 1, float scaleY = 1)
|
||||
public static Matrix3 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 = MathF.Sin(angle);
|
||||
var cos = MathF.Cos(angle);
|
||||
var sin = (float) Math.Sin(angle);
|
||||
var cos = (float) Math.Cos(angle);
|
||||
|
||||
return new Matrix3
|
||||
{
|
||||
@@ -446,12 +447,12 @@ namespace Robust.Shared.Maths
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Matrix3 CreateInverseTransform(float posX, float posY, float angle, float scaleX = 1, float scaleY = 1)
|
||||
public static Matrix3 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 = MathF.Sin(angle);
|
||||
var cos = MathF.Cos(angle);
|
||||
var sin = (float) Math.Sin(angle);
|
||||
var cos = (float) Math.Cos(angle);
|
||||
|
||||
return new Matrix3
|
||||
{
|
||||
@@ -493,7 +494,17 @@ namespace Robust.Shared.Maths
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Matrix3 CreateInverseTransform(in Vector2 position, in Angle angle, in Vector2 scale)
|
||||
{
|
||||
return CreateInverseTransform(position.X, position.Y, (float)angle.Theta, scale.X, scale.Y);
|
||||
return CreateInverseTransform(position.X, position.Y, angle.Theta, scale.X, scale.Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the rotation of the Matrix. Will have some precision loss.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly Angle Rotation()
|
||||
{
|
||||
return new Vector2(R0C0, R1C0).ToAngle();
|
||||
}
|
||||
|
||||
#endregion Constructors
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using NUnit.Framework;
|
||||
@@ -11,6 +12,20 @@ namespace Robust.UnitTesting.Shared.Maths
|
||||
[TestOf(typeof(Matrix3))]
|
||||
public sealed class Matrix3_Test
|
||||
{
|
||||
[Test]
|
||||
public void GetRotationTest()
|
||||
{
|
||||
Assert.That(Matrix3.Identity.Rotation(), Is.EqualTo(Angle.Zero));
|
||||
|
||||
var piOver2 = new Angle(Math.PI / 2);
|
||||
var piOver2Mat = Matrix3.CreateRotation(piOver2.Theta);
|
||||
Assert.That(piOver2Mat.Rotation(), Is.EqualTo(piOver2));
|
||||
|
||||
var pi = new Angle(Math.PI);
|
||||
var piMat = Matrix3.CreateRotation(pi.Theta);
|
||||
Assert.That(piMat.Rotation(), Is.EqualTo(pi));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TranslationTest()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user