< Summary - ReFlex - Library

Information
Class: ReFlex.Core.Interactivity.Util.ContactMotion
Assembly: ReFlex.Core.Interactivity
File(s): D:\a\reflex\reflex\library\src\Core\Interactivity\Util\ContactMotion.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 43
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Estimate(...)100%88100%
Forecast(...)100%11100%

File(s)

D:\a\reflex\reflex\library\src\Core\Interactivity\Util\ContactMotion.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using MathNet.Numerics;
 4using ReFlex.Core.Common.Components;
 5
 6namespace ReFlex.Core.Interactivity.Util;
 7
 8/// <summary>Fits real timestamped measurements and creates forecasts without updating track state.</summary>
 9internal static class ContactMotion
 10{
 11    public static (Point3 Velocity, Point3 Acceleration, int Count) Estimate(
 12        IEnumerable<(double Time, Interaction Measurement)> measurements, double lastSeenTime, int sampleCount, bool use
 122013    {
 122014        var samples = measurements.TakeLast(sampleCount)
 451615            .Select(sample => (sample.Time, Position: sample.Measurement.Position)).ToArray();
 122016        if (samples.Length < 2)
 20617            return (new Point3(), new Point3(), samples.Length);
 18
 19        // Center times at the newest measurement: the fitted linear coefficient is v(now),
 20        // not the average velocity half-way between measurements. Gaps retain their duration.
 101421        var span = lastSeenTime - samples[0].Time;
 22        // Normalize the independent variable for well-conditioned fits, then recover derivatives per second.
 410423        var times = samples.Select(sample => (sample.Time - lastSeenTime) / span).ToArray();
 101424        var order = useAcceleration && samples.Length >= 3 ? 2 : 1;
 410425        var x = Fit.Polynomial(times, samples.Select(sample => (double)sample.Position.X).ToArray(), order);
 410426        var y = Fit.Polynomial(times, samples.Select(sample => (double)sample.Position.Y).ToArray(), order);
 410427        var z = Fit.Polynomial(times, samples.Select(sample => (double)sample.Position.Z).ToArray(), order);
 101428        var velocity = new Point3((float)(x[1] / span), (float)(y[1] / span), (float)(z[1] / span));
 101429        var acceleration = order == 2
 101430            ? new Point3((float)(2 * x[2] / (span * span)),
 101431                (float)(2 * y[2] / (span * span)), (float)(2 * z[2] / (span * span)))
 101432            : new Point3();
 101433        return (velocity, acceleration, samples.Length);
 122034    }
 35
 36    public static InteractionVelocity Forecast(Point3 position,
 37        (Point3 Velocity, Point3 Acceleration, int Count) motion, float accelerationWeight, float horizon)
 122038    {
 122039        var forecast = new InteractionVelocity();
 122040        forecast.Update(position, motion.Velocity, motion.Acceleration, accelerationWeight, horizon);
 122041        return forecast;
 122042    }
 43}