Skip to content

[3.0] Generic Maths Implementation. #2459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 38 commits into
base: develop/3.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
3c95d6d
start of 3.0 Silk.Net.Maths rewrite
Tweety-Lab May 28, 2025
04846e7
Start work on Vector2I.
Tweety-Lab May 28, 2025
7afd57f
Finish implementing Vector2I Interfaces.
Tweety-Lab May 28, 2025
249be1f
Add Vector2I Unit properties.
Tweety-Lab May 28, 2025
709c142
Add Vector3I Maths methods.
Tweety-Lab May 28, 2025
94a912d
Add more missing Vector2I requirements.
Tweety-Lab May 28, 2025
051785d
Add `Vector3I`.
Tweety-Lab May 28, 2025
88b8405
Change VectorI XML comments.
Tweety-Lab May 28, 2025
d1eade9
Add `Vector4I`.
Tweety-Lab May 28, 2025
9b68e35
Improve VectorI equality checks.
Tweety-Lab May 28, 2025
6a3cf88
Start work on new `Quaternion`.
Tweety-Lab May 28, 2025
cc7ebb6
Clarify current state of new Math type's indexers.
Tweety-Lab May 28, 2025
6af8612
Add lower-dimensional VectorI constructors.
Tweety-Lab May 30, 2025
737b5a8
Add `Vector2F`.
Tweety-Lab May 30, 2025
191de4e
Include generated code.
otac0n May 30, 2025
1cecebe
Merge pull request #1 from otac0n/feature/math-3.0
Tweety-Lab May 30, 2025
291ec1e
Generated constructors, operators, and indexers.
otac0n May 30, 2025
cfee012
Whitespace and comments.
otac0n May 31, 2025
91c7982
Added transpose, negate, and identity.
otac0n May 31, 2025
650f30b
Added some necessary vector bits for testing.
otac0n May 31, 2025
8ebcfb9
Merge pull request #2 from otac0n/feature/math-3.0
Tweety-Lab May 31, 2025
9bd27fe
Add Lerp and a couple of bugfixes.
otac0n May 31, 2025
a617eb8
Remove `IBinaryFloatingPointIeee754` reference.
Tweety-Lab May 31, 2025
d57f94d
Make `VectorNI` Vectors use a `ref` indexer.
Tweety-Lab May 31, 2025
d5a8b54
Use partial implementations from code gen.
otac0n May 31, 2025
3c538bd
Make `Vector2F` Vector use a `ref` indexer.
Tweety-Lab May 31, 2025
4f74ab3
Fix vector `ref` indexer exception messages.
Tweety-Lab May 31, 2025
c9e57f2
Merge branch 'feature/math-3.0' of https://github.com/Tweety-Lab/Silk…
otac0n May 31, 2025
a4b8a6c
Move constructors & existing interfaces.
otac0n May 31, 2025
5a82141
IReadOnlyList<T> code gen.
otac0n May 31, 2025
16e68e2
Fix errors with Quaternion accessibility.
otac0n May 31, 2025
7efc1eb
Merge pull request #3 from otac0n/feature/math-3.0
Tweety-Lab May 31, 2025
0cb1569
Move common operators and interfaces into templates.
otac0n May 31, 2025
c9ce628
Use Legacy namespace qualifications for lecacy Quaternion.
otac0n May 31, 2025
aac298a
Generate constructors to extend smaller vectors.
otac0n May 31, 2025
25f6994
Removed AllBitsSet from Vector2F.
otac0n May 31, 2025
41aa58d
Generate vector specific methods in templates.
otac0n May 31, 2025
0f2d43c
Merge pull request #4 from otac0n/feature/math-3.0
Tweety-Lab Jun 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
794 changes: 794 additions & 0 deletions sources/Maths/Maths/Legacy/Quaternion.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static Vector2D<T> TransformNormal<T>(Vector2D<T> normal, Matrix4X4<T> ma
/// <param name="value">The source vector to be rotated.</param>
/// <param name="rotation">The rotation to apply.</param>
/// <returns>The transformed vector.</returns>
public static Vector2D<T> Transform<T>(Vector2D<T> value, Quaternion<T> rotation)
public static Vector2D<T> Transform<T>(Vector2D<T> value, Legacy.Quaternion<T> rotation)
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
{
T x2 = Scalar.Add(rotation.X, rotation.X);
Expand Down Expand Up @@ -285,4 +285,4 @@ public static Vector2D<T> Multiply<T>(Vector2D<T> value1, Matrix2X2<T> value2)
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
=> value1 * value2;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public static Vector3D<T> Transform<T>(Vector3D<T> position, Matrix4X4<T> matrix
/// <param name="rotation">The rotation to apply.</param>
/// <returns>The transformed vector.</returns>
[MethodImpl((MethodImplOptions) 768)]
public static Vector3D<T> Transform<T>(Vector3D<T> value, Quaternion<T> rotation)
public static Vector3D<T> Transform<T>(Vector3D<T> value, Legacy.Quaternion<T> rotation)
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
{
T x2 = Scalar.Add(rotation.X, rotation.X);
Expand Down Expand Up @@ -296,4 +296,4 @@ public static Vector3D<T> TransformNormal<T>(Vector3D<T> normal, Matrix4X4<T> ma
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public static Vector4D<T> Transform<T>(Vector2D<T> position, Matrix4X4<T> matrix
/// <param name="rotation">The rotation to apply.</param>
/// <returns>The transformed vector.</returns>
[MethodImpl((MethodImplOptions) 768)]
public static Vector4D<T> Transform<T>(Vector2D<T> value, Quaternion<T> rotation)
public static Vector4D<T> Transform<T>(Vector2D<T> value, Silk.NET.Maths.Legacy.Quaternion<T> rotation)
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
{
T x2 = Scalar.Add(rotation.X, rotation.X);
Expand Down Expand Up @@ -283,7 +283,7 @@ public static Vector4D<T> Transform<T>(Vector3D<T> position, Matrix4X4<T> matrix
/// <param name="rotation">The rotation to apply.</param>
/// <returns>The transformed vector.</returns>
[MethodImpl((MethodImplOptions) 768)]
public static Vector4D<T> Transform<T>(Vector3D<T> value, Quaternion<T> rotation)
public static Vector4D<T> Transform<T>(Vector3D<T> value, Legacy.Quaternion<T> rotation)
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
{
T x2 = Scalar.Add(rotation.X, rotation.X);
Expand Down Expand Up @@ -329,7 +329,7 @@ public static Vector4D<T> Transform<T>(Vector4D<T> vector, Matrix4X4<T> matrix)
/// <param name="rotation">The rotation to apply.</param>
/// <returns>The transformed vector.</returns>
[MethodImpl((MethodImplOptions) 768)]
public static Vector4D<T> Transform<T>(Vector4D<T> value, Quaternion<T> rotation)
public static Vector4D<T> Transform<T>(Vector4D<T> value, Legacy.Quaternion<T> rotation)
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
{
T x2 = Scalar.Add(rotation.X, rotation.X);
Expand All @@ -354,4 +354,4 @@ public static Vector4D<T> Transform<T>(Vector4D<T> value, Quaternion<T> rotation
);
}
}
}
}
8 changes: 4 additions & 4 deletions sources/Maths/Maths/Matrix2X3.Ops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static Matrix2X3<T> CreateFromAxisAngle<T>(Vector3D<T> axis, T angle)
/// <summary>Creates a rotation matrix from the given Quaternion rotation value.</summary>
/// <param name="quaternion">The source Quaternion.</param>
/// <returns>The rotation matrix.</returns>
public static Matrix2X3<T> CreateFromQuaternion<T>(Quaternion<T> quaternion)
public static Matrix2X3<T> CreateFromQuaternion<T>(Silk.NET.Maths.Legacy.Quaternion<T> quaternion)
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
{
Matrix2X3<T> result = Matrix2X3<T>.Identity;
Expand Down Expand Up @@ -140,7 +140,7 @@ public static Matrix2X3<T> CreateFromQuaternion<T>(Quaternion<T> quaternion)
public static Matrix2X3<T> CreateFromYawPitchRoll<T>(T yaw, T pitch, T roll)
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
{
Quaternion<T> q = Quaternion<T>.CreateFromYawPitchRoll(yaw, pitch, roll);
Legacy.Quaternion<T> q = Legacy.Quaternion<T>.CreateFromYawPitchRoll(yaw, pitch, roll);
return CreateFromQuaternion(q);
}

Expand Down Expand Up @@ -223,7 +223,7 @@ public static unsafe Matrix2X3<T> Lerp<T>(Matrix2X3<T> matrix1, Matrix2X3<T> mat
/// <param name="value">The source matrix to transform.</param>
/// <param name="rotation">The rotation to apply.</param>
/// <returns>The transformed matrix.</returns>
public static Matrix2X3<T> Transform<T>(Matrix2X3<T> value, Quaternion<T> rotation)
public static Matrix2X3<T> Transform<T>(Matrix2X3<T> value, Legacy.Quaternion<T> rotation)
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
{
// Compute rotation matrix.
Expand Down Expand Up @@ -260,4 +260,4 @@ public static Matrix2X3<T> Transform<T>(Matrix2X3<T> value, Quaternion<T> rotati
return new(value.M11 * q1 + value.M12 * q2 + value.M13 * q3, value.M21 * q1 + value.M22 * q2 + value.M23 * q3);
}
}
}
}
129 changes: 129 additions & 0 deletions sources/Maths/Maths/Matrix2x2F.gen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
namespace Silk.NET.Maths
{
using System.Diagnostics.CodeAnalysis;
using System.Numerics;

partial struct Matrix2x2F<T> :
IEquatable<Matrix2x2F<T>>
where T : IFloatingPointIeee754<T>
{
/// <summary>The multiplicative identity matrix of size 2x2.</summary>
public static readonly Matrix2x2F<T> Identity = new(
new(T.MultiplicativeIdentity, T.Zero),
new(T.Zero, T.MultiplicativeIdentity));

/// <summary>The 1st row of the matrix represented as a vector.</summary>
public Vector2F<T> Row1;

/// <summary>The 2nd row of the matrix represented as a vector.</summary>
public Vector2F<T> Row2;

/// <summary>
/// Constructs a <see cref="Matrix2x2F{T}"/> from the given rows.
/// </summary>
public Matrix2x2F(Vector2F<T> row1, Vector2F<T> row2) => (Row1, Row2) = (row1, row2);

[UnscopedRef]
public ref Vector2F<T> this[int row]
{
get
{
switch (row)
{
case 0:
return ref Row1;
case 1:
return ref Row2;
}

throw new ArgumentOutOfRangeException(nameof(row));
}
}

[UnscopedRef]
public ref T this[int row, int column] => ref this[row][column];

/// <summary>Gets the element in the 1st row and 1st column of the matrix.</summary>
[UnscopedRef]
public ref T M11 => ref Row1.X;

/// <summary>Gets the element in the 1st row and 2nd column of the matrix.</summary>
[UnscopedRef]
public ref T M12 => ref Row1.Y;

/// <summary>Gets the element in the 2nd row and 1st column of the matrix.</summary>
[UnscopedRef]
public ref T M21 => ref Row2.X;

/// <summary>Gets the element in the 2nd row and 2nd column of the matrix.</summary>
[UnscopedRef]
public ref T M22 => ref Row2.Y;

/// <inheridoc/>
public override bool Equals(object? obj) => obj is Matrix2x2F<T> other && Equals(other);

/// <inheridoc/>
public bool Equals(Matrix2x2F<T> other) => this == other;

/// <inheridoc/>
public override int GetHashCode() => HashCode.Combine(Row1, Row2);

/// <summary>Computes the transpose of the matrix.</summary>
public Matrix2x2F<T> Transpose() =>
new(new(M11, M21),
new(M12, M22));

/// <summary>Returns a boolean indicating whether the given two matrices are equal.</summary>
/// <param name="left">The first matrix to compare.</param>
/// <param name="right">The second matrix to compare.</param>
/// <returns><c>true</c> if the given matrices are equal; <c>false</c> otherwise.</returns>
public static bool operator ==(Matrix2x2F<T> left, Matrix2x2F<T> right) =>
left.Row1 == right.Row1 &&
left.Row2 == right.Row2;

/// <summary>Returns a boolean indicating whether the given two matrices are not equal.</summary>
/// <param name="left">The first matrix to compare.</param>
/// <param name="right">The second matrix to compare.</param>
/// <returns><c>true</c> if the given matrices are not equal; <c>false</c> otherwise.</returns>
public static bool operator !=(Matrix2x2F<T> left, Matrix2x2F<T> right) => !(left == right);

/// <summary>Adds two matrices together.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the addition.</returns>
public static Matrix2x2F<T> operator +(Matrix2x2F<T> left, Matrix2x2F<T> right) =>
new(left.Row1 + right.Row1,
left.Row2 + right.Row2);

/// <summary>Subtracts the second matrix from the first.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the subtraction.</returns>
public static Matrix2x2F<T> operator -(Matrix2x2F<T> left, Matrix2x2F<T> right) =>
new(left.Row1 - right.Row1,
left.Row2 - right.Row2);

/// <summary>Returns a new matrix with the negated elements of the given matrix.</summary>
/// <param name="value">The source matrix.</param>
/// <returns>The negated matrix.</returns>
public static Matrix2x2F<T> operator -(Matrix2x2F<T> value) =>
new(-value.Row1,
-value.Row2);

/// <summary>Multiplies a matrix by another matrix.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the multiplication.</returns>
public static Matrix2x2F<T> operator *(Matrix2x2F<T> left, Matrix2x2F<T> right) =>
new(left.M11 * right.Row1 + left.M12 * right.Row2,
left.M21 * right.Row1 + left.M22 * right.Row2);
}

static partial class Matrix2x2F
{
public static Matrix2x2F<T> Lerp<T>(Matrix2x2F<T> value1, Matrix2x2F<T> value2, T amount)
where T : IFloatingPointIeee754<T> =>
new(new(T.Lerp(value1.M11, value2.M11, amount), T.Lerp(value1.M12, value2.M12, amount)),
new(T.Lerp(value1.M21, value2.M21, amount), T.Lerp(value1.M22, value2.M22, amount)));
}
}
122 changes: 122 additions & 0 deletions sources/Maths/Maths/Matrix2x2I.gen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
namespace Silk.NET.Maths
{
using System.Diagnostics.CodeAnalysis;
using System.Numerics;

partial struct Matrix2x2I<T> :
IEquatable<Matrix2x2I<T>>
where T : IBinaryInteger<T>
{
/// <summary>The multiplicative identity matrix of size 2x2.</summary>
public static readonly Matrix2x2I<T> Identity = new(
new(T.MultiplicativeIdentity, T.Zero),
new(T.Zero, T.MultiplicativeIdentity));

/// <summary>The 1st row of the matrix represented as a vector.</summary>
public Vector2I<T> Row1;

/// <summary>The 2nd row of the matrix represented as a vector.</summary>
public Vector2I<T> Row2;

/// <summary>
/// Constructs a <see cref="Matrix2x2I{T}"/> from the given rows.
/// </summary>
public Matrix2x2I(Vector2I<T> row1, Vector2I<T> row2) => (Row1, Row2) = (row1, row2);

[UnscopedRef]
public ref Vector2I<T> this[int row]
{
get
{
switch (row)
{
case 0:
return ref Row1;
case 1:
return ref Row2;
}

throw new ArgumentOutOfRangeException(nameof(row));
}
}

[UnscopedRef]
public ref T this[int row, int column] => ref this[row][column];

/// <summary>Gets the element in the 1st row and 1st column of the matrix.</summary>
[UnscopedRef]
public ref T M11 => ref Row1.X;

/// <summary>Gets the element in the 1st row and 2nd column of the matrix.</summary>
[UnscopedRef]
public ref T M12 => ref Row1.Y;

/// <summary>Gets the element in the 2nd row and 1st column of the matrix.</summary>
[UnscopedRef]
public ref T M21 => ref Row2.X;

/// <summary>Gets the element in the 2nd row and 2nd column of the matrix.</summary>
[UnscopedRef]
public ref T M22 => ref Row2.Y;

/// <inheridoc/>
public override bool Equals(object? obj) => obj is Matrix2x2I<T> other && Equals(other);

/// <inheridoc/>
public bool Equals(Matrix2x2I<T> other) => this == other;

/// <inheridoc/>
public override int GetHashCode() => HashCode.Combine(Row1, Row2);

/// <summary>Computes the transpose of the matrix.</summary>
public Matrix2x2I<T> Transpose() =>
new(new(M11, M21),
new(M12, M22));

/// <summary>Returns a boolean indicating whether the given two matrices are equal.</summary>
/// <param name="left">The first matrix to compare.</param>
/// <param name="right">The second matrix to compare.</param>
/// <returns><c>true</c> if the given matrices are equal; <c>false</c> otherwise.</returns>
public static bool operator ==(Matrix2x2I<T> left, Matrix2x2I<T> right) =>
left.Row1 == right.Row1 &&
left.Row2 == right.Row2;

/// <summary>Returns a boolean indicating whether the given two matrices are not equal.</summary>
/// <param name="left">The first matrix to compare.</param>
/// <param name="right">The second matrix to compare.</param>
/// <returns><c>true</c> if the given matrices are not equal; <c>false</c> otherwise.</returns>
public static bool operator !=(Matrix2x2I<T> left, Matrix2x2I<T> right) => !(left == right);

/// <summary>Adds two matrices together.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the addition.</returns>
public static Matrix2x2I<T> operator +(Matrix2x2I<T> left, Matrix2x2I<T> right) =>
new(left.Row1 + right.Row1,
left.Row2 + right.Row2);

/// <summary>Subtracts the second matrix from the first.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the subtraction.</returns>
public static Matrix2x2I<T> operator -(Matrix2x2I<T> left, Matrix2x2I<T> right) =>
new(left.Row1 - right.Row1,
left.Row2 - right.Row2);

/// <summary>Returns a new matrix with the negated elements of the given matrix.</summary>
/// <param name="value">The source matrix.</param>
/// <returns>The negated matrix.</returns>
public static Matrix2x2I<T> operator -(Matrix2x2I<T> value) =>
new(-value.Row1,
-value.Row2);

/// <summary>Multiplies a matrix by another matrix.</summary>
/// <param name="left">The first source matrix.</param>
/// <param name="right">The second source matrix.</param>
/// <returns>The result of the multiplication.</returns>
public static Matrix2x2I<T> operator *(Matrix2x2I<T> left, Matrix2x2I<T> right) =>
new(left.M11 * right.Row1 + left.M12 * right.Row2,
left.M21 * right.Row1 + left.M22 * right.Row2);
}

}
Loading
Loading