feat(render): orient, straighten and let the chosen proportions win in the render geometry

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>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-04 19:35:05 +01:00
1 parent e4364c4f25
commit bee66da63a
18 files changed
+1052 -67

No files matched your search

+60 -49
View File
@@ -3,21 +3,24 @@ using Chronos.Core.Export;
using Chronos.Core.Imaging.Geometry;
using Chronos.Core.Motion;
using Chronos.Core.Project;
using Chronos.Core.Video;
namespace Chronos.Core.Render;
/// <summary>
/// Output size, decode size and per-frame warp of a render. <see cref="CameraSettings.Crop"/> is a normalised
/// [x, y, width, height] rectangle of the camera view; the output aspect is the export's when it fixes both dimensions,
/// otherwise the camera aspect (source = frame aspect). Virtual-camera keyframes are keyed to source frame positions, so
/// they stay attached to the content when timing, frame rate or fitting change. The stabiliser correction, the camera
/// pose (zoomed by the stabilisation zoom) and the crop become one output→source map, resampled once.
/// Output size, decode size and per-frame warp of a render. The decoded frame is turned, mirrored, flipped and
/// straightened into the view frame T (<see cref="FrameOrientation"/>); <see cref="CameraSettings.Crop"/> is a normalised
/// [x, y, width, height] rectangle of T and the keyframe centres are normalised in it. The chosen proportions win
/// (owner review 1, R5): the output ratio is the camera aspect, a fixed-size preset adapting its size to it
/// (<see cref="RenderSize"/>), and "source" keeps the preset's ratio (else the view's). Virtual-camera keyframes are keyed
/// to source frame positions, so they stay attached to the content when timing, frame rate or fitting change. The
/// stabiliser correction, the orientation, the camera pose (zoomed by the stabilisation zoom) and the crop become one
/// output→source map, <c>stab⁻¹ · O⁻¹ · V</c>, resampled once.
/// </summary>
public sealed class RenderGeometry
{
private static readonly CameraState Neutral = new(0.5, 0.5, 1, 0);
private readonly VirtualCamera? _camera;
private readonly FrameOrientation _orientation;
private readonly RectD _crop;
private readonly double _offsetX;
private readonly double _offsetY;
@@ -27,12 +30,13 @@ public sealed class RenderGeometry
private readonly double _maxZoom;
private readonly int _maxWorkingLongEdge;
private RenderGeometry(VirtualCamera? camera, RectD crop, double offsetX, double offsetY, double stabilizationZoom, int sourceWidth, int sourceHeight,
private RenderGeometry(VirtualCamera? camera, FrameOrientation orientation, RectD crop, double offsetX, double offsetY, double stabilizationZoom, int sourceWidth, int sourceHeight,
int outputWidth, int outputHeight, double fittedWidth, double fittedHeight, double maxZoom, int maxWorkingLongEdge, IReadOnlyList<RenderWarning> warnings,
double outputRatio)
{
OutputRatio = outputRatio;
_camera = camera;
_orientation = orientation;
_crop = crop;
_offsetX = offsetX;
_offsetY = offsetY;
@@ -57,10 +61,13 @@ public sealed class RenderGeometry
/// Width of the view frame T at full resolution (<see cref="FrameOrientation"/>): the source turned, mirrored and
/// straightened. <see cref="CameraSettings.Crop"/> and the keyframe centres are normalised in it.
/// </summary>
public int ViewWidth => SourceWidth;
public int ViewWidth => _orientation.ViewWidth;
/// <inheritdoc cref="ViewWidth"/>
public int ViewHeight => SourceHeight;
public int ViewHeight => _orientation.ViewHeight;
/// <summary>The camera's orientation at full resolution, its values as rendered (<see cref="FrameOrientation.Of(CameraSettings, int, int)"/>).</summary>
public FrameOrientation Orientation => _orientation;
/// <summary>The output ratio (width / height) the crop is fitted to (<see cref="RenderSize.Ratio"/>), before rounding to whole pixels.</summary>
public double OutputRatio { get; }
@@ -81,8 +88,9 @@ public sealed class RenderGeometry
public IReadOnlyList<RenderWarning> Warnings { get; }
/// <summary>
/// Long edge to decode source frames at: enough that the most zoomed-in view still has one source pixel per output
/// pixel, never more than the source, capped by <see cref="RenderOptions.MaxWorkingLongEdge"/>.
/// Long edge to decode source frames at: enough that the most zoomed-in view (keyframe zoom × stabilisation zoom ×
/// straighten cover zoom) still has one source pixel per output pixel, never more than the source, capped by
/// <see cref="RenderOptions.MaxWorkingLongEdge"/>. Quarter turns do not change the long edge.
/// </summary>
public int WorkingLongEdge
{
@@ -105,46 +113,27 @@ public sealed class RenderGeometry
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sourceWidth);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sourceHeight);
var warnings = new List<RenderWarning>();
var orientation = OrientationOf(camera, sourceWidth, sourceHeight, warnings);
var crop = NormalisedCrop(camera.Crop, warnings);
if (!AspectRatio.TryParse(camera.Aspect.Resolve("source"), out var aspect))
{
if (!AspectRatio.TryParse(camera.Aspect.Resolve("source"), out _))
warnings.Add(new RenderWarning("Export.Render.AspectIgnored", camera.Aspect.Resolve(string.Empty)));
aspect = AspectRatio.Source;
}
var fixedSize = export.Width > 0 && export.Height > 0;
var ratio = fixedSize ? export.Width / (double)export.Height : aspect.Resolve(sourceWidth, sourceHeight);
if (fixedSize && !aspect.IsSource && double.Abs(aspect.Resolve(sourceWidth, sourceHeight) / ratio - 1) > 0.01)
warnings.Add(new RenderWarning("Export.Render.AspectFromPreset", aspect.ToString(), string.Create(CultureInfo.InvariantCulture, $"{export.Width}:{export.Height}")));
var cropPixels = new RectD(crop.X * sourceWidth, crop.Y * sourceHeight, crop.Width * sourceWidth, crop.Height * sourceHeight);
// The chosen proportions win: a fixed-size preset is a box the aspect is fitted in (RenderSize).
var ratio = RenderSize.Ratio(camera, export, sourceWidth, sourceHeight);
var (viewWidth, viewHeight) = (orientation.ViewWidth, orientation.ViewHeight);
var cropPixels = new RectD(crop.X * viewWidth, crop.Y * viewHeight, crop.Width * viewWidth, crop.Height * viewHeight);
var fitted = AspectRatio.FitCrop(cropPixels, ratio, camera.OffsetX, camera.OffsetY);
int width, height;
if (fixedSize) (width, height) = (export.Width, export.Height);
else if (export.Width > 0) (width, height) = (export.Width, (int)double.Round(export.Width / ratio));
else if (export.Height > 0) (width, height) = ((int)double.Round(export.Height * ratio), export.Height);
else
{
var scale = maxWorkingLongEdge > 0 ? double.Min(1, maxWorkingLongEdge / double.Max(fitted.Width, fitted.Height)) : 1;
(width, height) = ((int)double.Round(fitted.Width * scale), (int)double.Round(fitted.Height * scale));
}
if (!VideoCodecInfo.IsManaged(export.Codec))
{
width &= ~1;
height &= ~1;
}
width = int.Max(VideoCodecInfo.IsManaged(export.Codec) ? 1 : 2, width);
height = int.Max(VideoCodecInfo.IsManaged(export.Codec) ? 1 : 2, height);
var (width, height) = RenderSize.Of(camera, export, sourceWidth, sourceHeight, maxWorkingLongEdge);
var cameraPath = Camera(camera.Keyframes, warnings);
var maxZoom = (cameraPath?.Keyframes.Max(k => k.Zoom) ?? 1) * stabilizationZoom;
return new RenderGeometry(cameraPath, crop, camera.OffsetX, camera.OffsetY, stabilizationZoom, sourceWidth, sourceHeight, width, height,
var maxZoom = (cameraPath?.Keyframes.Max(k => k.Zoom) ?? 1) * stabilizationZoom * orientation.CoverZoom;
return new RenderGeometry(cameraPath, orientation, crop, camera.OffsetX, camera.OffsetY, stabilizationZoom, sourceWidth, sourceHeight, width, height,
fitted.Width, fitted.Height, maxZoom, maxWorkingLongEdge, warnings, ratio);
}
/// <summary>The same geometry rendering into a different output size (e.g. capped by the encoder).</summary>
public RenderGeometry WithOutputSize(int width, int height) => width == OutputWidth && height == OutputHeight
? this
: new RenderGeometry(_camera, _crop, _offsetX, _offsetY, _stabilizationZoom, SourceWidth, SourceHeight, width, height, _fittedWidth, _fittedHeight,
: new RenderGeometry(_camera, _orientation, _crop, _offsetX, _offsetY, _stabilizationZoom, SourceWidth, SourceHeight, width, height, _fittedWidth, _fittedHeight,
_maxZoom, _maxWorkingLongEdge, Warnings, OutputRatio);
/// <summary>Camera pose at a (fractional) source position, including the stabilisation zoom.</summary>
@@ -154,14 +143,21 @@ public sealed class RenderGeometry
return pose with { Zoom = pose.Zoom * _stabilizationZoom };
}
/// <summary>Geometry of one decoded frame of <paramref name="frameWidth"/>×<paramref name="frameHeight"/> pixels.</summary>
/// <summary>
/// Geometry of one decoded frame of <paramref name="frameWidth"/>×<paramref name="frameHeight"/> pixels: the crop in the
/// view frame, and the warp <c>stab⁻¹ · O⁻¹ · V</c> from the camera view (the pose in the view frame) through the view
/// frame to the stabilised and then the decoded frame.
/// </summary>
/// <param name="stabilization">The frame's stabiliser correction at this frame size (source → stabilised).</param>
public GeometrySettings For(int frameWidth, int frameHeight, double sourcePosition, Affine2D stabilization)
{
var view = VirtualCamera.ViewToSource(PoseAt(sourcePosition), frameWidth, frameHeight);
var orientation = _orientation.Resized(frameWidth, frameHeight);
var (viewWidth, viewHeight) = (orientation.ViewWidth, orientation.ViewHeight);
var view = VirtualCamera.ViewToSource(PoseAt(sourcePosition), viewWidth, viewHeight);
if (!orientation.IsIdentity) view = orientation.ViewToFrame * view;
return new GeometrySettings
{
Crop = new RectD(_crop.X * frameWidth, _crop.Y * frameHeight, _crop.Width * frameWidth, _crop.Height * frameHeight),
Crop = new RectD(_crop.X * viewWidth, _crop.Y * viewHeight, _crop.Width * viewWidth, _crop.Height * viewHeight),
Aspect = AspectRatio.Source,
OffsetX = _offsetX,
OffsetY = _offsetY,
@@ -181,7 +177,7 @@ public sealed class RenderGeometry
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(frameWidth);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(frameHeight);
return Affine2D.Identity;
return _orientation.Resized(frameWidth, frameHeight).ViewToFrame;
}
/// <summary>
@@ -192,12 +188,14 @@ public sealed class RenderGeometry
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(outputWidth);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(outputHeight);
var orientation = _orientation.Resized(frameWidth, frameHeight);
return new GeometrySettings
{
Crop = RectD.FromSize(orientation.ViewWidth, orientation.ViewHeight),
Aspect = AspectRatio.Source,
OutputWidth = outputWidth,
OutputHeight = outputHeight,
Warp = ViewToFrame(frameWidth, frameHeight),
Warp = orientation.ViewToFrame,
Border = BorderMode.Clamp,
};
}
@@ -212,21 +210,34 @@ public sealed class RenderGeometry
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(frameWidth);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(frameHeight);
if (plan is not { } p) return new RectD(0, 0, 1, 1);
var toView = ViewToFrame(frameWidth, frameHeight).Invert() * p.OutputToSource;
var orientation = _orientation.Resized(frameWidth, frameHeight);
var toView = orientation.FrameToView * p.OutputToSource;
double left = double.MaxValue, top = double.MaxValue, right = double.MinValue, bottom = double.MinValue;
foreach (var (u, v) in new[] { (-0.5, -0.5), (p.OutputWidth - 0.5, -0.5), (-0.5, p.OutputHeight - 0.5), (p.OutputWidth - 0.5, p.OutputHeight - 0.5) })
{
var (x, y) = toView.Apply(u, v);
(left, top, right, bottom) = (double.Min(left, x + 0.5), double.Min(top, y + 0.5), double.Max(right, x + 0.5), double.Max(bottom, y + 0.5));
}
var (viewWidth, viewHeight) = ViewSizeOf(frameWidth, frameHeight);
var (viewWidth, viewHeight) = (orientation.ViewWidth, orientation.ViewHeight);
left = double.Clamp(left / viewWidth, 0, 1);
top = double.Clamp(top / viewHeight, 0, 1);
return new RectD(left, top, double.Clamp(right / viewWidth, 0, 1) - left, double.Clamp(bottom / viewHeight, 0, 1) - top);
}
/// <summary>The view frame's size for a decoded frame of the given size.</summary>
private static (int Width, int Height) ViewSizeOf(int frameWidth, int frameHeight) => (frameWidth, frameHeight);
/// <summary>
/// The camera's orientation as rendered: a rotation that is not a quarter turn counts as none
/// (<c>Export.Render.RotationIgnored</c>), a straighten beyond ±15° (or not finite) is clamped
/// (<c>Export.Render.StraightenClamped</c>).
/// </summary>
private static FrameOrientation OrientationOf(CameraSettings camera, int sourceWidth, int sourceHeight, List<RenderWarning> warnings)
{
if (!FrameOrientation.IsValidRotate(camera.Rotate))
warnings.Add(new RenderWarning("Export.Render.RotationIgnored", camera.Rotate.ToString(CultureInfo.InvariantCulture)));
if (!FrameOrientation.IsValidStraighten(camera.Straighten))
warnings.Add(new RenderWarning("Export.Render.StraightenClamped", camera.Straighten.ToString(CultureInfo.InvariantCulture),
FrameOrientation.StraightenOf(camera.Straighten).ToString(CultureInfo.InvariantCulture)));
return FrameOrientation.Of(camera, sourceWidth, sourceHeight);
}
private static RectD NormalisedCrop(double[]? crop, List<RenderWarning> warnings)
{