From 45ede19e614400c0bba88611efbb07ee71b0dcfe Mon Sep 17 00:00:00 2001 From: ikpil Date: Fri, 20 Oct 2023 00:10:11 +0900 Subject: [PATCH] refactor: preparing for switching build with System.Numerics.Vector3 --- src/DotRecast.Core/Numerics/RcVec3f.cs | 28 ++++++++++--------- .../Numerics/RcVecExtensions.cs | 10 +++++++ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/DotRecast.Core/Numerics/RcVec3f.cs b/src/DotRecast.Core/Numerics/RcVec3f.cs index 8da28a6..4543f14 100644 --- a/src/DotRecast.Core/Numerics/RcVec3f.cs +++ b/src/DotRecast.Core/Numerics/RcVec3f.cs @@ -98,7 +98,14 @@ namespace DotRecast.Core.Numerics [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly float Length() { - return (float)Math.Sqrt(X * X + Y * Y + Z * Z); + float lengthSquared = LengthSquared(); + return MathF.Sqrt(lengthSquared); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly float LengthSquared() + { + return Dot(this, this); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -114,15 +121,7 @@ namespace DotRecast.Core.Numerics return left + right; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly RcVec3f Scale(float scale) - { - return new RcVec3f( - X * scale, - Y * scale, - Z * scale - ); - } + /// Derives the dot product of two vectors on the xz-plane. (@p u . @p v) @@ -331,14 +330,17 @@ namespace DotRecast.Core.Numerics [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Dot(RcVec3f v1, RcVec3f v2) { - return (v1.X * v2.X) + (v1.Y * v2.Y) - + (v1.Z * v2.Z); + return (v1.X * v2.X) + + (v1.Y * v2.Y) + + (v1.Z * v2.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]; + return v1[0] * v2[0] + + v1[1] * v2[1] + + v1[2] * v2[2]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/DotRecast.Core/Numerics/RcVecExtensions.cs b/src/DotRecast.Core/Numerics/RcVecExtensions.cs index 008cd7f..4513b1e 100644 --- a/src/DotRecast.Core/Numerics/RcVecExtensions.cs +++ b/src/DotRecast.Core/Numerics/RcVecExtensions.cs @@ -28,5 +28,15 @@ namespace DotRecast.Core.Numerics default: throw new IndexOutOfRangeException("vector3f index out of range"); } } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static RcVec3f Scale(this RcVec3f v, float scale) + { + return new RcVec3f( + v.X * scale, + v.Y * scale, + v.Z * scale + ); + } } } \ No newline at end of file