Skip to content

Commit

Permalink
refactor: RcVec3f.Lerp
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Oct 26, 2023
1 parent 93b6b52 commit 21b3cd6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
13 changes: 7 additions & 6 deletions src/DotRecast.Core/Numerics/RcVec3f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,14 @@ public override string ToString()
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RcVec3f Lerp(RcVec3f v1, RcVec3f v2, float t)
public static RcVec3f Lerp(RcVec3f value1, RcVec3f value2, float amount)
{
return new RcVec3f(
v1.X + (v2.X - v1.X) * t,
v1.Y + (v2.Y - v1.Y) * t,
v1.Z + (v2.Z - v1.Z) * t
);
return (value1 * (1f - amount)) + (value2 * amount);
// return new RcVec3f(
// value1.X + (value2.X - value1.X) * amount,
// value1.Y + (value2.Y - value1.Y) * amount,
// value1.Z + (value2.Z - value1.Z) * amount
// );
}

/// Returns the distance between two points.
Expand Down
38 changes: 18 additions & 20 deletions test/DotRecast.Core.Test/Vector3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,18 @@ public void TestVectorCopyTo()
{
var v1 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle());
var array1 = new float[3];
var array2 = new float[3];
v1.CopyTo(array1);
v1.CopyTo(array2, 0);

var v11 = new RcVec3f(v1.X, v1.Y, v1.Z);
var array11 = new float[3];
var array22 = new float[3];
v11.CopyTo(array11);
v11.CopyTo(array22, 0);

Assert.That(array1, Is.EqualTo(array11));
Assert.That(array2, Is.EqualTo(array22));
}

[Test]
Expand Down Expand Up @@ -167,26 +172,19 @@ public void TestVectorMinMax()

[Test]
[Repeat(100000)]
public void TestVectorIsFinite()
public void TestVectorLerp()
{
// IsFinite
var amt = Random.Shared.NextSingle();
var v1 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle());
var v2 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle());
var v3 = Vector3.Lerp(v1, v2, amt);

var v11 = new RcVec3f(v1.X, v1.Y, v1.Z);
var v22 = new RcVec3f(v2.X, v2.Y, v2.Z);
var v33 = RcVec3f.Lerp(v11, v22, amt);

Assert.That(v3.X, Is.EqualTo(v33.X));
Assert.That(v3.Y, Is.EqualTo(v33.Y));
Assert.That(v3.Z, Is.EqualTo(v33.Z));
}

// [Test]
// [Repeat(100000)]
// public void TestVectorLerp()
// {
// var amt = Random.Shared.NextSingle();
// var v1 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle());
// var v2 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle());
// var v3 = Vector3.Lerp(v1, v2, amt);
//
// var v11 = new RcVec3f(v1.X, v1.Y, v1.Z);
// var v22 = new RcVec3f(v2.X, v2.Y, v2.Z);
// var v33 = RcVec3f.Lerp(v11, v22, amt);
//
// Assert.That(v3.X, Is.EqualTo(v33.X));
// Assert.That(v3.Y, Is.EqualTo(v33.Y));
// Assert.That(v3.Z, Is.EqualTo(v33.Z));
// }
}

0 comments on commit 21b3cd6

Please sign in to comment.