DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs

63 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
namespace AsbCloudInfrastructure.Services.Subsystems.Utils
{
internal class DepthInterpolation
{
2022-10-19 13:55:10 +05:00
private readonly TimeSpan maxDeltaDate = TimeSpan.FromHours(12);
private readonly IEnumerator<(DateTimeOffset x, float y)> enumerator;
private (DateTimeOffset x, float y) p0;
private bool canMoveNext;
public DepthInterpolation(IEnumerable<(DateTimeOffset x, float y)> collection)
{
enumerator = collection.GetEnumerator();
canMoveNext = enumerator.MoveNext();
p0 = enumerator.Current;
}
~DepthInterpolation()
{
2022-10-19 13:55:10 +05:00
enumerator.Dispose();
}
public float GetDepth(DateTimeOffset date)
2022-10-19 13:55:10 +05:00
{
// ошибка в телеметрии см. прим.: idTelemetry = 93 && date between '2021-11-16 17:18:40.000 +0500' and '2021-11-16 17:19:37.000 +0500'
while (canMoveNext && enumerator.Current.x < date)
{
2022-10-19 13:55:10 +05:00
p0 = enumerator.Current;
canMoveNext = enumerator.MoveNext();
}
2022-10-19 13:55:10 +05:00
return CalcValue(date);
}
2022-10-19 13:55:10 +05:00
private float CalcValue(DateTimeOffset date)
{
2022-10-19 13:55:10 +05:00
var p1 = enumerator.Current;
if (canMoveNext || p1 == default || p0.y == p1.y || p0.x > date)
return p0.y;
if (p1.x < date)
return p1.y;
if (p1.x - p0.x > maxDeltaDate && Math.Abs(p1.y - p0.y) > 0.2d)
2022-10-13 23:54:14 +05:00
{
2022-10-19 13:55:10 +05:00
if (date - p0.x < p1.x - date)
return p0.y;
else
return p1.y;
}
2022-10-19 13:55:10 +05:00
var a = (p1.y - p0.y) / (p1.x - p0.x).TotalSeconds;
var b = p0.y;
var x = (date - p0.x).TotalSeconds;
var y = a * x + b;
return (float)y;
}
}
}