61 lines
1.5 KiB
C#
61 lines
1.5 KiB
C#
|
using System.Collections.Immutable;
|
||
|
using System.Numerics;
|
||
|
|
||
|
namespace MoonTools.Curve
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// A concatenation of 3D curves with time values.
|
||
|
/// </summary>
|
||
|
public struct SplineCurve3D
|
||
|
{
|
||
|
public ImmutableArray<ICurve3D> Curves { get; }
|
||
|
public ImmutableArray<float> Times { get; }
|
||
|
public float TotalTime { get; }
|
||
|
public bool Loop { get; }
|
||
|
|
||
|
public SplineCurve3D(ImmutableArray<ICurve3D> curves, ImmutableArray<float> times, bool loop = false)
|
||
|
{
|
||
|
TotalTime = 0;
|
||
|
|
||
|
for (int i = 0; i < times.Length; i++)
|
||
|
{
|
||
|
TotalTime += times[i];
|
||
|
}
|
||
|
|
||
|
Curves = curves;
|
||
|
Times = times;
|
||
|
Loop = loop;
|
||
|
}
|
||
|
|
||
|
public Vector3 Point(float t)
|
||
|
{
|
||
|
if (!Loop && t >= TotalTime)
|
||
|
{
|
||
|
var lastIndex = Curves.Length - 1;
|
||
|
return Curves[lastIndex].Point(Times[lastIndex], 0, Times[lastIndex]);
|
||
|
}
|
||
|
|
||
|
t %= TotalTime;
|
||
|
|
||
|
var index = 0;
|
||
|
var startTime = 0f;
|
||
|
var incrementalTime = 0f;
|
||
|
|
||
|
for (int i = 0; i < Times.Length; i++)
|
||
|
{
|
||
|
incrementalTime += Times[i];
|
||
|
|
||
|
if (t < incrementalTime)
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
index++;
|
||
|
startTime = Times[i];
|
||
|
}
|
||
|
|
||
|
return Curves[index].Point(t - startTime, 0, Times[index]);
|
||
|
}
|
||
|
}
|
||
|
}
|