using Titano.Core.Imaging.Geometry; using Titano.Testing; namespace Titano.Core.Tests.Geometry; public sealed class Affine2DTests { [Test] public void CompositionAppliesInnerFirst() { var scale = Affine2D.Scaling(2, 3); var move = Affine2D.Translation(5, -1); var (x, y) = (move * scale).Apply(1, 1); Assert.Approximately(7, x, 1e-15); Assert.Approximately(2, y, 1e-15); Assert.Equal(move * scale, scale.Then(move)); } [Test] public void InverseUndoesTheMap() { var map = Affine2D.Similarity(1.3, 0.4, 12, -7) * Affine2D.Scaling(1, 0.8); var inverse = map.Invert(); Assert.True((inverse * map).IsIdentity(1e-12)); Assert.True((map * inverse).IsIdentity(1e-12)); var (x, y) = map.Apply(3.5, -2.25); var (bx, by) = inverse.Apply(x, y); Assert.Within(3.5, -2.25, bx, by, 1e-12); } [Test] public void SingularMapsDoNotInvert() { Assert.False(new Affine2D(1, 2, 2, 4, 0, 0).TryInvert(out _)); Assert.Throws(() => Affine2D.Scaling(0, 1).Invert()); } [Test] public void RotationAboutPivotKeepsThePivot() { var map = Affine2D.About(Affine2D.Rotation(0.7), 40, 30); var (x, y) = map.Apply(40, 30); Assert.Within(40, 30, x, y, 1e-12); var (vx, vy) = map.ApplyVector(1, 0); Assert.Within(double.Cos(0.7), double.Sin(0.7), vx, vy, 1e-15); } [Test] public void SimilarityDecomposes() { var map = Affine2D.Similarity(1.25, -0.3, 4, 5); Assert.Approximately(1.25, map.Scale, 1e-12); Assert.Approximately(-0.3, map.Angle, 1e-12); Assert.Approximately(1.25 * 1.25, map.Determinant, 1e-12); } }