Math classes inling optimizations

This commit is contained in:
2026-06-14 01:49:24 +02:00
parent 7b458c4ec8
commit 704ecea09c
6 changed files with 91 additions and 81 deletions

View File

@@ -3,7 +3,7 @@ using System.Globalization;
namespace _2DGAMELIB namespace _2DGAMELIB
{ {
public static class Qua public static class Quaternion
{ {
public static QuaternionD RotationZQ(this double angle) public static QuaternionD RotationZQ(this double angle)
{ {
@@ -29,14 +29,13 @@ namespace _2DGAMELIB
public static Vector2D TransformCoordinateBP(this Vector2D coord, Vector2D BasePoint, QuaternionD rotation) 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); 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; return result;
} }
} }
//used in a total of 1 places //used in a total of 1 places
[Serializable] [Serializable]
public struct QuaternionD public struct QuaternionD

View File

@@ -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;
}
}
}

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Globalization; using System.Globalization;
using System.Runtime.CompilerServices;
namespace _2DGAMELIB namespace _2DGAMELIB
{ {
@@ -21,17 +22,19 @@ namespace _2DGAMELIB
X = x; X = x;
Y = y; Y = y;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double Length() public double Length()
{ {
return System.Math.Sqrt(X * X + Y * Y); return System.Math.Sqrt(X * X + Y * Y);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double LengthSquared() public double LengthSquared()
{ {
return X * X + Y * Y; return X * X + Y * Y;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Normalize() public void Normalize()
{ {
double num = Length(); double num = Length();

View File

@@ -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;
}
}
}

View File

@@ -195,17 +195,17 @@ namespace _2DGAMELIB
public static Vector2D TransformCoordinateBP(this Vector2D coord, Vector2D BasePoint, MatrixD transform) 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); 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; return result;
} }
public static Vector2D TransformCoordinateBP(ref Vector2D coord, ref Vector2D BasePoint, ref MatrixD transform) 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); 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; return result;
} }
} }

View File

@@ -44,7 +44,7 @@ namespace SlaveMatrix
public void (RenderArea Are) 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); Are.Draw(Lay);
} }