From ef8ef94826ca600b6d301067f093c28b42d6287b Mon Sep 17 00:00:00 2001 From: ikpil Date: Tue, 24 Oct 2023 23:33:24 +0900 Subject: [PATCH] refactor: TestVectorDot --- src/DotRecast.Core/Numerics/RcVec3f.cs | 23 ++++--------------- src/DotRecast.Core/Numerics/RcVecUtils.cs | 18 ++++++++++++++- .../RcFilledVolumeRasterization.cs | 8 +++---- test/DotRecast.Core.Test/Vector3Tests.cs | 15 ++++++++++++ 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/DotRecast.Core/Numerics/RcVec3f.cs b/src/DotRecast.Core/Numerics/RcVec3f.cs index bde01a5..2c6d5c9 100644 --- a/src/DotRecast.Core/Numerics/RcVec3f.cs +++ b/src/DotRecast.Core/Numerics/RcVec3f.cs @@ -288,28 +288,13 @@ namespace DotRecast.Core.Numerics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static float Dot(RcVec3f v1, RcVec3f v2) + public static float Dot(RcVec3f vector1, RcVec3f vector2) { - return (v1.X * v2.X) - + (v1.Y * v2.Y) - + (v1.Z * v2.Z); + return (vector1.X * vector2.X) + + (vector1.Y * vector2.Y) + + (vector1.Z * vector2.Z); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static float Dot(float[] v1, float[] v2) - { - return v1[0] * v2[0] - + v1[1] * v2[1] - + v1[2] * v2[2]; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static float Dot(float[] v1, RcVec3f v2) - { - return v1[0] * v2.X + v1[1] * v2.Y + v1[2] * v2.Z; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float PerpXZ(RcVec3f a, RcVec3f b) { diff --git a/src/DotRecast.Core/Numerics/RcVecUtils.cs b/src/DotRecast.Core/Numerics/RcVecUtils.cs index 76b1f41..58ecd57 100644 --- a/src/DotRecast.Core/Numerics/RcVecUtils.cs +++ b/src/DotRecast.Core/Numerics/RcVecUtils.cs @@ -96,7 +96,7 @@ namespace DotRecast.Core.Numerics dest[1] = v1[2] * v2[0] - v1[0] * v2[2]; dest[2] = v1[0] * v2[1] - v1[1] * v2[0]; } - + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Copy(float[] @out, int n, float[] @in, int m) { @@ -104,5 +104,21 @@ namespace DotRecast.Core.Numerics @out[n + 1] = @in[m + 1]; @out[n + 2] = @in[m + 2]; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static float Dot(float[] v1, float[] v2) + { + return v1[0] * v2[0] + + v1[1] * v2[1] + + v1[2] * v2[2]; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static float Dot(float[] v1, RcVec3f vector2) + { + return v1[0] * vector2.X + + v1[1] * vector2.Y + + v1[2] * vector2.Z; + } } } \ No newline at end of file diff --git a/src/DotRecast.Recast/RcFilledVolumeRasterization.cs b/src/DotRecast.Recast/RcFilledVolumeRasterization.cs index 2f71c61..995fe05 100644 --- a/src/DotRecast.Recast/RcFilledVolumeRasterization.cs +++ b/src/DotRecast.Recast/RcFilledVolumeRasterization.cs @@ -525,7 +525,7 @@ namespace DotRecast.Recast { if (Math.Abs(planes[j][1]) > EPSILON) { - float dotNormalPoint = RcVec3f.Dot(planes[j], point); + float dotNormalPoint = RcVecUtils.Dot(planes[j], point); float t = (planes[j][3] - dotNormalPoint) / planes[j][1]; float y = point.Y + t; bool valid = true; @@ -729,15 +729,15 @@ namespace DotRecast.Recast private static bool RayTriangleIntersection(RcVec3f point, int plane, float[][] planes, out float y) { y = 0.0f; - float t = (planes[plane][3] - RcVec3f.Dot(planes[plane], point)) / planes[plane][1]; + float t = (planes[plane][3] - RcVecUtils.Dot(planes[plane], point)) / planes[plane][1]; float[] s = { point.X, point.Y + t, point.Z }; - float u = RcVec3f.Dot(s, planes[plane + 1]) - planes[plane + 1][3]; + float u = RcVecUtils.Dot(s, planes[plane + 1]) - planes[plane + 1][3]; if (u < 0.0f || u > 1.0f) { return false; } - float v = RcVec3f.Dot(s, planes[plane + 2]) - planes[plane + 2][3]; + float v = RcVecUtils.Dot(s, planes[plane + 2]) - planes[plane + 2][3]; if (v < 0.0f) { return false; diff --git a/test/DotRecast.Core.Test/Vector3Tests.cs b/test/DotRecast.Core.Test/Vector3Tests.cs index e2e111a..46a511a 100644 --- a/test/DotRecast.Core.Test/Vector3Tests.cs +++ b/test/DotRecast.Core.Test/Vector3Tests.cs @@ -108,6 +108,21 @@ public class Vector3Tests Assert.That(array1, Is.EqualTo(array11)); } + [Test] + [Repeat(100000)] + public void TestVectorDot() + { + 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()); + float d3 = Vector3.Dot(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.Dot(v11, v22); + + Assert.That(d3, Is.EqualTo(d33)); + } + [Test] [Repeat(100000)] public void TestVectorDistance()