using System.Collections.Immutable; using System.Numerics; namespace MoonTools.Curve { /// /// A concatenation of 3D curves with time values. /// public struct SplineCurve3D { public ImmutableArray Curves { get; } public ImmutableArray Times { get; } public float TotalTime { get; } public bool Loop { get; } public SplineCurve3D(ImmutableArray curves, ImmutableArray 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]); } } }