Owner review 1, phase 4, package P1: the Core geometry behind the visual crop (R5) and the orientation controls (R6). - RenderGeometry: one warp stab⁻¹ · O⁻¹ · V with the crop and the keyframe centres in the view frame T (turned, mirrored, flipped, straightened); the output size comes from RenderSize, so a fixed preset adapts to the aspect (4K at 9:16 = 2160×3840) and AspectFromPreset is gone; the decode grows by the straighten cover zoom; ViewFor, ViewToFrame, EditableCrop and FootprintInView get their real bodies; a rotation that is not a quarter turn and an out-of-range straighten render as none or clamped, with their warnings. - FrameOrientation: the cover zoom is taken on the pixel-centre extents, so every output pixel samples inside the frame (full alpha coverage at ±15°, never a smeared border). - CropFrame: crops are written on the 12-decimal grid OrientationEdit remaps on, so four turns or two mirrors give back the same text; a stored crop is read value by value as the render reads it. - Advisor: the aspect (and the horizon framing) is advised on the view turned by the camera (SequenceContext.Rotate/Flip, ForCamera); the analyser passes the project's camera. - Self-test: proc.orient, proc.straighten, proc.portrait. - Tests: exact pixel permutations for turns, mirror and flip, the orientation algebra, straighten against an analytic ramp and its full coverage, Create sizes, a crop keeping its content through every press, CropFrame move/resize/refit/rows, OrientationEdit round trips and its agreement with FrameOrientation, the turned-view advice, determinism. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
2.3 KiB
C#
69 lines
2.3 KiB
C#
using Chronos.Core.Imaging.Geometry;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.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<InvalidOperationException>(() => 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 AQuarterTurnRotationIsNotExact()
|
|
{
|
|
// Why FrameOrientation writes its quarter turns as integer matrices: cos(π/2) is about 6e-17, not 0.
|
|
var rotation = Affine2D.Rotation(double.Pi / 2);
|
|
Assert.NotEqual(0.0, rotation.M11);
|
|
Assert.Approximately(0, rotation.M11, 1e-16);
|
|
var turn = new Affine2D(0, -1, 1, 0, 0, 0);
|
|
Assert.Equal(Affine2D.Identity, turn * turn * turn * turn);
|
|
Assert.Equal(new Affine2D(0, 1, -1, 0, 0, 0), turn.Invert());
|
|
}
|
|
|
|
[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);
|
|
}
|
|
}
|