130 lines
4.0 KiB
C#
130 lines
4.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BracerLib.Utility;
|
|
using NUnit.Framework;
|
|
using UnityEngine;
|
|
|
|
namespace BracerLib.Tests.Utility
|
|
{
|
|
public class TriangulatorTest : TestBase
|
|
{
|
|
private static IEnumerable AreaValueTests()
|
|
{
|
|
var points = new List<Vector2>();
|
|
|
|
yield return new TestCaseData(points.ToArray(), 0f).SetName("No points to calculate");
|
|
|
|
points.AddRange(new[]
|
|
{
|
|
Vector2.one,
|
|
new Vector2(5f, 7f),
|
|
new Vector2(-2f, 8f)
|
|
});
|
|
|
|
yield return new TestCaseData(points.ToArray(), 23f).SetName("Area of a triangle");
|
|
|
|
points.Add(new Vector2(3f, 5f));
|
|
|
|
yield return new TestCaseData(points.ToArray(), 10f).SetName("Area of a quad");
|
|
}
|
|
|
|
private static IEnumerable InsideTrianglePointsTest()
|
|
{
|
|
yield return new TestCaseData(
|
|
Vector2.zero,
|
|
new Vector2(3f, 3f),
|
|
new Vector2(3f, 0f),
|
|
new Vector2(2f, 1f),
|
|
true
|
|
).SetName("Point inside triangle");
|
|
|
|
yield return new TestCaseData(
|
|
Vector2.zero,
|
|
new Vector2(3f, 3f),
|
|
new Vector2(3f, 0f),
|
|
new Vector2(1f, 0f),
|
|
true
|
|
).SetName("Point on the line");
|
|
|
|
yield return new TestCaseData(
|
|
Vector2.zero,
|
|
new Vector2(3f, 3f),
|
|
new Vector2(3f, 0f),
|
|
new Vector2(3f, 0f),
|
|
false
|
|
).SetName("Point is one of triangle points");
|
|
|
|
yield return new TestCaseData(
|
|
Vector2.zero,
|
|
new Vector2(3f, 3f),
|
|
new Vector2(3f, 0f),
|
|
new Vector2(-2f, 1f),
|
|
false
|
|
).SetName("Point outside triangle");
|
|
}
|
|
|
|
private static IEnumerable TriangulatePointsTest()
|
|
{
|
|
yield return new TestCaseData(
|
|
new[]
|
|
{
|
|
Vector2.zero,
|
|
Vector2.right,
|
|
Vector2.right + Vector2.up,
|
|
Vector2.up,
|
|
Vector2.left + Vector2.up,
|
|
Vector2.left
|
|
},
|
|
new[] { 5,3,2,2,0,5,5,4,3,2,1,0 }
|
|
).SetName("Positive triangulation");
|
|
|
|
yield return new TestCaseData(
|
|
new[]
|
|
{
|
|
Vector2.zero,
|
|
Vector2.right,
|
|
Vector2.right + Vector2.down,
|
|
Vector2.down,
|
|
Vector2.left + Vector2.down,
|
|
Vector2.left
|
|
},
|
|
new[] { 1,3,4,4,0,1,1,2,3,4,5,0 }
|
|
).SetName("Negative triangulation");
|
|
|
|
yield return new TestCaseData(
|
|
new[] { Vector2.zero, Vector2.right },
|
|
Array.Empty<int>()
|
|
).SetName("Empty triangulation");
|
|
}
|
|
|
|
[Test]
|
|
[TestCaseSource(nameof(AreaValueTests))]
|
|
public void AreaCalculatesCorrectly(Vector2[] points, float expected)
|
|
{
|
|
var result = Triangulator.Area(points);
|
|
Assert.AreEqual(expected, result);
|
|
}
|
|
|
|
[Test]
|
|
[TestCaseSource(nameof(InsideTrianglePointsTest))]
|
|
public void PointsInsideTriangleCorrectly(Vector2 a, Vector2 b, Vector2 c, Vector2 p, bool expected)
|
|
{
|
|
var result = Triangulator.InsideTriangle(a, b, c, p);
|
|
Assert.AreEqual(expected, result);
|
|
}
|
|
|
|
[Test]
|
|
[TestCaseSource(nameof(TriangulatePointsTest))]
|
|
public void TriangulatePoints(Vector2[] points, int[] expected)
|
|
{
|
|
var result = Triangulator.Triangulate(points);
|
|
Assert.AreEqual(expected.Length, result.Length);
|
|
for (var i = 0; i < result.Length; i++)
|
|
{
|
|
Assert.AreEqual(expected[i], result[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|