refactor: RcVec3f.Distance

This commit is contained in:
ikpil 2023-10-24 00:20:42 +09:00
parent 0d805b5d07
commit 617eaa1287
2 changed files with 47 additions and 8 deletions

View File

@ -274,12 +274,17 @@ namespace DotRecast.Core.Numerics
/// @param[in] v2 A point. [(x, y, z)] /// @param[in] v2 A point. [(x, y, z)]
/// @return The distance between the two points. /// @return The distance between the two points.
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Distance(RcVec3f v1, RcVec3f v2) public static float Distance(RcVec3f value1, RcVec3f value2)
{ {
float dx = v2.X - v1.X; float distanceSquared = DistanceSquared(value1, value2);
float dy = v2.Y - v1.Y; return MathF.Sqrt(distanceSquared);
float dz = v2.Z - v1.Z; }
return (float)Math.Sqrt(dx * dx + dy * dy + dz * dz);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float DistanceSquared(RcVec3f value1, RcVec3f value2)
{
var difference = value1 - value2;
return Dot(difference, difference);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -91,7 +91,6 @@ public class Vector3Tests
Assert.That(v3.X, Is.EqualTo(v33.X)); Assert.That(v3.X, Is.EqualTo(v33.X));
Assert.That(v3.Y, Is.EqualTo(v33.Y)); Assert.That(v3.Y, Is.EqualTo(v33.Y));
Assert.That(v3.Z, Is.EqualTo(v33.Z)); Assert.That(v3.Z, Is.EqualTo(v33.Z));
} }
[Test] [Test]
@ -109,4 +108,39 @@ public class Vector3Tests
Assert.That(array1, Is.EqualTo(array11)); Assert.That(array1, Is.EqualTo(array11));
} }
[Test]
[Repeat(100000)]
public void TestVectorDistance()
{
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 d3 = Vector3.Distance(v1, v2);
var d4 = Vector3.DistanceSquared(v1, v2);
var v11 = new RcVec3f(v1.X, v1.Y, v1.Z);
var v22 = new RcVec3f(v2.X, v2.Y, v2.Z);
var d33 = RcVec3f.Distance(v11, v22);
var d44 = RcVec3f.DistanceSquared(v11, v22);
Assert.That(d3, Is.EqualTo(d33));
Assert.That(d4, Is.EqualTo(d44));
}
// [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));
// }
} }