From 704ecea09c4a81402d476aaf13cfa6b0b83cea63 Mon Sep 17 00:00:00 2001 From: REDCODE Date: Sun, 14 Jun 2026 01:49:24 +0200 Subject: [PATCH] Math classes inling optimizations --- 2DGAMELIB/_2DGAMELIB/QuaternionD.cs | 9 +-- 2DGAMELIB/_2DGAMELIB/Vec.cs | 68 ----------------- 2DGAMELIB/_2DGAMELIB/Vector2D.cs | 9 ++- 2DGAMELIB/_2DGAMELIB/VectorMath.cs | 76 +++++++++++++++++++ 2DGAMELIB/_2DGAMELIB/rewrite/Common.cs | 8 +- .../GameClasses/ElementInstance.cs | 2 +- 6 files changed, 91 insertions(+), 81 deletions(-) delete mode 100644 2DGAMELIB/_2DGAMELIB/Vec.cs create mode 100644 2DGAMELIB/_2DGAMELIB/VectorMath.cs diff --git a/2DGAMELIB/_2DGAMELIB/QuaternionD.cs b/2DGAMELIB/_2DGAMELIB/QuaternionD.cs index f9d2c5f..ffee1f5 100644 --- a/2DGAMELIB/_2DGAMELIB/QuaternionD.cs +++ b/2DGAMELIB/_2DGAMELIB/QuaternionD.cs @@ -3,7 +3,7 @@ using System.Globalization; namespace _2DGAMELIB { - public static class Qua + public static class Quaternion { public static QuaternionD RotationZQ(this double angle) { @@ -29,14 +29,13 @@ namespace _2DGAMELIB public static Vector2D TransformCoordinateBP(this Vector2D coord, Vector2D BasePoint, QuaternionD rotation) { - Vec.Subtract(ref coord, ref BasePoint, out coord); + VectorMath.Subtract(ref coord, ref BasePoint, out coord); TransformCoordinate(ref coord, ref rotation, out var result); - Vec.Add(ref result, ref BasePoint, out result); + VectorMath.Add(ref result, ref BasePoint, out result); return result; } } - - + //used in a total of 1 places [Serializable] public struct QuaternionD diff --git a/2DGAMELIB/_2DGAMELIB/Vec.cs b/2DGAMELIB/_2DGAMELIB/Vec.cs deleted file mode 100644 index e6a9e3b..0000000 --- a/2DGAMELIB/_2DGAMELIB/Vec.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System; - -namespace _2DGAMELIB -{ - public static class Vec - { - - public static void Add(ref Vector2D v1, ref Vector2D v2, out Vector2D r) - { - r.X = v1.X + v2.X; - r.Y = v1.Y + v2.Y; - } - - public static void Subtract(ref Vector2D v1, ref Vector2D v2, out Vector2D r) - { - r.X = v1.X - v2.X; - r.Y = v1.Y - v2.Y; - } - - public static double DistanceSquared(this Vector2D v1, Vector2D v2) - { - double num = v1.X - v2.X; - double num2 = v1.Y - v2.Y; - return num * num + num2 * num2; - } - - public static void Dot(ref Vector2D v1, ref Vector2D v2, out double r) - { - r = v1.X * v2.X + v1.Y * v2.Y; - } - - public static double Cross(ref Vector2D v1, ref Vector2D v2) - { - return v1.X * v2.Y - v1.Y * v2.X; - } - - public static Vector2D newNormalize(this Vector2D vector) - { - vector.Normalize(); - return vector; - } - - public static double Angle(ref Vector2D v1, ref Vector2D v2) - { - Dot(ref v1, ref v2, out var r); - r /= v1.Length() * v2.Length(); - if (r > 1.0) - { - r = 1.0; - } - else if (r < -1.0) - { - r = -1.0; - } - return System.Math.Acos(r); - } - - public static double Angle02π(this Vector2D v1, Vector2D v2) - { - double num = Angle(ref v1, ref v2); - if (Cross(ref v1, ref v2) < 0.0) - { - num = System.Math.PI * 2.0 - num; - } - return num; - } - } -} diff --git a/2DGAMELIB/_2DGAMELIB/Vector2D.cs b/2DGAMELIB/_2DGAMELIB/Vector2D.cs index e689606..8e5dd74 100644 --- a/2DGAMELIB/_2DGAMELIB/Vector2D.cs +++ b/2DGAMELIB/_2DGAMELIB/Vector2D.cs @@ -1,5 +1,6 @@ using System; using System.Globalization; +using System.Runtime.CompilerServices; namespace _2DGAMELIB { @@ -21,17 +22,19 @@ namespace _2DGAMELIB X = x; Y = y; } - + [MethodImpl(MethodImplOptions.AggressiveInlining)] public double Length() { return System.Math.Sqrt(X * X + Y * Y); } - + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public double LengthSquared() { return X * X + Y * Y; } - + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Normalize() { double num = Length(); diff --git a/2DGAMELIB/_2DGAMELIB/VectorMath.cs b/2DGAMELIB/_2DGAMELIB/VectorMath.cs new file mode 100644 index 0000000..9c356b8 --- /dev/null +++ b/2DGAMELIB/_2DGAMELIB/VectorMath.cs @@ -0,0 +1,76 @@ +using System; +using System.Runtime.CompilerServices; + +namespace _2DGAMELIB +{ + public static class VectorMath + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Add(ref Vector2D a, ref Vector2D b, out Vector2D result) + { + result.X = a.X + b.X; + result.Y = a.Y + b.Y; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Subtract(ref Vector2D a, ref Vector2D b, out Vector2D result) + { + result.X = a.X - b.X; + result.Y = a.Y - b.Y; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double DistanceSquared(this Vector2D a, Vector2D b) + { + double num = a.X - b.X; + double num2 = a.Y - b.Y; + return num * num + num2 * num2; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Dot(ref Vector2D a, ref Vector2D b, out double result) + { + result = a.X * b.X + a.Y * b.Y; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double Cross(ref Vector2D a, ref Vector2D b) + { + return a.X * b.Y - a.Y * b.X; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2D newNormalize(this Vector2D vector) + { + vector.Normalize(); + return vector; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double Angle(ref Vector2D a, ref Vector2D b) + { + Dot(ref a, ref b, out var r); + r /= a.Length() * b.Length(); + if (r > 1.0) + { + r = 1.0; + } + else if (r < -1.0) + { + r = -1.0; + } + return System.Math.Acos(r); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double Angle02π(this Vector2D a, Vector2D b) + { + double num = Angle(ref a, ref b); + if (Cross(ref a, ref b) < 0.0) + { + num = System.Math.PI * 2.0 - num; + } + return num; + } + } +} diff --git a/2DGAMELIB/_2DGAMELIB/rewrite/Common.cs b/2DGAMELIB/_2DGAMELIB/rewrite/Common.cs index 0bfaad2..b275b9b 100644 --- a/2DGAMELIB/_2DGAMELIB/rewrite/Common.cs +++ b/2DGAMELIB/_2DGAMELIB/rewrite/Common.cs @@ -195,17 +195,17 @@ namespace _2DGAMELIB public static Vector2D TransformCoordinateBP(this Vector2D coord, Vector2D BasePoint, MatrixD transform) { - Vec.Subtract(ref coord, ref BasePoint, out coord); + VectorMath.Subtract(ref coord, ref BasePoint, out coord); TransformCoordinate(ref coord, ref transform, out var result); - Vec.Add(ref result, ref BasePoint, out result); + VectorMath.Add(ref result, ref BasePoint, out result); return result; } public static Vector2D TransformCoordinateBP(ref Vector2D coord, ref Vector2D BasePoint, ref MatrixD transform) { - Vec.Subtract(ref coord, ref BasePoint, out var r); + VectorMath.Subtract(ref coord, ref BasePoint, out var r); TransformCoordinate(ref r, ref transform, out var result); - Vec.Add(ref result, ref BasePoint, out result); + VectorMath.Add(ref result, ref BasePoint, out result); return result; } } diff --git a/SlaveMatrix/SlaveMatrix/GameClasses/ElementInstance.cs b/SlaveMatrix/SlaveMatrix/GameClasses/ElementInstance.cs index cbf5b2e..7f9f7c7 100644 --- a/SlaveMatrix/SlaveMatrix/GameClasses/ElementInstance.cs +++ b/SlaveMatrix/SlaveMatrix/GameClasses/ElementInstance.cs @@ -44,7 +44,7 @@ namespace SlaveMatrix public void 描画(RenderArea Are) { - Vec.Add(ref Position, ref PositionCont, out Lay.Position); + VectorMath.Add(ref Position, ref PositionCont, out Lay.Position); Are.Draw(Lay); }