forked from ddrilling/AsbCloudServer
49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
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)
|
|
{
|
|
var isNotEnd = true;
|
|
while (isOnInterval(date) && isNotEnd)
|
|
{
|
|
lastValue.x = date;
|
|
lastValue.y = CalcValue(enumerator.Current, (date- enumerator.Current.dateMin).TotalSeconds);
|
|
isNotEnd = enumerator.MoveNext();
|
|
}
|
|
return lastValue.y;
|
|
|
|
}
|
|
|
|
private bool isOnInterval(DateTimeOffset date)
|
|
=> date >= enumerator.Current.dateMin && date <= enumerator.Current.dateMax;
|
|
private float CalcValue((DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax) item, double timestamp)
|
|
{
|
|
if (item.depthMin == item.depthMax)
|
|
{
|
|
return item.depthMin;
|
|
}
|
|
var w = timestamp / (item.dateMax - item.dateMin).TotalSeconds;
|
|
var d = ((item.depthMax - item.depthMin) * w)+ item.depthMin;
|
|
return (float)d;
|
|
}
|
|
}
|
|
#nullable disable
|
|
}
|