feat(ui-visuals): add layout, scale, decimation, viewport and curve math kernels

WPF-free geometry behind the visual controls: wrapped-grid layout with O(1)
index/offset mapping and keyboard targets, nice axis ticks, min/max
decimation, zoom/pan transforms, monotone curve editing rules, filmstrip and
heatmap cell layout, an LRU cache and device-pixel snapping, with tests.

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 10:03:20 +00:00
1 parent c40b6a7cd3
commit 661be6c94b
16 files changed
+1410

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
namespace Titano.Ui.Visuals;
/// <summary>Uniform scale and translation from image pixels to view units: view = image × <see cref="Scale"/> + offset.</summary>
public readonly record struct ViewTransform(double Scale, double OffsetX, double OffsetY)
{
public (double X, double Y) ToView(double x, double y) => (x * Scale + OffsetX, y * Scale + OffsetY);
public (double X, double Y) ToImage(double x, double y) => ((x - OffsetX) / Scale, (y - OffsetY) / Scale);
}
/// <summary>Zoom and pan math of the preview: fit, zoom around a fixed point, panning limits and pixel alignment.</summary>
public static class ViewportMath
{
/// <summary>Largest scale at which the whole image fits the view (0 when either is empty).</summary>
public static double FitScale(double imageWidth, double imageHeight, double viewWidth, double viewHeight)
{
if (!(imageWidth > 0) || !(imageHeight > 0) || !(viewWidth > 0) || !(viewHeight > 0)) return 0;
return Math.Min(viewWidth / imageWidth, viewHeight / imageHeight);
}
/// <summary>The image at <paramref name="scale"/>, centred in the view.</summary>
public static ViewTransform Centered(double scale, double imageWidth, double imageHeight, double viewWidth, double viewHeight) =>
new(scale, (viewWidth - imageWidth * scale) / 2, (viewHeight - imageHeight * scale) / 2);
/// <summary>
/// Changes the scale to <paramref name="scale"/> keeping the image point under (<paramref name="viewX"/>,
/// <paramref name="viewY"/>) where it is.
/// </summary>
public static ViewTransform ZoomAt(ViewTransform t, double scale, double viewX, double viewY)
{
if (!(t.Scale > 0) || !(scale > 0)) return t;
var k = scale / t.Scale;
return new ViewTransform(scale, viewX - (viewX - t.OffsetX) * k, viewY - (viewY - t.OffsetY) * k);
}
/// <summary>
/// Keeps the image covering the view on each axis where it is larger than the view, and centred on each axis
/// where it is smaller, so panning can never lose the picture.
/// </summary>
public static ViewTransform Clamp(ViewTransform t, double imageWidth, double imageHeight, double viewWidth, double viewHeight) =>
t with
{
OffsetX = ClampAxis(t.OffsetX, imageWidth * t.Scale, viewWidth),
OffsetY = ClampAxis(t.OffsetY, imageHeight * t.Scale, viewHeight),
};
/// <summary>Moves the offset by (<paramref name="dx"/>, <paramref name="dy"/>) view units.</summary>
public static ViewTransform Pan(ViewTransform t, double dx, double dy) => t with { OffsetX = t.OffsetX + dx, OffsetY = t.OffsetY + dy };
/// <summary>Rounds the offset to whole device pixels (<paramref name="pixelsPerUnit"/> device pixels per view unit) so 1:1 shows raw pixels.</summary>
public static ViewTransform SnapOffset(ViewTransform t, double pixelsPerUnit)
{
if (!(pixelsPerUnit > 0)) return t;
return t with
{
OffsetX = Math.Round(t.OffsetX * pixelsPerUnit) / pixelsPerUnit,
OffsetY = Math.Round(t.OffsetY * pixelsPerUnit) / pixelsPerUnit,
};
}
/// <summary>Next zoom level for a wheel movement of <paramref name="notches"/> (positive = in), by a factor of 1.25 per notch.</summary>
public static double WheelZoom(double scale, double notches, double minScale, double maxScale) =>
Math.Clamp(scale * Math.Pow(1.25, notches), minScale, maxScale);
private static double ClampAxis(double offset, double size, double view)
{
if (size <= view) return (view - size) / 2;
return Math.Clamp(offset, view - size, 0);
}
}