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>
180 lines
8.5 KiB
C#
180 lines
8.5 KiB
C#
using Chronos.Core.Imaging.Geometry;
|
||
using Chronos.Core.Project;
|
||
using Chronos.Testing;
|
||
|
||
namespace Chronos.Core.Tests.Geometry;
|
||
|
||
public sealed class OrientationEditTests
|
||
{
|
||
private static readonly OrientationOp[] Ops = [OrientationOp.RotateLeft, OrientationOp.RotateRight, OrientationOp.Mirror, OrientationOp.Flip];
|
||
|
||
private static ProjectSettings Framed { get; } = new()
|
||
{
|
||
Camera = new CameraSettings
|
||
{
|
||
Crop = CropFrame.ToCrop(CropFrame.FromRows(0.7, 1 / 3.0, -0.2, 16 / 9.0 * 4000 / 6000)),
|
||
Aspect = new Auto<string>().WithManual("16:9").WithAdvice("3:2", null),
|
||
OffsetX = 0.25,
|
||
OffsetY = -0.5,
|
||
Keyframes = [new CameraKey(0, 0.25, 0.4, 1.5, 3), new CameraKey(9, 0.6, 0.125, 2, -1.5, "Linear")],
|
||
Straighten = 2.5,
|
||
},
|
||
PendingFrames = [new PendingFrame { Sha256 = "ab", Keyframes = [new CameraKey(4, 0.75, 0.5, 1, 2)] }],
|
||
};
|
||
|
||
[Test]
|
||
public void FourRightTurnsGiveBackTheText()
|
||
{
|
||
var text = Framed.ToJson();
|
||
var settings = Framed;
|
||
for (var k = 0; k < 4; k++)
|
||
{
|
||
settings = OrientationEdit.Apply(settings, OrientationOp.RotateRight);
|
||
Assert.Equal((k + 1) % 4 * 90, settings.Camera.Rotate);
|
||
if (k < 3) Assert.NotEqual(text, settings.ToJson());
|
||
}
|
||
Assert.Equal(text, settings.ToJson());
|
||
|
||
// Without a straighten, the turns leave nothing of format 2 behind.
|
||
var plain = new ProjectSettings { Camera = new CameraSettings { Crop = [0.1, 0.2, 0.3, 0.4] } };
|
||
var turned = plain;
|
||
for (var k = 0; k < 4; k++) turned = OrientationEdit.Apply(turned, OrientationOp.RotateRight);
|
||
Assert.Equal(plain.ToJson(), turned.ToJson());
|
||
Assert.Equal(ProjectSettings.BaseVersion, turned.WrittenFormat);
|
||
}
|
||
|
||
[Test]
|
||
public void EveryPressIsUndoneByItsOpposite()
|
||
{
|
||
var text = Framed.ToJson();
|
||
foreach (var start in new[] { Framed, OrientationEdit.Apply(Framed, OrientationOp.Mirror), OrientationEdit.Apply(Framed, OrientationOp.RotateLeft) })
|
||
{
|
||
var before = start.ToJson();
|
||
Assert.Equal(before, OrientationEdit.Apply(OrientationEdit.Apply(start, OrientationOp.RotateRight), OrientationOp.RotateLeft).ToJson());
|
||
Assert.Equal(before, OrientationEdit.Apply(OrientationEdit.Apply(start, OrientationOp.Mirror), OrientationOp.Mirror).ToJson());
|
||
Assert.Equal(before, OrientationEdit.Apply(OrientationEdit.Apply(start, OrientationOp.Flip), OrientationOp.Flip).ToJson());
|
||
}
|
||
Assert.Equal(text, Framed.ToJson());
|
||
}
|
||
|
||
[Test]
|
||
public void MirrorMovesTheCropAndNegatesTheAngles()
|
||
{
|
||
var camera = new CameraSettings
|
||
{
|
||
Crop = [0.1, 0.2, 0.3, 0.4],
|
||
OffsetX = 0.5,
|
||
OffsetY = 0.25,
|
||
Keyframes = [new CameraKey(0, 0.2, 0.7, 2, 5)],
|
||
Straighten = 3,
|
||
};
|
||
var mirrored = OrientationEdit.Apply(camera, OrientationOp.Mirror);
|
||
Assert.True(mirrored.Mirror);
|
||
Assert.SequenceEqual([0.6, 0.2, 0.3, 0.4], mirrored.Crop!);
|
||
Assert.Equal((-0.5, 0.25), (mirrored.OffsetX, mirrored.OffsetY));
|
||
Assert.Equal(new CameraKey(0, 0.8, 0.7, 2, -5), mirrored.Keyframes.Single());
|
||
Assert.Equal(-3.0, mirrored.Straighten);
|
||
Assert.Equal(camera.Aspect, mirrored.Aspect);
|
||
|
||
var flipped = OrientationEdit.Apply(camera, OrientationOp.Flip);
|
||
Assert.True(flipped.Flip);
|
||
Assert.SequenceEqual([0.1, 0.4, 0.3, 0.4], flipped.Crop!);
|
||
Assert.Equal((0.5, -0.25), (flipped.OffsetX, flipped.OffsetY));
|
||
Assert.Equal(new CameraKey(0, 0.2, 0.3, 2, -5), flipped.Keyframes.Single());
|
||
|
||
// A zero stays a zero (JSON would write −0 as "-0").
|
||
Assert.False(double.IsNegative(OrientationEdit.Apply(new CameraSettings(), OrientationOp.Mirror).Straighten));
|
||
}
|
||
|
||
[Test]
|
||
public void AQuarterTurnTransposesTheCropAndTheAspect()
|
||
{
|
||
var camera = new CameraSettings
|
||
{
|
||
Crop = [0.1, 0.2, 0.3, 0.4],
|
||
Aspect = new Auto<string>().WithManual("16:9").WithAdvice("4:3", null),
|
||
OffsetX = 0.5,
|
||
OffsetY = 0.25,
|
||
Keyframes = [new CameraKey(0, 0.2, 0.7, 2, 5)],
|
||
Straighten = 3,
|
||
};
|
||
var right = OrientationEdit.Apply(camera, OrientationOp.RotateRight);
|
||
Assert.Equal(90, right.Rotate);
|
||
Assert.SequenceEqual([0.4, 0.1, 0.4, 0.3], right.Crop!);
|
||
Assert.Equal((-0.25, 0.5), (right.OffsetX, right.OffsetY));
|
||
Assert.Equal(new CameraKey(0, 0.3, 0.2, 2, 5), right.Keyframes.Single());
|
||
Assert.Equal("9:16", right.Aspect.Manual);
|
||
Assert.Equal("3:4", right.Aspect.AutoValue);
|
||
Assert.Equal(3.0, right.Straighten);
|
||
|
||
var left = OrientationEdit.Apply(camera, OrientationOp.RotateLeft);
|
||
Assert.Equal(270, left.Rotate);
|
||
Assert.SequenceEqual([0.2, 0.6, 0.4, 0.3], left.Crop!);
|
||
|
||
// "source", a square and text that cannot be read stay as they are.
|
||
foreach (var aspect in new[] { "source", "1:1", "wide" })
|
||
Assert.Equal(aspect, OrientationEdit.Transposed(new Auto<string>().WithManual(aspect)).Manual);
|
||
Assert.Null(OrientationEdit.Transposed(new Auto<string>()).Manual);
|
||
}
|
||
|
||
[Test]
|
||
public void ATurnSeenThroughOneMirrorIsStoredTheOtherWay()
|
||
{
|
||
Assert.Equal(270, OrientationEdit.Apply(new CameraSettings { Mirror = true }, OrientationOp.RotateRight).Rotate);
|
||
Assert.Equal(90, OrientationEdit.Apply(new CameraSettings { Flip = true }, OrientationOp.RotateLeft).Rotate);
|
||
Assert.Equal(90, OrientationEdit.Apply(new CameraSettings { Mirror = true, Flip = true }, OrientationOp.RotateRight).Rotate);
|
||
Assert.Equal(0, OrientationEdit.Apply(new CameraSettings { Rotate = 90 }, OrientationOp.RotateLeft).Rotate);
|
||
Assert.Equal(0, OrientationEdit.Apply(new CameraSettings { Rotate = -90 }, OrientationOp.RotateRight).Rotate);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Each press moves the view as the user sees it: a frame point shown at normalised (x, y) before the press is shown
|
||
/// at <see cref="OrientationEdit.MapPoint"/>(x, y) after it, whatever the stored turn, mirror, flip and straighten.
|
||
/// </summary>
|
||
[Test]
|
||
public void PressesMoveThePictureAsShown()
|
||
{
|
||
const int width = 60, height = 40;
|
||
foreach (var rotate in new[] { 0, 90, 180, 270 })
|
||
{
|
||
foreach (var (mirror, flip) in new[] { (false, false), (true, false), (false, true), (true, true) })
|
||
{
|
||
foreach (var straighten in new[] { 0.0, 6.5 })
|
||
{
|
||
var camera = new CameraSettings { Rotate = rotate, Mirror = mirror, Flip = flip, Straighten = straighten };
|
||
var before = FrameOrientation.Of(camera, width, height);
|
||
foreach (var op in Ops)
|
||
{
|
||
var after = FrameOrientation.Of(OrientationEdit.Apply(camera, op), width, height);
|
||
foreach (var (px, py) in new[] { (0.0, 0.0), (59.0, 0.0), (12.5, 30.0), (30.0, 20.0), (44.0, 7.25) })
|
||
{
|
||
var (bx, by) = Normalised(before, px, py);
|
||
var (ax, ay) = Normalised(after, px, py);
|
||
var (ex, ey) = OrientationEdit.MapPoint(bx, by, op);
|
||
Assert.Within(ex, ey, ax, ay, 1e-9, $"{op} on rotate {rotate}, mirror {mirror}, flip {flip}, straighten {straighten} at ({px}, {py})");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
[Test]
|
||
public void ParkedKeyframesMoveWithTheProject()
|
||
{
|
||
var turned = OrientationEdit.Apply(Framed, OrientationOp.RotateRight);
|
||
Assert.Equal(new CameraKey(4, 0.5, 0.75, 1, 2), turned.PendingFrames!.Single().Keyframes!.Single());
|
||
Assert.Equal("ab", turned.PendingFrames!.Single().Sha256);
|
||
var mirrored = OrientationEdit.Apply(Framed, OrientationOp.Mirror);
|
||
Assert.Equal(new CameraKey(4, 0.25, 0.5, 1, -2), mirrored.PendingFrames!.Single().Keyframes!.Single());
|
||
Assert.Null(OrientationEdit.Apply(new ProjectSettings(), OrientationOp.Flip).PendingFrames);
|
||
}
|
||
|
||
/// <summary>A frame pixel centre in normalised edge coordinates of the view.</summary>
|
||
private static (double X, double Y) Normalised(FrameOrientation orientation, double x, double y)
|
||
{
|
||
var (vx, vy) = orientation.FrameToView.Apply(x, y);
|
||
return ((vx + 0.5) / orientation.ViewWidth, (vy + 0.5) / orientation.ViewHeight);
|
||
}
|
||
}
|