using Titano.Core.Mathematics; using Titano.Testing; namespace Titano.Core.Tests.Mathematics; public sealed class SplineTests { // Monotone data with a plateau and a sharp rise: the classic overshoot trap for C² splines. private static readonly double[] StepX = [0, 1, 2, 3, 4, 5, 6]; private static readonly double[] StepY = [0, 0, 0, 1, 1, 1, 1]; [Test] public void MonotoneSplineInterpolatesKnots() { var spline = new MonotoneSpline(StepX, StepY); for (var i = 0; i < StepX.Length; i++) Assert.Equal(StepY[i], spline.Evaluate(StepX[i])); Assert.Equal(0.0, spline.Evaluate(-5)); Assert.Equal(1.0, spline.Evaluate(50)); } [Test] public void MonotoneSplineNeverOvershootsWhileNaturalSplineDoes() { var monotone = new MonotoneSpline(StepX, StepY); var natural = new NaturalCubicSpline(StepX, StepY); var samples = Enumerable.Range(0, 601).Select(i => monotone.Evaluate(i / 100.0)).ToList(); Assert.Monotonic(samples); Assert.InRange(samples.Min(), 0, 0); Assert.InRange(samples.Max(), 1, 1); var naturalMax = Enumerable.Range(0, 601).Max(i => natural.Evaluate(i / 100.0)); Assert.Greater(naturalMax, 1.01); } [Test] public void MonotoneSplineOnRandomMonotoneDataIsMonotoneAndBounded() { var random = new Random(5); for (var trial = 0; trial < 50; trial++) { var n = 2 + random.Next(20); var x = new double[n]; var y = new double[n]; for (var i = 1; i < n; i++) { x[i] = x[i - 1] + 0.01 + random.NextDouble() * 3; y[i] = y[i - 1] + (random.NextDouble() < 0.3 ? 0 : random.NextDouble() * random.NextDouble() * 10); } var spline = new MonotoneSpline(x, y); Assert.True(spline.IsMonotone); var values = Enumerable.Range(0, 400).Select(i => spline.Evaluate(x[^1] * i / 399.0)).ToList(); Assert.True(values.All(double.IsFinite)); Assert.Monotonic(values, message: $"trial {trial}"); for (var k = 0; k < n - 1; k++) { var mid = spline.Evaluate(0.5 * (x[k] + x[k + 1])); Assert.InRange(mid, y[k], y[k + 1], $"interval {k} of trial {trial}"); } } } [Test] public void MonotoneSplineDerivativeMatchesFiniteDifference() { double[] x = [0, 0.5, 2, 3, 5]; double[] y = [1, 1.5, 2.2, 4, 4.5]; var spline = new MonotoneSpline(x, y); foreach (var at in new[] { 0.2, 0.9, 2.5, 4.1 }) { const double h = 1e-6; var numeric = (spline.Evaluate(at + h) - spline.Evaluate(at - h)) / (2 * h); Assert.Approximately(numeric, spline.Derivative(at), 1e-6); } } [Test] public void MonotoneSplineInverseRoundTrips() { double[] x = [0, 1, 2.5, 4, 6]; double[] y = [10, 8, 8, 3, -1]; var spline = new MonotoneSpline(x, y); for (var i = 0; i <= 120; i++) { var at = i * 0.05; var value = spline.Evaluate(at); var back = spline.Inverse(value); Assert.Approximately(value, spline.Evaluate(back), 1e-10); } Assert.Equal(1.0, spline.Inverse(8)); Assert.Equal(0.0, spline.Inverse(99)); Assert.Equal(6.0, spline.Inverse(-99)); Assert.Throws(() => new MonotoneSpline([0, 1, 2], [0, 1, 0]).Inverse(0.5)); } [Test] public void SplinesRejectInvalidKnots() { Assert.Throws(() => new MonotoneSpline([0, 0], [1, 2])); Assert.Throws(() => new NaturalCubicSpline([0, 1], [1, double.NaN])); Assert.Throws(() => new MonotoneSpline([], [])); } [Test] public void NaturalSplineReproducesLinesAndHasFreeEnds() { double[] x = [0, 1, 3, 4, 7]; var line = new NaturalCubicSpline(x, x.Select(v => 2 - 0.5 * v).ToArray()); foreach (var at in new[] { -2.0, 0.3, 2.2, 6.9, 9 }) Assert.Approximately(2 - 0.5 * at, line.Evaluate(at), 1e-12); var sine = new NaturalCubicSpline(Enumerable.Range(0, 21).Select(i => i * Math.PI / 10).ToArray(), Enumerable.Range(0, 21).Select(i => Math.Sin(i * Math.PI / 10)).ToArray()); Assert.Approximately(0, sine.SecondDerivative(1e-9), 1e-6); for (var i = 0; i < 100; i++) { var at = 0.3 + i * 0.055; Assert.Approximately(Math.Sin(at), sine.Evaluate(at), 2e-4); Assert.Approximately(Math.Cos(at), sine.Derivative(at), 5e-3); } } [Test] public void CatmullRomPassesThroughControlPoints() { Point2[] points = [new(0, 0), new(1, 2), new(3, 3), new(4, 1), new(6, 0)]; foreach (var kind in Enum.GetValues()) { for (var i = 0; i < points.Length; i++) { var p = CatmullRom.Evaluate(points, i, kind); Assert.Within(points[i].X, points[i].Y, p.X, p.Y, 1e-12); } } } [Test] public void UniformScalarCatmullRomMatchesClosedForm() { double[] v = [1, 4, 2, 5, 3]; for (var i = 0; i <= 40; i++) { var t = 1 + i * 0.025; var u = t - 1; double p0 = v[0], p1 = v[1], p2 = v[2], p3 = v[3]; var expected = 0.5 * (2 * p1 + (-p0 + p2) * u + (2 * p0 - 5 * p1 + 4 * p2 - p3) * u * u + (-p0 + 3 * p1 - 3 * p2 + p3) * u * u * u); Assert.Approximately(expected, CatmullRom.Evaluate(v, t), 1e-12); } } [Test] public void CentripetalCatmullRomDoesNotBacktrackOnUnevenSpacing() { // Collinear points with a very short middle segment: the uniform spline overshoots past 10.1 and // turns back; the centripetal one stays monotone along the line. Point2[] points = [new(0, 0), new(10, 0), new(10.1, 0), new(20, 0)]; double MaxX(CatmullRomParameterization kind) => Enumerable.Range(0, 101).Max(i => CatmullRom.Evaluate(points, 1 + i / 100.0, kind).X); Assert.Greater(MaxX(CatmullRomParameterization.Uniform), 10.15); var centripetal = Enumerable.Range(0, 101).Select(i => CatmullRom.Evaluate(points, 1 + i / 100.0, CatmullRomParameterization.Centripetal).X).ToList(); Assert.Monotonic(centripetal); Assert.LessOrEqual(centripetal.Max(), 10.1 + 1e-12); } [Test] public void BezierEndpointsAndDerivative() { var curve = new CubicBezier(new(0, 0), new(1, 2), new(3, 2), new(4, 0)); Assert.Equal(new Point2(0, 0), curve.Evaluate(0)); Assert.Equal(new Point2(4, 0), curve.Evaluate(1)); var d0 = curve.Derivative(0); Assert.Within(3, 6, d0.X, d0.Y, 1e-12); var mid = curve.Evaluate(0.5); Assert.Within(2, 1.5, mid.X, mid.Y, 1e-12); } [Theory] [Case("ease", 0.5, 0.802403387585)] [Case("ease-in", 0.5, 0.315356812573)] [Case("ease-out", 0.25, 0.378138130825)] [Case("ease-in-out", 0.5, 0.5)] [Case("linear", 0.37, 0.37)] public void CssEasingMatchesReference(string name, double x, double expected) { var easing = name switch { "ease" => CubicBezierEasing.Ease, "ease-in" => CubicBezierEasing.EaseIn, "ease-out" => CubicBezierEasing.EaseOut, "ease-in-out" => CubicBezierEasing.EaseInOut, _ => CubicBezierEasing.Linear, }; Assert.Approximately(expected, easing.Evaluate(x), 1e-9); } [Test] public void EasingIsMonotoneAndClamped() { var easing = new CubicBezierEasing(0.9, -0.2, 0.1, 1.2); Assert.Equal(0.0, easing.Evaluate(-1)); Assert.Equal(1.0, easing.Evaluate(2)); var ts = Enumerable.Range(0, 201).Select(i => easing.SolveT(i / 200.0)).ToList(); Assert.Monotonic(ts); Assert.Throws(() => new CubicBezierEasing(1.5, 0, 0.5, 1)); } }