using System; using System.Collections.Generic; using System.Linq; namespace AsbCloudInfrastructure.Services.Subsystems.Utils { #nullable enable internal class DepthInterpolation { private IEnumerator<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> enumerator; (DateTimeOffset x, float? y) lastValue = default; private DateTimeOffset dateMin; public DepthInterpolation(IOrderedEnumerable<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> collection) { enumerator = collection.GetEnumerator(); enumerator.MoveNext(); dateMin = enumerator.Current.dateMin; } public float GetDepth(DateTimeOffset date) { { while (isLess(date)) { lastValue.x = date; lastValue.y = CalcValue(enumerator.Current, (date - dateMin).TotalSeconds); enumerator.MoveNext(); } } return (float)lastValue.y; } private bool isLess(DateTimeOffset date) { if (date >= enumerator.Current.dateMin) return true; return false; } private float CalcValue((DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax) item, double timestamp) { var a = (item.depthMax - item.depthMin) / (item.dateMax - item.dateMin).TotalSeconds; var b = item.depthMin * (item.dateMax.Second - (item.depthMax - item.depthMin) / (item.dateMax.Second - item.dateMin.Second)); var resultDepth = a * timestamp + b; return (float)resultDepth; } } #nullable disable }