using Titano.Core.Imaging.Geometry; using Titano.Core.Motion; using Titano.Testing; namespace Titano.Core.Tests.Motion; public sealed class VirtualCameraTests { private static readonly CameraKeyframe[] Path = [ new(0, 0.5, 0.5, 1.0, 0), new(30, 0.3, 0.6, 2.0, 5), new(75, 0.7, 0.4, 1.5, -3), new(100, 0.55, 0.45, 3.0, 0) { Easing = BezierEasing.Linear }, ]; [Test] public void EasedPathHitsKeyframesExactly() { var camera = new VirtualCamera(Path.Reverse()); foreach (var k in Path) Assert.Equal(k.State, camera.Evaluate(k.Frame)); Assert.Equal(Path[0].State, camera.Evaluate(-10)); Assert.Equal(Path[^1].State, camera.Evaluate(250)); } [Test] public void EaseInOutPathIsC1Continuous() { var camera = new VirtualCamera(Path); const double h = 1e-5; foreach (var frame in new[] { 30.0, 75.0 }) { foreach (var component in Components) { var at = component(camera.Evaluate(frame)); var left = (at - component(camera.Evaluate(frame - h))) / h; var right = (component(camera.Evaluate(frame + h)) - at) / h; Assert.Approximately(left, right, 1e-3); Assert.Approximately(0, right, 1e-3); } } // No jumps in velocity anywhere: neighbouring derivatives differ only by curvature × step. foreach (var component in Components) { double? previous = null; for (var t = 0.0; t <= 100; t += 0.05) { var d = (component(camera.Evaluate(t + h)) - component(camera.Evaluate(t - h))) / (2 * h); if (previous is { } p) Assert.Less(double.Abs(d - p), 2e-3); previous = d; } } } [Test] public void EasingPresetsBehave() { for (var t = 0.0; t <= 1.0; t += 0.01) { Assert.Approximately(t, BezierEasing.Linear.Evaluate(t), 1e-9); Assert.Approximately(1, BezierEasing.EaseInOut.Evaluate(t) + BezierEasing.EaseInOut.Evaluate(1 - t), 1e-9); } Assert.Less(BezierEasing.EaseIn.Evaluate(0.5), 0.5); Assert.Greater(BezierEasing.EaseOut.Evaluate(0.5), 0.5); Assert.Equal(0.0, BezierEasing.EaseInOut.Evaluate(0)); Assert.Equal(1.0, BezierEasing.EaseInOut.Evaluate(1)); Assert.Monotonic(Enumerable.Range(0, 101).Select(i => BezierEasing.EaseInOut.Evaluate(i / 100.0))); // CSS "ease" reference value: cubic-bezier(0.25, 0.1, 0.25, 1) at t = 0.5 ≈ 0.8024. Assert.Approximately(0.8024, new BezierEasing(0.25, 0.1, 0.25, 1).Evaluate(0.5), 1e-3); var overshoot = new BezierEasing(0.3, 0, 0.6, 1.6); Assert.Greater(Enumerable.Range(0, 101).Max(i => overshoot.Evaluate(i / 100.0)), 1.0); Assert.Throws(() => _ = new BezierEasing(1.2, 0, 0.5, 1)); } [Test] public void ZoomInterpolatesGeometrically() { var camera = new VirtualCamera([new CameraKeyframe(0, 0.5, 0.5, 1, 0) { Easing = BezierEasing.Linear }, new CameraKeyframe(10, 0.5, 0.5, 4, 0)]); Assert.Approximately(2, camera.Evaluate(5).Zoom, 1e-12); Assert.Throws(() => new VirtualCamera([new CameraKeyframe(0, 0.5, 0.5, 0.5, 0)])); Assert.Throws(() => new VirtualCamera([new CameraKeyframe(3, 0.5, 0.5, 1, 0), new CameraKeyframe(3, 0.2, 0.5, 1, 0)])); } [Test] public void ViewMapsToSourceWindow() { const int w = 1920, h = 1080; Assert.True(VirtualCamera.ViewToSource(new CameraState(0.5, 0.5, 1, 0), w, h).IsIdentity(1e-12)); var zoomed = VirtualCamera.ViewToSource(new CameraState(0.25, 0.5, 2, 0), w, h); var (cx, cy) = zoomed.Apply((w - 1) / 2.0, (h - 1) / 2.0); Assert.Within(479.5, 539.5, cx, cy, 1e-9); // The view's left edge lands half a view-width (w/4 source pixels) left of the centre. var (lx, _) = zoomed.Apply(-0.5, (h - 1) / 2.0); Assert.Approximately(480 - w / 4.0 - 0.5, lx, 1e-9); var rolled = VirtualCamera.ViewToSource(new CameraState(0.5, 0.5, 1, 90), w, h); var (vx, vy) = rolled.ApplyVector(1, 0); Assert.Within(0, 1, vx, vy, 1e-12); } [Test] public void ComposesWithStabilisationIntoOneAffine() { const int w = 640, h = 480; var camera = new VirtualCamera(Path); var correction = Affine2D.About(Affine2D.Similarity(1.01, 0.004, 1.5, -2.25), (w - 1) / 2.0, (h - 1) / 2.0); var combined = camera.ToSource(42.5, w, h, correction); var view = VirtualCamera.ViewToSource(camera.Evaluate(42.5), w, h); foreach (var (x, y) in new[] { (0.0, 0.0), (320.0, 240.0), (639.0, 100.0) }) { // Stabilised coordinates of the source sample must be where the view looks. var (sx, sy) = combined.Apply(x, y); var (tx, ty) = correction.Apply(sx, sy); var (ex, ey) = view.Apply(x, y); Assert.Within(ex, ey, tx, ty, 1e-9); } Assert.Equal(view, camera.ToSource(42.5, w, h)); } private static readonly Func[] Components = [s => s.CenterX, s => s.CenterY, s => s.Zoom, s => s.Rotation / 100]; }