The Camera step now shows its crop box on the Source picture with its mask on
by default, and the box is moved and resized right there (owner review 1, R5).
A press takes a handle within 6 px, then the wipe divider, then the inside of
the box, then pans; a resize stays anchored at the opposite corner or edge and
locked to the ratio, Alt resizes about the centre. Esc cancels a drag, and once
the box is taken (a press on it or Enter) the arrows, + and - nudge it. Cursors
follow the same priority and screen readers read "x, y, size" from a read-only
value pattern.
Each finished drag goes through CropCommit to CommitCropCommand as one undo
step; a run of key presses merges into one ("Camera.Crop"). The commit writes
the locked box with the offsets at 0, so the render keeps it as drawn. Editing
is gated on the Camera step, the mask on, an editable project, a current
format and frames to show.
PreviewRenderer frames Source through ViewFor, so the turned, mirrored and
straightened view frame is what the box is drawn on; the mask comes from
EditableCrop on the Camera step and FootprintInView elsewhere, and the horizon
overlay is mapped into the view. A straighten grid shows while IsStraightening.
xaml-lint now requires a tooltip on PreviewViewport.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
145 lines
6.7 KiB
C#
145 lines
6.7 KiB
C#
using Chronos.Core.Imaging.Geometry;
|
||
using Chronos.Testing;
|
||
using Chronos.Ui.Visuals;
|
||
|
||
namespace Chronos.App.Tests.Visuals;
|
||
|
||
public sealed class ViewportMathTests
|
||
{
|
||
[Test]
|
||
public void FitUsesTheLimitingAxisAndCentres()
|
||
{
|
||
// 8192×5464 into 900×430: height limits.
|
||
var scale = ViewportMath.FitScale(8192, 5464, 900, 430);
|
||
Assert.Approximately(430.0 / 5464, scale, 1e-12);
|
||
var t = ViewportMath.Centered(scale, 8192, 5464, 900, 430);
|
||
Assert.Approximately(0, t.OffsetY, 1e-9);
|
||
Assert.Approximately((900 - 8192 * scale) / 2, t.OffsetX, 1e-9);
|
||
Assert.Approximately(0, ViewportMath.FitScale(0, 100, 900, 430), 0);
|
||
}
|
||
|
||
[Test]
|
||
public void ZoomAtKeepsThePointUnderTheCursorFixed()
|
||
{
|
||
var t = new ViewTransform(0.25, 37, -12);
|
||
var (ix, iy) = t.ToImage(410, 220);
|
||
var zoomed = ViewportMath.ZoomAt(t, 1.7, 410, 220);
|
||
var (vx, vy) = zoomed.ToView(ix, iy);
|
||
Assert.Within(410, 220, vx, vy, 1e-9);
|
||
Assert.Approximately(1.7, zoomed.Scale, 0);
|
||
}
|
||
|
||
[Test]
|
||
public void WheelZoomStepsGeometricallyAndClamps()
|
||
{
|
||
Assert.Approximately(1.25, ViewportMath.WheelZoom(1, 1, 0.01, 32), 1e-12);
|
||
Assert.Approximately(1 / 1.5625, ViewportMath.WheelZoom(1, -2, 0.01, 32), 1e-12);
|
||
Assert.Approximately(32, ViewportMath.WheelZoom(30, 3, 0.01, 32), 0);
|
||
}
|
||
|
||
[Test]
|
||
public void ClampCentresSmallImagesAndCoversWithLargeOnes()
|
||
{
|
||
// Image 400×300 at scale 1 inside 900×430: centred on both axes whatever the offset.
|
||
var small = ViewportMath.Clamp(new ViewTransform(1, -500, 999), 400, 300, 900, 430);
|
||
Assert.Approximately(250, small.OffsetX, 1e-9);
|
||
Assert.Approximately(65, small.OffsetY, 1e-9);
|
||
|
||
// Image 4000×3000 at scale 1: the view must stay covered.
|
||
var large = ViewportMath.Clamp(new ViewTransform(1, 100, -5000), 4000, 3000, 900, 430);
|
||
Assert.Approximately(0, large.OffsetX, 1e-9);
|
||
Assert.Approximately(430 - 3000, large.OffsetY, 1e-9);
|
||
}
|
||
|
||
[Test]
|
||
public void PanMovesAndRoundTripsThroughImageCoordinates()
|
||
{
|
||
var t = ViewportMath.Pan(new ViewTransform(2, 10, 20), -4, 6);
|
||
Assert.Approximately(6, t.OffsetX, 0);
|
||
Assert.Approximately(26, t.OffsetY, 0);
|
||
var (x, y) = t.ToView(123.5, 77.25);
|
||
var (ix, iy) = t.ToImage(x, y);
|
||
Assert.Within(123.5, 77.25, ix, iy, 1e-12);
|
||
}
|
||
|
||
[Test]
|
||
public void SnapOffsetAlignsToDevicePixels()
|
||
{
|
||
// 150 % DPI: 1.5 device pixels per DIP.
|
||
var t = ViewportMath.SnapOffset(new ViewTransform(1, 10.4, -3.1), 1.5);
|
||
Assert.Approximately(Math.Round(10.4 * 1.5), t.OffsetX * 1.5, 1e-9);
|
||
Assert.Approximately(Math.Round(-3.1 * 1.5), t.OffsetY * 1.5, 1e-9);
|
||
}
|
||
|
||
[Test]
|
||
public void ACropPressTakesAHandleWithinTheGripThenTheInsideAtEveryZoom()
|
||
{
|
||
// A 400×300 image with the box [0.25, 0.2, 0.5, 0.6]: the grip is 6 view units whatever the zoom.
|
||
var crop = new RectD(0.25, 0.2, 0.5, 0.6);
|
||
foreach (var view in new[] { new ViewTransform(0.25, 40, 10), new ViewTransform(1, -30, 5), new ViewTransform(4, -500, -300) })
|
||
{
|
||
CropHandle Hit(double nx, double ny, double dx, double dy)
|
||
{
|
||
var (x, y) = view.ToView(nx * 400, ny * 300);
|
||
return ViewportMath.HitCrop(view, 400, 300, crop, x + dx, y + dy, 6);
|
||
}
|
||
var zoom = $"scale {view.Scale}";
|
||
Assert.Equal(CropHandle.TopLeft, Hit(0.25, 0.2, -5, 4), zoom);
|
||
Assert.Equal(CropHandle.BottomRight, Hit(0.75, 0.8, 6, -6), zoom);
|
||
Assert.Equal(CropHandle.Top, Hit(0.5, 0.2, 3, -2), zoom);
|
||
Assert.Equal(CropHandle.Left, Hit(0.25, 0.5, 2, 0), zoom);
|
||
Assert.Equal(CropHandle.Inside, Hit(0.25, 0.5, 0, 7), "beside a handle, inside the box: a move");
|
||
Assert.Equal(CropHandle.None, Hit(0.25, 0.5, -7, 0), "outside the box and the grip: a pan");
|
||
Assert.Equal(CropHandle.Inside, Hit(0.5, 0.5, 0, 0), zoom);
|
||
Assert.Equal(CropHandle.None, Hit(0.1, 0.1, 0, 0), zoom);
|
||
}
|
||
Assert.Equal(CropHandle.None, ViewportMath.HitCrop(new ViewTransform(0, 0, 0), 400, 300, crop, 0, 0, 6), "no view yet");
|
||
}
|
||
|
||
[Test]
|
||
public void TheNearestHandleWinsOnATinyBox()
|
||
{
|
||
// At 1/4 zoom a box of 40×30 image pixels is 10×7.5 view units: every handle is within the grip of the centre.
|
||
var view = new ViewTransform(0.25, 0, 0);
|
||
var crop = new RectD(0.45, 0.45, 0.1, 0.1);
|
||
var (x, y) = view.ToView(0.55 * 400, 0.55 * 300);
|
||
Assert.Equal(CropHandle.BottomRight, ViewportMath.HitCrop(view, 400, 300, crop, x - 1, y - 1, 6));
|
||
var (cx, cy) = view.ToView(0.5 * 400, 0.45 * 300);
|
||
Assert.Equal(CropHandle.Top, ViewportMath.HitCrop(view, 400, 300, crop, cx, cy + 0.5, 6));
|
||
}
|
||
|
||
[Test]
|
||
public void TheHandlesSitOnTheCornersAndTheEdgeMidpoints()
|
||
{
|
||
var crop = new RectD(0.1, 0.2, 0.4, 0.6);
|
||
Assert.Count(8, ViewportMath.CropHandles);
|
||
Assert.Within(0.1, 0.2, ViewportMath.HandleAt(crop, CropHandle.TopLeft).X, ViewportMath.HandleAt(crop, CropHandle.TopLeft).Y, 1e-12);
|
||
Assert.Within(0.3, 0.8, ViewportMath.HandleAt(crop, CropHandle.Bottom).X, ViewportMath.HandleAt(crop, CropHandle.Bottom).Y, 1e-12);
|
||
Assert.Within(0.5, 0.5, ViewportMath.HandleAt(crop, CropHandle.Right).X, ViewportMath.HandleAt(crop, CropHandle.Right).Y, 1e-12);
|
||
Assert.Within(0.3, 0.5, ViewportMath.HandleAt(crop, CropHandle.Inside).X, ViewportMath.HandleAt(crop, CropHandle.Inside).Y, 1e-12);
|
||
}
|
||
|
||
[Test]
|
||
public void PlusAndMinusResizeAboutTheCentreLockedAndWithinTheLimits()
|
||
{
|
||
const double ratio = 0.5;
|
||
var largest = CropFrame.Largest(ratio);
|
||
var box = CropFrame.FromRows(0.5, 0, 0, ratio);
|
||
var grown = ViewportMath.ResizeCrop(box, 0.02, ratio);
|
||
Assert.Approximately(box.Height + 0.02 * largest.Height, grown.Height, 1e-12);
|
||
Assert.Approximately(ratio, grown.Width / grown.Height, 1e-12);
|
||
Assert.Within(box.CenterX, box.CenterY, grown.CenterX, grown.CenterY, 1e-12);
|
||
|
||
var smallest = CropFrame.FromRows(CropFrame.MinimumShare, 0, 0, ratio);
|
||
Assert.Approximately(smallest.Height, ViewportMath.ResizeCrop(smallest, -0.02, ratio).Height, 1e-12, "not below the minimum");
|
||
var whole = ViewportMath.ResizeCrop(largest, 0.02, ratio);
|
||
Assert.Approximately(largest.Height, whole.Height, 1e-12, "not beyond the largest box");
|
||
|
||
// At an edge the grown box moves back inside the view.
|
||
var corner = CropFrame.FromRows(0.9, 1, 1, ratio);
|
||
var pushed = ViewportMath.ResizeCrop(corner, 0.08, ratio);
|
||
Assert.LessOrEqual(pushed.Right, 1 + 1e-12);
|
||
Assert.LessOrEqual(pushed.Bottom, 1 + 1e-12);
|
||
}
|
||
}
|