using System;
using System.Collections.Generic;
using UnityEngine;
using NUnit.Framework;
namespace Framework.Geometry.Lines.Fitting {
/// Unit tests for the 2D line segment fitting algorithms
[TestFixture]
public static class Segment2FitterTest {
///
/// Verifies that attempting to fit a segment to no points results in a zero-length
/// segment at the center of the coordinate system
///
[Test]
public static void NoPointsResultInDefaultLine() {
var noPoints = new Vector2[0];
Segment2 fittedSegment = Segment2Fitter.Fit(noPoints);
Assert.That(fittedSegment.From, Is.EqualTo(Vector2.zero));
Assert.That(fittedSegment.To, Is.EqualTo(Vector2.zero));
}
///
/// Verifies that fitting a segment to a single point produces a zero-length segment
///
[Test]
public static void SinglePointResultsInZeroLengthLine() {
var singlePoint = new Vector2[] { new Vector2(1.2f, 3.4f) };
Segment2 fittedSegment = Segment2Fitter.Fit(singlePoint);
Assert.That(fittedSegment.From, Is.EqualTo(singlePoint[0]));
Assert.That(fittedSegment.To, Is.EqualTo(singlePoint[0]));
}
///
/// Verifies that fitting a bunch of points all on top of each other produces
/// a zero-length line segment
///
[Test]
public static void ClumpedPointsResultInZeroLengthSegment() {
var clumpedPoints = new Vector2[] {
new Vector2(1.2f, 3.4f),
new Vector2(1.2f, 3.4f),
new Vector2(1.2f, 3.4f),
new Vector2(1.2f, 3.4f)
};
Segment2 fittedSegment = Segment2Fitter.Fit(clumpedPoints);
Assert.That(fittedSegment.From, Is.EqualTo(clumpedPoints[0]));
Assert.That(fittedSegment.To, Is.EqualTo(clumpedPoints[0]));
}
///
/// Verifies that fitting a line segment to a series in a flat row builds
/// a horizontal line segment
///
[Test]
public static void FlatHorizontalSegmentCanBeFitted() {
var horizontalPoints = new Vector2[] {
new Vector2(4.0f, 5.0f),
new Vector2(9.0f, 5.0f),
new Vector2(14.0f, 5.0f),
new Vector2(21.0f, 5.0f)
};
Segment2 fittedSegment = Segment2Fitter.Fit(horizontalPoints);
Assert.That(fittedSegment.From, Is.EqualTo(horizontalPoints[0]));
Assert.That(fittedSegment.To, Is.EqualTo(horizontalPoints[3]));
}
///
/// Verifies that fitting a line segment to a series in a flat column builds
/// a vertical line segment
///
[Test]
public static void FlatVerticalSegmentCanBeFitted() {
var verticalPoints = new Vector2[] {
new Vector2(5.0f, 4.0f),
new Vector2(5.0f, 9.0f),
new Vector2(5.0f, 14.0f),
new Vector2(5.0f, 21.0f)
};
Segment2 fittedSegment = Segment2Fitter.Fit(verticalPoints);
Assert.That(fittedSegment.From, Is.EqualTo(verticalPoints[0]));
Assert.That(fittedSegment.To, Is.EqualTo(verticalPoints[3]));
}
///
/// Verifies that the fitted segment's direction matches the general direction
/// of the points it is fitted to
///
[Test]
public static void SegmentDirectionMatchesPointOrder() {
var points = new Vector2[] {
new Vector2(-5.0f, 5.0f),
new Vector2(5.0f, 2.5f), new Vector2(10.0f, 7.5f),
new Vector2(5.0f, 7.5f), new Vector2(10.0f, 2.5f),
new Vector2(15.0f, 5.0f)
};
Segment2 fittedSegment = Segment2Fitter.Fit(points);
Assert.That(fittedSegment.From, Is.EqualTo(new Vector2(-5.0f, 5.0f)));
Assert.That(fittedSegment.To, Is.EqualTo(new Vector2(15.0f, 5.0f)));
var reversePoints = new List(points);
reversePoints.Reverse();
fittedSegment = Segment2Fitter.Fit(reversePoints);
Assert.That(fittedSegment.From, Is.EqualTo(new Vector2(15.0f, 5.0f)));
Assert.That(fittedSegment.To, Is.EqualTo(new Vector2(-5.0f, 5.0f)));
}
}
} // namespace Framework.Geometry.Lines.Fitting