feat(motion): add Bézier-eased virtual camera composed with stabilisation

BezierEasing evaluates CSS-style cubic-bezier timing curves (Newton with
bisection fallback) with linear, ease-in, ease-out and ease-in-out
presets. VirtualCamera interpolates centre, roll and geometric zoom
between keyframes and returns one view-to-source affine composed with
the inverse stabiliser correction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 09:36:42 +00:00
1 parent acde397c52
commit 749eafeebd
3 files changed
+294

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
namespace Titano.Core.Motion;
/// <summary>
/// Cubic Bézier timing curve from (0, 0) to (1, 1) with control points (X1, Y1) and (X2, Y2), as in CSS
/// <c>cubic-bezier()</c>. X1 and X2 must lie in [0, 1] so time is monotonic; Y values may overshoot.
/// </summary>
public readonly record struct BezierEasing
{
public BezierEasing(double x1, double y1, double x2, double y2)
{
if (!(x1 is >= 0 and <= 1) || !(x2 is >= 0 and <= 1)) throw new ArgumentOutOfRangeException(nameof(x1), "Control-point x must be within [0, 1].");
if (!double.IsFinite(y1) || !double.IsFinite(y2)) throw new ArgumentOutOfRangeException(nameof(y1));
(X1, Y1, X2, Y2) = (x1, y1, x2, y2);
}
public double X1 { get; }
public double Y1 { get; }
public double X2 { get; }
public double Y2 { get; }
public static BezierEasing Linear { get; } = new(0, 0, 1, 1);
/// <summary>Starts at rest, arrives at speed.</summary>
public static BezierEasing EaseIn { get; } = new(0.42, 0, 1, 1);
/// <summary>Starts at speed, arrives at rest.</summary>
public static BezierEasing EaseOut { get; } = new(0, 0, 0.58, 1);
/// <summary>At rest at both ends: chains of these segments are C1-continuous.</summary>
public static BezierEasing EaseInOut { get; } = new(0.42, 0, 0.58, 1);
/// <summary>Eased progress y for time fraction t ∈ [0, 1] (exactly 0 and 1 at the ends).</summary>
public double Evaluate(double t)
{
if (t <= 0) return 0;
if (t >= 1) return 1;
return Polynomial(Y1, Y2, Parameter(t));
}
/// <summary>Curve parameter s with x(s) = t: Newton's method from s = t, bisection when it stalls.</summary>
private double Parameter(double t)
{
var s = t;
for (var i = 0; i < 8; i++)
{
var error = Polynomial(X1, X2, s) - t;
if (double.Abs(error) < 1e-12) return s;
var slope = Derivative(X1, X2, s);
if (double.Abs(slope) < 1e-9) break;
var next = s - error / slope;
if (next is < 0 or > 1) break;
s = next;
}
double lo = 0, hi = 1;
s = t;
for (var i = 0; i < 60 && hi - lo > 1e-14; i++)
{
if (Polynomial(X1, X2, s) < t) lo = s;
else hi = s;
s = 0.5 * (lo + hi);
}
return s;
}
/// <summary>B(s) = 3(1−s)²s·p1 + 3(1−s)s²·p2 + s³ in Horner form.</summary>
private static double Polynomial(double p1, double p2, double s)
{
var c = 3 * p1;
var b = 3 * (p2 - p1) - c;
var a = 1 - c - b;
return ((a * s + b) * s + c) * s;
}
private static double Derivative(double p1, double p2, double s)
{
var c = 3 * p1;
var b = 3 * (p2 - p1) - c;
var a = 1 - c - b;
return (3 * a * s + 2 * b) * s + c;
}
}
+87
View File
@@ -0,0 +1,87 @@
using Titano.Core.Imaging.Geometry;
namespace Titano.Core.Motion;
/// <summary>
/// Virtual camera pose: view centre in normalised source coordinates (0..1 across the frame), zoom ≥ 1
/// (1 shows the whole frame) and roll in degrees (positive turns the camera clockwise on screen, so the
/// content appears turned counter-clockwise).
/// </summary>
public readonly record struct CameraState(double CenterX, double CenterY, double Zoom, double Rotation);
/// <summary>A virtual camera keyframe; <see cref="Easing"/> shapes the segment from this keyframe to the next.</summary>
public readonly record struct CameraKeyframe(double Frame, double CenterX, double CenterY, double Zoom, double Rotation)
{
public BezierEasing Easing { get; init; } = BezierEasing.EaseInOut;
public CameraState State => new(CenterX, CenterY, Zoom, Rotation);
}
/// <summary>
/// Ken Burns style pan/zoom/roll: keyframes interpolated with per-segment cubic Bézier easing (centre and
/// roll linearly in the eased progress, zoom geometrically so equal steps look equally fast). The pose
/// becomes one affine that is composed with the stabiliser correction, so the frame is resampled once.
/// </summary>
public sealed class VirtualCamera
{
private readonly CameraKeyframe[] _keyframes;
public VirtualCamera(IEnumerable<CameraKeyframe> keyframes)
{
_keyframes = keyframes.OrderBy(k => k.Frame).ToArray();
if (_keyframes.Length == 0) throw new ArgumentException("At least one keyframe is required.", nameof(keyframes));
for (var i = 0; i < _keyframes.Length; i++)
{
var k = _keyframes[i];
if (!(k.Zoom >= 1) || !double.IsFinite(k.Zoom)) throw new ArgumentOutOfRangeException(nameof(keyframes), "Zoom must be ≥ 1.");
if (!double.IsFinite(k.Frame + k.CenterX + k.CenterY + k.Rotation)) throw new ArgumentOutOfRangeException(nameof(keyframes), "Keyframe values must be finite.");
if (i > 0 && k.Frame == _keyframes[i - 1].Frame) throw new ArgumentException("Two keyframes share a frame.", nameof(keyframes));
}
}
public IReadOnlyList<CameraKeyframe> Keyframes => _keyframes;
/// <summary>Pose at a (possibly fractional) output frame; held constant before the first and after the last keyframe.</summary>
public CameraState Evaluate(double frame)
{
if (frame <= _keyframes[0].Frame) return _keyframes[0].State;
if (frame >= _keyframes[^1].Frame) return _keyframes[^1].State;
var i = 0;
while (_keyframes[i + 1].Frame <= frame) i++;
var a = _keyframes[i];
if (frame == a.Frame) return a.State;
var b = _keyframes[i + 1];
var p = a.Easing.Evaluate((frame - a.Frame) / (b.Frame - a.Frame));
return new CameraState(
a.CenterX + (b.CenterX - a.CenterX) * p,
a.CenterY + (b.CenterY - a.CenterY) * p,
a.Zoom * double.Pow(b.Zoom / a.Zoom, p),
a.Rotation + (b.Rotation - a.Rotation) * p);
}
/// <summary>
/// Maps view pixel-centre coordinates (a frame-sized view) to source pixel-centre coordinates:
/// x_src = C + R(roll)·(x_view − view centre) / zoom.
/// </summary>
public static Affine2D ViewToSource(CameraState state, int sourceWidth, int sourceHeight)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sourceWidth);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sourceHeight);
var centreX = state.CenterX * sourceWidth - 0.5;
var centreY = state.CenterY * sourceHeight - 0.5;
return Affine2D.Translation(centreX, centreY)
* Affine2D.Similarity(1 / state.Zoom, state.Rotation * double.Pi / 180, 0, 0)
* Affine2D.Translation(-(sourceWidth - 1) / 2.0, -(sourceHeight - 1) / 2.0);
}
/// <summary>
/// The single view→source map for an output frame: the camera view composed with the inverse of the
/// stabiliser correction (which maps source to stabilised coordinates). Feed it to
/// <see cref="GeometrySettings.Warp"/>.
/// </summary>
public Affine2D ToSource(double frame, int sourceWidth, int sourceHeight, Affine2D? stabilization = null)
{
var view = ViewToSource(Evaluate(frame), sourceWidth, sourceHeight);
return stabilization is { } correction ? correction.Invert() * view : view;
}
}