Skip to content

Commit

Permalink
Clean up more math structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Jul 11, 2024
1 parent 3b4e0de commit 5f1c041
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private GlfwWindowRegistration WindowSetup(Window* window, WindowCreateSettings
Pointer = window,
Id = new WindowId(_nextWindowId++),

Ratio = framebufferSize.Ratio,
Ratio = framebufferSize.AspectRatio,
Size = size,
FramebufferSize = framebufferSize
};
Expand Down
21 changes: 11 additions & 10 deletions Hypercube.Shared.Math/Matrix/Matrix3X3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@

namespace Hypercube.Shared.Math.Matrix;

// TODO: May be it's can be immutable, and also layout broken
[StructLayout(LayoutKind.Sequential)]
public partial struct Matrix3X3
{
private const int IndexRaw0 = 0;
private const int IndexRaw1 = 1;
private const int IndexRaw2 = 2;
private const int IndexRow0 = 0;
private const int IndexRow1 = 1;
private const int IndexRow2 = 2;

private const int IndexColumn0 = 0;
private const int IndexColumn1 = 1;
private const int IndexColumn2 = 2;

public static Matrix3X3 Zero => new(Vector3.Zero);
public static Matrix3X3 One => new(Vector3.One);
public static Matrix3X3 Identity => new(Vector3.Right, Vector3.Up, Vector3.Forward);
public static Matrix3X3 Identity => new(Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ);

public Vector3 Row0;
public Vector3 Row1;
Expand Down Expand Up @@ -126,21 +127,21 @@ public float M22
{
get => raw switch
{
IndexRaw0 => colum switch
IndexRow0 => colum switch
{
IndexColumn0 => M00,
IndexColumn1 => M01,
IndexColumn2 => M02,
_ => throw new ArgumentOutOfRangeException(nameof(colum), colum, null)
},
IndexRaw1 => colum switch
IndexRow1 => colum switch
{
IndexColumn0 => M10,
IndexColumn1 => M11,
IndexColumn2 => M12,
_ => throw new ArgumentOutOfRangeException(nameof(colum), colum, null)
},
IndexRaw2 => colum switch
IndexRow2 => colum switch
{
IndexColumn0 => M20,
IndexColumn1 => M21,
Expand All @@ -153,7 +154,7 @@ public float M22
{
switch (raw)
{
case IndexRaw0:
case IndexRow0:
switch (colum)
{
case IndexColumn0:
Expand All @@ -173,7 +174,7 @@ public float M22
}
break;

case IndexRaw1:
case IndexRow1:
switch (colum)
{
case IndexColumn0:
Expand All @@ -193,7 +194,7 @@ public float M22
}
break;

case IndexRaw2:
case IndexRow2:
switch (colum)
{
case IndexColumn0:
Expand Down
9 changes: 1 addition & 8 deletions Hypercube.Shared.Math/Matrix/Matrix4X4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,9 @@ namespace Hypercube.Shared.Math.Matrix;
[StructLayout(LayoutKind.Sequential)]
public partial struct Matrix4X4 : IEquatable<Matrix4X4>
{
public const int Size = 4 * Vector4.Size;

public static Matrix4X4 Zero => new(Vector4.Zero);
public static Matrix4X4 One => new(Vector4.One);

public static Matrix4X4 Identity => new(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
public static Matrix4X4 Identity => new(Vector4.UnitX, Vector4.UnitY, Vector4.UnitZ, Vector4.UnitW);

public Vector4 Row0;
public Vector4 Row1;
Expand Down
22 changes: 22 additions & 0 deletions Hypercube.Shared.Math/Vector/Vector2.Compatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,34 @@ public readonly partial struct Vector2
* Self Compatibility
*/

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vector2Int(Vector2 vector)
{
return new Vector2Int((int)vector.X, (int)vector.Y);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vector3(Vector2 vector)
{
return new Vector3(vector.X, vector.Y, 0f);
}

/*
* Tuple Compatibility
*/

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vector2((float x, float y) a)
{
return new Vector2(a.x, a.y);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator (float x, float y)(Vector2 a)
{
return (a.X, a.Y);
}

/*
* System.Numerics Compatibility
*/
Expand Down
27 changes: 18 additions & 9 deletions Hypercube.Shared.Math/Vector/Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,33 @@
namespace Hypercube.Shared.Math.Vector;

[StructLayout(LayoutKind.Sequential)]
public readonly partial struct Vector2(float x, float y) : IEquatable<Vector2>
public readonly partial struct Vector2 : IEquatable<Vector2>
{
public static readonly Vector2 Zero = new(0, 0);
public static readonly Vector2 One = new(1, 1);
public static readonly Vector2 Up = new(0, 1);
public static readonly Vector2 Down = new(0, -1);
public static readonly Vector2 Right = new(1, 0);
public static readonly Vector2 Left = new(-1, 0);

public static readonly Vector2 UnitX = new(1, 0);
public static readonly Vector2 UnitY = new(0, 1);

public readonly float X = x;
public readonly float Y = y;
public readonly float X;
public readonly float Y;

public float AspectRatio
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => X / Y;
}

public float Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => MathF.Sqrt(x * x + y * y);
get => MathF.Sqrt(X * X + Y * Y);
}

public Vector2(float x, float y)
{
X = x;
Y = y;
}

public Vector2(float value) : this(value, value)
Expand Down Expand Up @@ -65,7 +74,7 @@ public override int GetHashCode()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override string ToString()
{
return $"{x}, {y}";
return $"{X}, {Y}";
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
68 changes: 68 additions & 0 deletions Hypercube.Shared.Math/Vector/Vector2Int.Compatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,42 @@ namespace Hypercube.Shared.Math.Vector;

public readonly partial struct Vector2Int
{
/*
* Self Compatibility
*/

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vector2(Vector2Int vector)
{
return new Vector2(vector.X, vector.Y);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vector3(Vector2Int vector)
{
return new Vector3(vector.X, vector.Y, 0f);
}

/*
* Tuple Compatibility
*/

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vector2Int((int x, int y) a)
{
return new Vector2Int(a.x, a.y);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator (int x, int y)(Vector2Int a)
{
return (a.X, a.Y);
}

/*
* System.Drawing Compatibility
*/

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vector2Int(System.Drawing.Size size)
{
Expand All @@ -15,4 +51,36 @@ public static implicit operator System.Drawing.Size(Vector2Int vector2)
{
return new System.Drawing.Size(vector2.X, vector2.Y);
}

/*
* OpenTK Compatibility
*/

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vector2Int(OpenTK.Mathematics.Vector2i vector)
{
return new Vector2Int(vector.X, vector.Y);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator OpenTK.Mathematics.Vector2i(Vector2Int vector)
{
return new OpenTK.Mathematics.Vector2i(vector.X, vector.Y);
}

/*
* OpenToolkit Compatibility
*/

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vector2Int(OpenToolkit.Mathematics.Vector2i vector)
{
return new Vector2Int(vector.X, vector.Y);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator OpenToolkit.Mathematics.Vector2i(Vector2Int vector)
{
return new OpenToolkit.Mathematics.Vector2i(vector.X, vector.Y);
}
}
Loading

0 comments on commit 5f1c041

Please sign in to comment.