Files
Chronos/src/Chronos.Core/Motion/MotionField.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

155 lines
5.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Buffers;
namespace Chronos.Core.Motion;
/// <summary>Summary of a motion field's vector magnitudes (pixels per frame).</summary>
public readonly record struct MotionStatistics(
double MeanMagnitude,
double MedianMagnitude,
double P95Magnitude,
double MaxMagnitude,
double MeanU,
double MeanV,
double ReliableFraction);
/// <summary>
/// Dense motion vectors (full-resolution pixels per frame) on a regular grid: node (i, j) describes the
/// pixel at (i·Step, j·Step). A vector (u, v) at p means the content at p in the first frame is found at
/// p + (u, v) in the second. <see cref="Error"/> holds the forward–backward consistency error in pixels
/// (NaN when not checked). Storage is pooled; dispose the field when done.
/// </summary>
public sealed class MotionField : IDisposable
{
private float[]? _u;
private float[]? _v;
private float[]? _error;
public MotionField(int imageWidth, int imageHeight, int step)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(imageWidth);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(imageHeight);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(step);
ImageWidth = imageWidth;
ImageHeight = imageHeight;
Step = step;
Columns = (imageWidth - 1) / step + 1;
Rows = (imageHeight - 1) / step + 1;
var n = Columns * Rows;
_u = ArrayPool<float>.Shared.Rent(n);
_v = ArrayPool<float>.Shared.Rent(n);
_error = ArrayPool<float>.Shared.Rent(n);
U.Clear();
V.Clear();
Error.Fill(float.NaN);
}
public int ImageWidth { get; }
public int ImageHeight { get; }
/// <summary>Grid spacing in full-resolution pixels.</summary>
public int Step { get; }
public int Columns { get; }
public int Rows { get; }
public int Count => Columns * Rows;
/// <summary>Horizontal components, row-major over the grid.</summary>
public Span<float> U => Storage(_u);
/// <summary>Vertical components, row-major over the grid.</summary>
public Span<float> V => Storage(_v);
/// <summary>Forward–backward error |w(p) + w'(p + w(p))| in pixels; NaN when unchecked.</summary>
public Span<float> Error => Storage(_error);
/// <summary>True when <see cref="Error"/> has been filled by a forward–backward check.</summary>
public bool HasConsistency { get; internal set; }
/// <summary>Bilinearly interpolated vector at a full-resolution pixel-centre position (clamped to the grid).</summary>
public (float U, float V) Sample(double x, double y)
{
var gx = (float)(x / Step);
var gy = (float)(y / Step);
return (PlaneOps.Bilinear(U, Columns, Rows, gx, gy), PlaneOps.Bilinear(V, Columns, Rows, gx, gy));
}
/// <summary>Bilinearly interpolated forward–backward error (NaN when unchecked).</summary>
public float SampleError(double x, double y) =>
HasConsistency ? PlaneOps.Bilinear(Error, Columns, Rows, (float)(x / Step), (float)(y / Step)) : float.NaN;
/// <summary>Counts of vector magnitudes in <paramref name="bins"/> equal bins over [0, max]; larger values land in the last bin.</summary>
public int[] MagnitudeHistogram(int bins, double maxMagnitude)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bins);
if (!(maxMagnitude > 0)) throw new ArgumentOutOfRangeException(nameof(maxMagnitude));
var histogram = new int[bins];
var u = U;
var v = V;
var scale = bins / maxMagnitude;
for (var i = 0; i < u.Length; i++)
{
var m = double.Sqrt((double)u[i] * u[i] + (double)v[i] * v[i]);
histogram[int.Min(bins - 1, (int)(m * scale))]++;
}
return histogram;
}
/// <summary>Magnitude statistics; vectors with a forward–backward error above <paramref name="reliableError"/> px count as unreliable.</summary>
public MotionStatistics Statistics(double reliableError = 1.0)
{
var n = Count;
var magnitudes = ArrayPool<float>.Shared.Rent(n);
try
{
var u = U;
var v = V;
var error = Error;
double sum = 0, su = 0, sv = 0, max = 0;
var reliable = 0;
for (var i = 0; i < n; i++)
{
var m = float.Sqrt(u[i] * u[i] + v[i] * v[i]);
magnitudes[i] = m;
sum += m;
su += u[i];
sv += v[i];
max = double.Max(max, m);
if (!HasConsistency || error[i] <= reliableError) reliable++;
}
var span = magnitudes.AsSpan(0, n);
var median = PlaneOps.Median(span);
var p95 = PlaneOps.Select(span, int.Min(n - 1, (int)double.Ceiling(0.95 * n) - 1));
return new MotionStatistics(sum / n, median, p95, max, su / n, sv / n, reliable / (double)n);
}
finally
{
ArrayPool<float>.Shared.Return(magnitudes);
}
}
/// <summary>Sets every vector to (u, v).</summary>
public void Fill(float u, float v)
{
U.Fill(u);
V.Fill(v);
}
public void Dispose()
{
Return(ref _u);
Return(ref _v);
Return(ref _error);
}
private Span<float> Storage(float[]? array) => (array ?? throw new ObjectDisposedException(nameof(MotionField))).AsSpan(0, Count);
private static void Return(ref float[]? array)
{
var a = Interlocked.Exchange(ref array, null);
if (a is not null) ArrayPool<float>.Shared.Return(a);
}
}