Add extra helpers to Direction (#2093)

This commit is contained in:
Ygg01
2021-10-08 13:34:45 +02:00
committed by GitHub
parent c8e7fe9f1f
commit 915a812832
2 changed files with 96 additions and 41 deletions

View File

@@ -84,25 +84,62 @@ namespace Robust.Shared.Maths
return ang;
}
private static Vector2[] directionVectors = new[]
{
new Vector2(0, -1),
private static readonly Vector2[] DirectionVectors = {
new (0, -1),
new Vector2(1, -1).Normalized,
new Vector2(1, 0),
new (1, 0),
new Vector2(1, 1).Normalized,
new Vector2(0, 1),
new (0, 1),
new Vector2(-1, 1).Normalized,
new Vector2(-1, 0),
new (-1, 0),
new Vector2(-1, -1).Normalized
};
private static readonly Vector2i[] IntDirectionVectors = {
new (0, -1),
new (1, -1),
new (1, 0),
new (1, 1),
new (0, 1),
new (-1, 1),
new (-1, 0),
new (-1, -1)
};
/// <summary>
/// Converts a Direction to a normalized Direction vector.
/// </summary>
/// <param name="dir"></param>
/// <returns></returns>
/// <returns>a normalized 2D Vector</returns>
/// <exception cref="IndexOutOfRangeException">if invalid Direction is used</exception>
/// <seealso cref="Vector2"/>
public static Vector2 ToVec(this Direction dir)
{
return directionVectors[(int) dir];
return DirectionVectors[(int) dir];
}
/// <summary>
/// Converts a Direction to a Vector2i. Useful for getting adjacent tiles.
/// </summary>
/// <param name="dir">Direction</param>
/// <returns>an 2D int Vector</returns>
/// <exception cref="IndexOutOfRangeException">if invalid Direction is used</exception>
/// <seealso cref="Vector2i"/>
public static Vector2i ToIntVec(this Direction dir)
{
return IntDirectionVectors[(int) dir];
}
/// <summary>
/// Offset 2D integer vector by a given direction.
/// Convenience for adding <see cref="ToIntVec"/> to <see cref="Vector2i"/>
/// </summary>
/// <param name="vec">2D integer vector</param>
/// <param name="dir">Direction by which we offset</param>
/// <returns>a newly vector offset by the <param name="dir">dir</param> or exception if the direction is invalid</returns>
public static Vector2i Offset(this Vector2i vec, Direction dir)
{
return vec + dir.ToIntVec();
}
/// <summary>

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using Robust.Shared.Maths;
using NUnit.Framework;
@@ -7,67 +6,86 @@ namespace Robust.UnitTesting.Shared.Maths
{
[Parallelizable(ParallelScope.All | ParallelScope.Fixtures)]
[TestFixture]
internal class Direction_Test
internal class DirectionTest
{
private const double Epsilon = 1.0e-8;
private static IEnumerable<(float x, float y, Direction dir, double angle)> sources => new(float, float, Direction, double)[]
private static IEnumerable<TestCaseData> Sources()
{
(1, 0, Direction.East, Angle.FromDegrees(90)),
(1, 1, Direction.NorthEast, Angle.FromDegrees(135)),
(0, 1, Direction.North, Angle.FromDegrees(180)),
(-1, 1, Direction.NorthWest, Angle.FromDegrees(-135)),
(-1, 0, Direction.West, Angle.FromDegrees(-90)),
(-1, -1, Direction.SouthWest, Angle.FromDegrees(-45)),
(0, -1, Direction.South, 0),
(1, -1, Direction.SouthEast, Angle.FromDegrees(45))
};
yield return new TestCaseData(1, 0, Direction.East, Angle.FromDegrees(90));
yield return new TestCaseData(1, 1, Direction.NorthEast, Angle.FromDegrees(135));
yield return new TestCaseData(0, 1, Direction.North, Angle.FromDegrees(180));
yield return new TestCaseData(-1, 1, Direction.NorthWest, Angle.FromDegrees(-135));
yield return new TestCaseData(-1, 0, Direction.West, Angle.FromDegrees(-90));
yield return new TestCaseData(-1, -1, Direction.SouthWest, Angle.FromDegrees(-45));
yield return new TestCaseData(0, -1, Direction.South, Angle.FromDegrees(0));
yield return new TestCaseData(1, -1, Direction.SouthEast, Angle.FromDegrees(45));
}
[Test]
[Sequential]
public void TestDirectionToAngle([ValueSource(nameof(sources))] (float, float, Direction, double) test)
[TestCaseSource(nameof(Sources))]
[Parallelizable]
public void TestDirectionToAngle(float x, float y, Direction dir, Angle angle)
{
var control = test.Item4;
var val = test.Item3.ToAngle();
double control = angle;
var val = dir.ToAngle();
Assert.That(val.Theta, Is.EqualTo(control).Within(Epsilon));
}
[Test]
[Sequential]
public void TestDirectionToVec([ValueSource(nameof(sources))] (float, float, Direction, double) test)
[TestCaseSource(nameof(Sources))]
[Parallelizable]
public void TestDirectionToVec(float x, float y, Direction dir, Angle _)
{
var control = new Vector2(test.Item1, test.Item2).Normalized;
var val = test.Item3.ToVec();
var control = new Vector2(x, y).Normalized;
var controlInt = new Vector2i((int)x, (int)y);
var val = dir.ToVec();
var intVec = dir.ToIntVec();
Assert.That(val, Is.Approximately(control));
Assert.That(intVec, Is.EqualTo(controlInt));
}
[Test]
[Parallelizable]
public void TestDirectionOffset()
{
var v = new Vector2i(1, 1);
var expected = new Vector2i(2, 2);
var dir = Direction.NorthEast;
Assert.That(v.Offset(dir), Is.EqualTo(expected));
}
[Test]
[Sequential]
public void TestVecToAngle([ValueSource(nameof(sources))] (float, float, Direction, double) test)
[TestCaseSource(nameof(Sources))]
[Parallelizable]
public void TestVecToAngle(float x, float y, Direction dir, Angle angle)
{
var target = new Vector2(test.Item1, test.Item2).Normalized;
var target = new Vector2(x, y).Normalized;
Assert.That(target.ToWorldAngle().Reduced(), Is.Approximately(new Angle(test.Item4)));
Assert.That(target.ToWorldAngle().Reduced(), Is.Approximately(new Angle(angle)));
}
[Test]
[Sequential]
public void TestVector2ToDirection([ValueSource(nameof(sources))] (float, float, Direction, double) test)
[TestCaseSource(nameof(Sources))]
[Parallelizable]
public void TestVector2ToDirection(float x, float y, Direction dir, Angle angle)
{
var target = new Vector2(test.Item1, test.Item2).Normalized;
var target = new Vector2(x, y).Normalized;
Assert.That(target.GetDir(), Is.EqualTo(test.Item3));
Assert.That(target.GetDir(), Is.EqualTo(dir));
}
[Test]
[Sequential]
public void TestAngleToDirection([ValueSource(nameof(sources))] (float, float, Direction, double) test)
[TestCaseSource(nameof(Sources))]
[Parallelizable]
public void TestAngleToDirection(float x, float y, Direction dir, Angle angle)
{
var target = new Angle(test.Item4);
var target = new Angle(angle);
Assert.That(target.GetDir(), Is.EqualTo(test.Item3));
Assert.That(target.GetDir(), Is.EqualTo(dir));
}
}