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:
16 files changed
+1410
No files matched your search
@@ -0,0 +1,80 @@
|
||||
namespace Titano.Ui.Visuals;
|
||||
|
||||
/// <summary>
|
||||
/// Reduces a long series to at most two samples per pixel column (its minimum and maximum, in index order) so a
|
||||
/// line over 100k+ points draws in time proportional to the plot width while every spike stays visible.
|
||||
/// </summary>
|
||||
public static class MinMaxDecimator
|
||||
{
|
||||
/// <summary>Index written to the output where the line must break (a run without finite samples).</summary>
|
||||
public const int Gap = -1;
|
||||
|
||||
/// <summary>Output capacity needed for <paramref name="count"/> samples over <paramref name="columns"/> columns.</summary>
|
||||
public static int Capacity(int count, int columns) => Math.Max(0, Math.Min(count, 2 * Math.Max(1, columns)));
|
||||
|
||||
/// <summary>
|
||||
/// Writes ascending sample indices to draw into <paramref name="output"/> and returns how many were written.
|
||||
/// When the series has at most two samples per column every finite sample is kept; otherwise each column
|
||||
/// keeps the indices of its minimum and maximum. Non-finite samples are skipped and a run of them (or a
|
||||
/// column without any finite sample) becomes one <see cref="Gap"/> entry.
|
||||
/// </summary>
|
||||
public static int Decimate(ReadOnlySpan<double> values, int columns, Span<int> output)
|
||||
{
|
||||
columns = Math.Max(1, columns);
|
||||
var n = values.Length;
|
||||
if (output.Length < Capacity(n, columns))
|
||||
throw new ArgumentException("Output span is smaller than Capacity(count, columns).", nameof(output));
|
||||
|
||||
var written = 0;
|
||||
if (n <= 2 * columns)
|
||||
{
|
||||
for (var i = 0; i < n; i++)
|
||||
{
|
||||
if (double.IsFinite(values[i])) output[written++] = i;
|
||||
else if (written > 0 && output[written - 1] != Gap) output[written++] = Gap;
|
||||
}
|
||||
return Trim(output, written);
|
||||
}
|
||||
|
||||
for (var c = 0; c < columns; c++)
|
||||
{
|
||||
var start = (int)((long)c * n / columns);
|
||||
var end = (int)((long)(c + 1) * n / columns);
|
||||
int minIndex = -1, maxIndex = -1;
|
||||
double min = double.PositiveInfinity, max = double.NegativeInfinity;
|
||||
for (var i = start; i < end; i++)
|
||||
{
|
||||
var v = values[i];
|
||||
if (!double.IsFinite(v)) continue;
|
||||
if (v < min)
|
||||
{
|
||||
min = v;
|
||||
minIndex = i;
|
||||
}
|
||||
if (v > max)
|
||||
{
|
||||
max = v;
|
||||
maxIndex = i;
|
||||
}
|
||||
}
|
||||
if (minIndex < 0)
|
||||
{
|
||||
if (written > 0 && output[written - 1] != Gap) output[written++] = Gap;
|
||||
continue;
|
||||
}
|
||||
if (minIndex == maxIndex)
|
||||
{
|
||||
output[written++] = minIndex;
|
||||
}
|
||||
else
|
||||
{
|
||||
output[written++] = Math.Min(minIndex, maxIndex);
|
||||
output[written++] = Math.Max(minIndex, maxIndex);
|
||||
}
|
||||
}
|
||||
return Trim(output, written);
|
||||
}
|
||||
|
||||
/// <summary>A trailing gap carries no information.</summary>
|
||||
private static int Trim(Span<int> output, int written) => written > 0 && output[written - 1] == Gap ? written - 1 : written;
|
||||
}
|
||||
Reference in new issue
Block a user