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

61 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2024-08-19 10:01:07 +05:00
namespace AsbCloudInfrastructure.Services.Subsystems.Utils;
internal class DepthInterpolation
{
2024-08-19 10:01:07 +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;
}
2024-08-19 10:01:07 +05:00
~DepthInterpolation()
{
2024-08-19 10:01:07 +05:00
enumerator.Dispose();
}
2022-10-19 13:55:10 +05:00
2024-08-19 10:01:07 +05:00
public float GetDepth(DateTimeOffset date)
{
// ошибка в телеметрии см. прим.: 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;
2024-08-19 10:01:07 +05:00
canMoveNext = enumerator.MoveNext();
2022-10-19 13:55:10 +05:00
}
2024-08-19 10:01:07 +05:00
return CalcValue(date);
}
private float CalcValue(DateTimeOffset date)
{
var p1 = enumerator.Current;
if (canMoveNext || p1 == default || p0.y == p1.y || p0.x > date)
return p0.y;
2022-10-19 13:55:10 +05:00
2024-08-19 10:01:07 +05:00
if (p1.x < date)
return p1.y;
2024-08-19 10:01:07 +05:00
if (p1.x - p0.x > maxDeltaDate && Math.Abs(p1.y - p0.y) > 0.2d)
{
2024-08-19 10:01:07 +05:00
if (date - p0.x < p1.x - date)
2022-10-19 13:55:10 +05:00
return p0.y;
2024-08-19 10:01:07 +05:00
else
2022-10-19 13:55:10 +05:00
return p1.y;
}
2024-08-19 10:01:07 +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;
}
}