Skip to content

Commit

Permalink
Added tests for vector3s.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Nov 26, 2024
1 parent 1ff37e0 commit 6abb03d
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 13 deletions.
105 changes: 92 additions & 13 deletions FinModelUtility/Fin/Fin Tests/animation/keyframes/GetAllFramesTests.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System;
using System.Linq;
using System.Numerics;

using fin.animation.interpolation;
using fin.animation.types;
using fin.animation.types.single;
using fin.animation.types.vector3;
using fin.util.asserts;
using fin.util.optional;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using NUnit.Framework;

using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;

namespace fin.animation.keyframes;

public class GetAllFramesTests {
Expand All @@ -27,7 +25,7 @@ public void TestStairstepNonlooping() {
impl.SetKeyframe(6, 2);
impl.SetKeyframe(8, 3);

AssertGetAllFramesMatchesInterpolated_(impl);
AssertGetAllFramesMatchesInterpolated_(impl, 10);
}

[Test]
Expand All @@ -42,9 +40,10 @@ public void TestStairstepLooping() {
impl.SetKeyframe(6, 2);
impl.SetKeyframe(8, 3);

AssertGetAllFramesMatchesInterpolated_(impl);
AssertGetAllFramesMatchesInterpolated_(impl, 10);
}


[Test]
public void TestInterpolatedNonlooping() {
var impl = new InterpolatedKeyframes<Keyframe<float>, float>(
Expand All @@ -58,7 +57,7 @@ public void TestInterpolatedNonlooping() {
impl.SetKeyframe(6, 2);
impl.SetKeyframe(8, 3);

AssertGetAllFramesMatchesInterpolated_(impl);
AssertGetAllFramesMatchesInterpolated_(impl, 10);
}

[Test]
Expand All @@ -74,16 +73,96 @@ public void TestInterpolatedLooping() {
impl.SetKeyframe(6, 2);
impl.SetKeyframe(8, 3);

AssertGetAllFramesMatchesInterpolated_(impl);
AssertGetAllFramesMatchesInterpolated_(impl, 10);
}

private static void AssertGetAllFramesMatchesInterpolated_<T>(
IConfiguredInterpolatable<T> impl) where T : unmanaged {
var length = impl.SharedConfig.AnimationLength;

[Test]
public void TestSeparateVector3Nonlooping() {
var impl = new SeparateVector3Keyframes<Keyframe<float>>(
new SharedInterpolationConfig { AnimationLength = 10, },
FloatKeyframeInterpolator.Instance,
new IndividualInterpolationConfig<float>
{ DefaultValue = Optional.Of(-1f) });

impl.SetKeyframe(0, 1, 0);
impl.SetKeyframe(1, 2, 1);
impl.SetKeyframe(2, 3, 2);

impl.SetKeyframe(0, 5, 3);
impl.SetKeyframe(1, 5, 1);
impl.SetKeyframe(2, 5, 2);

impl.SetKeyframe(0, 7, 4);
impl.SetKeyframe(1, 7, 2);
impl.SetKeyframe(2, 7, 3);

AssertGetAllFramesMatchesInterpolated_(impl, 10);
}

[Test]
public void TestSeparateVector3Looping() {
var impl = new SeparateVector3Keyframes<Keyframe<float>>(
new SharedInterpolationConfig { AnimationLength = 10, Looping = true},
FloatKeyframeInterpolator.Instance,
new IndividualInterpolationConfig<float>
{ DefaultValue = Optional.Of(-1f) });

impl.SetKeyframe(0, 1, 0);
impl.SetKeyframe(1, 2, 1);
impl.SetKeyframe(2, 3, 2);

impl.SetKeyframe(0, 5, 3);
impl.SetKeyframe(1, 5, 1);
impl.SetKeyframe(2, 5, 2);

impl.SetKeyframe(0, 7, 4);
impl.SetKeyframe(1, 7, 2);
impl.SetKeyframe(2, 7, 3);

AssertGetAllFramesMatchesInterpolated_(impl, 10);
}

[Test]
public void TestCombinedVector3Nonlooping() {
var impl = new CombinedVector3Keyframes<Keyframe<Vector3>>(
new SharedInterpolationConfig { AnimationLength = 10 },
Vector3KeyframeInterpolator.Instance,
new IndividualInterpolationConfig<Vector3>
{ DefaultValue = Optional.Of(new Vector3(-1, -1, -1)) });

impl.SetKeyframe(2, new Vector3(1, 2, 3));
impl.SetKeyframe(4, new Vector3(4, 2, 3));
impl.SetKeyframe(6, new Vector3(5, 5, 5));
impl.SetKeyframe(8, new Vector3(6, 7, 8));

AssertGetAllFramesMatchesInterpolated_(impl, 10);

}

[Test]
public void TestCombinedVector3Looping() {
var impl = new CombinedVector3Keyframes<Keyframe<Vector3>>(
new SharedInterpolationConfig { AnimationLength = 10, Looping = true },
Vector3KeyframeInterpolator.Instance,
new IndividualInterpolationConfig<Vector3>
{ DefaultValue = Optional.Of(new Vector3(-1, -1, -1)) });

impl.SetKeyframe(2, new Vector3(1, 2, 3));
impl.SetKeyframe(4, new Vector3(4, 2, 3));
impl.SetKeyframe(6, new Vector3(5, 5, 5));
impl.SetKeyframe(8, new Vector3(6, 7, 8));

AssertGetAllFramesMatchesInterpolated_(impl, 10);
}


private static void AssertGetAllFramesMatchesInterpolated_<T>(
IInterpolatable<T> impl,
int length) where T : unmanaged {
Span<T> interpolatedFrames = stackalloc T[length];
for (var i = 0; i < length; i++) {
Asserts.True(impl.TryGetAtFrameOrDefault(i, out var value));
Asserts.True(impl.TryGetAtFrame(i, out var value));
interpolatedFrames[i] = value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Linq;
using System.Runtime.CompilerServices;

using fin.animation.types;
using fin.animation.types.vector3;

namespace fin.animation.keyframes;

public static class KeyframesExtensions {
Expand Down Expand Up @@ -38,4 +41,13 @@ public static void SetAllKeyframes<T>(this IKeyframes<Keyframe<T>> keyframes,
keyframes.Add(new Keyframe<T>(frame, value));
}
}


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetKeyframe(
this ISeparateVector3Keyframes<Keyframe<float>> keyframes,
int axis,
float frame,
float value)
=> keyframes.Axes[axis].Add(new Keyframe<float>(frame, value));
}

0 comments on commit 6abb03d

Please sign in to comment.