diff --git a/Hypercube.Client/Graphics/Windows/Manager/GlfwWindowManager.Window.cs b/Hypercube.Client/Graphics/Windows/Manager/GlfwWindowManager.Window.cs index ae1e6c1..bae94b1 100644 --- a/Hypercube.Client/Graphics/Windows/Manager/GlfwWindowManager.Window.cs +++ b/Hypercube.Client/Graphics/Windows/Manager/GlfwWindowManager.Window.cs @@ -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 }; diff --git a/Hypercube.Shared.Math/Hypercube.Shared.Math.csproj b/Hypercube.Shared.Math/Hypercube.Shared.Math.csproj index 8f59722..b60e9ec 100644 --- a/Hypercube.Shared.Math/Hypercube.Shared.Math.csproj +++ b/Hypercube.Shared.Math/Hypercube.Shared.Math.csproj @@ -9,6 +9,7 @@ + diff --git a/Hypercube.Shared.Math/Matrix/Matrix3X3.Compatibility.cs b/Hypercube.Shared.Math/Matrix/Matrix3X3.Compatibility.cs new file mode 100644 index 0000000..1d6affa --- /dev/null +++ b/Hypercube.Shared.Math/Matrix/Matrix3X3.Compatibility.cs @@ -0,0 +1,39 @@ +using System.Runtime.CompilerServices; +using OpenTK.Mathematics; + +namespace Hypercube.Shared.Math.Matrix; + +public partial struct Matrix3X3 +{ + /* + * OpenTK Compatibility + */ + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Matrix3(Matrix3X3 matrix3) + { + return new Matrix3(matrix3.Row0, matrix3.Row1, matrix3.Row2); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Matrix3X3(Matrix3 matrix3) + { + return new Matrix3X3(matrix3.Row0, matrix3.Row1, matrix3.Row2); + } + + /* + * OpenToolkit Compatibility + */ + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator OpenToolkit.Mathematics.Matrix3(Matrix3X3 matrix3) + { + return new OpenToolkit.Mathematics.Matrix3(matrix3.Row0, matrix3.Row1, matrix3.Row2); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Matrix3X3(OpenToolkit.Mathematics.Matrix3 matrix3) + { + return new Matrix3X3(matrix3.Row0, matrix3.Row1, matrix3.Row2); + } +} \ No newline at end of file diff --git a/Hypercube.Shared.Math/Matrix/Matrix3X3.cs b/Hypercube.Shared.Math/Matrix/Matrix3X3.cs index 4c6cbb1..ddf205f 100644 --- a/Hypercube.Shared.Math/Matrix/Matrix3X3.cs +++ b/Hypercube.Shared.Math/Matrix/Matrix3X3.cs @@ -4,12 +4,13 @@ namespace Hypercube.Shared.Math.Matrix; +// TODO: May be it's can be immutable, and also layout broken [StructLayout(LayoutKind.Sequential)] -public struct Matrix3X3(Vector3 x, Vector3 y, Vector3 z) +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; @@ -17,11 +18,11 @@ public struct Matrix3X3(Vector3 x, Vector3 y, Vector3 z) 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 Raw0 = x; - public Vector3 Raw1 = y; - public Vector3 Raw2 = z; + public Vector3 Row0; + public Vector3 Row1; + public Vector3 Row2; /// /// Matrix x: 0, y: 0 element. @@ -29,9 +30,9 @@ public struct Matrix3X3(Vector3 x, Vector3 y, Vector3 z) public float M00 { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => Raw0.X; + get => Row0.X; [MethodImpl(MethodImplOptions.AggressiveInlining)] - set => Raw0 = Raw0.WithX(value); + set => Row0 = Row0.WithX(value); } /// @@ -40,9 +41,9 @@ public float M00 public float M01 { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => Raw0.Y; + get => Row0.Y; [MethodImpl(MethodImplOptions.AggressiveInlining)] - set => Raw0 = Raw0.WithY(value); + set => Row0 = Row0.WithY(value); } /// @@ -51,9 +52,9 @@ public float M01 public float M02 { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => Raw0.Z; + get => Row0.Z; [MethodImpl(MethodImplOptions.AggressiveInlining)] - set => Raw0 = Raw0.WithZ(value); + set => Row0 = Row0.WithZ(value); } /// @@ -62,9 +63,9 @@ public float M02 public float M10 { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => Raw1.X; + get => Row1.X; [MethodImpl(MethodImplOptions.AggressiveInlining)] - set => Raw1 = Raw1.WithX(value); + set => Row1 = Row1.WithX(value); } /// @@ -73,9 +74,9 @@ public float M10 public float M11 { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => Raw1.Y; + get => Row1.Y; [MethodImpl(MethodImplOptions.AggressiveInlining)] - set => Raw1 = Raw1.WithY(value); + set => Row1 = Row1.WithY(value); } /// @@ -84,9 +85,9 @@ public float M11 public float M12 { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => Raw1.Z; + get => Row1.Z; [MethodImpl(MethodImplOptions.AggressiveInlining)] - set => Raw1 = Raw1.WithZ(value); + set => Row1 = Row1.WithZ(value); } /// @@ -95,9 +96,9 @@ public float M12 public float M20 { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => Raw2.X; + get => Row2.X; [MethodImpl(MethodImplOptions.AggressiveInlining)] - set => Raw2 = Raw2.WithX(value); + set => Row2 = Row2.WithX(value); } /// @@ -106,9 +107,9 @@ public float M20 public float M21 { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => Raw2.Y; + get => Row2.Y; [MethodImpl(MethodImplOptions.AggressiveInlining)] - set => Raw2 = Raw2.WithY(value); + set => Row2 = Row2.WithY(value); } /// @@ -117,30 +118,30 @@ public float M21 public float M22 { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => Raw2.Z; + get => Row2.Z; [MethodImpl(MethodImplOptions.AggressiveInlining)] - set => Raw2 = Raw2.WithZ(value); + set => Row2 = Row2.WithZ(value); } public float this[int raw, int colum] { 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, @@ -153,7 +154,7 @@ public float M22 { switch (raw) { - case IndexRaw0: + case IndexRow0: switch (colum) { case IndexColumn0: @@ -173,7 +174,7 @@ public float M22 } break; - case IndexRaw1: + case IndexRow1: switch (colum) { case IndexColumn0: @@ -193,7 +194,7 @@ public float M22 } break; - case IndexRaw2: + case IndexRow2: switch (colum) { case IndexColumn0: @@ -218,11 +219,33 @@ public float M22 } } } + /// + /// Creates 3x3 matrix + /// + /// x.X | x.Y | x.Z + /// y.X | y.Y | y.Z + /// z.X | z.Y | z.Z + /// + /// + public Matrix3X3(Vector3 x, Vector3 y, Vector3 z) + { + Row0 = x; + Row1 = y; + Row2 = z; + } public Matrix3X3(Vector3 value) : this(value, value, value) { } + /// + /// Creates 3x3 matrix + /// + /// m00 | m01 | m02 + /// m10 | m11 | m12 + /// m20 | m21 | m22 + /// + /// public Matrix3X3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22) : this(new Vector3(m00, m01, m02), @@ -379,6 +402,6 @@ public static Matrix3X3 CreateTransform(Vector2 position, Angle angle, Vector2 s public static Vector3 operator *(Matrix3X3 a, Vector3 b) { - return a.Raw0 * b.X + a.Raw1 * b.Y + a.Raw2 * b.Z; + return a.Row0 * b.X + a.Row1 * b.Y + a.Row2 * b.Z; } } \ No newline at end of file diff --git a/Hypercube.Shared.Math/Matrix/Matrix4X4.Compability.cs b/Hypercube.Shared.Math/Matrix/Matrix4X4.Compability.cs deleted file mode 100644 index f930d1c..0000000 --- a/Hypercube.Shared.Math/Matrix/Matrix4X4.Compability.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Runtime.CompilerServices; - -namespace Hypercube.Shared.Math.Matrix; - -public partial struct Matrix4X4 -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator OpenTK.Mathematics.Matrix4(Matrix4X4 matrix4X4) - { - return new OpenTK.Mathematics.Matrix4(matrix4X4.Row0, matrix4X4.Row1, matrix4X4.Row2, matrix4X4.Row3); - } -} \ No newline at end of file diff --git a/Hypercube.Shared.Math/Matrix/Matrix4X4.Compatibility.cs b/Hypercube.Shared.Math/Matrix/Matrix4X4.Compatibility.cs new file mode 100644 index 0000000..d626311 --- /dev/null +++ b/Hypercube.Shared.Math/Matrix/Matrix4X4.Compatibility.cs @@ -0,0 +1,38 @@ +using System.Runtime.CompilerServices; +using OpenTK.Mathematics; + +namespace Hypercube.Shared.Math.Matrix; + +public partial struct Matrix4X4 +{ + /* + * OpenTK Compatibility + */ + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator OpenTK.Mathematics.Matrix4(Matrix4X4 matrix4X4) + { + return new OpenTK.Mathematics.Matrix4(matrix4X4.Row0, matrix4X4.Row1, matrix4X4.Row2, matrix4X4.Row3); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Matrix4X4(Matrix4 matrix4) + { + return new Matrix4X4(matrix4.Row0, matrix4.Row1, matrix4.Row2, matrix4.Row3); + } + + /* + * Open Toolkit Compatibility + */ + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator OpenToolkit.Mathematics.Matrix4(Matrix4X4 matrix4X4) + { + return new OpenToolkit.Mathematics.Matrix4(matrix4X4.Row0, matrix4X4.Row1, matrix4X4.Row2, matrix4X4.Row3); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Matrix4X4(OpenToolkit.Mathematics.Matrix4 matrix4) + { + return new Matrix4X4(matrix4.Row0, matrix4.Row1, matrix4.Row2, matrix4.Row3); + } +} \ No newline at end of file diff --git a/Hypercube.Shared.Math/Matrix/Matrix4X4.cs b/Hypercube.Shared.Math/Matrix/Matrix4X4.cs index 486b94d..17cef3d 100644 --- a/Hypercube.Shared.Math/Matrix/Matrix4X4.cs +++ b/Hypercube.Shared.Math/Matrix/Matrix4X4.cs @@ -8,16 +8,9 @@ namespace Hypercube.Shared.Math.Matrix; [StructLayout(LayoutKind.Sequential)] public partial struct Matrix4X4 : IEquatable { - 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; @@ -204,7 +197,19 @@ public float M33 [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Row3 = Row3.WithW(value); } - + /// + /// Creates new matrix 4x4 + /// + /// Row0.X | Row0.Y | Row0.Z | Row0.W + /// Row1.X | Row1.Y | Row1.Z | Row1.W + /// Row2.X | Row2.Y | Row2.Z | Row2.W + /// Row2.X | Row2.Y | Row2.Z | Row2.W + /// + /// + /// Row 0 + /// Row 1 + /// Row 2 + /// Row 3 public Matrix4X4(Vector4 x, Vector4 y, Vector4 z, Vector4 w) { Row0 = x; @@ -212,7 +217,10 @@ public Matrix4X4(Vector4 x, Vector4 y, Vector4 z, Vector4 w) Row2 = z; Row3 = w; } - + /// + /// Creates new matrix with all rows is "" + /// + /// Vector4 to make rows out of public Matrix4X4(Vector4 value) : this(value, value, value, value) { } diff --git a/Hypercube.Shared.Math/Vector/Vector2.Compability.cs b/Hypercube.Shared.Math/Vector/Vector2.Compability.cs deleted file mode 100644 index 0f012c5..0000000 --- a/Hypercube.Shared.Math/Vector/Vector2.Compability.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Runtime.CompilerServices; - -namespace Hypercube.Shared.Math.Vector; - -public readonly partial struct Vector2 -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Vector3(Vector2 vector) - { - return new Vector3(vector.X, vector.Y, 0f); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Vector2(System.Numerics.Vector2 vector) - { - return new Vector2(vector.X, vector.Y); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator System.Numerics.Vector2(Vector2 vector) - { - return new System.Numerics.Vector2(vector.X, vector.Y); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Vector2(OpenTK.Mathematics.Vector2 vector) - { - return new Vector2(vector.X, vector.Y); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator OpenTK.Mathematics.Vector2(Vector2 vector) - { - return new OpenTK.Mathematics.Vector2(vector.X, vector.Y); - } -} \ No newline at end of file diff --git a/Hypercube.Shared.Math/Vector/Vector2.Compatibility.cs b/Hypercube.Shared.Math/Vector/Vector2.Compatibility.cs new file mode 100644 index 0000000..b79f9eb --- /dev/null +++ b/Hypercube.Shared.Math/Vector/Vector2.Compatibility.cs @@ -0,0 +1,86 @@ +using System.Runtime.CompilerServices; + +namespace Hypercube.Shared.Math.Vector; + +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 + */ + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Vector2(System.Numerics.Vector2 vector) + { + return new Vector2(vector.X, vector.Y); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator System.Numerics.Vector2(Vector2 vector) + { + return new System.Numerics.Vector2(vector.X, vector.Y); + } + + /* + * OpenTK Compatibility + */ + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Vector2(OpenTK.Mathematics.Vector2 vector) + { + return new Vector2(vector.X, vector.Y); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator OpenTK.Mathematics.Vector2(Vector2 vector) + { + return new OpenTK.Mathematics.Vector2(vector.X, vector.Y); + } + + /* + * OpenToolkit Compatibility + */ + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Vector2(OpenToolkit.Mathematics.Vector2 vector) + { + return new Vector2(vector.X, vector.Y); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator OpenToolkit.Mathematics.Vector2(Vector2 vector) + { + return new OpenToolkit.Mathematics.Vector2(vector.X, vector.Y); + } +} \ No newline at end of file diff --git a/Hypercube.Shared.Math/Vector/Vector2.cs b/Hypercube.Shared.Math/Vector/Vector2.cs index 8ce7e98..4742380 100644 --- a/Hypercube.Shared.Math/Vector/Vector2.cs +++ b/Hypercube.Shared.Math/Vector/Vector2.cs @@ -4,24 +4,33 @@ namespace Hypercube.Shared.Math.Vector; [StructLayout(LayoutKind.Sequential)] -public readonly partial struct Vector2(float x, float y) : IEquatable +public readonly partial struct Vector2 : IEquatable { 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) @@ -65,7 +74,7 @@ public override int GetHashCode() [MethodImpl(MethodImplOptions.AggressiveInlining)] public override string ToString() { - return $"{x}, {y}"; + return $"{X}, {Y}"; } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/Hypercube.Shared.Math/Vector/Vector2Int.Compability.cs b/Hypercube.Shared.Math/Vector/Vector2Int.Compability.cs deleted file mode 100644 index f1df15b..0000000 --- a/Hypercube.Shared.Math/Vector/Vector2Int.Compability.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Runtime.CompilerServices; - -namespace Hypercube.Shared.Math.Vector; - -public readonly partial struct Vector2Int -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Vector2Int(System.Drawing.Size size) - { - return new Vector2Int(size.Width, size.Height); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator System.Drawing.Size(Vector2Int vector2) - { - return new System.Drawing.Size(vector2.X, vector2.Y); - } -} \ No newline at end of file diff --git a/Hypercube.Shared.Math/Vector/Vector2Int.Compatibility.cs b/Hypercube.Shared.Math/Vector/Vector2Int.Compatibility.cs new file mode 100644 index 0000000..fc6d717 --- /dev/null +++ b/Hypercube.Shared.Math/Vector/Vector2Int.Compatibility.cs @@ -0,0 +1,86 @@ +using System.Runtime.CompilerServices; + +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) + { + return new Vector2Int(size.Width, size.Height); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + 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); + } +} \ No newline at end of file diff --git a/Hypercube.Shared.Math/Vector/Vector2Int.cs b/Hypercube.Shared.Math/Vector/Vector2Int.cs index c61fba7..554e67f 100644 --- a/Hypercube.Shared.Math/Vector/Vector2Int.cs +++ b/Hypercube.Shared.Math/Vector/Vector2Int.cs @@ -1,114 +1,158 @@ -using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; namespace Hypercube.Shared.Math.Vector; [StructLayout(LayoutKind.Sequential)] -public readonly partial struct Vector2Int(int x, int y) +public readonly partial struct Vector2Int : IEquatable { public static readonly Vector2Int Zero = new(0, 0); public static readonly Vector2Int One = new(1, 1); - public static readonly Vector2Int Up = new(0, 1); - public static readonly Vector2Int Down = new(0, -1); - public static readonly Vector2Int Right = new(1, 0); - public static readonly Vector2Int Left = new(-1, 0); + + public static readonly Vector2Int UnitX = new(1, 0); + public static readonly Vector2Int UnitY = new(0, 1); + + public readonly int X; + public readonly int Y; + + public float AspectRatio + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => X / (float)Y; + } + + public float Length + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => MathF.Sqrt(X * X + Y * Y); + } + + public Vector2Int(int x, int y) + { + X = x; + Y = y; + } + + public Vector2Int(int value) : this(value, value) + { + } + + public Vector2Int(Vector2Int vector2Int) : this(vector2Int.X, vector2Int.Y) + { + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector2Int WithX(int value) + { + return new Vector2Int(value, Y); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector2Int WithY(int value) + { + return new Vector2Int(X, value); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Equals(Vector2Int other) + { + return X == other.X && + Y == other.Y; + } - public readonly int X = x; - public readonly int Y = y; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override bool Equals(object? obj) + { + return obj is Vector2Int vector && Equals(vector); + } - public float Ratio => x / (float)y; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override int GetHashCode() + { + return HashCode.Combine(X, Y); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override string ToString() + { + return $"{X}, {Y}"; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector2Int operator +(Vector2Int a, Vector2Int b) { return new Vector2Int(a.X + b.X, a.Y + b.Y); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector2Int operator +(Vector2Int a, int b) { return new Vector2Int(a.X + b, a.Y + b); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2Int operator -(Vector2Int a) + { + return new Vector2Int(-a.X, -a.Y); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector2Int operator -(Vector2Int a, Vector2Int b) { return new Vector2Int(a.X - b.X, a.Y - b.Y); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector2Int operator -(Vector2Int a, int b) { return new Vector2Int(a.X - b, a.Y - b); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector2Int operator *(Vector2Int a, Vector2Int b) { return new Vector2Int(a.X * b.X, a.Y * b.Y); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector2Int operator *(Vector2Int a, int b) { return new Vector2Int(a.X * b, a.Y * b); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector2 operator *(Vector2Int a, float b) { return new Vector2(a.X * b, a.Y * b); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector2Int operator /(Vector2Int a, Vector2Int b) { return new Vector2Int(a.X / b.X, a.Y / b.Y); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector2Int operator /(Vector2Int a, int b) { return new Vector2Int(a.X / b, a.Y / b); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector2 operator /(Vector2Int a, float b) { return new Vector2(a.X / b, a.Y / b); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(Vector2Int a, Vector2Int b) { return a.Equals(b); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(Vector2Int a, Vector2Int b) { return !a.Equals(b); } - - public static implicit operator Vector2(Vector2Int a) - { - return new Vector2(a.X, a.Y); - } - - public static implicit operator Vector2Int(Vector2 a) - { - return new Vector2Int((int)a.X, (int)a.Y); - } - - public static implicit operator Vector2Int((int x, int y) a) - { - return new Vector2Int(a.x, a.y); - } - - public readonly bool Equals(Vector2Int other) - { - return X == other.X && Y == other.Y; - } - - public readonly override bool Equals(object? obj) - { - return obj is Vector2Int vector && Equals(vector); - } - - public override int GetHashCode() - { - return x + y; - } - - public override string ToString() - { - return $"{x}, {y}"; - } } \ No newline at end of file diff --git a/Hypercube.Shared.Math/Vector/Vector3.Compability.cs b/Hypercube.Shared.Math/Vector/Vector3.Compability.cs deleted file mode 100644 index 2045295..0000000 --- a/Hypercube.Shared.Math/Vector/Vector3.Compability.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Runtime.CompilerServices; - -namespace Hypercube.Shared.Math.Vector; - -public readonly partial struct Vector3 -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Vector2(Vector3 vector) - { - return new Vector2(vector.X, vector.Y); - } -} \ No newline at end of file diff --git a/Hypercube.Shared.Math/Vector/Vector3.Compatibility.cs b/Hypercube.Shared.Math/Vector/Vector3.Compatibility.cs new file mode 100644 index 0000000..5236e58 --- /dev/null +++ b/Hypercube.Shared.Math/Vector/Vector3.Compatibility.cs @@ -0,0 +1,86 @@ +using System.Runtime.CompilerServices; + +namespace Hypercube.Shared.Math.Vector; + +public readonly partial struct Vector3 +{ + /* + * Self Compatibility + */ + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Vector2(Vector3 vector) + { + return new Vector2(vector.X, vector.Y); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Vector2Int(Vector3 vector) + { + return new Vector2Int((int)vector.X, (int)vector.Y); + } + + /* + * Tuple Compatibility + */ + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Vector3((float x, float y, float z) a) + { + return new Vector3(a.x, a.y, a.z); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator (float x, float y, float z)(Vector3 a) + { + return (a.X, a.Y, a.Z); + } + + /* + * System.Numerics Compatibility + */ + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator System.Numerics.Vector3(Vector3 vector3) + { + return new System.Numerics.Vector3(vector3.X, vector3.Y, vector3.Z); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Vector3(System.Numerics.Vector3 vector3) + { + return new Vector3(vector3.X, vector3.Y, vector3.Z); + } + + /* + * OpenTK Compatibility + */ + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator OpenTK.Mathematics.Vector3(Vector3 vector) + { + return new OpenTK.Mathematics.Vector3(vector.X, vector.Y, vector.Z); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Vector3(OpenTK.Mathematics.Vector3 vector) + { + return new Vector3(vector.X, vector.Y, vector.Z); + } + + /* + * OpenToolkit Compatibility + */ + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator OpenToolkit.Mathematics.Vector3(Vector3 vector) + { + return new OpenToolkit.Mathematics.Vector3(vector.X, vector.Y, vector.Z); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Vector3(OpenToolkit.Mathematics.Vector3 vector) + { + return new Vector3(vector.X, vector.Y, vector.Z); + } +} \ No newline at end of file diff --git a/Hypercube.Shared.Math/Vector/Vector3.cs b/Hypercube.Shared.Math/Vector/Vector3.cs index b6912a5..e9f8463 100644 --- a/Hypercube.Shared.Math/Vector/Vector3.cs +++ b/Hypercube.Shared.Math/Vector/Vector3.cs @@ -1,19 +1,15 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using Hypercube.Shared.Math.Extensions; namespace Hypercube.Shared.Math.Vector; [StructLayout(LayoutKind.Sequential)] -public readonly partial struct Vector3(float x, float y, float z) +public readonly partial struct Vector3(float x, float y, float z) : IEquatable { public static readonly Vector3 Zero = new(0, 0, 0); public static readonly Vector3 One = new(1, 1, 1); - public static readonly Vector3 Forward = new(0, 0, 1); - public static readonly Vector3 Back = new(0, 0, -1); - public static readonly Vector3 Up = new(0, 1, 0); - public static readonly Vector3 Down = new(0, -1, 0); - public static readonly Vector3 Right = new(1, 0, 0); - public static readonly Vector3 Left = new(-1, 0, 0); + public static readonly Vector3 UnitX = new(1, 0, 0); public static readonly Vector3 UnitY = new(0, 1, 0); public static readonly Vector3 UnitZ = new(0, 0, 1); @@ -79,7 +75,27 @@ public Vector3 Cross(Vector3 other) { return Vector3.Cross(this, other); } - + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Equals(Vector3 other) + { + return X.AboutEquals(other.X) && + Y.AboutEquals(other.Y) && + Z.AboutEquals(other.Z); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override bool Equals(object? obj) + { + return obj is Vector3 other && Equals(other); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override int GetHashCode() + { + return HashCode.Combine(X, Y, Z); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public override string ToString() { @@ -164,6 +180,18 @@ public override string ToString() return new Vector3(a.X / b, a.Y / b, a.Z / b); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Vector3 a, Vector3 b) + { + return a.Equals(b); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Vector3 a, Vector3 b) + { + return !a.Equals(b); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Cross(Vector3 left, Vector3 right) { diff --git a/Hypercube.Shared.Math/Vector/Vector4.Compability.cs b/Hypercube.Shared.Math/Vector/Vector4.Compatibility.cs similarity index 62% rename from Hypercube.Shared.Math/Vector/Vector4.Compability.cs rename to Hypercube.Shared.Math/Vector/Vector4.Compatibility.cs index 9d42586..61adf86 100644 --- a/Hypercube.Shared.Math/Vector/Vector4.Compability.cs +++ b/Hypercube.Shared.Math/Vector/Vector4.Compatibility.cs @@ -4,6 +4,10 @@ namespace Hypercube.Shared.Math.Vector; public readonly partial struct Vector4 { + /* + * System.Numerics Compatibility + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator Vector4(System.Numerics.Vector4 vector4) { @@ -16,6 +20,10 @@ public static implicit operator System.Numerics.Vector4(Vector4 vector4) return new System.Numerics.Vector4(vector4.X, vector4.Y, vector4.Z, vector4.W); } + /* + * OpenTK Compatibility + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator Vector4(OpenTK.Mathematics.Vector4 vector4) { @@ -27,4 +35,20 @@ public static implicit operator OpenTK.Mathematics.Vector4(Vector4 vector4) { return new OpenTK.Mathematics.Vector4(vector4.X, vector4.Y, vector4.Z, vector4.W); } + + /* + * OpenToolkit Compatibility + */ + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator OpenToolkit.Mathematics.Vector4(Vector4 vector4) + { + return new OpenToolkit.Mathematics.Vector4(vector4.X, vector4.Y, vector4.Z, vector4.W); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Vector4(OpenToolkit.Mathematics.Vector4 vector4) + { + return new Vector4(vector4.X, vector4.Y, vector4.Z, vector4.W); + } } \ No newline at end of file diff --git a/Hypercube.Shared.Math/Vector/Vector4.cs b/Hypercube.Shared.Math/Vector/Vector4.cs index 95bc583..224dcb1 100644 --- a/Hypercube.Shared.Math/Vector/Vector4.cs +++ b/Hypercube.Shared.Math/Vector/Vector4.cs @@ -7,10 +7,9 @@ namespace Hypercube.Shared.Math.Vector; [StructLayout(LayoutKind.Sequential)] public readonly partial struct Vector4 : IEquatable { - public const int Size = 4 * sizeof(float); - public static readonly Vector4 Zero = new(0, 0, 0, 0); public static readonly Vector4 One = new(1, 1, 1, 1); + public static readonly Vector4 UnitX = new(1, 0, 0, 0); public static readonly Vector4 UnitY = new(0, 1, 0, 0); public static readonly Vector4 UnitZ = new(0, 0, 1, 0);